Mini Kanren and the Da Vinci challenge


The logic programming system used by The Reasoned Schmer is MiniKanren. It is a much simplified version of Kanren and runs in any R5RS Scheme. Without further ado here is a MiniKanren program to solve the board puzzles from the Da Vinci Quest:
(define distinct
(lambda (x1 x2 x3 x4)
(let ([xs (list x1 x2 x3 x4)])
(all
(membero 'blade xs)
(membero 'cross xs)
(membero 'fleur xs)
(membero 'omega xs)))))
> (run* (b)
(fresh (x1 x2 x3 x4
x5 x6 x7 x8
x9 x10 x11 x12
x13 x14 x15 x16)
(alli
; the board consists of fields fields
(== b (list (list x1 x2 x3 x4)
(list x5 x6 x7 x8)
(list x9 x10 x11 x12)
(list x13 x14 x15 x16)))
(== b (list (list x1 x2 x3 x4)
(list x5 x6 x7 'blade)
(list x9 'omega x11 x12)
(list x13 'fleur x15 'cross)))
; symbols in rows are distinct
(distinct x1 x2 x3 x4)
(distinct x5 x6 x7 x8)
(distinct x9 x10 x11 x12)
(distinct x13 x14 x15 x16)
; symbols in columns are distinct
(distinct x1 x5 x9 x13)
(distinct x2 x6 x10 x14)
(distinct x3 x7 x11 x15)
(distinct x4 x8 x12 x16)
; symbols in each colored area are distinct
(distinct x3 x4 x7 x8)
(distinct x9 x13 x14 x15)
)))
(((fleur blade cross omega)
(omega cross fleur blade)
(cross omega blade fleur)
(blade fleur omega cross)))
#<procedure:succeed>
1 Comments:
It is a much simplified version of Kanren and runs in any R5RS Scheme. Without further ado here is a MiniKanren program to solve the board puzzles from the Da Vinci Quest.
Term papers
Post a Comment
Links to this post:
Create a Link
<< Home