Bottom-up FO-FLP Sementics

25
A Bottom-Up Semantics for FLP Giovanni Bacci, Marco Comini 6th October 2009 1 / 18

description

We present a bottom-up fix-point semantics for first-order functonal logic programs like Curry and Toy.

Transcript of Bottom-up FO-FLP Sementics

Page 1: Bottom-up FO-FLP Sementics

A Bottom-Up Semantics for FLP

Giovanni Bacci, Marco Comini

6th October 2009

1 / 18

Page 2: Bottom-up FO-FLP Sementics

Outline

+ Main features of Functional Logic Programs

+ Operational Sematics

+ Bottom-up Semantics Operator (examples)

+ Abstract Semantics (examples)

+ Conclusions

2 / 18

Page 3: Bottom-up FO-FLP Sementics

Functional Logic Languages (FLP)

Are defined over a bipartite signature Σ := C ] D+ Overlapping Rules

R1 : x ? y → xR2 : x ? y → y

1 ? 2

1R1

2

R2

NON-DETERMINISTICFUNCTIONS!!

Example:

R3 : reach(x)→ x ? reach(adj(x))where ∀i , k (i , jk) ∈ E . adj(i)→ j1 ? . . . ? jn

3 / 18

Page 4: Bottom-up FO-FLP Sementics

Functional Logic Languages (FLP)

+ Logical variables

R1 : True && y → yR2 : path(x)→ [x]R3 : path(x)→ adj(x , y) && (x : path(y))

where ∀(i , j) ∈ E . adj(i , j)→ True

Narrowing:

t σ s iff ∃σ. σ(t)→ s

path(1) [1]ε

adj(1, y) && (1 : path(y))

ε

True && (1 : path(2))

{y/2}

(1 : path(2))

ε

[1, 2]ε

...

{y/3}

1

23

4

5

4 / 18

Page 5: Bottom-up FO-FLP Sementics

Needed Narrowing Strategy [Antoy & Hanus]

Example:

R1 : 0 + y → yR2 : s(x) + y → s(x + y)

R3 : 0 ≤ y → TrueR4 : s(x) ≤ 0→ FalseR5 : s(x) ≤ s(y)→ x ≤ y

. . . running a goal Definitional Trees:

y + y ≤ x 0 ≤ x([], y/0, R1)

True([], ε, R3)

s(y1 + s(y1)) ≤ x

([1], y/s(y1), R2)

False

([], x/0, R1)

y1 + s(y1) ≤ x1

([], x/s(x1), R5)

〈y + y ≤ x , {x/0, y/s(y1)},False〉...

x1 + x2

0 + x2 s(x1) + x2

R1 R2

y1 ≤ y2

0 ≤ y2 s(y3) ≤ y2

R3

s(y3) ≤ s(y4)

R5

s(y3) ≤ 0

R4

5 / 18

Page 6: Bottom-up FO-FLP Sementics

FLP vs LP

y + y ≤ 0 returns one computed answer:

〈y + y ≤ 0, {y/s(y1)},False〉

For the same Prolog program:

leq(0,Y , true). sum(0,Y ,Y ).leq(s(X ), 0, false). sum(s(X ),Y , s(Z)) :– sum(X ,Y ,Z).leq(s(X ), s(Y ),Z) :– leq(X ,Y ,Z).

the corresponding goal :– sum(Y ,Y ,D), leq(D, 0,Z ). has anenumerable set of computed answers:

{{Y /sn(0),Z/false} | n ≥ 0}

Lazyness + Narrowing = Neededness⇑ ⇓

nested expressions built-in search

6 / 18

Page 7: Bottom-up FO-FLP Sementics

FLP vs LP

y + y ≤ 0 returns one computed answer:

〈y + y ≤ 0, {y/s(y1)},False〉

For the same Prolog program:

leq(0,Y , true). sum(0,Y ,Y ).leq(s(X ), 0, false). sum(s(X ),Y , s(Z)) :– sum(X ,Y ,Z).leq(s(X ), s(Y ),Z) :– leq(X ,Y ,Z).

the corresponding goal :– sum(Y ,Y ,D), leq(D, 0,Z ). has anenumerable set of computed answers:

{{Y /sn(0),Z/false} | n ≥ 0}

Lazyness + Narrowing = Neededness⇑ ⇓

nested expressions built-in search

6 / 18

Page 8: Bottom-up FO-FLP Sementics

Needed Narrowing Strategy (Formal def.)

NN(t) := {(p · q, σ,R) | t|p outermost op-rooted subterm of t

T def.tree for root(t|p), (q, σ,R) ∈ NN(t|p,T )}

where NN(t,T ) is the least set satisfying:

NN(t,T ) 3

8>>>>>>>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>>:

(Λ, ε, π → r) T = leaf (π → r)

(q, ρi ◦ σ,R) branch(π, p,−→Tn), t|p = x

ρi = {x/ pat(Ti |p)}∃i . (q, σ,R) ∈ NN(ρi (t),Ti )

(p · q, σ,R) T = branch(π, p,−→Tn), t|p = f (. . . )

(q, σ,R) ∈ NN(t|p)

NN(Ti ) T = branch(π, p,−→Tn), t|p = c(. . . )

∃i . pat(Ti ) = c(~y)

s T = or(T1 ,T2 ), ∃i . s ∈ NN(t,Ti )

7 / 18

Page 9: Bottom-up FO-FLP Sementics

Toward a compositional Semantics

Definition (Compositionality)

Js(t1, . . . , tn)KP = ops(Jt1KP , . . . , JtnKP) where s ∈ Σ = C ] D

A natural notion of semantics:

JtKP := {〈t, σ, v〉 | t ∗σ v , v is a value}JPK :=

⋃{JtKP | t is an expression}

IS NOTCOMPOSITIONAL

since, the semantics of the previous program is:

JPK = {〈x1 + x2, {x1/sn(0)}, sn(x2)〉 | n ≥ 0} ∪

{〈x1 ≤ x2, {x1/sn(0), x2/s

n(x ′2)},True〉 | n ≥ 0} ∪

{〈x1 ≤ x2, {x1/sn+1(x ′1), x2/s

n(0)},False〉 | n ≥ 0}

Jy + yKP = {〈y + y , {y/sn(0)}, s2n(0)〉 | n ≥ 0}

..but y + y ≤ x ∗{y/s(y1),x/0} False

8 / 18

Page 10: Bottom-up FO-FLP Sementics

Composing Narrowing Trees

Definition (Compositionality)

Js(t1, . . . , tn)KP = ops(Jt1KP , . . . , JtnKP) where s ∈ Σ = C ] D

We can reconstruct Js(t1, . . . , tn)KP from Jt1KP , . . . , JtnKP bymeans of a “step by step specialization” of Js(x1, . . . , xn)KP

Js(t1, . . . , tn)KP = Js(x1, . . . , xn)KP • Φ[xi/JtiKP ]i=1,...,n

Idea:

t t ′ . . .p, σ = {~x/~v}

Θ = {~x/~T , . . . }

θ = {~x/ rt(~T ), . . . }

θ(t) q,φ . . .

General Step: Parameters: Spec. Step:

we must be able to reconstruct all and only 〈q, φ〉 pairs given bythe needed narrowing strategy

9 / 18

Page 11: Bottom-up FO-FLP Sementics

Embedding the strategy in the step

NN(t,T ) 3

8>>>>><>>>>>:

[. . . ]

(q, ρi ◦ σ,R) branch(π, p,−→Tn), t|p = x

ρi = {x/ pat(Ti |p)}∃i . (q, σ,R) ∈ NN(ρi (t),Ti )

[. . . ]

Definition (Canonical decomposition)

Let t be a term and s = (p,R, σ) ∈ NN(t). The canonicaldecomposition of s is (p,R, ρ1 ◦ · · · ◦ ρn).

The canonical decomposition is unique and, in fact, describes the“travel” made by NN through the fixed set of def. tree forcomputing a single step.

10 / 18

Page 12: Bottom-up FO-FLP Sementics

Composition (Example)

z1 ≤ z2

False

Λz1/s(z′

1) ◦ z2/0•

y + y ≤ x

0 ≤ x

