ts t Pttr t - - PowerPoint PPT Presentation

t s t p tt r t
SMART_READER_LITE
LIVE PREVIEW

ts t Pttr t - - PowerPoint PPT Presentation

ts t Pttr t rst sqtr


slide-1
SLIDE 1

✶✴✶✽

▲❡t✬s ❯♥✐❢② ❲✐t❤ ❙❝❛❧❛ P❛tt❡r♥ ▼❛t❝❤✐♥❣✦

❊❞♠✉♥❞ ▲❛♠ ■❧✐❛♥♦ ❈❡r✈❡s❛t♦

s❧❧❛♠❅q❛t❛r✳❝♠✉✳❡❞✉ ✐❧✐❛♥♦❅❝♠✉✳❡❞✉ ❈❛r♥❡❣✐❡ ▼❡❧❧♦♥ ❯♥✐✈❡rs✐t②✱ ◗❛t❛r ❈❛♠♣✉s

❙✉♣♣♦rt❡❞ ❜② ◗◆❘❋ ❣r❛♥t ❏❙❘❊P ✹✲✵✵✸✲✷✲✵✵✶ ❛♥❞ ◆P❘P ✹✲✸✹✶✲✶✲✵✺✾

✷✻ ❏✉♥❡ ✷✵✶✻

slide-2
SLIDE 2

❙❝❛❧❛✱ ❛ ▼♦❞❡r♥✱ ●❡♥❡r❛❧✲P✉r♣♦s❡✱ Pr♦❣r❛♠♠✐♥❣ ▲❛♥❣✉❛❣❡

❚♦♥s ♦❢ ♠♦❞❡r♥ ❢❡❛t✉r❡s✿ ❖❜❥❡❝t✲♦r✐❡♥t❡❞ ♣r♦❣r❛♠♠✐♥❣ ❋✉♥❝t✐♦♥❛❧ ♣r♦❣r❛♠♠✐♥❣ ❆❧❣❡❜r❛✐❝ ❞❛t❛ t②♣❡s ❊①t❡♥s✐❜❧❡ ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚②♣❡ ✐♥❢❡r❡♥❝❡ ▲❛③② ❡✈❛❧✉❛t✐♦♥ ❚❤❡ ❧✐st ❣♦❡s ♦♥ ✳ ✳ ✳ ❇✉t ✳ ✳ ✳ ♥♦ ❧♦✈❡ ❢♦r ✉♥✐✜❝❛t✐♦♥ ✳ ✳ ✳ ❂✭ ◆♦ ❜✉✐❧t✲✐♥ s✉♣♣♦rt ◆♦ ♦✣❝✐❛❧ ❧✐❜r❛r✐❡s

✷✴✶✽

slide-3
SLIDE 3

❙❝❛❧❛✱ ❛ ▼♦❞❡r♥✱ ●❡♥❡r❛❧✲P✉r♣♦s❡✱ Pr♦❣r❛♠♠✐♥❣ ▲❛♥❣✉❛❣❡

❚♦♥s ♦❢ ♠♦❞❡r♥ ❢❡❛t✉r❡s✿ ❖❜❥❡❝t✲♦r✐❡♥t❡❞ ♣r♦❣r❛♠♠✐♥❣ ❋✉♥❝t✐♦♥❛❧ ♣r♦❣r❛♠♠✐♥❣ ❆❧❣❡❜r❛✐❝ ❞❛t❛ t②♣❡s ❊①t❡♥s✐❜❧❡ ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚②♣❡ ✐♥❢❡r❡♥❝❡ ▲❛③② ❡✈❛❧✉❛t✐♦♥ ❚❤❡ ❧✐st ❣♦❡s ♦♥ ✳ ✳ ✳ ❇✉t ✳ ✳ ✳ ♥♦ ❧♦✈❡ ❢♦r ✉♥✐✜❝❛t✐♦♥ ✳ ✳ ✳ ❂✭ ◆♦ ❜✉✐❧t✲✐♥ s✉♣♣♦rt ◆♦ ♦✣❝✐❛❧ ❧✐❜r❛r✐❡s

✸✴✶✽

slide-4
SLIDE 4

✹✴✶✽

❆❧❣❡❜r❛✐❝ ❉❛t❛ ❚②♣❡s ❛♥❞ P❛tt❡r♥ ▼❛t❝❤✐♥❣ ✐♥ ❙❝❛❧❛

❉❡✜♥✐♥❣ ❛❧❣❡❜r❛✐❝ ❞❛t❛ t②♣❡s✿

abstract class Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term case class App(f: Term, v: Term) extends Term

❇✉✐❧t✲✐♥ s✉♣♣♦rt ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣✿

def printTerm(term: Term) { term match { case Var(n) => print(n) case Fun(x, b) => print("^" + x + ".") printTerm(b) case App(f, v) => printTerm(f) print(" ") printTerm(v) } }

slide-5
SLIDE 5

✺✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

slide-6
SLIDE 6

✻✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

1

val x: Term = new LogVar()

2

val y: Term = new LogVar()

3

val f: Term = F(Const(5),x)

4

f unify (

5

Const(4) withMgu θ => {

6

...

7 8

},

9

F(y,Const(4)) >=> {

10

...

11

}

12

)

slide-7
SLIDE 7

✼✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

1

val x: Term = new LogVar() // declaring new logical variable x

2

val y: Term = new LogVar()

3

val f: Term = F(Const(5),x)

4

f unify (

5

Const(4) withMgu θ => {

6

...

7 8

},

9

F(y,Const(4)) >=> {

10

...

11

}

12

)

slide-8
SLIDE 8

✽✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

1

val x: Term = new LogVar() // declaring new logical variable x

2

val y: Term = new LogVar() // declaring new logical variable y

3

val f: Term = F(Const(5),x)

4

f unify (

5

Const(4) withMgu θ => {

6

...

7 8

},

9

F(y,Const(4)) >=> {

10

...

11

}

12

)

slide-9
SLIDE 9

✾✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

1

val x: Term = new LogVar() // declaring new logical variable x

2

val y: Term = new LogVar() // declaring new logical variable y

3

val f: Term = F(Const(5),x) // f is the term F(Const(5),x)

4

f unify (

5

Const(4) withMgu θ => {

6

...

7 8

},

9

F(y,Const(4)) >=> {

10

...

11

}

12

)

slide-10
SLIDE 10

✶✵✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

1

val x: Term = new LogVar() // declaring new logical variable x

2

val y: Term = new LogVar() // declaring new logical variable y

3

val f: Term = F(Const(5),x) // f is the term F(Const(5),x)

4

f unify ( // the unification control statement

5

Const(4) withMgu θ => {

6

...

7 8

},

9

F(y,Const(4)) >=> {

10

...

11

}

12

)

slide-11
SLIDE 11

✶✶✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

1

val x: Term = new LogVar() // declaring new logical variable x

2

val y: Term = new LogVar() // declaring new logical variable y

3

val f: Term = F(Const(5),x) // f is the term F(Const(5),x)

4

f unify ( // the unification control statement

5

Const(4) withMgu θ => { // try unifying f and Const(4), producing mgu θ

6

... // it’s pure: no side-effects on x and y,

7

// substitution θ available

8

},

9

F(y,Const(4)) >=> {

10

...

11

}

12

)

slide-12
SLIDE 12

✶✷✴✶✽

❯♥✐✜❝❛t✐♦♥ ▲✐❜r❛r② ✐♥ ❙❝❛❧❛

