Skip to content

tomstuart/kanren

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

An example Ruby implementation of μKanren.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages