next up previous contents
Next: B Type-System Dialog Up: RELFUN Guide: Programming with Previous: References

A Tutorial Dialog

 

rfi-p> exec "conventional.bat"

relfun
rfi-p> %      Developing RELFUN from Conventional Language Constructs
rfi-p> %      =======================================================
rfi-p> 
rfi-p> %  Harold Boley       -       Kaiserslautern       -        11-Jul-96
rfi-p> 
rfi-p> 
rfi-p> help destroy
destroy:

format: destroy

effect: destroy the current database module.

see also: consult, replace
rfi-p> destroy
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Function calls in prefix notation (arithmetical builtins):
rfi-p> 
rfi-p> +(3,8)
11
rfi-p> +(3.1415, 8)
11.1415
rfi-p> +(3.1415, 8, 5.0)
16.1415
rfi-p> +(3.1415, *(2,4), 5.0)
16.1415
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Valued conjunctions return values of right-most calls
rfi-p> % (assignments via is-primitive, Variables capitalized):
rfi-p> 
rfi-p> Prod is *(2,4), +(3.1415, Prod, 5.0)
16.1415
Prod=8
rfi-p> 
rfi-p> Sum is +(1,2,3,4), Prod is *(1,2,3,4), /(Sum,Prod)
5/12
Sum=10
Prod=24
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Repeated assignments to a single variable must be consistent
rfi-p> % (inconsistent values cause failures signaled by unknown):
rfi-p> 
rfi-p> Res is +(1,2,3,4), Res is *(1,2,3,4), /(Res,Res)
unknown
rfi-p> 
rfi-p> Res is +(2,2), Res is *(2,2), /(Res,Res)
1
Res=4
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Further builtins in three groups:
rfi-p> 
rfi-p> builtins
[functions[+,
           -,
           *,
           /,
           1+,
           1-,
           abs,
           rem,
           floor,
           ceiling,
           truncate,
           round,
           sqrt,
           expt,
           log,
           sin,
           cos,
           tan,
           asin,
           acos,
           atan,
           max,
           min,
           mod,
           first,
           rest,
           last,
           length,
           intersection,
           union,
           set-difference,
           remove-duplicates,
           gentemp,
           princ-to-string,
           date,
           operators,
           setvar,
           getvar,
           format-to-string,
           format-to-string*,
           code-char,
           instance-classes],
 predicates[<,
            <=,
            =,
            /=,
            >,
            >=,
            string<,
            string<=,
            string=,
            string/=,
            string>,
            string>=,
            null,
            atom,
            symbolp,
            numberp,
            integerp,
            plusp,
            minusp],
 extras[break,
        readl,
        relfun,
        rf-print,
        rf-princ,
        rf-terpri,
        rf-fresh-line,
        rf-pprint,
        rf-format,
        pretty-print,
        wait,
        tracer-increment-level,
        tracer-decrement-level,
        tracer-check-max,
        tracer-print-heading,
        tracer-print-head,
        tracer-print-foot,
        tracer-print-hn-or-ft,
        tracer-cps]]
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Relation calls (arithmetical predicates):
rfi-p> 
rfi-p> numberp(3.1415)
true
rfi-p> numberp(pi)
false
rfi-p> 
rfi-p> >(2,1)
true
rfi-p> <=(2,1)
false
rfi-p> 
rfi-p> Data is 3.1415, numberp(Data), *(Data,Data)
9.86902225
Data=3.1415
rfi-p> Data is pi, numberp(Data), *(Data,Data)
unknown
rfi-p> % Data is pi,              *(Data,Data)     would be an error!
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Incrementally defined user functions
rfi-p> % (equating call patterns with returned values):
rfi-p> 
rfi-p> help az
az:

format: az <clause>

options: <clause> a relfun clause

effect: the <clause> will be inserted at the end of the
        (possibly empty) current database module.

see also: anull, azhn, azft, consult, destroy, replace
rfi-p> 
rfi-p> az  fib(0) :- & 1.
rfi-p> az  fib(1) :- & 1.
rfi-p> 
rfi-p> Res is fib(0), Res is fib(1), Res
1
Res=1
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Completing Fibonacci function definition
rfi-p> % (conditional recursive equation):
rfi-p> 
rfi-p> az  fib(N)  :-  >(N,1)  &  +(fib(-(N,2)),fib(-(N,1))).
rfi-p> 
rfi-p> help listing
listing:

