Module: MiniKraken::Glue::DSL

Defined in:
lib/mini_kraken/glue/dsl.rb

Overview

The mixin module that implements the methods for the DSL (DSL = Domain Specific Langague) that allows MiniKraken users to embed Minikanren in their Ruby code.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mth, *args) ⇒ Object (private)



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/mini_kraken/glue/dsl.rb', line 167

def method_missing(mth, *args)
  result = nil

  begin
    result = super(mth, *args)
  rescue NameError
    name = mth.id2name
    @defrels ||= {}
    if @defrels.include?(name)
      def_relation = @defrels[name]
      result = Core::Goal.new(def_relation, args.map { |el| convert(el) })
    else
      default_mode unless instance_variable_defined?(:@dsl_mode)
      if @dsl_mode == :defrel && @defrel_formals.include?(name)
        result = Core::FormalRef.new(name)
      else
        result = Core::LogVarRef.new(name)
      end
    end
  end

  result
end

Instance Method Details

#_failCore::Fail

Returns A goal that unconditionally fails.

Returns:

  • (Core::Fail)

    A goal that unconditionally fails.



73
74
75
# File 'lib/mini_kraken/glue/dsl.rb', line 73

def _fail
  goal_class.new(Core::Fail.instance, [])
end

#conde(*goals) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mini_kraken/glue/dsl.rb', line 25

def conde(*goals)
  args = goals.map do |goal_maybe|
    if goal_maybe.kind_of?(Array)
      goal_maybe.map { |g| convert(g) }
    else
      convert(goal_maybe)
    end
  end

  Core::Goal.new(Rela::Conde.instance, args)
end

#conj2(arg1, arg2) ⇒ Core::Failure|Core::Success

conj2 stands for conjunction of two arguments. Returns a goal linked to the Core::Conj2 relation. The rule of that relation succeeds when both arguments succeed.

Parameters:

Returns:

  • (Core::Failure|Core::Success)


43
44
45
# File 'lib/mini_kraken/glue/dsl.rb', line 43

def conj2(arg1, arg2)
 goal_class.new(Rela::Conj2.instance, [convert(arg1), convert(arg2)])
end

#cons(car_item, cdr_item = nil) ⇒ Object



47
48
49
50
# File 'lib/mini_kraken/glue/dsl.rb', line 47

def cons(car_item, cdr_item = nil)
  tail = cdr_item.nil? ? cdr_item : convert(cdr_item)
  Composite::ConsCell.new(convert(car_item), tail)
end

#defrel(relationName, theFormals, aGoalExpr) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mini_kraken/glue/dsl.rb', line 56

def defrel(relationName, theFormals, aGoalExpr)
  case theFormals
    when String
      formals = [theFormals]
    when Array
      formals = theFormals
  end

  rela = Rela::DefRelation.new(relationName, aGoalExpr, formals)
  add_defrel(rela)
end

#disj2(arg1, arg2) ⇒ Object



68
69
70
# File 'lib/mini_kraken/glue/dsl.rb', line 68

def disj2(arg1, arg2)
  goal_class.new(Rela::Disj2.instance, [convert(arg1), convert(arg2)])
end

#fresh(names, subgoal) ⇒ Core::Goal

Returns:



82
83
84
85
# File 'lib/mini_kraken/glue/dsl.rb', line 82

def fresh(names, subgoal)
  # puts "#{__callee__} #{names}"
  Rela::Fresh.build_goal(names, subgoal)
end

#list(*members) ⇒ Object



87
88
89
90
91
92
# File 'lib/mini_kraken/glue/dsl.rb', line 87

def list(*members)
  return null if members.empty?

  converted = members.map { |e| convert(e) }
  Composite::List.make_list(converted)
end

#nullConsCell

Returns an empty list, that is, a pair whose members are nil.

Returns:

  • (ConsCell)

    Returns an empty list, that is, a pair whose members are nil.



95
96
97
# File 'lib/mini_kraken/glue/dsl.rb', line 95

def null
  Composite::ConsCell.null
end

#null_listObject



52
53
54
# File 'lib/mini_kraken/glue/dsl.rb', line 52

def null_list
  Composite::ConsCell.new(nil, nil)
end

#run_star(var_names, goal) ⇒ Composite::ConsCell

A run* expression tries to find all the solutions that meet the given goal.

Returns:



20
21
22
23
# File 'lib/mini_kraken/glue/dsl.rb', line 20

def run_star(var_names, goal)
  program = RunStarExpression.new(var_names, goal)
  program.run
end

#succeedCore::Succeed

Returns A goal that unconditionally succeeds.

Returns:



100
101
102
# File 'lib/mini_kraken/glue/dsl.rb', line 100

def succeed
  goal_class.new(Core::Succeed.instance, [])
end

#unify(arg1, arg2) ⇒ Object



77
78
79
# File 'lib/mini_kraken/glue/dsl.rb', line 77

def unify(arg1, arg2)
  goal_class.new(Rela::Unify.instance, [convert(arg1), convert(arg2)])
end