next up previous contents
Next: F Module-System Dialog Up: RELFUN Guide: Programming with Previous: D User-Defined Sorts Dialog

E Dynamic-Signatures Dialog

 

rfi-p> exec "dyn-sg.bat"

relfun
rfi-p> sp
rfi-p> destroy
rfi-p> inter
rfi-p> sortstyle static
rfi-p> destroy-sortbase
rfi-p> sl
rfi-l> ;  EXAMPLE DIALOG ON DYNAMIC SIGNATURE UNIFICATION
rfi-l> ; =================================================
rfi-l> 
rfi-l> ; Simone Andel
rfi-l> 
rfi-l> ; A signature clause which determines the sort or type of an argument
rfi-l> ; always has to be placed before the procedure clauses it belongs to.
rfi-l> ; Its scope extends over the following clauses until the next 
rfi-l> ; signature clause.
rfi-l> 
rfi-l> consult "bsp.rf"
Reading file "bsp.rf"
rfi-l> l
(sg (p 5 _z))
(ft (p _x _y)
    (+ _x _y) )
rfi-l> (p 5 3)
8
rfi-l> (p 6 3)
unknown
rfi-l> ; the number 5 is requested by the signature clause for p
rfi-l> (p _z 3)
8
(_z = 5)
rfi-l>
rfi-l> destroy
rfi-l> ; signature clause for empty procedure
rfi-l> az (sg (p 5 _x))
rfi-l> listing
(sg (p 5 _x))
rfi-l> (p 5 3)
unknown
rfi-l>
rfi-l> sp
rfi-p> destroy
rfi-p> mcd sortbase   
Module:  sortbase
Context: 
rfi-p> consult "veb-base-sub.rfp" 
Reading file "veb-base-sub.rfp"
rfi-p> compile-sortbase
rfi-p> listing
dog(lassy).
dog(fido).
cat(tom).
cat(garfield).
goldfish(goldy).
canary(tweety).
rfi-p> 
rfi-p> mcd
Module:  workspace
Context: 
rfi-p> consult "bsp1.rf"
Reading file "bsp1.rf"
rfi-p> listing
age(tom) :- & 4.
age(garfield) :- & 6.
age(goldy) :- & 1.
sg(young($fish))
young(X) :- <(age(X),2).
sg(young($cat))
young(X) :- <(age(X),5).
sg(eats($cat,$fish))
eats(X,Y) :- young(X).
rfi-p> % Dynamic signature unification for more than one signature clause 
rfi-p> % in the procedure
rfi-p> 
rfi-p> young(goldy)
true
rfi-p> m
unknown
rfi-p> young(tom)
true
rfi-p> young(X)
true
X=goldy
rfi-p> m
true
X=tom
rfi-p> m
unknown
rfi-p> eats(tom, goldy) 
true
rfi-p> eats(tom, fido)
unknown
rfi-p> eats(garfield, goldy)
unknown
rfi-p> 
rfi-p> destroy
rfi-p> consult "rev.rf"
Reading file "rev.rf"
rfi-p> listing
sg(rev(tup(|_),tup(|_)))
rev(L,R) :- rev(L,[],R).
sg(rev(tup(|_),tup(|_),tup(|_)))
rev([],L,L).
rev([Y|L1],L2,R) :- rev(L1,[Y|L2],R).
rfi-p> rev([1,2,3],A)
true
A=[3,2,1]
rfi-p> m
unknown
rfi-p> rev(L,[3,2,1])
true
L=[1,2,3]
rfi-p> rev([1,2,3],[],L)
true
L=[3,2,1]
rfi-p> rev([1,2,3]|A)
true
A=[[3,2,1]]
rfi-p> m
true
A=[[|Anon736*2],[3,2,1|Anon736*2]]
rfi-p> m
unknown
rfi-p> rev([],1,1)
unknown
rfi-p> % 1 is not a list
rfi-p> 
rfi-p> sl
rfi-l> rx (sg (rev (tup | id) (tup | id))) 
rfi-l> rx (sg (rev (tup | id) (tup | id) (tup | id))) 
rfi-l> sp
rfi-p> listing
rev(L,R) :- rev(L,[],R).
rev([],L,L).
rev([Y|L1],L2,R) :- rev(L1,[Y|L2],R).
rfi-p> rev([],1,1)
true
rfi-p> % without signature: true although 1 is not a list
rfi-p> 
rfi-p> destroy
rfi-p> destroy-sortbase
rfi-p> mcd sortbase
Module:  sortbase
Context: 
rfi-p> consult "int-and-real.rfp"
Reading file "int-and-real.rfp"
rfi-p> compile-sortbase
rfi-p> listing
woman(peggy_bundy).
woman(marcy_darcy).
man(al_bundy).
man(jefferson_darcy).
boy(bud_bundy).
girl(kelly_bundy).
dog(buck).
fish(marcies_goldfish).
int(0).
int(1).
int(2).
int(3).
int(4).
int(5).
rfi-p> mcd
Module:  workspace
Context: 
rfi-p> 
rfi-p> consult "fib-sg.rf"
Reading file "fib-sg.rf"
rfi-p> listing
sg(fib($int))
fib(0) :- & 1.
fib(1) :- & 1.
fib(N) :- & +(fib(-(N,1)),fib(-(N,2))).
sg(fib(null))
fib(null) :- add1(null,One) & One.
sg(fib(s(X)))
fib(One) :- add1(null,One) & One.
fib(N) :- 
         sub1(N,Nm1),
         sub1(Nm1,Nm2),
         R1 is fib(Nm1),
         R2 is fib(Nm2),
         plus(R1,R2,R) &
         R.