format: listing
        listing <op>
        listing <pat>

options: <op> an operator,
         <pat> a head pattern

effect: shows the clauses in the current database module.
        if no argument is given, the whole database will be printed on
	the terminal.
	if an operator is given, only those clauses in the current database 
        module will be printed which use this operator as their procedure 
        name.
	if a pattern is given, only those clauses in the current database 
        module will be printed whose head matches the pattern <pat>.

see also: consult, ll
rfi-p> listing
fib(0) :- & 1.
fib(1) :- & 1.
fib(N) :- >(N,1) & +(fib(-(N,2)),fib(-(N,1))).
rfi-p> 
rfi-p> fib(2)
2
rfi-p> fib(3)
3
rfi-p> fib(4)
5
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Extensionally defined relations
rfi-p> % (simple facts constitute relational tables):
rfi-p> 
rfi-p> az  sister(mary,fred).
rfi-p> az  sister(mary,susan).
rfi-p> az  sister(susan,fred).
rfi-p> 
rfi-p> az  father(fred,john).
rfi-p> 
rfi-p> help more
more:

format: more

effect: if you asked a goal and you got one solution, you can get
	the next one. this is only possible if you haven't typed in
	another goal, and you haven't destroyed (modified) the database.

see also: m, ori
rfi-p> 
rfi-p> sister(mary,Whose)
true
Whose=fred
rfi-p> more
true
Whose=susan
rfi-p> more
unknown
rfi-p> father(Who,Whose)
true
Who=fred
Whose=john
rfi-p> more
unknown
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Intensional family relationships
rfi-p> % (simple rule defines relational view):
rfi-p> 
rfi-p> az  aunt(Female,Person) :- sister(Female,Parent), father(Parent,Person).
rfi-p> 
rfi-p> aunt(Who,Whose)
true
Who=mary
Whose=john
rfi-p> more
true
Who=susan
Whose=john
rfi-p> more
unknown
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Ground lists:
rfi-p> 
rfi-p> 1
1
rfi-p> a
a
rfi-p> [1,a,3,b]
[1,a,3,b]
rfi-p> tup(fib(0),a,fib(3),b)
[1,a,3,b]
rfi-p> 
rfi-p> X is [1,a,3,b], rest(X)
[a,3,b]
X=[1,a,3,b]
rfi-p> X is [1,a,3,b], tup(first(X),rest(X),last(X),length(X))
[1,[a,3,b],[b],4]
X=[1,a,3,b]
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Non-ground lists
rfi-p> % (patterns with ordinary as well as rest or "|" variables):
rfi-p> 
rfi-p> [X,a,Y,b]
[X,a,Y,b]
rfi-p> 
rfi-p> Lst is [X,a,Y,b], Y is 3, Lst
[X,a,3,b]
Lst=[X,a,3,b]
Y=3
rfi-p> Y is 3, Lst is [X,a,Y,b], Lst
[X,a,3,b]
Y=3
Lst=[X,a,3,b]
rfi-p> Lst is [X,a,Y,b], Y is 3, [X,Lst,Y,Lst]
[X,[X,a,3,b],3,[X,a,3,b]]
Lst=[X,a,3,b]
Y=3
rfi-p> 
rfi-p> [X,a,Y,b] is [1,a,3,b], [X,Y]
[1,3]
X=1
Y=3
rfi-p> [X,a,X,b] is [1,a,3,b], [X,Y]
unknown
rfi-p> [Head | Tail] is [1,a,3,b], [Head,Tail]
[1,[a,3,b]]
Head=1
Tail=[a,3,b]
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Ground and non-ground structures
rfi-p> % (anonymous or don't-care variables are written as "_"):
rfi-p> 
rfi-p> quad[1,a,3,b]
quad[1,a,3,b]
rfi-p> quad[X,a,Y,b]
quad[X,a,Y,b]
rfi-p> 
rfi-p> quad[X,a,Y,b] is quad[1,a,3,b]
quad[1,a,3,b]
X=1
Y=3
rfi-p> quad[X,a,X,b] is quad[1,a,3,b]
unknown
rfi-p> quad[X,a,_,b] is quad[1,a,3,b]
quad[1,a,3,b]
X=1
rfi-p> quad[_,a,_,b] is quad[1,a,3,b]
quad[1,a,3,b]
rfi-p> quad[H | T] is quad[X,a,Y,b]
quad[X,a,Y,b]
H=X
T=[a,Y,b]
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Calls using rest or "|" variables
rfi-p> % (variables may be "_"-prefixed naturals):
rfi-p> 
rfi-p> _1234 is [1,2,3,4], Sum is +(0 | _1234), Prod is *(| _1234)
24
_1234=[1,2,3,4]
Sum=10
Prod=24
rfi-p> 
rfi-p> 
rfi-p> % Infinitely non-deterministic function
rfi-p> % (recursion can run into the negative):
rfi-p> 
rfi-p> az  inftree(N) :- & N.
rfi-p> az  inftree(N) :- & sqrt(N).
rfi-p> az  inftree(N) :- Aux is inftree(-(N,1)) & tree[N,Aux,N].
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> inftree(4)
4
rfi-p> more
2.0
rfi-p> more
tree[4,3,4]
rfi-p> more
tree[4,1.7320508075688772,4]
rfi-p> more
tree[4,tree[3,2,3],4]
rfi-p> more
tree[4,tree[3,1.4142135623730952,3],4]
rfi-p> more
tree[4,tree[3,tree[2,1,2],3],4]
rfi-p> more
tree[4,tree[3,tree[2,1.0,2],3],4]
rfi-p> more
tree[4,tree[3,tree[2,tree[1,0,1],2],3],4]
rfi-p> more
tree[4,tree[3,tree[2,tree[1,0.0,1],2],3],4]
rfi-p> more
tree[4,tree[3,tree[2,tree[1,tree[0,-1,0],1],2],3],4]
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % Finitely non-deterministic function
rfi-p> % (recursion stopped at 0):
rfi-p> 
rfi-p> az  fintree(N) :-  >=(N,0) & N.
rfi-p> az  fintree(N) :-  >=(N,0) & sqrt(N).
rfi-p> az  fintree(N) :-  >=(N,0), Aux is fintree(-(N,1))  &  tree[N,Aux,N].
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> fintree(4)
4
rfi-p> more
2.0
rfi-p> more
tree[4,3,4]
rfi-p> more
tree[4,1.7320508075688772,4]
rfi-p> more
tree[4,tree[3,2,3],4]
rfi-p> more
tree[4,tree[3,1.4142135623730952,3],4]
rfi-p> more
tree[4,tree[3,tree[2,1,2],3],4]
rfi-p> more
tree[4,tree[3,tree[2,1.0,2],3],4]
rfi-p> more
tree[4,tree[3,tree[2,tree[1,0,1],2],3],4]
rfi-p> more
tree[4,tree[3,tree[2,tree[1,0.0,1],2],3],4]
rfi-p> more
unknown
rfi-p> 
rfi-p> pause()

relfun
rfi-p> bye
true
rfi-p> 
rfi-p> 
rfi-p> % All-solutions builtin (only for finitely non-deterministic calls!):
rfi-p> 
rfi-p> tupof(fintree(4))
[4,
 2.0,
 tree[4,3,4],
 tree[4,1.7320508075688772,4],
 tree[4,tree[3,2,3],4],
 tree[4,tree[3,1.4142135623730952,3],4],
 tree[4,tree[3,tree[2,1,2],3],4],
 tree[4,tree[3,tree[2,1.0,2],3],4],
 tree[4,tree[3,tree[2,tree[1,0,1],2],3],4],
 tree[4,tree[3,tree[2,tree[1,0.0,1],2],3],4]]
rfi-p> 
rfi-p> [_,_,_,Fourth|_] is tupof(fintree(4)), Fourth
tree[4,1.7320508075688772,4]
Fourth=tree[4,1.7320508075688772,4]
rfi-p> bye

relfun



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