module MicroKanren::Lisp

Public Instance Methods

assp(func, alist) click to toggle source

Search association list by predicate function. Based on lua implementation by silentbicycle: github.com/silentbicycle/lua-ukanren/blob/master/ukanren.lua#L53:L61

Additional reference for this function is scheme: Ref for assp: www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-4.html

# File lib/micro_kanren/lisp.rb, line 39
def assp(func, alist)
  if alist
    first_pair  = car(alist)
    first_value = car(first_pair)

    if func.call(first_value)
      first_pair
    else
      assp(func, cdr(alist))
    end
  else
    false
  end
end
car(z) click to toggle source
# File lib/micro_kanren/lisp.rb, line 11
def car(z)     ; z.call(-> (p, q) { p }) ; end
ccel?() click to toggle source
# File lib/micro_kanren/lisp.rb, line 7
def ccel? ; true ; end
cdr(z) click to toggle source
# File lib/micro_kanren/lisp.rb, line 12
def cdr(z)     ; z.call(-> (p, q) { q }) ; end
cons(x, y) click to toggle source

Returns a Cons cell that is also marked as such for later identification.

# File lib/micro_kanren/lisp.rb, line 5
def cons(x, y)
  -> (m) { m.call(x, y) }.tap do |func|
    func.instance_eval{ def ccel? ; true ; end }
  end
end
cons?(d) click to toggle source
# File lib/micro_kanren/lisp.rb, line 14
def cons?(d)
  d.respond_to?(:ccel?) && d.ccel?
end
Also aliased as: pair?
length(list) click to toggle source
# File lib/micro_kanren/lisp.rb, line 23
def length(list)
  list.nil? ? 0 : 1 + length(cdr(list))
end
lists_equal?(a, b) click to toggle source
# File lib/micro_kanren/lisp.rb, line 73
def lists_equal?(a, b)
  if cons?(a) && cons?(b)
    lists_equal?(car(a), car(b)) && lists_equal?(cdr(a), cdr(b))
  else
    a == b
  end
end
lprint(node, cons_in_cdr = false) click to toggle source

Converts Lisp AST to a String. Algorithm is a recursive implementation of www.mat.uc.pt/~pedro/cientificos/funcional/lisp/gcl_22.html#SEC1238.

# File lib/micro_kanren/lisp.rb, line 56
def lprint(node, cons_in_cdr = false)
  if cons?(node)
    str = cons_in_cdr ? '' : '('
    str += lprint(car(node))

    if cons?(cdr(node))
      str += ' ' + lprint(cdr(node), true)
    else
      str += ' . ' + lprint(cdr(node)) unless cdr(node).nil?
    end

    cons_in_cdr ? str : str << ')'
  else
    atom_string(node)
  end
end
map(func, list) click to toggle source
# File lib/micro_kanren/lisp.rb, line 19
def map(func, list)
  cons(func.call(car(list)), map(func, cdr(list))) if list
end
pair?(d)
Alias for: cons?
procedure?(elt) click to toggle source

We implement scheme cons cells as Procs. This function returns a boolean identically to the Scheme procedure? function to avoid false positives.

# File lib/micro_kanren/lisp.rb, line 29
def procedure?(elt)
  elt.is_a?(Proc) && !cons?(elt)
end

Private Instance Methods

atom_string(node) click to toggle source
# File lib/micro_kanren/lisp.rb, line 83
def atom_string(node)
  case node
  when NilClass, Array, String
    node.inspect
  else
    node.to_s
  end
end