[1], y/0

s(y1 + s(y1)) ≤ x

[1], y/s(y1)

False

Λ, x/0

y + y

0

Λ, y/0

s(y1 + s(y1))

Λ, y/s(y1)

s(s(y2 + s(y2)))

[1], y1/s(y2)

s(s(0))

[1], y1/0

z1/

z2/ x

z ′1/ y1 + s(y1)

s(y2 + s(y2))

Λ, y1/s(y2)

s(0)

Λ, y1/0

11 / 18

Page 13: Bottom-up FO-FLP Sementics

Composition (Example)

z1 ≤ z2

False

Λz1/s(z′

1) ◦ z2/0•

y + y ≤ x

0 ≤ x

[1], y/0

s(y1 + s(y1)) ≤ x

[1], y/s(y1)

False

Λ, x/0

y + y

0

Λ, y/0

s(y1 + s(y1))

Λ, y/s(y1)

s(s(y2 + s(y2)))

[1], y1/s(y2)

s(s(0))

[1], y1/0

z1/

z2/ x

z ′1/ y1 + s(y1)

s(y2 + s(y2))

Λ, y1/s(y2)

s(0)

Λ, y1/0

11 / 18

Page 14: Bottom-up FO-FLP Sementics

Composition (Example)

z1 ≤ z2

False

Λz1/s(z′

1) ◦ z2/0•

y + y ≤ x

0 ≤ x

[1], y/0

s(y1 + s(y1)) ≤ x

[1], y/s(y1)

False

Λ, x/0

y + y

0

Λ, y/0

s(y1 + s(y1))

Λ, y/s(y1)

s(s(y2 + s(y2)))

[1], y1/s(y2)

s(s(0))

[1], y1/0

z1/

z1/

z2/ x

z ′1/ y1 + s(y1)

s(y2 + s(y2))

Λ, y1/s(y2)

s(0)

Λ, y1/0

11 / 18

Page 15: Bottom-up FO-FLP Sementics

Composition (Example)

z1 ≤ z2

False

Λz1/s(z′

1) ◦ z2/0•

y + y ≤ x

0 ≤ x

[1], y/0

s(y1 + s(y1)) ≤ x

[1], y/s(y1)

False

Λ, x/0

y + y

0

Λ, y/0

s(y1 + s(y1))

Λ, y/s(y1)

s(s(y2 + s(y2)))

[1], y1/s(y2)

s(s(0))

[1], y1/0

z1/

z2/ x

z ′1/ y1 + s(y1)

s(y2 + s(y2))

Λ, y1/s(y2)

s(0)

Λ, y1/0

11 / 18

Page 16: Bottom-up FO-FLP Sementics

Composition (Example)

z1 ≤ z2

False

Λz1/s(z′

1) ◦ z2/0•

y + y ≤ x

0 ≤ x

[1], y/0

s(y1 + s(y1)) ≤ x

[1], y/s(y1)

False

Λ, x/0

y + y

0

Λ, y/0

s(y1 + s(y1))

Λ, y/s(y1)

s(s(y2 + s(y2)))

[1], y1/s(y2)

s(s(0))

[1], y1/0

z1/

z1/

z2/ x

z ′1/ y1 + s(y1)

s(y2 + s(y2))

Λ, y1/s(y2)

s(0)

Λ, y1/0

11 / 18

Page 17: Bottom-up FO-FLP Sementics

Composition (Example)

z1 ≤ z2

False

Λz1/s(z′

1) ◦ z2/0•

y + y ≤ x

0 ≤ x

[1], y/0

s(y1 + s(y1)) ≤ x

[1], y/s(y1)

False

Λ, x/0

y + y

0

Λ, y/0

s(y1 + s(y1))

Λ, y/s(y1)

s(s(y2 + s(y2)))

[1], y1/s(y2)

s(s(0))

[1], y1/0

z1/

z1/

z2/ x

z ′1/ y1 + s(y1)

s(y2 + s(y2))

Λ, y1/s(y2)

s(0)

Λ, y1/0

11 / 18

Page 18: Bottom-up FO-FLP Sementics

Composition (Example)

z1 ≤ z2