sub1(s[N],N).
add1(N,s[N]).
plus(null,Y,Y).
plus(X,Y,R) :- sub1(X,Xm1), add1(Y,Yp1), plus(Xm1,Yp1,R).
rfi-p> fib(null)
s[null]
rfi-p> fib(s[null]) 
s[null]
rfi-p> fib(s[s[s[s[s[null]]]]])
s[s[s[s[s[s[s[s[null]]]]]]]]
rfi-p> fib(0)
1
rfi-p> fib(1)
1
rfi-p> fib(5)
8
rfi-p> fib(11)
unknown
rfi-p> 
rfi-p> destroy
rfi-p> sl
rfi-l> az (sg (likes $woman $man))
rfi-l> az (hn (likes peggy_bundy al_bundy))
rfi-l> az (sg (hates $woman $man))
rfi-l> az (hn (hates peggy_bundy al_bundy))
rfi-l> sp
rfi-p> listing
sg(likes($woman,$man))
likes(peggy_bundy,al_bundy).
sg(hates($woman,$man))
hates(peggy_bundy,al_bundy).
rfi-p> P(peggy_bundy,al_bundy)
[peggy_bundy,al_bundy]
P=tup
rfi-p> m
true
P=likes
rfi-p> m
true
P=hates
rfi-p> 
rfi-p> destroy
rfi-p> sl
rfi-l> az (ft (plus _x _y) (+ _x _y))
rfi-l> listing
(ft (plus _x _y)
    (+ _x _y) )
rfi-l> (plus 5 0)
5
rfi-l> (plus 5 peggy_bundy)
error - the value of number2, peggy_bundy, should be a number
rfi-l> ; gives an error 
rfi-l> 
rfi-l> a0 (sg (plus $int $int))
rfi-l> listing
(sg (plus $int $int))
(ft (plus _x _y)
    (+ _x _y) )
rfi-l> (plus 5 0)
5
rfi-l> (plus 5 peggy_bundy)
unknown
rfi-l> ; fails at signature clause
rfi-l>
rfi-l> destroy
rfi-l> destroy-sortbase
rfi-l> consult "qsort.rf"
Reading file "qsort.rf"
rfi-l> sp
rfi-p> listing
sg(qsort(Cr)(tup()))
qsort[Cr]([]) :- & [].
sg(qsort(Cr)(tup(_|_)))
qsort[Cr]([X|Y]) :- 
         partition[Cr](X,Y,Sm,Gr) &
         appfun(qsort[Cr](Sm),tup(X|qsort[Cr](Gr))).
partition[Cr](X,[Y|Z],[Y|Sm],Gr) :- 
         Cr(Y,X), partition[Cr](X,Z,Sm,Gr).
partition[Cr](X,[Y|Z],Sm,[Y|Gr]) :- 
         Cr(X,Y), partition[Cr](X,Z,Sm,Gr).
partition[Cr](X,[X|Z],Sm,Gr) :- partition[Cr](X,Z,Sm,Gr).
partition[Cr](X,[],[],[]).
appfun([],L) :- & L.
appfun([H|R],L) :- & tup(H|appfun(R,L)).
second<([_,N],[_,M]) :- <(N,M).
first<(found[N,_],found[M,_]) :- string<(N,M).
rfi-p> 
rfi-p> % Higher-order procedures with signature clauses
rfi-p> qsort[<]([3,1,2])     
[1,2,3]
rfi-p> qsort[second<]([[ma,2],[lu,3],[kl,1]])
[[kl,1],[ma,2],[lu,3]]
rfi-p> qsort[<](f[3,2,1])    
unknown



Harold Boley (boley@informatik.uni-kl.de)