Class: MiniKraken::Rela::Unify
- Inherits:
-
BinaryRelation
- Object
- Core::Specification
- Core::Relation
- BinaryRelation
- MiniKraken::Rela::Unify
- Includes:
- Singleton
- Defined in:
- lib/mini_kraken/rela/unify.rb
Overview
Corresponds to the ‘==’ relation in canonical miniKanren implementation in Scheme. Implements the core of the unification algorithm.
Instance Attribute Summary
Attributes inherited from Core::Specification
Instance Method Summary collapse
-
#initialize ⇒ Unify
constructor
Constructor.
-
#solver_for(actuals, ctx) ⇒ Fiber<Context>
A DuckFiber instance that yields one Context.
-
#unification(arg1, arg2, ctx) ⇒ Context
The updated context.
Methods inherited from BinaryRelation
Methods inherited from Core::Specification
#check_arity, #inspect, #variadic?
Constructor Details
#initialize ⇒ Unify
Constructor. Initialize the name of the relation
22 23 24 25 |
# File 'lib/mini_kraken/rela/unify.rb', line 22 def initialize super('unify') freeze end |
Instance Method Details
#solver_for(actuals, ctx) ⇒ Fiber<Context>
Returns A DuckFiber instance that yields one Context.
30 31 32 33 34 35 |
# File 'lib/mini_kraken/rela/unify.rb', line 30 def solver_for(actuals, ctx) arg1, arg2 = *actuals # context = unification(arg1, arg2, ctx) # Core::DuckFiber.new(-> { context }) Core::DuckFiber.new(-> { unification(arg1, arg2, ctx) }) end |
#unification(arg1, arg2, ctx) ⇒ Context
Returns The updated context.
41 42 43 44 45 46 47 |
# File 'lib/mini_kraken/rela/unify.rb', line 41 def unification(arg1, arg2, ctx) return ctx.succeeded! if arg1.equal?(arg2) return ctx.failed! if arg1.nil? || arg2.nil? new_arg1, new_arg2 = commute_cond(arg1, arg2, ctx) do_unification(new_arg1, new_arg2, ctx) end |