False

Λz1/s(z′

1) ◦ z2/0•

y + y ≤ x

0 ≤ x

[1], y/0

s(y1 + s(y1)) ≤ x

[1], y/s(y1)

False

Λ, x/0

y + y

0

Λ, y/0

s(y1 + s(y1))

Λ, y/s(y1)

s(s(y2 + s(y2)))

[1], y1/s(y2)

s(s(0))

[1], y1/0

z1/

z1/

z2/ x

z ′1/ y1 + s(y1)

s(y2 + s(y2))

Λ, y1/s(y2)

s(0)

Λ, y1/0

DONE!

11 / 18

Page 19: Bottom-up FO-FLP Sementics

A Fix-point Operator over Narrowing Trees

TP : C→ C where C := (PP → NT )/∼=

TP(I) := λf (~x).∑

f (~t)→r∈PLf (~x); {〈Λ, {~x/~t}, EJrKI〉}M

where EJKI is the evaluation function:

EJxKI := Lx ; ∅MEJc(t1, . . . , tn)KI := Lc(x1, . . . , xn); ∅M • Φ[xi/EJtiKI ]i=1,...,n

EJf (t1, . . . , tn)KI := I(f (x1, . . . , xn)) • Φ[xi/EJtiKI ]i=1,...,n

Theorem (Soundness & Completeness)

+ lfp(TP)(f (~x)) =C {d | d is a derivation starting from f (~x)}/∼=+ EJtKlfp(TP) =C {d | d is a derivation starting from t}/∼=

12 / 18

Page 20: Bottom-up FO-FLP Sementics

Toward a more abstract Semantics

The previous semantics is very precise ... even too muchWe are able to distinguish too many programs:

f → gg → 0g → 1

JP1K =

f 7→ f gε

1

ε

g 7→ g

1

ε

f → 0f → 1g → 0g → 1

JP1K =

f 7→ f

1

ε

g 7→ g

1

ε

13 / 18

Page 21: Bottom-up FO-FLP Sementics

The Context Tree Abstraction

f (g(x), x)

f (C(g(x1)),B(x1))

{x/B(x1)}

f (C(C(g(x2))),B(B(x2)))

{x1/B(x2)}

g(x2)

ε

C(g(x3))

{x2/B(x3)}

C(C(g(x4)))

{x3/B(x4)}

{x/B(x1)}

{x1/B(x2)}

ε

C(�)

{x2/B(x3)}

C(C(�))

{x3/B(x4)}

Functionsbehave as

a black box

Inner stepsare compressed

We just rememberhow the outermost

context grows

Γα(I) := λf (~x). Γ(f (~x))

14 / 18

Page 22: Bottom-up FO-FLP Sementics

The Abstract Fix-point Operator

Using standard techniques from Abstract Interpretation*, wedefined it just by means of Γ and TP :

TαP : A→ A where A := (PP → CT )/∼=

TαP (Iα) = λf (~x).

∑f (~x)→r∈PL�; Sf (~x)→r M

where

Sf (~x)→r = {〈Λ, {~x/~t}, EJrKIα〉 | r is C rooted} ∪

{〈Λ, {~x/~t} ◦ σ,T 〉 | r is D rooted, 〈p, σ,T 〉 ∈ steps(EJrKIα)}

and EJKIα is the abstract evaluation function.

Lemma (E is precise w.r.t. α)

∀I. ∀t. α(EJtKI) = EJtKα(I)

It is able to characterize the computed answer beavior

15 / 18

Page 23: Bottom-up FO-FLP Sementics

Implementation

...loading examplesit can take a while

16 / 18

Page 24: Bottom-up FO-FLP Sementics

Concusions

+ we characterize FLP semantics using a bottom-upconstruction

+ the semantics is goal-independent

+ we found a more abstract semantics which

+ precisely describe the computed answer behavior+ is independent from the chosen set of definitional trees

+ compositionality gives us the possibility to

+ define incremental and modular analysis tools+ ... as well as verification tools.

17 / 18

Page 25: Bottom-up FO-FLP Sementics

Wake up! ...now we can have a coffee

ANY QUESTIONS?

18 / 18