Project

kanren

0.01
No commit activity in last 3 years
No release in over 3 years
An example Ruby implementation of μKanren.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 3.3.0, ~> 3.3
 Project Readme

Kanren

This library provides an example Ruby implementation of μKanren, along with some simple data structures and relations. It is intended to accompany the article “Hello, declarative world”.

That article explains the details, but here’s a brief demonstration:

$ irb -Ilib
>> require 'kanren/micro'
=> true
>> include Kanren::Micro
=> Object

>> goal = Goal.with_variables { |x, y|
     Goal.either(Goal.equal(x, 1), Goal.equal(y, 2))
   }
=> #<Kanren::Micro::Goal …>
>> states = goal.pursue_in(State.new)
=> #<Enumerator: …>
>> states.next.values
=> {x=>1}
>> states.next.values
=> {y=>2}
>> states.next.values
StopIteration: iteration reached an end

>> include Kanren
=> Object

>> goal = Goal.with_variables { |x|
     Relations.multiply(Peano.from_integer(3), Peano.from_integer(4), x)
   }
=> #<Kanren::Micro::Goal …>
>> states = goal.pursue_in(State.new)
=> #<Enumerator: …>
>> Peano.to_integer(states.next.result)
=> 12

>> goal = Goal.with_variables { |x, y|
     Relations.add(x, y, Peano.from_integer(8))
   }
=> #<Kanren::Micro::Goal …>
>> states = goal.pursue_in(State.new)
=> #<Enumerator: …>
>> states.each do |state|
     p state.results(2).map { |peano| Peano.to_integer(peano) }
   end
[0, 8]
[1, 7]
[2, 6]
[3, 5]
[4, 4]
[5, 3]
[6, 2]
[7, 1]
[8, 0]
=> nil

If you have any questions, please get in touch via Twitter or email. If you find any bugs or other problems with the code, please open an issue.