Class: MiniKraken::Rela::Unify

Inherits:
BinaryRelation show all
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

#arity, #name

Instance Method Summary collapse

Methods inherited from BinaryRelation

symmetric

Methods inherited from Core::Specification

#check_arity, #inspect, #variadic?

Constructor Details

#initializeUnify

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.

Parameters:

  • actuals (Array<Term>)

    A two-elements array

  • ctx (Context)

    A context object

Returns:

  • (Fiber<Context>)

    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.

Parameters:

  • arg1 (Term)
  • arg2 (Term)
  • ctx (Context)

    A context object

Returns:

  • (Context)

    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