❲♦✉❧❞♥✬t ✐t ❜❡ ❣r❡❛t ✐❢ ❙❝❛❧❛ ♣r♦✈✐❞❡❞ ❝♦♥tr♦❧ st❛t❡♠❡♥ts ❢♦r ✉♥✐✜❝❛t✐♦♥❄ ❙✐♠✐❧❛r t♦ ♠❛t❝❤ st❛t❡♠❡♥t ❢♦r ♣❛tt❡r♥ ♠❛t❝❤✐♥❣ ❚❤❛t✬s ✇❤❛t ♦✉r ❙❝❛❧❛ ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ❞♦❡s✦

1

val x: Term = new LogVar() // declaring new logical variable x

2

val y: Term = new LogVar() // declaring new logical variable y

3

val f: Term = F(Const(5),x) // f is the term F(Const(5),x)

4

f unify ( // the unification control statement

5

Const(4) withMgu θ => { // try unifying f and Const(4), producing mgu θ

6

... // it’s pure: no side-effects on x and y,

7

// substitution θ available

8

},

9

F(y,Const(4)) >=> { // try unifying f and F(y,Const(4)), ‘‘imperatively’’

10

... // mgu [✺/y, ✹/x] applied to x and y as side-effect

11

}

12

)

slide-13
SLIDE 13

✶✸✴✶✽

❯♥✐✜❝❛t✐♦♥ ✇✐t❤ ❊①t❡♥s✐❜❧❡ P❛tt❡r♥ ▼❛t❝❤✐♥❣

❆♥ ❛❧t❡r♥❛t✐✈❡ ❛❜str❛❝t✐♦♥ ❖✉r ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ✏✐♥t❡❣r❛t❡s✑ ✇✐t❤ ♣❛tt❡r♥ ♠❛t❝❤✐♥❣✿

1

val x: Term = new LogVar()

2

val y: Term = new LogVar()

3

val f: Term = F(Const(5),x)

4

val unifA = new Unif( Const(4) ) // ‘‘Unification extractor’’ for Const(4)

5

val unifB = new Unif( F(y,Const(4)) ) // ‘‘Unification extractor’’ for F(y,Const(4))

6

f match {

7

case unifA(θ) => ... // Try unifying with Const(4) and extract θ

8

case unifB(θ) => ... // Try unifying with F(y,Const(4)) and extract θ

9

}

■t✬s ♣♦ss✐❜❧❡✱ ✇✐t❤ ❡①t❡♥s✐❜❧❡ ♣❛tt❡r♥ ♠❛t❝❤✐♥❣✦

slide-14
SLIDE 14

✶✹✴✶✽

❯♥✐✜❝❛t✐♦♥ ✇✐t❤ ❊①t❡♥s✐❜❧❡ P❛tt❡r♥ ▼❛t❝❤✐♥❣

❆♥ ❛❧t❡r♥❛t✐✈❡ ❛❜str❛❝t✐♦♥ ❖✉r ✉♥✐✜❝❛t✐♦♥ ❧✐❜r❛r② ✏✐♥t❡❣r❛t❡s✑ ✇✐t❤ ♣❛tt❡r♥ ♠❛t❝❤✐♥❣✿

1

val x: Term = new LogVar()

2

val y: Term = new LogVar()

3

val f: Term = F(Const(5),x)

4

val unifA = new Unif( Const(4) ) // ‘‘Unification extractor’’ for Const(4)

5

val unifB = new Unif( F(y,Const(4)) ) // ‘‘Unification extractor’’ for F(y,Const(4))

6

f match {

7

case unifA(θ) => ... // Try unifying with Const(4) and extract θ

8

case unifB(θ) => ... // Try unifying with F(y,Const(4)) and extract θ

9

}

■t✬s ♣♦ss✐❜❧❡✱ ✇✐t❤ ❡①t❡♥s✐❜❧❡ ♣❛tt❡r♥ ♠❛t❝❤✐♥❣✦

slide-15
SLIDE 15

✶✺✴✶✽

❊①t❡♥s✐❜❧❡ P❛tt❡r♥ ▼❛t❝❤✐♥❣ ✐♥ ❙❝❛❧❛

