cKanren miniKanren with Constraints Claire E. Alvis , Jeremiah J. - - PowerPoint PPT Presentation

ckanren
SMART_READER_LITE
LIVE PREVIEW

cKanren miniKanren with Constraints Claire E. Alvis , Jeremiah J. - - PowerPoint PPT Presentation

cKanren miniKanren with Constraints Claire E. Alvis , Jeremiah J. Willcock, Kyle M. Carter, William E. Byrd, Daniel P. Friedman { calvis, jewillco, kylcarte, webyrd, dfried } @cs.indiana.edu School of Informatics and Computing Indiana


slide-1
SLIDE 1

cKanren

miniKanren with Constraints Claire E. Alvis, Jeremiah J. Willcock, Kyle M. Carter, William E. Byrd, Daniel P. Friedman

{calvis, jewillco, kylcarte, webyrd, dfried}@cs.indiana.edu

School of Informatics and Computing Indiana University, Bloomington

October 23, 2011

slide-2
SLIDE 2

Overview

  • 1. Introduction to Logic Programming/miniKanren
  • 2. Introduction to Constraints
  • 3. Examples
  • 4. Implementation Overview
slide-3
SLIDE 3

miniKanren

Logic programming language extending Scheme

slide-4
SLIDE 4

miniKanren

Logic programming language extending Scheme Three important operators: ≡, fresh, and conde

slide-5
SLIDE 5

miniKanren

Logic programming language extending Scheme Three important operators: ≡, fresh, and conde Intuition: The goal (== 5 5) succeeds while (== 5 6) fails

slide-6
SLIDE 6

miniKanren

Logic programming language extending Scheme Three important operators: ≡, fresh, and conde Intuition: The goal (== 5 5) succeeds while (== 5 6) fails (fresh (x) (conde ((== x 5)) ((== x 6)))) unifies x with 5 or 6

slide-7
SLIDE 7

miniKanren

Logic programming language extending Scheme Uses run as an interface operator (run1 (y) (fresh (x z) (== x z) (== 3 y))) ⇒ (3)

slide-8
SLIDE 8

miniKanren

Logic programming language extending Scheme Uses run as an interface operator (run1 (y) (fresh (x z) (== x z) (== 3 y))) ⇒ (3) (run1 (y) (fresh (x z) (== x z) (== 3 z) (== y x))) ⇒ (3)

slide-9
SLIDE 9

miniKanren

Logic programming language extending Scheme Uses run as an interface operator (run1 (y) (fresh (x z) (== x z) (== 3 y))) ⇒ (3) (run1 (y) (fresh (x z) (== x z) (== 3 z) (== y x))) ⇒ (3) (run1 (y) (fresh (y) (conde ((== y 4)) ((== y 5)))) (== 3 y)) ⇒ (3)

slide-10
SLIDE 10

Constraints

Imposing a certain restriction on a variable or set of variables Find a solution such that every constraint is satisfied

slide-11
SLIDE 11

Constraints

Imposing a certain restriction on a variable or set of variables Find a solution such that every constraint is satisfied Examples: Set of equations x + y + z = h h + 3 = m − x y − 7 = h + z

slide-12
SLIDE 12

Constraints

Imposing a certain restriction on a variable or set of variables Find a solution such that every constraint is satisfied Examples: Set of equations, Tree Disequality

slide-13
SLIDE 13

Constraints

Imposing a certain restriction on a variable or set of variables Find a solution such that every constraint is satisfied Examples: Set of equations, Tree Disequality ’oak ≡ ’pine ’((1 x) y 7) ≡ ’(z 5 w)

slide-14
SLIDE 14

Constraints

Imposing a certain restriction on a variable or set of variables Find a solution such that every constraint is satisfied Examples: Set of equations, Tree Disequality, N-Queens

slide-15
SLIDE 15

Send More Money

Find the correct letter values to satisfy the following equation: S E N D + M O R E M O N E Y Each letter represents a different digit in the range 0 through 9

slide-16
SLIDE 16

Motivation

slide-17
SLIDE 17

Motivation

◮ miniKanren does not use mathematical reasoning to rule out

unrealizable values

slide-18
SLIDE 18

Motivation

◮ miniKanren does not use mathematical reasoning to rule out

unrealizable values

◮ Performs very slowly on standard constraint problems

slide-19
SLIDE 19

Motivation

◮ miniKanren does not use mathematical reasoning to rule out

unrealizable values

◮ Performs very slowly on standard constraint problems ◮ Extensions to miniKanren are incompatible with each other

slide-20
SLIDE 20

cKanren

◮ A framework for defining constraint systems on top of

miniKanren

slide-21
SLIDE 21

cKanren

◮ A framework for defining constraint systems on top of

miniKanren

◮ Retains all miniKanren functionality

slide-22
SLIDE 22

cKanren

◮ A framework for defining constraint systems on top of

miniKanren

◮ Retains all miniKanren functionality ◮ Includes two constraint systems:

finite domains and tree disequality

slide-23
SLIDE 23

cKanren

◮ A framework for defining constraint systems on top of

miniKanren

◮ Retains all miniKanren functionality ◮ Includes two constraint systems:

finite domains and tree disequality

◮ Easy to add or compose additional constraints systems

slide-24
SLIDE 24

Constraints Over Finite Domains

We can associate a domain with a variable x

slide-25
SLIDE 25

Constraints Over Finite Domains

We can associate a domain with a variable x We consider only finite domains of natural numbers such as x ∈ {1, 2, 3, 7, 8, 9} ... but there are others (interval domains, boolean domains, etc.)

slide-26
SLIDE 26

Constraints Over Finite Domains

New operators:

◮ (domfd x n∗)

slide-27
SLIDE 27

Constraints Over Finite Domains

New operators:

◮ (domfd x n∗) ◮ (≤fd u v)

slide-28
SLIDE 28

Constraints Over Finite Domains

New operators:

◮ (domfd x n∗) ◮ (≤fd u v) ◮ (+fd u v w)

slide-29
SLIDE 29

Constraints Over Finite Domains

New operators:

◮ (domfd x n∗) ◮ (≤fd u v) ◮ (+fd u v w) ◮ (≡fd u v)

slide-30
SLIDE 30

Constraints Over Finite Domains

New operators:

◮ (domfd x n∗) ◮ (≤fd u v) ◮ (+fd u v w) ◮ (≡fd u v) ◮ (all-diff fd v∗)

slide-31
SLIDE 31

Constraints Over Finite Domains

New operators:

◮ (domfd x n∗) ◮ (≤fd u v) ◮ (+fd u v w) ◮ (≡fd u v) ◮ (all-diff fd v∗)

Derived goals:

◮ infd to assign multiple variables a single initial domain

slide-32
SLIDE 32

Constraints Over Finite Domains

New operators:

◮ (domfd x n∗) ◮ (≤fd u v) ◮ (+fd u v w) ◮ (≡fd u v) ◮ (all-diff fd v∗)

Derived goals:

◮ infd to assign multiple variables a single initial domain ◮ (<fdu v)

slide-33
SLIDE 33

Example

(run* (q) (fresh (x y z) (domfd x ’(7 8 9 10)) (domfd y ’(4 5 8 9 12)) (domfd z ’(1 2 12 16)) ...))

slide-34
SLIDE 34

Example

(run* (q) (fresh (x y z) (domfd x ’(7 8 9 10)) (domfd y ’(4 5 8 9 12)) (domfd z ’(1 2 12 16)) ...)) x ∈ {7, 8, 9, 10} y ∈ {4, 5, 8, 9, 12} z ∈ {1, 2, 12, 16}

slide-35
SLIDE 35

Example

(run* (q) (fresh (x y z) (domfd x ’(7 8 9 10)) (domfd y ’(4 5 8 9 12)) (domfd z ’(1 2 12 16)) (<=fd x y) ...)) x ∈ {7, 8, 9, 10} y ∈ {8, 9, 12} z ∈ {1, 2, 12, 16}

slide-36
SLIDE 36

Example

(run* (q) (fresh (x y z) (domfd x ’(7 8 9 10)) (domfd y ’(4 5 8 9 12)) (domfd z ’(1 2 12 16)) (<=fd x y) (+fd x y z) ...)) x ∈ {7, 8} y ∈ {8, 9} z ∈ {16}

slide-37
SLIDE 37

Example

(run* (q) (fresh (x y z) (domfd x ’(7 8 9 10)) (domfd y ’(4 5 8 9 12)) (domfd z ’(1 2 12 16)) (<=fd x y) (+fd x y z) (=/=fd x y) ...)) x ∈ {7} y ∈ {9} z ∈ {16}

slide-38
SLIDE 38

Example

(run* (q) (fresh (x y z) (domfd x ’(7 8 9 10)) (domfd y ’(4 5 8 9 12)) (domfd z ’(1 2 12 16)) (<=fd x y) (+fd x y z) (=/=fd x y) (== q ‘(,x ,y ,z)))) ⇒ ((7 9 16))

slide-39
SLIDE 39

Example

(run* (q) (fresh (x y z) (<=fd x y) (domfd x ’(7 8 9 10)) (+fd x y z) (=/=fd x y) (== q ‘(,x ,y ,z)) (domfd y ’(4 5 8 9 12)) (domfd z ’(1 2 12 16)))) ⇒ ((7 9 16))

slide-40
SLIDE 40

Disequality Over Trees

New operator ≡ (more general than ≡fd)

slide-41
SLIDE 41

Disequality Over Trees

New operator ≡ (more general than ≡fd) (run* (q) (fresh (x y) (conde ((== x 1) (== y 1)) ((== x 2) (== y 2)) ((== x 1) (== y 2)) ((== x 2) (== y 1))) (== q ‘(,x ,y))))

slide-42
SLIDE 42

Disequality Over Trees

New operator ≡ (more general than ≡fd) (run* (q) (fresh (x y) (conde ((== x 1) (== y 1)) ((== x 2) (== y 2)) ((== x 1) (== y 2)) ((== x 2) (== y 1))) (== q ‘(,x ,y)))) ⇒ ((1 1) (2 2) (1 2) (2 1))

slide-43
SLIDE 43

Disequality Over Trees

New operator ≡ (more general than ≡fd) (run* (q) (fresh (x y) (conde ((== x 1) (== y 1)) ((== x 2) (== y 2)) ((== x 1) (== y 2)) ((== x 2) (== y 1))) (=/= ‘(,x ,y) ‘(,y ,x)) (== q ‘(,x ,y))))

slide-44
SLIDE 44

Disequality Over Trees

New operator ≡ (more general than ≡fd) (run* (q) (fresh (x y) (conde ((== x 1) (== y 1)) ((== x 2) (== y 2)) ((== x 1) (== y 2)) ((== x 2) (== y 1))) (=/= ‘(,x ,y) ‘(,y ,x)) (== q ‘(,x ,y)))) ⇒ ((1 2) (2 1))

slide-45
SLIDE 45

Implementation Overview

slide-46
SLIDE 46

Data Structures

cKanren uses a package to store information

slide-47
SLIDE 47

Data Structures

cKanren uses a package to store information Substitution Example: ((x . 1) (y . #t) (z . x))

slide-48
SLIDE 48

Data Structures

cKanren uses a package to store information Substitution Example: ((x . 1) (y . #t) (z . x)) Domain store Example: ((x . (7 8 9)) (y . (2 3 4 5)))

slide-49
SLIDE 49

Data Structures

cKanren uses a package to store information Substitution Example: ((x . 1) (y . #t) (z . x)) Domain store Example: ((x . (7 8 9)) (y . (2 3 4 5))) Constraint store Example: ((proc ≤fd y x) (proc all-diff fd ’(x z h 7)))

slide-50
SLIDE 50

Framework

  • 1. ≡
  • 2. Fixpoint algorithm
  • 3. Consistency checks
  • 4. reify
slide-51
SLIDE 51

Equivalence

◮ Only constraint that is not kept in the constraint store

slide-52
SLIDE 52

Equivalence

◮ Only constraint that is not kept in the constraint store ◮ Uses miniKanren unification

slide-53
SLIDE 53

Fixpoint Algorithm

No constraints directly interact with one another

slide-54
SLIDE 54

Fixpoint Algorithm

No constraints directly interact with one another A framework function reruns constraints on newly ground variables

slide-55
SLIDE 55

Fixpoint Algorithm

No constraints directly interact with one another A framework function reruns constraints on newly ground variables Example: (run* (q) (fresh (x) (infd x q ’(1 2 3)) (+fd x 1 q) ... (== x 2) ...))

slide-56
SLIDE 56

Fixpoint Algorithm

  • 1. Receives variables x∗

For example, x from previous slide, after being unified with 2

slide-57
SLIDE 57

Fixpoint Algorithm

  • 1. Receives variables x∗

For example, x from previous slide, after being unified with 2

  • 2. Grabs current constraint store

Constraint store (... (proc +fd x 1 q) ...)

slide-58
SLIDE 58

Fixpoint Algorithm

  • 1. Receives variables x∗

For example, x from previous slide, after being unified with 2

  • 2. Grabs current constraint store

Constraint store (... (proc +fd x 1 q) ...)

  • 3. Run every constraint involving any variables in x∗ again

... but only if the constraint is still in the store Reruns +fd constraint with new information that x is 2.

slide-59
SLIDE 59

Consistency

Programs with irrelevant but unsatisfiable constraints will fail

slide-60
SLIDE 60

Consistency

Programs with irrelevant but unsatisfiable constraints will fail (run* (q) (fresh (x y z) (infd x y z ’(1 2)) (all-difffd ‘(,x ,y ,z)) (== q 5))) ⇒ ()

slide-61
SLIDE 61

Consistency

Programs with irrelevant but unsatisfiable constraints will fail (run* (q) (fresh (x y z) (infd x y z ’(1 2)) (all-difffd ‘(,x ,y ,z)) (== q 5))) ⇒ () Before returning anything to the user, each variable with finite domain constraints is re-evaluated, to guarantee that there is at least one acceptable value for each constrained variable.

slide-62
SLIDE 62

Reification

reify

◮ Produces the final result returned to the user

slide-63
SLIDE 63

Reification

reify

◮ Produces the final result returned to the user ◮ Constraint store may need consolidation or reformatting

slide-64
SLIDE 64

Reification

reify

◮ Produces the final result returned to the user ◮ Constraint store may need consolidation or reformatting

(run* (q) (=/= q 5)) ⇒ ((

0 :

(=/= (( 0 . 5)))))

slide-65
SLIDE 65

Parameters

slide-66
SLIDE 66

Parameters

process-prefix Can rerun constraints for the variables with new associations

slide-67
SLIDE 67

Parameters

process-prefix Can rerun constraints for the variables with new associations enforce-constraints Consistency checks before reification

slide-68
SLIDE 68

Parameters

process-prefix Can rerun constraints for the variables with new associations enforce-constraints Consistency checks before reification reify-constraints Builds a Scheme data structure that packages the constraint information in a way that is readable to the user

slide-69
SLIDE 69

Composition

Having multiple constraint systems in the same session is tricky, as parameter definitions will overwrite each other

slide-70
SLIDE 70

Composition

Having multiple constraint systems in the same session is tricky, as parameter definitions will overwrite each other (let ((ls (run* (q) (n-queens q 8)))) (run* (s) (all-diffo ls))) Implementor must define parameters in a way that makes sense

slide-71
SLIDE 71

Future Work

◮ Performance ◮ Specialized operators ◮ Adding αKanren ◮ Using different domains? Simulaneously?

slide-72
SLIDE 72

Questions?