Created
April 5, 2015 03:01
-
-
Save orb/e8344c0ee6741396d7f5 to your computer and use it in GitHub Desktop.
some ruby minikanren scribblings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mini_kanren' | |
include MiniKanren::Extras | |
$section = "--" | |
def section!(label) | |
$counter = 0 | |
puts "====== #{label}" | |
end | |
def show(result) | |
$counter = $counter + 1 | |
puts "#{$counter}: #{result}" | |
end | |
x = MiniKanren.exec do | |
q,r,s = fresh 3 | |
section! "slide 8 - basic call" | |
show run(q, succeed) | |
show run(q, fail) | |
show run("hello", succeed) | |
show run("hello", fail) | |
section! "slide 9 - multiple returns" | |
show run([q], succeed) | |
show run([q,r,s], succeed) | |
show run([q,r,s], fail) | |
section! "slide 10-? - unification" | |
show run(q, eq(q, :hello)) | |
show run(q, eq(q, [:hello, :world])) | |
show run(q, | |
eq(q, :hello), | |
eq(q, :hello)) | |
show run(q, | |
eq(q, :hello), | |
eq(q, :world)) | |
section! "slide 14 - conde" | |
show run(q, | |
conde(eq(q, :hello), | |
eq(q, :world))) | |
## doesn't support disunification constraint (or any constraints) | |
section! "slide 16 - fresh" | |
show run(q, | |
fresh {|a| eq(a,1)}) | |
show run(q, | |
fresh {|a| eq(q,1)}) | |
show run(q, | |
fresh {|a| all(eq(a,2), | |
eq(a,q))}) | |
show run(q, | |
fresh {|a,b| | |
all(eq(a,3), | |
eq(b,4), | |
eq([a,b],q))}) | |
show run(q, | |
fresh {|a| all(eq(a,1), | |
eq(a,2))}) | |
## requires extras | |
section! "slide 19 - membero" | |
show run(q, | |
membero(q, [])) | |
smurf = fresh | |
smurfs = [:papa, [:brainy, [:lazy, [:handy, []]]]] | |
show run(smurf, | |
membero(smurf, smurfs)) | |
show run(q, | |
fresh {|smurf| | |
all(membero(smurf, smurfs), | |
eq(q, [smurf, smurf]))}) | |
show run(q, | |
fresh {|smurf1, smurf2| | |
all(membero(smurf1, smurfs), | |
membero(smurf2, smurfs), | |
eq(q, [smurf1, smurf2]))}) | |
## can't write distincto without disequality :( | |
section! "slide 25 - rps" | |
def beatso(move1, move2) | |
conde(all(eq(move1, :rock), eq(move2, :scissors)), | |
all(eq(move1, :scissors), eq(move2, :paper)), | |
all(eq(move1, :paper), eq(move2, :rock))) | |
end | |
show run(q, beatso(:rock, :paper)) | |
show run(q, beatso(:rock, q)) | |
show run(q, beatso(q, :paper)) | |
show run(q, | |
fresh{|winner,loser| | |
all(beatso(winner,loser), | |
eq(q, [winner, loser]))}) | |
section! "slide 30 - rpsls" | |
# no fact db | |
def rpsl(move) | |
membero(move, [:rock, [:paper, [:scissors, [:lizzard, [:spock, nil]]]]]) | |
end | |
def rpsl_beats(winner,loser) | |
conde(all(eq(move1, :rock), conde(eq(move2, :scissors), eq(move2, :lizard))), | |
all(eq(move1, :scissors), conde(eq(move2, :paper), eq(move2, :spock)), | |
all(eq(move1, :paper), conde(eq(move2, :paper), eq(move2, :spock)) | |
end | |
show run(q, rpsl(q)) | |
section! "more" | |
show run([q,r,s], | |
eq(r, 4), | |
eq(s,5)) | |
show run(a = fresh, eq(a,1)) # not recommended? | |
end | |
puts "Result #{x}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment