Slide 1

Slide 1 text

miniKanren

Slide 2

Slide 2 text

@igorwhilefalse

Slide 3

Slide 3 text

prologue

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

logic programming

Slide 11

Slide 11 text

(run 1 (q) (== #t #t)) ;=> (_.0)

Slide 12

Slide 12 text

(run 1 (q) (== q 'hello)) ;=> (hello)

Slide 13

Slide 13 text

(run 1 (q) (== #t #f)) ;=> ()

Slide 14

Slide 14 text

(run 1 (q) (== q 'doughnut) (== q 'bagel)) ;=> ()

Slide 15

Slide 15 text

(run 1 (q) (conde [(== q 'doughnut)] [(== q 'bagel)])) ;=> (doughnut)

Slide 16

Slide 16 text

(run* (q) (conde [(== q 'doughnut)] [(== q 'bagel)])) ;=> (doughnut bagel)

Slide 17

Slide 17 text

==

Slide 18

Slide 18 text

(run* (q) (fresh (foods d) (== foods '(maple-syrup bacon pancakes)) (== foods `(,q . ,d)))) ;=> (maple-syrup)

Slide 19

Slide 19 text

(define append (lambda (l s) (cond ((null? l) s) (else (cons (car l) (append (cdr l) s))))))

Slide 20

Slide 20 text

(append '(l o v e) '(l a c e)) ;=> (l o v e l a c e)

Slide 21

Slide 21 text

(define appendo (lambda (l s out) (conde [(== '() l) (== s out)] [(fresh (a d res) (== `(,a . ,d) l) (== `(,a . ,res) out) (appendo d s res))])))

Slide 22

Slide 22 text

(run* (q) (appendo '(l o v e) '(l a c e) q)) ;=> ((l o v e l a c e))

Slide 23

Slide 23 text

(run* (q) (appendo '(l o v e) q '(l o v e l a c e))) ;=> ((l a c e))

Slide 24

Slide 24 text

(run* (q r) (appendo q r '(l o v e))) ;=> ((() (l o v e)) ; ((l) (o v e)) ; ((l o) (v e)) ; ((l o v) (e)) ; ((l o v e) ()))

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

(define eval-expo (lambda (exp env val) (conde ((fresh (v) (== `(quote ,v) exp) (not-in-envo 'quote env) (noo 'closure v) (== v val))) ((fresh (a*) (== `(list . ,a*) exp) (not-in-envo 'list env) (noo 'closure a*) (proper-listo a* env val))) ((symbolo exp) (lookupo exp env val)) ((fresh (rator rand x body env^ a) (== `(,rator ,rand) exp) (eval-expo rator env `(closure ,x ,body ,env^)) (eval-expo rand env a) (eval-expo body `((,x . ,a) . ,env^) val))) ((fresh (x body) (== `(lambda (,x) ,body) exp) (symbolo x) (not-in-envo 'lambda env) (== `(closure ,x ,body ,env) val)))))) (define not-in-envo (lambda (x env) (conde ((fresh (y v rest) (== `((,y . ,v) . ,rest) env) (=/= y x) (not-in-envo x rest))) ((== '() env))))) (define proper-listo (lambda (exp env val) (conde ((== '() exp) (== '() val)) ((fresh (a d t-a t-d) (== `(,a . ,d) exp) (== `(,t-a . ,t-d) val) (eval-expo a env t-a) (proper-listo d env t-d)))))) (define lookupo (lambda (x env t) (fresh (rest y v) (== `((,y . ,v) . ,rest) env) (conde ((== y x) (== v t)) ((=/= y x) (lookupo x rest t)))))) github.com/webyrd/quines

Slide 27

Slide 27 text

(run 1 (q) (eval-expo '((lambda (x) x) 'me!) '() q)) ;=> (me!)

Slide 28

Slide 28 text

(run 3 (q) (eval-expo q '() 'me!)) ;=> ('me! ; ((lambda (_.0) 'me!) '_.1) ; ((lambda (_.0) _.0) 'me!))

Slide 29

Slide 29 text

(run 1 (q) (eval-expo q '() q)) ;=> ((lambda (_.0) (list _.0 (list 'quote _.0))) ; '(lambda (_.0) (list _.0 (list 'quote _.0))))

Slide 30

Slide 30 text

((lambda (_.0) (list _.0 (list 'quote _.0))) '(lambda (_.0) (list _.0 (list 'quote _.0)))) ;=> ((lambda (_.0) (list _.0 (list 'quote _.0))) ; '(lambda (_.0) (list _.0 (list 'quote _.0))))

Slide 31

Slide 31 text

why

Slide 32

Slide 32 text

core.logic gif: @bodil

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

µKanren (define (var c) (vector c)) (define (var? x) (vector? x)) (define (var=? x1 x2) (= (vector-ref x1 0) (vector-ref x2 0))) (define (walk u s) (let ((pr (and (var? u) (assp (lambda (v) (var=? u v)) s)))) (if pr (walk (cdr pr) s) u))) (define (ext-s x v s) `((,x . ,v) . ,s)) (define (== u v) (lambda (s/c) (let ((s (unify u v (car s/c)))) (if s (unit `(,s . ,(cdr s/c))) mzero)))) (define (unit s/c) (cons s/c mzero)) (define mzero '()) (define (unify u v s) (let ((u (walk u s)) (v (walk v s))) (cond ((and (var? u) (var? v) (var=? u v)) s) ((var? u) (ext-s u v s)) ((var? v) (ext-s v u s)) ((and (pair? u) (pair? v)) (let ((s (unify (car u) (car v) s))) (and s (unify (cdr u) (cdr v) s)))) (else (and (eqv? u v) s))))) (define (call/fresh f) (lambda (s/c) (let ((c (cdr s/c))) ((f (var c)) `(,(car s/c) . ,(+ c 1)))))) (define (disj g1 g2) (lambda (s/c) (mplus (g1 s/c) (g2 s/c)))) (define (conj g1 g2) (lambda (s/c) (bind (g1 s/c) g2))) (define (mplus $1 $2) (cond ((null? $1) $2) ((procedure? $1) (lambda () (mplus $2 ($1)))) (else (cons (car $1) (mplus (cdr $1) $2))))) (define (bind $ g) (cond ((null? $) mzero) ((procedure? $) (lambda () (bind ($) g))) (else (mplus (g (car $)) (bind (cdr $) g)))))

Slide 35

Slide 35 text

References (1) The Reasoned Schemer
 Daniel Friedman, William Byrd, Oleg Kiselyov (2) Quine Generation via Relational Interpreters
 William Byrd, Eric Holk, Daniel Friedman (3) µKanren
 Jason Hemann, Daniel Friedman (4) Propositions as Types
 Philip Wadler

Slide 36

Slide 36 text

Thanks •Michael (@mrb_bk) •Danielle (@daniellesucher) •Volker (@__edorian) •Will (@webyrd)

Slide 37

Slide 37 text

Questions? •minikanren.org •@igorwhilefalse