Documente Academic
Documente Profesional
Documente Cultură
Programare Logic
a
Ioana Leustean
http://moodle.fmi.unibuc.ro/course/view.php?id=186
Introducere in MAUDE
Semantica termenilor
Specificatii algebrice
-algebra initiala
Rescrierea termenilor
Maude
Programare Logica
I
Maude
http://maude.cs.uiuc.edu/
Maude
Maude
Este un interpretor.
Comenzi sistem
Maude> in PL/Exemple-Curs1.maude
==========================================
fmod MYNAT
Maude> show module MYNAT .
fmod MYNAT is
sorts Nat .
op 0 : -> Nat .
op s_ : Nat -> Nat .
endfm
Maude> select MYNAT .
in ../nume-fisier.mod
show module nume-modul .
select nume-modul .
show sorts .
show ops .
reduce termen .
reduce termen1 == termen2 .
parse term .
set trace on .
set trace off .
quit
Manual de Maude (html)
10
Exemplul 1
Maude> reduce s s s s s 0 .
reduce in MYNAT : s s s s s 0 .
rewrites: 0 in 6641663802ms cpu (0ms real) (0 rew/sec)
result Nat: s s s s s 0
Maude> parse s s s s s 0 .
Nat: s s s s s 0
Maude> reduce s s s 0 == s s 0 .
reduce in MYNAT : s s s 0 == s s 0 .
rewrites: 1 in 10534570934ms cpu (0ms real) (0 rew/sec)
result Bool: false
fmod MYNAT is
sorts Nat .
op 0 : -> Nat .
op s_ : Nat -> Nat .
endfm
Acest modul introduce tipul de date Nat.
Datele de tip Nat sunt:
0, s 0, s s 0, s s s 0, s s s s 0, s s s s s
0, ...
Reprezentarea matematica:
S = {Nat}, = {0 : Nat, s : Nat Nat}
(S, ) este o signatura si defineste o clasa de algebre (structuri,
structuri algebrice). Algebra (N, 0, succesor ) este un obiect
privilegiat al acestei clase.
11
12
Importuri
fmod MYNATLIST is
protecting MYNAT .
sort ListNat .
subsort Nat < ListNat .
op nil : -> ListNat .
op _;_ : ListNat ListNat -> ListNat [ assoc ] .
var L : ListNat .
eq nil ; L = L .
eq L ; nil = L .
endfm
Structura general
a a unui modul:
importuri (protecting, extending, including),
declararea sorturilor si a subsorturilor,
declararea operatiilor,
declararea variabilelor,
ecuatii.
protecting
se foloseste atunci cand datele definite in modulul important
nu sunt afectate de operatiile dau ecuatiile noului modul: nu
se construiesc date noi pe sorturi vechi (no junk) si nu sunt
identificate date care in modulul initial erau diferite (no
confusion)
extending
permite aparitia unor date noi pe sorturile vechi (junk) dar
nu permite identificarea datelor care in modulul initial erau
diferite (no confusion)
including
nu are restrictii
13
[assoc]
14
Exemplul 2
16
Exemplul 2
Exemplul 2
Reprezentarea matematica:
Reprezentarea matematica:
Signatura (ordonat-sortat
a): ((S, ), )
S = {Nat, ListNat}, S S, = {(Nat, ListNat)}
= {0 : Nat, s : Nat Nat, nil : ListNat,
; : ListNat ListNat ListNat}
17
Exemplul 2
18
Exemplul 2
Date de tipul ListNat sunt:
fmod MYNATLIST is
protecting MYNAT .
sort ListNat .
subsort Nat < ListNat .
op nil : -> ListNat .
op _;_ : ListNat ListNat -> ListNat [ assoc ] .
var L : ListNat .
eq nil ; L = L .
eq L ; nil = L .
endfm
nil ; s 0
s s 0 ; s 0 ; 0
(nil ; s 0) ; s s 0 ; nil ; 0
Aceste date le numim expresii sau termeni.
Un program este un modul, adic
a o specificatie; modelul
matematic al unei specificatii este o algebr
a de termeni; o
executie este o rescriere n algebra de termeni asociat
a.
Maude> select MYNATLIST .
Maude> reduce (nil ; s 0) ; s s 0 ; nil ; 0 .
reduce in MYNATLIST : (nil ; s 0) ; s s 0 ; nil ; 0 .
rewrites: 2 in 2753904389ms cpu (0ms real)
result ListNat: s 0 ; s s 0 ; 0
20
Exemplul 2
Exemplul 2
fmod MYNATLIST is
protecting MYNAT .
sort ListNat .
subsort Nat < ListNat .
op nil : -> ListNat .
op _;_ : ListNat ListNat -> ListNat [ assoc ] .
var L : ListNat .
eq nil ; L = L .
eq L ; nil = L .
endfm
Ce date defineste MYNATLIST?
21
Exemplul 2
Exemplul 3
fmod MYNATLIST is
protecting MYNAT .
sort ListNat .
subsort Nat < ListNat .
op nil : -> ListNat .
op _;_ : ListNat ListNat -> ListNat [ assoc ] .
var L : ListNat .
eq nil ; L = L .
eq L ; nil = L .
endfm
S
Modulul MYNATLIST defineste k0 Nk
(secventele ordonate finite de numere naturale ).
Maude> reduce (0 ; s 0 ) == (0 ; s 0 ; 0) .
...
result Bool: false
22
fmod MYNATLIST1 is
protecting MYNAT .
sort ListNat .
subsort Nat < ListNat .
op nil : -> ListNat .
op _;_ : ListNat ListNat -> ListNat [ assoc comm ] .
var L : ListNat .
var I : Nat .
eq nil ; L = L .
eq L ; nil = L .
eq I ; I = I .
endfm
Maude> reduce (0 ; s 0)
...
result Bool: true
23
== (0 ; s 0 ; 0) .
24
Exemplul 4
Exemplul 5
fmod COMPLEXINT is
protecting INT .
sort ComplexInt .
subsort Int < ComplexInt .
op _+i_ : Int Int -> ComplexInt .
op i_ : Int -> ComplexInt .
op _+_ : ComplexInt ComplexInt -> ComplexInt [ditto] .
var z : Int .
eq 0 +i z = i z .
eq z +i 0 = z .
...
endfm
fmod GRUP is
sort Element .
op e : -> Element .
op _+_ : Element Element -> Element [ assoc ] .
op -_ : Element -> Element .
vars x y : Element .
eq e + x = x .
eq x + e = x .
eq (- x) + x = e .
eq x + (- x) = e .
eq - - x = x .
eq - e = e .
eq - (x + y) = (- y) + (- x) .
endfm
TRS canonic
Exemplul 5
26
t1 t si t2 t.
Maude-ul poate fi utilizat ca demonstrator.
27
- y + x)) == y .
(- y + x) == y .
x .
- - y
28
Exemplul 5
Exemplul 6
fmod MYNATLIST is
protecting MYNAT .
sort ListNat .
subsort Nat < ListNat .
op nil : -> ListNat .
op _;_ : ListNat ListNat -> ListNat [ assoc ] .
var L : ListNat .
eq nil ; L = L .
eq L ; nil = L .
endfm
*********** equation
eq x + - x = e .
x --> x
x + - x + y ---> e + y
*********** equation
eq e + x = x .
x --> y
e + y ---> y
*********** equation
(built-in equation for symbol _==_)
y == y ---> true
rewrites: 5 in 179156223ms cpu (23ms real)
result Bool: true
fmod MYNATLIST2 is
protecting MYNATLIST .
op _<_ : Nat Nat -> Bool .
...
endfm
29
Exemplul 6
30
Exemplul 6
fmod MYNATLIST2 is
protecting MYNATLIST .
op _<_ : Nat Nat -> Bool .
vars I J : Nat .
eq s I < s J = I < J .
eq 0 < s I = true .
eq I < 0 = false .
ceq I ; J = J ; I if (J < I ) .
endfm
ceq I ; J = J ; I if (J < I ) .
Ce tip de date defineste modulul MYNATLIST2 ?
31
32
Exemplul 7
Exemplul 8
fth TRIV is
sort Elt.
endfh
fmod STACK{X :: TRIV} is ... endfm
Instantiind modulul parametru X cu modulul predefinit NAT
definim stivele de numere naturale.
view Inst from TRIV to NAT is
sort Elt to Nat .
endv
fmod STACKNAT is
protecting STACK{Inst} .
endfm
34
33
Exemplul 8
35
modularizare si parametrizare,
extensibilitate,
36
Programare logica
Multimi S-sortate
S 6=
I
I
I
I
= {s }sS , s = or. s S,
A B As Bs or. s S,
A B = {As Bs }sS , A B = {As Bs }sS ,
A B = {As Bs }sS .
37
38
Functii S-sortate
Functii S-sortate
Daca f : A B si g : B C , definim
f ; g : A C , f ; g = {fs ; gs }sS
fs ; gs : As Cs ,
fs ; gs (a) := gs (fs (a)) or. s S, a As
1A : A A, 1A = {1As }sS
f ; 1B = f , 1A ; f = f or. f : A B
39
40
Signaturi multisortate
Signaturi
S multimea sorturilor
multimea simbolurilor de operatii : s1 sn s
I
I
= (w ,s )w S ,sS
w ,s : w S
w = s1 sn S
= (w ,s )w S ,sS
w ,s : w s
w = s1 sn S
42
Exemple
BOOL = (S, )
I
I
Exemple
S = {bool}
= {T : bool, F : bool,
: bool bool,
: bool bool bool,
: bool bool bool}
NAT = (S, )
I
I
NATBOOL = (S, )
S = {nat}
= {0 : nat, succ : nat nat}
43
S = {bool, nat}
= {T : bool, F : bool, 0 : nat,
succ : nat nat,
: nat nat bool}
= (w ,s )w S ,sS
,bool = {T , F }, ,nat = {0},
nat,nat = {succ}, nat nat,bool = {},
w ,s = pentru celelalte < w , s > S S
44
Exemple
Exemple
I
I
STIVA = (S, )
I
I
AUTOMAT = (S, )
I
S = {elem, stiva}
= {0 : elem, empty : stiva,
push : elem stiva stiva,
pop : stiva stiva,
top : stiva elem}
GRAF = (S, )
I
I
S = {arc, nod}
= {v 0 : arc nod, v 1 : arc nod}
45
46
Signaturi ordonat-sortate
Algebre multisortate
conditia de monotonie
w1 ,s1 w2 ,s2 , w1 w2 s1 s2
Exemplu:
S = {elem, stiva, nvstiva}, elem stiva, nvstiva stiva
= {empty : stiva, push : elem stiva nvstiva,
pop : nvstiva stiva, top : nvstiva elem}.
In practica se folosesc signaturi ordonat-sortate.
daca : s atunci A As
daca : s1 sn s atunci A : As1 Asn As
47
48
Exemple
Exemple
BOOL = (S = {bool}, )
= {T : bool, F : bool, : bool bool,
: bool bool bool, : bool bool bool}
BOOL-algebra A:
Abool := {0, 1}
AT := 1, AF := 0, A (x) := 1 x,
A (x, y ) := max(x, y ), A (x, y ) := min(x, y )
BOOL-algebra B:
Bbool := P(N)
BT := N, BF := , B (X ) := N \ X ,
B (X , Y ) := X Y , B (X , Y ) := X Y
NAT = (S = {nat}, )
= {0 : nat, succ : nat nat}
NAT -algebra A:
Anat := N
A0 := 0, Asucc (x) := x + 1
NAT -algebra B:
Bnat := {0, 1}
B0 := 0, Bsucc (x) := 1 x
NAT -algebra C :
Cnat := {2n |n N}
C0 := 1, Csucc (2n ) := 2n+1
50
49
Exemple
STIVA-algebra A:
Aelem := N, Astiva := N
A0 := 0, Aempty := , Apush (n, n1 nk ) := n n1 nk ,
Apop () = Apop (n) := ,
Apop (n1 n2 nk ) := n2 nk pt. k 2,
Atop () := 0, Atop (n1 nk ) := n1 pt. k 1.
STIVA-algebra B:
Belem := {0}, Bstiva := N
B0 := 0, Bempty := 0, Bpush (0, n) := n + 1 or. n,
Bpop (0) := 0, Bpop (n) := n 1 pt. n 1,
Btop (n) := 0 or. n.
Exemple
51
AUTOMAT -algebra A:
Aintrare = {x, y }, Astare = {s0, s1}, Aiesire := {T , F }
As0 := s0, Ag (s0) := F , Ag (s1) := T ,
Af (x, s0) := s0, Af (y , s0) := s1,
Af (x, s1) := s0, Af (y , s1) := s1
AUTOMAT -algebra B:
Bintrare = Bstare = Biesire := N
Bs0 := 0, Bf (m, n) := m + n, Bg (n) := n + 1
52
Algebre ordonat-sortate
Multime de variabile
s1 s2 As1 As2
w1 ,s1 w2 ,s2 , w1 w2
2 ,s2
1 ,s1
Aw
(x) = Aw
(x) oricare x Aw1 .
Xs Xs 0 = or. s 6= s 0
|X | || =
54
53
(T1) Xs T (X )s
T = T ()
55
56
Exemple
Exemple
STIVA = (S, )
S = {elem, stiva}, Xelem = {x, y }, Xstiva = ,
= {0 : elem, empty : stiva,
push : elem stiva stiva,
pop : stiva stiva,
top : stiva elem}
T (X )elem = {0, x, y , top(pop(empty)),
top(push(x, empty)), . . .}
T (X )stiva = {empty, push(y , empty), pop(empty),
push(top(empty), empty), . . .}
NATBOOL = (S, )
S = {bool, nat}
= {T : bool, F : bool, 0 : nat,
s : nat nat,
: nat nat bool}
58
57
Exemple
Expresii aritmetice
NATEXP = (S = {nat}, )
59
60
Inductia pe termeni
Algebra termenilor
Algebra termenilor
pasul initial:
P(x) = true or. x X , P() = true or. : s,
pasul de inductie:
daca t1 T (X )s1 , . . ., tn T (X )sn si
P(t1 ) = = P(tn ) = true atunci
P((t1 , . . . , tn )) = true or. : s1 . . . sn s.
61
62
63
64
NATEXP = (S = {nat}, )
T0 := 0,
Ts (t) := s(t),
T+ (t1 , t2 ) := +(t1 , t2 )
T (t1 , t2 ) := (t1 , t2 )
Programare logica
Morfisme
Exemplu. Signatura
MONOID = (S = {s}, = {e : s, : ss s})
Fie (A, 1, ?) si (B, 0, +) monoizi
(Ae := 1, A := ?, Be := 0, B := +)
O functie f : A B este morfism de monoizi daca:
Morfisme.
Tipuri abstracte de date.
Algebre initiale
f (1) = 0 f (Ae ) = Be
A
f
A
f
A
f
B
66
65
Morfisme
Exemplu
fs (A ) = (B ) oricare : s,
NAT -algebra A:
Anat := N, A0 := 0, Asucc (x) := x + 1
NAT -algebra B:
Bnat := {0, 1}, B0 := 0, Bsucc (x) := 1 x
As1
fs1
Bs1
...
Asn
fsn
Bsn
As
fs
Bs
Observatie
1A : A A este morfism or. A (S, )-algebra.
67
68
Exemple
Exemple
STIVA-algebra A:
Aelem := N, Astiva :=
A0 := 0, Aempty := , Apush (n, n1 nk ) := n n1 nk ,
Apop () = Apop (n) := ,
Apop (n1 n2 nk ) := n2 nk pt. k 2,
Atop () := 0, Atop (n1 nk ) := n1 pt. k 1.
STIVA-algebra B:
Belem := {0}, Bstiva := N
B0 := 0, Bempty := 0, Bpush (0, n) := n + 1 or. n,
Bpop (0) := 0, Apop (n) := n 1 pt. n 1,
Btop (n) := 0 or. n.
69
70
Morfisme de mpo
Proprietati
Observatie.
Propozitia 1.
Dem.
I
: s
(f ; g )s (A ) = gs (fs (A )) = gs (B ) = C ,
71
72
Proprietati
Continuare
Teorema 2.
Exista un unic morfism f : T B or. B (S, )-algebra.
Demonstram ca f este morfism.
dc. : s , at. fs (T ) = fs () = B ,
dc. : s1 . . . sn s si
t1 T s1 , . . ., tn T sn at.
fs (T (t1 , . . . , tn )) = fs ((t1 , . . . , tn ))
= B (fs1 (t1 ), , fsn (tn ))
74
Continuare
Exemplu
D = (DS , D ), Ds := N or. s S
daca : s atunci D := 0,
daca : s1 . . . sn s, k1 , . . ., kn N
D (k1 , . . . , kn ) := 1 + max(k1 , . . . , kn )
fD : T D unicul morfism
Ce reprezinta valoarea fD (t) pentru un termen t?
76
Termeni ca arbori
Izomorfisme
T s
t = (t1 , . . . , tn )
arb() :=
Propozitia 3
arb(t) :=
arb(t1 )
arb(tn )
77
78
Continuare
Izomorfisme de mpo
dc. : s1 . . . sn s si
b1 Bs1 , . . ., bn Bsn at.
ex. a1 As1 , . . ., an Asn a..
fs1 (a1 ) = b1 , . . ., fsn (an ) = bn si
fs1 (B )
= A ,
(A = {x1 , x2 , x3 , x4 }, ), unde x1 x2 x3 x4
f :AB
f (00) = x1 , f (01) = x2 , f (10) = x3 , f (11) = x4
este functie bijectiva si crescatoare
A 6' B
79
80
Izomorfisme
Exemple
A'B B'A
A 6' B
A'C
f : A C , f (n) := 2n este un NAT -izomorfism.
Observatie.
A ' B As ' Bs oricare s S
81
82
Exemple
BOOL = (S = {bool}, )
= {T : bool, F : bool, : bool bool,
: bool bool bool, : bool bool bool}
Abool := {0, 1}
AT := 1, AF := 0, A (x) := 1 x,
A (x, y ) := max(x, y ), A (x, y ) := min(x, y )
Bbool := P(N)
BT := N, BF := , B (X ) := N \ X ,
B (X , Y ) := X Y , B (X , Y ) := X Y
A 6' B
Exemple
83
BOOL-algebra C :
Cbool := {t : N {0, 1}| t functie}
CT (n) := 1, CF (n) := 0 or. n N
C (t)(n) := 1 t(n) or. t Cbool , n N
C (t1 , t2 )(n) := max(t1 (n), t2 (n)) or. t1 , t2 Cbool , n N
C (t1 , t2 )(n) := min(t1 (n), t2 (n)) or. t1 , t2 Cbool , n N
B'C
f : B C , f (Y ) := Y oricare Y P(N)
f (Y )(n) = 1 daca n Y , f (Y )(n) = 0 daca n 6 Y
f este BOOL-izomorfism
84
Exemple
Exemple
STIVA-algebra A:
Aelem := N, Astiva := N
Aempty := , Apush (n, n1 nk ) := n n1 nk ,
Apop () = Apop (n) := ,
Apop (n1 n2 nk ) := n2 nk pt. k 2,
Atop () := 0, Atop (n1 nk ) := n1 pt. k 1.
STIVA-algebra C :
Celem := Z, Cstiva := Z
C0 := 0, Cempty := , Cpush (x, x1 xk ) := x1 xk x,
Cpop () = Cpop (x) := ,
Cpop (x1 xk1 xk ) := x1 xk1 pt. k 2,
Ctop () := 0, Ctop (x1 xk ) := xk pt. k 1.
A 6' B
A0 := 0,
85
86
Exemple
ADT
STIVA-algebra A:
Aelem := N, Astiva := N
STIVA-algebra C :
Celem := Z, Cstiva := Z
f : A C , f = (felem : N Z, fstiva : N Z )
felem (2k) := k, felem (2k + 1) := k 1 pt. k N,
fstiva (n1 nk ) := felem (nk ) felem (n1 ) pt. n1 nk N .
f este STIVA-izomorfism
87
88
ADT
Algebra initiala
Propozitia 4
A, B C A ' B.
I
89
90
Continuare
Existenta. h := A ; fB : A B mofism
(b) A1 si A2 initiale n K
91
92
Programare logica
Subalgebre
A Bs or. : s,
A (b1 , . . . , bn ) Bs or. : s1 sn s,
or. (b1 , . . . , bn ) Bs1 Bsn .
B := A or. : s,
93
94
Exemple
Exemple
BOOL-algebra B:
Bbool := P(N)
BT := N, BF := , B (X ) := N \ X ,
B (X , Y ) := X Y , B (X , Y ) := X Y
B1 = {, N} {{n}|n N}
nu este subalgebra.
B2 = {, N} {{n}, N \ {n}}
este subalgebra (n N fixat).
95
AUTOMAT -algebra A
Aintrare = {x, y }, Astare = {s0, s1}, Aiesire := {T , F }
As0 := s0, Ag (s0) := F , Ag (s1) := T ,
Af (x, s0) := s0, Af (y , s0) := s1,
Af (x, s1) := s0, Af (y , s1) := s1
96
Multimi de generatori
F := {B A |B subalgebra, X B}
I
I
I
A F, deci F 6= T
{Bi }iI F implica iI Bi F
T
X = {B | B F}
X0 := X ,
Xn+1,s := Xn,s {A | : s} {A (a1 , . . . , an ) |
: s1 . . . sn s, (a1 , . . . , an ) Xn,s1 Xn,sn }.
S
Propozitia 5. X = n Xn .
Dem: va fi adaugata!
98
97
Proprietati
Continuare
h : A B functie
h1 : P(B) P(A), h1 (Y ) := {a A|h(a) Y }
imaginea inversa
Propozitia 6.
Fie h : A B morfism.
I
(d) h1 (Y ) h1 ( Y ) pentru Y B.
99
100
Algebre libere
Observatii
101
102
103
104
Exemple
STIVA-algebra A:
Aelem := N, Astiva := N
A0 := 0, Aempty := , Apush (n, n1 nk ) := n n1 nk ,
Apop () = Apop (n) := ,
Apop (n1 n2 nk ) := n2 nk pt. k 2,
Atop () := 0, Atop (n1 nk ) := n1 pt. k 1.
Programare logica
Multime de variabile
Semantica termenilor
Xs Xs 0 = or. s 6= s 0
|X | || =
105
106
Algebra de termeni T (X )
(T1) Xs T (X )s
107
108
Semantica termenilor
e : T (X ) A.
Dem.
I Definim
e(t) prin inductie pe termeni:
I
I
I
I
I
or. x Xs ,
es (x) := es (x),
or. : s,
es () := A
or. : s1 sn s, or. t1 T (X )s1 , . . ., tn T (X )sn
es ((t1 , . . . , tn )) := A (
es1 (t1 ), . . . ,
esn (tn ))
Demonstram ca
e : T (X ) A este morfism.
Daca f : T (X ) A morfism si f |X = e atunci se
.
demonstreaza prin inductie pe termeni ca f = e
110
109
Exemple
STIVA-algebra A:
Aelem := N, Astiva := N
A0 := 0, Aempty := , Apush (n, n1 nk ) := n n1 nk ,
Apop () = Apop (n) := ,
Apop (n1 n2 nk ) := n2 nk pt. k 2,
Atop () := 0, Atop (n1 nk ) := n1 pt. k 1.
Exemplu
e:X A
e(x) := 5, e(y ) := 3, e(s) := 6 7
STIVA-algebra B:
Belem := {0}, Bstiva := N
B0 := 0, Bempty := 0, Bpush (0, n) := n + 1 or. n,
Bpop (0) := 0, Bpop (n) := n 1 pt. n 1,
Btop (n) := 0 or. n.
e:X B
e(x) := 0, e(y ) := 0, e(s) := 10
111
112
Consecinte
Proprietati
Propozitia 8.
X =
T este (S, )-algebra initiala.
A = T (Y )
O substitutie este o atribuire : X T (Y ).
113
Proprietati
Proprietati
Propozitia 10.
Propozitia 9.
X ' Y T (X ) ' T (Y )
114
Ab
h
g
/B
O
f
T (X )
116
A o -algebra, t T (X )
At (a1 , . . . , an ) :=
e(t), unde e(xi ) := ai or. i = 1, . . . , n
Mem := {e : X D| e functie}
Sem(x := t) : Mem Mem
e(t) daca y = x,
Sem(x := t)(e)(y ) :=
e(y ) daca y =
6 x.
117
118
119
120
Programare logica
(X )t =s t 0 .
A |= (X )t =s t 0 .
121
122
Necesitatea cuantificarii
Ecuatiile conditionate
(X )t =s t 0 if H.
si este formata din:
I
T,s = , T,bool = {T , F }
T |= (X )T =bool F ,
unde Xs := {x}, Xbool :=
o multime de variabile X ,
124
Ecuatiile conditionate
Exemplu
A (S, )-algebra A, := (X )t =s t 0 if H
STIVA-algebra A:
Aelem := N, Astiva := N
A0 := 0, Aempty := , Apush (n, n1 nk ) := n n1 nk ,
Apop () = Apop (n) := ,
Apop (n1 n2 nk ) := n2 nk pt. k 2,
Atop () := 0, Atop (n1 nk ) := n1 pt. k 1.
STIVA-algebra B:
Belem := {0}, Bstiva := N
B0 := 0, Bempty := 0, Bpush (0, n) := n + 1 or. n,
Bpop (0) := 0, Bpop (n) := n 1 pt. n 1, Btop (n) := 0 or. n.
A |= si B |=
es 0 (u) =
es 0 (v ) or. u =s 0 v H
es (t) =
es (t 0 ).
pentru orice e : X A.
Vom nota A |= (X )t =s t 0 if H.
I
A |= (X )t =s t 0 A |= (X )t =s t 0 if
126
125
Exemplu
Exemplu
STIVA-algebra C :
Celem := N, Cstiva := N
C0 := 0, Cempty := , Cpush (n, n1 nk ) := n1 nk n,
Cpop () = Cpop (n) := ,
Cpop (n1 n2 nk ) := n2 nk pt. k 2,
Ctop () := 0, Ctop (n1 nk ) := n1 pt. k 1.
C 6|=
e : X C evaluarea definita prin
128
Programare logica
-algebre
Specificatii algebrice
130
129
-algebre
Specificatii
Teorema 11.
t0
if H.
A |= B |=
I
es 0 (u) =
es 0 (v ) or. u =s 0 v H.
Fie e : X B a..
Definim f : X A prin f := e;
Atunci f =
e; , deoarece f|X = (
e; )|X .
fs 0 (u) = s 0 (
es 0 (u)) = s 0 (
es 0 (v )) = fs 0 (v ) or. u =s 0 v H
Din ipoteza rezulta fs (t) = fs (t 0 ), i.e.
s (
es (t)) = s (
es (t 0 )).
este injectiv, deci
es (t) =
es (t 0 ).
endfth
131
132
Specificatii echivalente
Consecinte semantice
fth GROUP is
sort Element .
op e : -> Element.
op _+_ : Element Element -> Element [assoc] .
op -_ : Element -> Element .
vars x y : Element .
eq e + x = x .
eq x + e = x .
eq (- x) + x = e .
eq x + (- x) = e .
endfth
133
Teoria grupurilor
134
Specificatii echivalente
({x})e + x = x,
({x})x + e = x,
({x})(x) + x = e,o
({x})x + (x) = e
A |= 1 A |= 2
Observatie
I
I
2 := ({x, y })x + y = y + x
|= 1 , 6|= 2
135
136
-algebra initiala
Specificatie corecta
(S, , ) specificatie
I, := {A | A initiala n Alg (S, , )}
Exemplu
endfm
Continuare
Continuare
fmod Z4 is
sort s .
op 0 : -> s .
op succ : s -> s .
vars x : s .
eq succ(succ(succ(succ(x)))) = x .
endfm
(a) A |= (x)succ(succ(succ(succ(x)))) = x
Daca X := {x} si e : X A, atunci
e(succ(succ(succ(succ(x))))) =
Asucc (Asucc (Asucc (Asucc (e(x))))) = e(x) + 4 (mod 4)
= e(x) =
e(x)
(b) Fie B o -algebra.
Existenta: definim f : A B prin
f (0) := B0 , f (x + 1) := Bsucc (f (x)) pt. 0 x 2 .
:= {(x)succ(succ(succ(succ(x)))) = x}
A este -algebra initiala daca:
I
(a) A |= or. ,
138
140
Continuare
Exercitiu
fmod NSET is
sorts nat nset .
op 0 : -> nat .
op s : nat -> nat .
op vid : -> nset .
op ins : nat nset -> nset .
vars x y : nat .
var A : nset .
eq ins(x,ins(x,A)) = ins(x,A) .
eq ins(x,ins(y,A)) = ins(y,ins(x,A)) .
endfm
Deoarece B |= (x)succ(succ(succ(succ(x)))) = x,
pentru e0 : X B, e0 (x) := B0 obtinem
Bsucc (Bsucc (Bsucc (Bsucc (B0 )))) =
e0 (succ(succ(succ(succ(x))))) =
e0 (x) = B0
Din (1) si (2) rezulta f (Asucc (3)) = Bsucc (f (3)).
Unicitatea: fie g : A B un morfism;
dem. ca g (x) = f (x) prin inductie pt. i {0, 1, 2, 3}
g (0) = g (A0 ) = B0 = f (0),
g (x + 1) = g (Asucc (x)) = Bsucc (g (x)) = Bsucc (f (x)) =
f (Asucc (x)) = f (x + 1).
141
142
143
144
Programare logica
Congruente
s As As echivalenta or. s S,
este compatibila cu operatiile
ai si bi or. i = 1, . . . , n A (a1 , . . . , an ) s A (b1 , . . . , bn )
or. : s1 . . . sn s
145
146
Exemple
Algebra cat
NAT -algebra A:
Anat := N, A0 := 0, Asucc (x) := x + 1
n1 k n2 k|(n1 n2 ) pentru k N fixat
n1 k n2 Asucc (n1 ) k Asucc (n2 )
As /s := {[a]s | a As } or. s S
A/ := {As /s }sS este (S, )-algebra
I
I
AUTOMAT -algebra B:
Bintrare = Bstare = Biesire := N
Bs0 := 0, Bf (m, n) := m + n, Bg (n) := n + 1
este congruenta pe B, unde intrare =stare =iesire :=k
(A/ ) := [A ] or. : s,
(A/ ) ([a1 ]s1 , . . . , [an ]sn ) := [A (a1 , . . . , an )]s
or. : s1 . . . sn s si (a1 , . . . , an ) As1 Asn
147
148
Exemple
Ker (f )
STIVA-algebra A:
Aelem := N, Astiva := N
A0 := 0, Aempty := , Apush (n, n1 nk ) := n n1 nk ,
Apop () = Apop (n) := ,
Apop (n1 n2 nk ) := n2 nk pt. k 2,
Atop () := 0, Atop (n1 nk ) := n1 pt. k 1.
elem := N N,
stiva := {(w , w 0 ) | (w , w 0 ) N N , |w | = |w 0 |}
= {elem , stiva } congruenta
Propozitia 12
A/ ' B, unde
STIVA-algebra B:
Belem := {0}, Bstiva := N
B0 := 0, Bempty := 0, Bpush (0, n) := n + 1 or. n,
Bpop (0) := 0, Bpop (n) := n 1 pt. n 1, Btop (n) := 0 or. n.
Dem. exercitiu
149
150
Proprietatea de universalitate
Continuare
h este bine definit: [a1 ]s = [a2 ]s hs (a1 ) = hs (a2 )
[a1 ]s = [a2 ]s (a1 , a2 ) s Ker (hs ) hs (a1 ) = hs (a2 )
h morfism:
dc. : s , at.
hs (A/e quiv ) = hs ([A ] ) = hs (A ) = B ,
dc. : s1 . . . sn s si
a1 As1 , . . ., an Asn at.
[]
/ A/
}
152
Consecinte
Continuare
Dem. exercitiu
Propozitia 15
Fie K o clasa de (S, )-algebre. Daca
K :=
K este congruenta pe T ,
[]K
/ T /
K
{
153
-algebre
154
Continuare
(S, , ) specificatie,
A algebra si o congruenta pe A
fs 0 (u) = fs 0 (v ) or. u =s 0 v H.
[]
[] : A A/ morfism surjectiv,
/ A/
Ab
O
f : T (X ) A/ morfism
f
Din Propozitia 10, ex.
e : T (X ) A
e
a. .
e; [] = f
T (X )
or. (X )t =s t 0 if H , or. e : X A
es 0 (u) s 0
es 0 (v ) or. u =s 0 v H
es (t) s
es (t 0 ).
Propozitia 16
Atunci
es 0 (u) s 0
es 0 (v ) or. u =s 0 v H si, deoarece este
nchisa la substitutie, obtinem
es (t) s
es (t 0 ), adica fs (t) = fs (t 0 ).
155
156
Echivalenta semantica
Continuare
Fie (X )t =s t 0 if H si e : X A a..
es 0 (u) s 0
es 0 (v ) or. u =s 0 v H.
Rezulta (
es 0 (u),
es 0 (v )) Ker (h) or. h : A B |= , deci
es 0 (v )) or. u =s 0 v H.
h(
es 0 (u)) = h(
Fie B |= i h : A B. Atunci
e; h : T (X ) B si
es (t)) = h(
es (t 0 )).
h(
es 0 (u)) = h(
es 0 (v )) or. u =s 0 v H, deci h(
0
Rezulta (
es (t),
es (t )) Ker (h) or. h : A B |= , deci
0
(
es (t),
es (t )) , i.e.
es (t) s
es (t 0 ).
A = T notam :=,T .
Propozitia 17
,A este o congruenta pe A nchisa la substitutie.
157
-algebra initiala
ADT
(S, , ) specificatie
(S, , ) specificatie
I
Definim
T pe T congruenta semantica
:= {Ker (f ) | f : T A, A |= },
care este nchisa la substitutie cf. Propozitiei 17.
158
Teorema 18.
T / este -algebra initiala.
159
160
Consecinte
Teorema
Sunt echivalente:
I
I
161
162
163
164
Programare logica
|= (X )t =s t 0 if H
Teorema constantelor
Demonstratii prin inductie
Propozitia 19
{(X )u =s 0 v | u =s 0 v H} |= (X )t =s t 0 .
atunci |= ()t =s t 0 if H.
165
166
Continuare
(X )
es 0 (u) =
es 0 (v ) or. u =s 0 v H. Deoarece A |= , din ipoteza,
es 0 (u) =
es 0 (v ) or. u =s 0 v H, rezulta
es (t) =
es (t 0 ).
6|= (X )y = 0 if {x = 0}.
(X ) := ((X )w ,s )w S ,sS
I
(b) Fie A |= ,
e : T A unicul morfism. Daca
es 0 (u) =
es 0 (v )
S
es 0 (u) =
es 0 (v ) or. u =s 0 v H, atunci
es (t) =
es (t 0 ).
167
(X )w ,s = w ,s pentru w 6=
(X ),s = ,s {cx |x Xs }
168
(X )-algebre
Teorema constantelor I
T(X ),s = T (X )s
Tcx := x oricare x Xs , s S
(a) A |= (X )t =s t 0 ,
as (t) =
as (t 0 ) or. a : X A
Observatie
T(X ) este (X )-algebra initiala
(A, a) (X )-algebra
: T(X ) A unicul morfism de (X )-algebre
a
170
169
Teorema constantelor II
Continuare
as 0 (u) =
as 0 (v ) or. u =s 0 v H, deoarece
(a) |= (X )t =s t 0 if H
S
a : X A a..
as 0 (u) =
as 0 (v ) or. u =s 0 v H.Atunci
(A, a) |=(X ) si
as (t) =
as (t 0 ).
172
fmod MYNAT is
sort
MyNat .
op 0 : -> MyNat .
op s_ : MyNat -> MyNat .
op _+_ : MyNat MyNat -> MyNat [comm assoc prec 50 ] .
op _*_ : MyNat MyNat -> MyNat [ comm assoc prec 49 ] .
vars x y
: MyNat .
eq x + 0 = x .
eq x + s y = s(x + y) .
eq x * 0 = 0 .
eq x * s y = (x * y) + x .
endfm
fmod IND0 is
including MYNAT .
ops cy cz : -> MyNat .
endfm
*** pasul initial
red 0 * (cy + cz) == 0 * cy + 0 * cz .
fmod IND is
including IND0 .
op cx : -> MyNat .
*** ipoteza inductie
eq cx * (cy + cz) = cx * cy + cx * cz .
endfm
*** pasul de inductie
red s cx * ( cy + cz) == s cx * cy + s cx * cz .
174
173
Constructori S = {s}
==========================================
fmod MYNAT
==========================================
fmod IND0
==========================================
reduce in IND0 : 0 * (cy + cz) == 0 * cy + 0 * cz .
rewrites: 5 in 10534559934ms cpu (0ms real)
result Bool: true
==========================================
fmod IND
==========================================
reduce in IND : s cx * (cy + cz) == cy * s cx + cz * s cx .
rewrites: 5 in 10534543934ms cpu (0ms real)
result Bool: true
175
. In exemplul anterior,
= {0 : MyNat, s : MyNat MyNat,
+ : MyNat MyNat MyNat, : MyNat MyNat MyNat}
dar n inducta sructurala am folosit numai
c = {0 : MyNat, s : MyNat MyNat}.
Fie o signatura (monosortata) si A o -algebra . O subsignatura
Cons se numeste signatura de constructori pentru A daca
unicul morfism h : TCons A este surjectiv.
Daca P = (, ) este o prezentare, notam TP = T / . Atunci
Cons se numeste signatura de constructori pentru P daca este
signatura de constructori pentru TP .
176
Constructori S = {s}
.
P = (, ) o prezentare, Cons signatura de constructori
TP := T /
t T , [t] := [t]
pasul initial:
Q([]) = true or. Cons,s
pasul de inductie:
daca t1 , . . ., tn T s si Q([t1 ]) = = Q([tn ]) = true atunci
Q([(t1 , . . . , tn )]) = true or. Conss n ,s .
178
179
180
Programare logica
(X )t =s t
(X )t1 =s t2
(X )t2 =s t1
(X )t1 =s t2 , (X )t2 =s t3
(X )t1 =s t3
: s1 sn s
Sub
unde
= s (t
0)
(X )(t)
(Y )t =s t 0 , : Y T (X )
182
181
Deductia sintactica
Exemplu
n = si
= {x + 0 = x, x + succ(y ) = succ(x + y )}
E ` 0 + succ(0) = succ(0)
x +0=x
Sub{x 0}
0
+
0
=
0
(Sub{x, y 0})
(C )
0 + succ(0) = succ(0 + 0)
succ(0 + 0) = succ(0)
x + succ(y ) = succ(x + y )
0 + succ(0) = succ(0)
183
(T )
184
Sub
(X )(t) =s (t 0 )
` {a, c}a =n c
Identificam substitutia : Y T (X ) cu
: T (Y ) T (X ).
Daca H = atunci
Sub
Sub
1 , 2
a =s c
(X )(t) =s (t 0 )
185
186
187
188
Programare logica
(X )t =s t
(X )t1 =s t2
(X )t2 =s t1
Logic
a ecuational
a
Corectitudine. Completitudine
(X )t1 =s t2 , (X )t2 =s t3
(X )t1 =s t3
Sub
: s1 sn s
unde
189
Continuare
Fie : s1 sn s si presupunem ca
1 , . . . , n
este corecta daca
|= 1 , . . ., |= n |=
Propozitia 22
Regulile deductiei ecuationale R, S, T, C , Sub sunt corecte.
191
192
Continuare
Deoarece A |= (Y )t =s t 0 if H , obtinem
Teorema 23 (Corectitudine)
` (X )t =s t 0 |= (X )t =s t 0
f )s (t) = (;
f )s (t 0 ) i.e. fs ((t))
0 )).
(;
= fs ((t
In consecinta, A |= (X )(t)
= s (t
0 ).
Deoarece A este o -algebra arbitrara,
= s (t
0 ).
|= (X )(t)
194
193
Continuare
Reg
(X )t =s t 0
t1 s1 t10 , . . ., tn sn tn0 t s t 0
Propozitia 24
Sunt echivalente:
I
196
-algebre
Echivalenta sintactica
CS(,T (X ))
t s t 0 ` (X )t =s t 0 oricare s S.
este echivalenta sintactica determinata de .
Propozitia 25
Propozitia 26
Sunt echivalente:
I
(c) T (X )/ |= .
197
Echivalenta semantica
198
Teorema 28 (Completitudine)
|= (X )t =s t 0 ` (X )t =s t 0
` (X )t =s t 0 .
Propozitia 27
,A este cea mai mica congruenta pe A nchisa la substitutie.
Dem. Din Propozitia 17, ,A este congruenta A nchisa la
substitutie. Fie o alta congruenta pe A nchisa la substitutie si
p : A A/ surjectia canonica ( p(a) = [a] or. a A). Din
Propozitia 16, A/ |= , deci = Ker (p) ,A .
Demonstratie scurta:
Se aplica Propozitiile 26 si 27.
199
200
Teorema de completitudine
Reguli de deductie
t t 0 ` (X )t =s t 0 (echiv. sintactica)
t t 0 |= (X )t =s t 0 (echiv. semantica)
(X )t1 =s t2
Abstractizarea
(X Y )t1 =s t2
Concretizarea
(X Y )t1 =s t2
(X )t1 =s t2
t1 , t2 T (X )s
Ys 6= T,s 6=
Propozitia 30
|= (X )t =s t 0 ` (X )t =s t 0
202
203
204
Exemplu
Orice functie inversabila la dreapta este injectiva.
S := {s}, := {f : s s, g : s s}
` ({x, y }) x = y if {f (x) = f (y )}
(7) () cx = cy (4,6, T)
Programare logica
Contexte
Logic
a ecuational
a
Regula SR (Subterm
Replacement)
Pentru t T (X ) si y X notam
nry (t) = numarul de aparitii ale lui y n t
206
205
Regula de deductie SR
Exemplu
(S, , ) specificatie
(X )(u) =s 0 (v ) or. u =s v H
SR
unde (Y )l =s r if H , : Y T (X ) substitutie
c T (X {z})s 00 , z 6 X , nrz (c) = 1
I
I
c = z SR = Sub
E multime de ecuatii neconditionate
SRE
E = {x + 0 = x, x + succ(y ) = succ(x + y )}
unde (Y )l =s r E , : Y T (X ) substitutie
c T (X {z})s 00 , z 6 X , nrz (c) = 1
207
(1)
(2)
(3)
(4)
(5)
x + succ(y ) = succ(x + y ) E
(1)
(2)
(3)
(4)
(5)
(6)
x + succ(y ) = succ(x + y ) E
208
Regula de deductie SR
Continuare
Propozitia 31
SR este regula de deductie corecta.
|= (X )(u) =s 0 (v ) or. u =s v H.
|= (X )(t1 , . . . , tk [z (l)], . . . , tn ) =s 00
(t1 , . . . , tk [z (r )], . . . , tn ).
Observam ca c[z t] = (t1 , . . . , tk [z t], . . . , tn ) or.
210
209
Regula de deductie SR
Continuare
SR este nchisa la C (schita)
Fie : s1 sn s 00 si t1 SR t10 , . . ., tn SR tn0 .
Definim SR T (X ) T (X ) prin
t SR t 0 `R,S,T ,SR (X )t =s t 0
Propozitia 32
SR este congruenta nchisa la substitutie.
Dem. Din definitie, SR este nchisa la R, S, T, deci este o relatie
de echivalenta.
I
211
212
Completitudinea SR
Teorema 33
Sunt echivalente:
214
215
216
Programare logica
Rescrierea
(S, ) signatura
I O regul
a de rescriere este formata dintr-o multime de variabile
Y si doi termeni l, r de acelasi sort din T (Y ) a..
I
I
l nu este variabila
Var (r ) Var (l) = Y .
Vom nota l r (l s r ).
Rescrierea termenilor
R = {x + 0 x, x + succ(y ) succ(x + y )}
217
Relatia R
218
Sistemul de rescriere RE
t0
219
220
Exemplu
Maude
fmod INTLIST is
protecting INT .
sort IntList .
subsort Int < IntList .
op nil : -> IntList .
op _,_ : IntList IntList -> IntList [ assoc id: nil] .
vars L1 L2 L3 : IntList . var x : Int .
eq L1 , x , L2 , - x , L3 = L1 , L2 , L3 .
endfm
E = {x + 0 = x, x + succ(y ) = succ(x + y )}
RE = {x + 0 x, x + succ(y ) succ(x + y )}
0 + succ(0) E succ(0 + 0)
(l := x + succ(y ), r := succ(x + y ), c := z, := {x, y 0})
succ(0 + 0) E succ(0)
(l := x + 0, r := x, c := succ(z), := {x 0})
0 + succ(0) E succ(0 + 0) E succ(0)
set trace on .
reduce 4 , 3 , 5 , 6 , - 3 , 8 , -5 .
221
Maude
222
Maude
*********** equation
eq L1,x,L2,- x,L3 = L1,L2,L3 .
L1 --> nil
x --> 3
L2 --> 6
L3 --> nil
3,6,-3,8
--->
(nil,6,nil),8
rewrites: 2 in 6264381186ms cpu (27ms real)
result IntList: 4,6,8
223
224
Exemplu
:= () (echivalenta generata)
(T (X )s , s ) sistem de rescriere abstract
= {(k, k)|k T }
Cine este ?
226
225
Rescriere si deductie
Continuare
Dem. (1) (2) (3) rezulta din E = E = SR
Din E SR rezulta E SR .
Teorema 34.
(1) E |= (X )t =s t 0
(4) t E t 0 ,
(Y )t =s t 0 E , : Y T (X ) substitutie implica t E t 0
sau t 0 E t.
E (t
0 ) pentru c = z.
Daca t E t 0 atunci (t)
0
0
) E (t)
pentru c = z.
Daca t E t atunci (t
0
Rezulta (t) E (t ).
E este nchisa la C
Fie : s1 sn s 00 si k {1, . . . , n}.
227
228
Continuare
Continuare
In consecinta,
nchisa la C si SubE , deci
E este
230
231
232
Programare logica
Substitutie
Substitutii. Unificare
F. Baader, T. Nipkow, Terms Rewriting and All That,
Cambridge University Press, 1998.
s () := or. : s,
s ((t1 , . . . , tn )) := (
s1 (t1 ), . . . , sn (tn )) or.
: s1 . . . sn s, or. t1 T (X )s1 , . . ., tn T (X )sn .
233
Substitutie
Exemplu
X , Y si Z multimi de variabile
I Dac
a : X T (Y ), : X T (Y ) atunci
= =
.
I
t = p(u, v , x, y , z)
compunerea substitutiilor : X T (Y ), : Y T (Z )
; : X T (Z ), (; )s (x) := ;
234
= {x f (y ), y f (a), z u}
(t) = p(u, v , f (y ), f (a), u)
= {y g (a), u z, v f (f (a))}
; = {x f (g (a)), y f (a), u z, v f (f (a))}
(; )(t) = p(z, f (f (a)), f (g (a)), f (a), z)
(; )(t) = p(u, f (f (a)), f (y ), g (a), u)
235
236
ht, t 0 i,
unde t,
t0
T (X ).
Egalitatea termenilor
Daca t = (t1 , . . . , tn ) si t 0 = (t10 , . . . , tk0 ) atunci
t = t 0 = , n = k, ti = ti0 or. i
237
238
Exemplu
Notatii
(x) := y , (y ) := y , (z) := z,
0 (x) := 0, 0 (y ) := 0, 0 (z) := z, 0 = ; {y 0}
(x) := z + 0, (y ) := z + 0, (z) := z,
= ; {y z + 0}
I
I
(x) := z, (y ) := z, (z) := z, = ; {y z}
{x1 t1 , , xn tn }U := {(t) = (t 0 ) | t = t 0 U}
si sunt cgu
cgu nu este unic
239
240
Unificare
Unificare
Propozitia 35.
xi X , x1 6= xj or. i 6= j
S
xi 6 ni=1 Var (ti ) or. i = 1, . . . , n.
R ; R = R .
Dem.
R (xi ) := {x1 t1 , . . . , xn tn }
R Unif (R)
(R
; 0 )(x)
0 (R (x))
0 (x) x 6 {x1 , . . . , xn },
0 (ti ) x = xi
242
Algoritmul de unificare
(S = {}, ) signatura, X multime de variabile
(1) Sterge: R {t = t} R
(3) R = {x = g (y ), y = z, h(g (y )) = w , y = z}
(4) R = {x = g (z), y = z, h(g (z)) = w , z = z}
243
244
(3) R = {x = g (y ), y = z, h(g (y )) = b, y = z}
(3) R = deoarece b 6= h
245
246
Terminare
Propozitia 36.
(3) R = {x = g (y ), g (y ) = y , h(g (y )) = w , y = z}
247
n3 := |{t = x R|t 6 X , x X }|
Fiecare pas al algoritmului modifica n1 , n2 , n3
n1 n2 n3
S
terge
>
Orienteaza
= >
Descompune >
Elimina
>
Daca la executia unui pas (n1 , n2 , n3 ) se schimba n (n10 , n20 , n30 ),
atunci (n1 , n2 , n3 ) >lex (n10 , n20 , n30 ), adica (n1 , n2 , n3 ) descreste
strict n ordine lexicografica.
248
Corectitudine
Completitudine
Propozitia 37.
Propozitia 38.
0
0
(e1 , . . . , en ) = (e1 , . . . , ek ) R cu 6= . Termenii
(e1 , . . . , en ) si (e10 , . . . , ek0 ) nu pot fi unificati deoarece ncep
cu caractere diferite.
I
249
Complexitate
Problema de unificare
Matching problem
251
fmod MATCH is
sort Elem .
op a : -> Elem .
ops _+_ _*_
: Elem Elem -> Elem .
vars x y : Elem
eq x + (y * y) = x * y .
endfm
reduce in MATCH : (a + y) + (x * x) .
eq x + (y * y) = x * y .
x --> a + y
y --> x
(a + y) + (x * x)
--->
(a + y) * x
rewrites: 1 in 9146759363ms cpu (4ms real)
result Elem: (a + y) * x
252
Matching problem
x + (y y ) = (a + cy ) + (cx cx )
{x = a + cy , y = cx }
254
255
256
Programare logica
257
258
Exemple
Definitii (T , ) se numeste
noetherian: nu exista reduceri infinite t0 t1
(orice rescriere se termina)
confluent: t1 t t2 t1 t2
local confluent: t1 tt2 t1 t2
Church-Rosser: t1 t2 t1 t2
normalizat: orice element are o forma normala
complet (convergent, canonic): confluent si noetherian
I
259
260
Proprietati
Proprietati
(T , ) confluent (T , ) Church-Rosser
U := {a T |a n1 , a n2 , n1 6= n2 forme normale}
Demonstram ca are loc urmatoarea proprietate:
(*) or. a U ex. b U a.. a b
Daca U 6= , atunci exista (an )n U cu
a1 a2 an
ceea ce contrazice proprietatea de terminare. In consecinta, U =
si orice termen t are o unica forma normala fn(t).
Propozitia 40
Daca (T , ) este complet atunci orice termen are o singura forma
normala. In acest caz vom nota fn(t) forma normala a lui t T .
Dem. Existenta formei normale rezulta din terminare. Fie t un
termen si presupunem ca t1 si t2 sunt forme normale distincte ale
262
261
Continuare
Exercitiu
T = {a, b, c, d}
n1 a n2 implica n1 = n2 (imposibil)
t = n1 ; avem b n1 , b n2 , deci b U si a b
n1 a n2 implica n1 b a n2 pentru un
b T ;avem b n1 , b n2 , deci b U si a b
263
264
Teorema
Continuare
(4)(3) t fn(t) t 0
(3)(4) Fie t1 , . . ., tn a.. t t1 tn t 0
Demonstram ca fn(t) = fn(t 0 ) prin inductie dupa n.
Teorema 42
(1) E |= (X )t =s t 0
(2) E ` (X )t =s t 0
(3) t E t 0
(4) fn(t)=fn(t 0 )
(t si t 0 au aceeasi forma normala)
266
267
268
t fn(t)
t 0 fn(t 0 )
E ` (X )t =s t 0 fn(t) = fn(t 0 )
Programare logica
(S, ) signatura
I O regul
a de rescriere este formata dintr-o multime de variabile
Y si doi termeni l, r de acelasi sort din T (Y ) a..
I
Rescrierea termenilor
Terminare. Confluent
a.
Completare
l nu este variabila
Var (r ) Var (l) = Y .
Vom nota l r (l s r ).
I
269
270
Terminarea
Arborele de reducere
Propozitie 43
t R t 0 (t) > (t 0 )
oricare t si t 0 , atunci R este noetherian.
Dem. N nu contine lanturi infinite n1 > n2 > > nk > .
Exemple
I
272
Arbore de reducere
Terminare
(S, ) signatura, R un TRS
R = {x + 0 x, x + succ(y ) succ(x + y )}
Propozitia 44
succ(0) + succ(0 + 0)
u
succ(0) + succ(0)
*
succ(succ(0) + (0 + 0))
succ(succ(0) + 0)
succ(succ(0) + 0)
succ(succ(0))
succ(succ(0))
Sunt echivalente:
(a) R este noetherian
(b) oricarui termen t i poate fi asociat un numar natural (t) N
astfel ncat t R t 0 implica (t) > (t 0 )
Dem. (a)(b) Din Lema lui Konig rezulta ca ntr-un sistem de
rescriere noetherian orice termen are un arbore de reducere finit si
definim (t) = adancimea arborelui asociat lui t.
(b)(a) rezulta din Propozitia 43.
273
Terminare
Exemplu
Propozitia 45
R = {x + 0 0, x + succ(y ) succ(x + y )}
A0 := 1, Asucc (k) := k + 1,
A+ (k, m) := k + 2 m
(k +A m := k + 2 m) or. k, m N
As = N or. s S,
(l) > e
(r ) or. l r R or. e : Var (l) A.
e
e(x) := n, e(y ) := m
(0)
(x + 0) = n +A 0 = n + 2 A0 = n + 2 > 1 = A0 = e
e
(x + succ(y )) = n +A Asucc (m) = n + 2 (m + 1) =
e
(succ(x + y ))
n + 2 m + 2 > n + 2 m + 1 = Asucc (n +A m) = e
276
(l1 )
(r1 )
&
l1 r1 , l2 r2 R,
Var (l1 ) Var (l2 ) = ,
l1 = c[z t],
(t) = (l2 ) c.g.u
(c)[z (r2 )]
277
Exemplu
t := f (f (x)), c = z, := {x y }
(r1 ) = y , (c)[z (r2 )] = y
t := f (x), c = f (z), := {x f (y )}
(r1 ) = f (y ), (c)[z (r2 )] = f (y )
278
280
Exemplu
S := {s}, := { : ss s},
E := {{x, y , v }(x y ) (y v ) = y )}
T = RE := {(x y ) (y v ) y }, (t) := lungimea termenului t
Determinam perechile critice pentru
l1 := (x y ) (y v ), r1 := y , l2 := (x 0 y 0 ) (y 0 v 0 ), r2 := y 0
Subtermenii lui l1 care nu sunt variabile sunt (x y ) si y v .
I t := x y , c = z (y v ), := {x x 0 y 0 , y y 0 v 0 }
(r1 ) = y 0 v 0 , (c)[z (r2 )] = y 0 ((y 0 v 0 ) v )
I t := y v , c = (x y ) z, := {y x 0 y 0 , v y 0 v 0 }
(r1 ) = x 0 y 0 , (c)[z (r2 )] = (x (x 0 y 0 )) y 0
CP = {(y 0 v 0 , y 0 ((y 0 v 0 ) v )), (x 0 y 0 , (x (x 0 y 0 )) y 0 )}
Deoarece y ((y x) v ) > y x si (x (v y )) y > v y ,
T := T {y ((y x) v ) y x, (x (v y )) y v y }
Demonstrati ca T este complet.
281
282
283
284
Programare logica
Maude
fmod MYNAT is
sort
MyNat .
op 0 : -> MyNat .
op s : MyNat -> MyNat .
op _+_ : MyNat MyNat -> MyNat .
op n : -> MyNat .
vars x y z : MyNat .
eq x + 0 = x .
eq x + s(y) = s(x + y) .
eq (x + y) + z = x + ( y + z ) .
endfm
Logica ecuational
a local
a
reduce in MYNAT : n + (0 + n) .
result MyNat: n + (0 + n)
286
285
Maude
Maude Manual
fmod MYNAT1 is
sort
MyNat .
op 0 : -> MyNat .
op s : MyNat -> MyNat .
op _+_ : MyNat MyNat -> MyNat [assoc] .
op n : -> MyNat .
vars x y : MyNat .
eq x + 0 = x .
eq x + s(y) = s(x + y) .
endfm
reduce in MYNAT1 : n + (0 + n) .
result MyNat: n + n
288
Ecuatiile(propozitiile) din A
t, t 0 T (X )s
a, c As , A fixata
In cele ce urmeaza
(X )t =s t 0
a =s c
(A)a =s c
clasic
local
290
289
Deductia sintactica in A
a, b, c, ai , bi A
a =s a
a =s b
b =s a
a =s b, b =s c
a =s c
A (a1 , . . . , an ) =s A (b1 , . . . , bn )
Sub
hs (t) =s hs (t 0 )
i sau
nota `A a =s a0
a1 =s1 b1 , . . . , an =sn bn
: s1 sn s
h : T (X ) A,
(X )t =s t 0 if H
291
292
Exemplu
S = {s}, := {0 : s, + : ss s}
A = (N, 0, +)
:= {({x})x + x = 0}
Demonstrati ca `A 3 = 1
Reg
a =s a0
2=0
(2) 1 = 1 (R)
(3) 2 + 1 = 0 + 1 ((1),(2), C )
3=1
293
294
-tautologiile lui A
Teorema de completitudine
a0
a0
0
0
a A
a)
a `A a =s a (echiv. sintactic
0
0
a A
a |=A a =s a (echiv. semantica)
A
Corectitudinea logicii locale: A
A
Completitudinea regulilor de deductie: A
Propozitia 47
Dem. exerciitiu
A
este cea mai mica congruenta pe A, inchisa la -substitutie.
Dem. exercitiu
295
296
Contexte
Inchiderea la contexte
A (S, )-algebra, Q A A
Q este nchisa la contexte daca
(a, a0 ) Q (c[a], c[a0 ]) Q or. c A[z] context
Propozitia 49
Sunt echivalente:
(a) Q este nchisa la contexte,
(b) Q este compatibila pe componente, i.e.
or. : s1 . . . sn s, i {1, . . . , n},
or. a1 As1 , . . ., ai1 Asi1 , ai+1 Asi+1 , . . ., an Asn ,
or. (a, a0 ) Qsi ,
(A (a1 , . . . , ai1 , a, ai+1 , . . . , an ), A (a1 , . . . , ai1 , a0 , ai+1 , . . . , an )) Qs .
A = (N, 0, +)
c =0+z
c = 4 + (z + 1000)
297
298
Continuare
Inchiderea la contexte
A (S, )-algebra, Q A A
Q := {(c[a], c[a0 ])|(a, a0 ) Q, c A[z] context}
Q := {(a, a0 )| ex. b A a. . a Q b si a0 Q b}
Propozitia 50
(a) Q este cea mai mica relatie nchisa la contexte care include Q.
299
300
Rescrierea locala
Rescrierea locala
(Y )l =s r if H ,
h : T (Y ) A este un morfism,
hs 0 (u),A hs 0 (v ) or. u =s 0 v H
Definim (Qn )n :
Qn A A or. n,
Q0 := , n
hs 0 (u)Qn hs 0 (v ) or . u =s 0 v H
S
Q := n Qn
Teorema 51
,A = ,A A
Notam ,A := Q , ,A := Q
302
301
Rescrierea locala
Maude
fmod RESLOC is
including NAT .
op _++_ : Nat Nat -> Nat .
op rel : Nat Nat -> Bool .
vars x y : Nat .
eq x ++ y = x + y + x * y .
ceq rel(x,y) = true if
x ++ y = 4 * x .
ceq rel(x , y) = false if x ++ y =/= 4 * x
endfm
Teorema 52
set trace on .
reduce rel (2 , 2) .
0
0
a A
a a a
303
304
Maude
Maude
h(x + +y ) Q1 h(x 4), deci (rel(2, 2), true) = (h(rel(x, y )), true) Q2
305
306
307
308
Programare logica
Maude
fmod MYNAT1 is
sort
MyNat .
op 0 : -> MyNat .
op s : MyNat -> MyNat .
op _+_ : MyNat MyNat -> MyNat [assoc] .
op n : -> MyNat .
vars x y : MyNat .
eq x + 0 = x .
eq x + s(y) = s(x + y) .
endfm
reduce in MYNAT1 : n + (0 + n) .
result MyNat: n + n
310
Echivalenta semantica
Exemplu:
fmod MYNAT1 is
...
endfm
Pentru t T (X )s notam
[t]F := [t]F clasa de echivalenta a lui t.
T,F (X ) := T (X )/F
311
312
Propozitia 53
[]F
y
/ T,F (X )
313
Propozitia 54
(a) B |= (X )t =s t 0
e([t]F ) =
e(t) =
e(t 0 ) = e([t 0 ]F )
(b) (a) Daca e : X B, atunci e([t]F ) = e([t 0 ]F ) din ipoteza.
Teorema 55
Sunt echivalente:
(a) E F |= (X )t =s t 0
316
Exemplu
fmod MYNAT1 is
sort
MyNat .
op 0 : -> MyNat .
op s : MyNat -> MyNat .
op _+_ : MyNat MyNat -> MyNat [assoc] .
vars x y : MyNat .
eq x + 0 = x .
eq x + s(y) = s(x + y) .
endfm
t F c[z (l)] si
t 0 = c[z (r )], unde
c T (X {z}), z 6 X , nrz (c) = 1
l r R cu Var (l) = Y ,
: Y T (X ) este o substitutie.
R := {x + 0 x, x + s(y ) s(x + y )}
318
319
320
Teorema 56
Sunt echivalente:
(2) t E F t 0
(3) [t] E /F [t 0 ]F
(T,F (X )s , E /F ) este un sistem de rescriere abstract, pentru care
proprietatile de confluenta, terminare si completare se definesc
uzual. Algoritmul de completare necesita unificare modulo ecuatii,
care este nedecidabila n general.
Programare Logica
Algebra initiala
(S, ) signatura
Teorema 2
Pentru orice (S, )-algebra A exista un unic morfism SA : T A.
SA (t) este interpretarea termenului t n A.
Exemplu
I D = (DS , D ), Ds := N or. s S
dac
a : s atunci D := 0,
dac
a : s1 . . . sn s, k1 , . . ., kn N
D (k1 , . . . , kn ) := 1 + max(k1 , . . . , kn )
I SD : T D unicul morfism
. &
arb(t1 ) arb(tn )
322
G = (S0 , N, T , P)
- N este multimea neterminalelor
- T este multimea terminalelor
- S0 este simbolul de start
- P N (N T ) multimea productiilor
S0 N, T N = ,
o productie p = (A, ) P va fi descrisa prin
[p] A
323
324
G = (S0 , N, T , P)
G = (S0 , N, T , P)
Exemplu.
descrierea unui numar natural ca sir de cifre
Gramatica G
N = {hcifrai, hnati}
T = {0, . . . , 9}, S0 = hnati
P = {c0, , c9, p1, p2}
[ci] hcifrai i, i = 0, 9
[p1] hnati hcifrai
[p2] hnati hnatihcifrai
p n1 nk ,n p = (n , t0 n1 t1 nk tk ) P,
unde n, n1 , . . . , nk N si t0 , . . . , tk T .
Observam ca
p : n1 nk n [p] n t0 n1 t1 nk tk .
Vom scrie simplu = P.
Signatura G
S =N
={
ci : hcifrai, i = 0, 9
p1 : hcifrai hnati
p2 : hnatihcifrai hnati }
326
325
G = (S0 , N, T , P)
LG = (LS , L ) G-algebra
I
Observatie
Ln = T , oricare n N
p : n1 nk n si (w1 , . . . , wk ) (T )k
Lp (w1 , . . . , wk ) = t0 w1 wk tk ,
unde [p] n t0 n1 t1 nk tk n P.
328
Limbajul L(G )
Teorema 57
SG
/ LG
tw o
SA
SG
SA
Sem(w ) = SA (tw )
{w T | n w } = SG ,n (T,n ).
In particular L(G ) = SG ,S0 (T,S0 ).
329
330
Exemple
331
Signatura G = (S = N, = P)
= {ci : hcifrai | i = 0, 9}
{p1 : hcifrai hnati,
p2 : hnatihcifrai hnati}
332
Exemplu: Sem(25)
hnati
Algebra semantica
I
hnati
hcifrai
Calculatorul de buzunar
1
6
2
7
3
8
4
9
334
333
0
5
hcifrai
Calculatorul de buzunar
+
*
(
)
IF
,
M
E
[ci] hcifrai i, i = 0, 9
[p1] hnati hcifrai
[p2] hnati hnatihcifrai
[r 1] hexpi hnati
[r 2] hexpi M
[r 3] hexpi hexpi + hexpi
[r 4] hexpi IF hexpi, hexpi, hexpi
[r 5] hexpi (hexpi)
[I 1] hinsti hexpiE OFF
[I 2] hinsti hexpiE hinsti
[Pr ] hprog i ONhinsti
ON
OFF
IF 2+M,(M+45)+3,5+6 E
335
336
Calculatorul de buzunar
Calculatorul de buzunar
Notatie A B := {f | f : A B functie}
I
ON e1 E e2 E en E OFF
337
338
Algebra semantica
Algebra semantica
I
I
I
I
I
Definim operatiile Ap , cu p .
I
I
I
339
SA : T A unicul morfism
SG : T LG unicul morfism
w = ON 5 + M E OFF
APr (I )=I (0)
Sem(w ) = AhPr i (AhI 1i (Ahr 3i (Ahr 1i (Ahp1i (Ahc5i )), Ahr 2i )))
=
AhI 1i (Ahr 3i (Ahr 1i (Ahp1i (Ahc5i )), Ahr 2i ))(0) =
Ahr 3i (Ahr 1i (Ahp1i (Ahc5i )), Ahr 2i )(0) =
Ahr 1i (Ahp1i (Ahc5i ))(0) + Ahr 2i (0) =
Ahp1i (Ahc5i ) + 0 =
Ahc5i + 0 =
5+0=5
Exemplu:
w = ON 5 + M E OFF
tw = ThPr i (ThI 1i (Thr 3i (Thr 1i (Thp1i (Thc5i )), Thr 2i )))
Sem(w ) = SA (t) = AhPr i (AhI 1i (Ahr 3i (Ahr 1i (Ahp1i (Ahc5i )), Ahr 2i )))
341
342
Exemple
343
G = (S0 , N, T , P)
N = {hprog i, hexpi, htermi, hfacti, hvar i},
S0 = hprog i,
T = {x, y , z, (, ), +, }, P = {p0, . . . , p9},
344
[p0]
[p1]
[p2]
[p3]
[p4]
[p5]
[p6]
[p7]
[p8]
[p9]
hprog i hexpi
hvar i x
hvar i y
hvar i z
hfacti hvar i
hfacti (hexpi)
htermi hfacti
htermi hfacti htermi
hexpi htermi
hexpi hexpi + htermi
Pol
SG
C
SA
"
tw o
/ LG
Cod
SG
SA
Sem(w ) = SA (tw )
346
345
347
Polp7 : T T T , Polp7 (, ) = ,
Polp9 : T T T , Polp7 (, ) = +,
348
Forma poloneza
SG : T LG unicul G-morfism
P : T Pol unicul G-morfism
w = y (x + z)
tw = p0(p8(p7(p4(p2), p5(p9(p8(p6(p4(p1))), p6(p4(p3)))))))
350
349
Functionarea SMA
Instructiunile SMA
Instructiunile SMA:
inc P ( incrementeaza P)
dec P ( decrementeaza P)
Ad R ( aduna valoarea din varful stivei cu cea din R,
iar rezultatul este depus n R)
Mu R ( nmulteste valoarea din varful stivei cu cea din R,
iar rezultatul este depus n R)
ld v
( ncarca n R valoarea din v )
st R ( muta n stiva valoarea din R )
print ( afiseaza continutul lui R)
en
op(e1, . . . , en )
en1
...
e1
...
...
...
351
352
Notam SI := Cods , s S
Cp0 : SI SI ,
Cp0 () = ; print
Cp7 : SI SI SI ,
Cp7 (, ) = ; st R ; inc P ; ; Mu R ; dec P
Cp9 : SI SI SI ,
Cp9 (, ) = ; st R ; inc P ; ; Ad R ; dec P
354
353
Compilarea
Compilarea
e = y (x + z)
te = p0(p8(p7(p4(p2), p5(p9(p8(p6(p4(p1))), p6(p4(p3)))))))
355
356
e = y (x + z), x = 3, y = 7, z = 2
Cod
ld y ;
st R ; inc P ;
ld x ;
st R ; inc P ;
ld z ;
Ad R ; dec P ;
Mu R ; dec P ;
print
R
7
7
3
3
2
5
35
35
Stiva()
7
7
3 7
3 7
7
-
P
1
1
2
2
3
3
2
1
357
358
359
360
Programare logica
Exemplu:
NATPRED = (S, , )
S := {nat}, := {0 : nat, succ : nat nat},
nat := {pos}, nat nat := {<}
362
361
Modele si morfisme
Exemplu
A = {A Aw | w }.
363
NATPRED-modelul A:
Anat := N, A0 := 0, Asucc (n) := n + 1
Apos := {n|n > 0}, A< := {(k, n)|k n}
NATPRED-modelul B:
Bnat := {2n |n N}, B0 := 1, Bsucc (2n ) := 2n+1
Bpos := {2n |n > 0}, B< := {(2k , 2n )|k n}
f : A B, f (n) := 2n or. n N
morfism de NATPRED-modele
364
Universul Herbrand
Clauze Horn
365
366
Satisfiabilitate
multime de clauze,
P1 , , Pn formule atomice cu variabile din X
|= X (P1 Pn ),
A |= L1 Lm A |= L1 sau sau A |= Lm
367
368
Clauze Horn
Rezolutia SLD
SLD
P1 Pn P
P
P1 Pn
P : P1 , , Pn
: P1 , , Pn
P1 Pi Pn
(P1 Q1 Qm Pn )
370
Rezolutia SLD
Exemplu
(1)
(2)
(3)
(4)
stramos(X , Y ) : parinte(X , Y )
stramos(X , Y ) : parinte(X , Z ), stramos(Z , Y )
parinte(dan, bogdan)
parinte(bogdan, ana)
Completitdinea SLD-rezolutiei
Sunt echivalente:
I
|= X (P1 Pn ),
unde G este : P1 , , Pn .
371
372
Exemplu
373
Sb
I b
:= S {b}, unde b 6 S,
:= {true : b},
I b
w ,b
:= w
(predicatele devin operatii cu rezultat boolean)
Exemplu
375
Sunt echivalente:
A |=, (X )p if {p1 , . . . , pn }
376