❯s❡r✲❞❡✜♥❛❜❧❡ ♣❛tt❡r♥ ❡①tr❛❝t♦rs✱ t♦ ❜❡ ✉s❡❞ ✐♥ ❙❝❛❧❛✬s ♠❛t❝❤ st❛t❡♠❡♥ts ❆ ❝❧❛ss✐❝ ❡①❛♠♣❧❡✿

1

  • bject Twice {

2

def unapply(x: Int): Option[Int] = if(x%2==0) Some(x/2) else None

3

def test(x: Int) {

4

x match {

5

case Twice(y) => println(x + "is even and twice " + y)

6

case _ => println(x + " is odd")

7

}

8

}

9

}

t♦ ♦❜t❛✐♥ y✱ unapply ✐s ✐♠♣❧✐❝✐t❧② ❝❛❧❧❡❞ ✐♥ t❤❡ ♠❛t❝❤ st❛t❡♠❡♥t ❯♥✐✜❝❛t✐♦♥ ❡①tr❛❝t♦r ❞❡✜♥❡s ❛ ✏❢❛♠✐❧②✑ ♦❢ ✉♥✐✜❝❛t✐♦♥ ♣❛tt❡r♥ ❡①tr❛❝t♦rs✿

slide-16
SLIDE 16

✶✻✴✶✽

❊①t❡♥s✐❜❧❡ P❛tt❡r♥ ▼❛t❝❤✐♥❣ ✐♥ ❙❝❛❧❛

❯s❡r✲❞❡✜♥❛❜❧❡ ♣❛tt❡r♥ ❡①tr❛❝t♦rs✱ t♦ ❜❡ ✉s❡❞ ✐♥ ❙❝❛❧❛✬s ♠❛t❝❤ st❛t❡♠❡♥ts ❆ ❝❧❛ss✐❝ ❡①❛♠♣❧❡✿

1

  • bject Twice {

2

def unapply(x: Int): Option[Int] = if(x%2==0) Some(x/2) else None

3

def test(x: Int) {

4

x match {

5

case Twice(y) => println(x + "is even and twice " + y)

6

case _ => println(x + " is odd")

7

}

8

}

9

}

t♦ ♦❜t❛✐♥ y✱ unapply ✐s ✐♠♣❧✐❝✐t❧② ❝❛❧❧❡❞ ✐♥ t❤❡ ♠❛t❝❤ st❛t❡♠❡♥t ❯♥✐✜❝❛t✐♦♥ ❡①tr❛❝t♦r Unif ❞❡✜♥❡s ❛ ✏❢❛♠✐❧②✑ ♦❢ ✉♥✐✜❝❛t✐♦♥ ♣❛tt❡r♥ ❡①tr❛❝t♦rs✿

1

class Unif[A](pat: Term[A]) {

2

def unapply(t: Term[A]): Option[Subst] =

3

t.mgu(pat) // return mgu of t and pat if it exists (option type)

4

}

slide-17
SLIDE 17

✶✼✴✶✽

❈✉rr❡♥t ❙t❛t✉s

❖♣❡♥✲s♦✉r❝❡ ❛♥❞ ❛✈❛✐❧❛❜❧❡ ❛t✿

https://github.com/sllam/unifscala

P❧❡❛s❡ st❛r ✐t✦ ❋✉t✉r❡ ✇♦r❦s✿ ❍✐❣❤❡r ❧❡✈❡❧ ❝♦♠❜✐♥❛t♦rs ✭❡✳❣✳✱ ❜❛❝❦tr❛❝❦✐♥❣✱ ❝♦♥str❛✐♥t s♦❧✈✐♥❣✮ ❯♥✐✜❝❛t✐♦♥ ♦✈❡r s❡ts ❛♥❞ ♠✉❧t✐s❡ts

slide-18
SLIDE 18

✶✽✴✶✽

❚❤❛♥❦ ②♦✉✦ ◗✉❡st✐♦♥s ♣❧❡❛s❡❄