Difference between revisions of "VIP7 Construct examples"

From wiki.visual-prolog.com

m
m (formatting)
Line 1: Line 1:
This page contains a collection of basic examples for (mainly) the newer constructs found in VIP7.
This page contains a collection of basic examples for (mainly) the newer constructs found in VIP7.


 
=== Fact variables ===
===FACT VARIABLES===


Fact variables are the only mutable types in VIP.
Fact variables are the only mutable types in VIP.
Line 9: Line 8:


<vip>facts
<vip>facts
ndb_int:(integer).  % a nondeterministic fact (0 to any number of values)
    ndb_int:(integer).  % a nondeterministic fact (0 to any number of values)
db_int:(integer,string) determ. % a deterministic fact (0 to 1 value)
    db_int:(integer,string) determ. % a deterministic fact (0 to 1 value)</vip>
</vip>
 
These types of fact are asserted and retracted.
These types of fact are asserted and retracted.


'''Examples - "fact variables"'''
'''Examples - "fact variables"'''
<vip>facts
<vip>facts
zz_int:integer:=0.
    zz_int:integer:=0.
zz_fred:integer:=erroneous.
    zz_fred:integer:=erroneous.


domains
domains
dom=dom(string,chaindb:ref).
    dom=dom(string,chaindb:ref).


facts
facts
zz_dom:dom:=erroneous.
    zz_dom:dom:=erroneous.
 
clauses
clauses
pred():-
    pred():-
zz_int:=7,
        zz_int:=7,
stdio::write("\n zz_int = ",zz_int), %will write "zz_int=7"
        stdio::write("\n zz_int = ",zz_int), %will write "zz_int=7"
zz_int:=zz_int+20,
        zz_int:=zz_int+20,
stdio::write("\n zz_int = ",zz_int), %will write "zz_int=27"
        stdio::write("\n zz_int = ",zz_int), %will write "zz_int=27"
succeed.
        succeed.</vip>
</vip>
 
A fact variable is great for counting eg.
A fact variable is great for counting eg.


<vip>predicates
<vip>predicates
pred:()=integer Count.
    pred:()=integer Count.
clauses
clauses
pred()=_:-
    pred()=_:-
zz_int:=0,
        zz_int:=0,
some_nondeterm_fact_or_pred(),
        some_nondeterm_fact_or_pred(),
zz_int:=zz_int+1,
            zz_int:=zz_int+1,
fail.
            fail.
pred()=zz_int.
    pred()=zz_int.</vip>
</vip>


=== LISTS - FINDALL AND LIST COMPREHENSION  ===  
=== Lists - findall and list comprehension ===


See [http://wiki.visual-prolog.com/index.php?title=Lists_and_Recursion Lists and Recursion]<br>
See [http://wiki.visual-prolog.com/index.php?title=Lists_and_Recursion Lists and Recursion]<br>
'''findall()''' has been depricated, so use the list comprehension construct.
<vp>findall()</vp> has been depricated, so use the list comprehension construct.


'''Examples'''
'''Examples'''
<vip>facts
<vip>facts
ndb:(integer). %default is nondeterm for a fact
    ndb:(integer). %default is nondeterm for a fact
clauses
clauses
ndb(1).
    ndb(1).
ndb(2).
    ndb(2).
ndb(3).
    ndb(3).
....
    ....
ndb(10).
    ndb(10).
clauses
clauses
pred_old():-
    pred_old():-
findall(X,ndb(X),List).
        findall(X,ndb(X),List).
%results in List = [1,2,3,4,5,6,7,8,9,10]
        %results in List = [1,2,3,4,5,6,7,8,9,10]
clauses
clauses
pred_new():-
    pred_new():-
List=[X||ndb(X)].
        List=[X||ndb(X)].
%results in List = [1,2,3,4,5,6,7,8,9,10]
        %results in List = [1,2,3,4,5,6,7,8,9,10]
clauses
clauses
pred_filter():-
    pred_filter():-
List=[X||ndb(X), X>6].
        List=[X||ndb(X), X>6].
%results in List = [7,8,9,10]
        %results in List = [7,8,9,10]


pred_filter():-
    pred_filter():-
List=[X||ndb(X), X mod 2=0].
        List=[X||ndb(X), X mod 2=0].
%results in List = [2,4,6,8,10]
        %results in List = [2,4,6,8,10]


pred_filter():-
    pred_filter():-
List=[X||ndb(X), X mod 2=0, X<7].
        List=[X||ndb(X), X mod 2=0, X<7].
%results in List = [2,4,6]
        %results in List = [2,4,6]
pred_filter():-
zz_int:=0,
List=[X||ndb(X), X mod 2=0, X<7, zz_int:=zz_int+1].
%results in List = [2,4,6] and zz_int=3.


pred_filter():-
    pred_filter():-
List=[X||ndb(X), Y=pred2(X), Y>10].  %with pred2(X)=X^2.
        zz_int:=0,
%results in List = [4,5,6,7,8,9,10]
        List=[X||ndb(X), X mod 2=0, X<7, zz_int:=zz_int+1].
%reason - X=3 gives Y=9 which is < 10.
        %results in List = [2,4,6] and zz_int=3.
pred_other():-
    L=[1,2,3,4,5,6,7,8,9,10],
LIST=[ X || X = list::getMember_nd(L)].
%results in List= L
</vip>


===IF THEN ELSE (CODE) ===  
    pred_filter():-
        List=[X||ndb(X), Y=pred2(X), Y>10].  %with pred2(X)=X^2.
        %results in List = [4,5,6,7,8,9,10]
        %reason - X=3 gives Y=9 which is < 10.
 
    pred_other():-
        L=[1,2,3,4,5,6,7,8,9,10],
        LIST=[ X || X = list::getMember_nd(L)].
        %results in List= L</vip>
 
=== if-then-else (code) ===


'''Examples'''
'''Examples'''


<vip>clauses
<vip>clauses
pred(X):-
    pred(X):-
if X>0 then
        if X>0 then
    stdio::write("\n X>0")  %the last line in each block has no comma
            stdio::write("\n X>0")  %the last line in each block has no comma
else
        else
    stdio::write("\n X<=0")
            stdio::write("\n X<=0")
end if.
        end if.


pred(X):-
    pred(X):-
if X>0 then
        if X>0 then
    stdio::write("\n X>0")
            stdio::write("\n X>0")
elseif X = 0 then
        elseif X = 0 then
    stdio::write("\n X=0")
            stdio::write("\n X=0")
else
        else
    stdio::write("\n X<0")
            stdio::write("\n X<0")
end if.
        end if.


clauses
clauses
pred(X,Y)=Z:-
    pred(X,Y)=Z:-
if X=0 then
        if X=0 then
    Z="x is zero"
            Z="x is zero"
elseif X>0 then
        elseif X>0 then
if pred3(Y)=true then
            if pred3(Y)=true then
Z="x>0 and pred(Y) is true"
                Z="x>0 and pred(Y) is true"
else
            else
Z="x>0 and pred(Y) is false"
                Z="x>0 and pred(Y) is false"
end if  %note, no comma here either
            end if  %note, no comma here either
else
        else
Z="x <0"
            Z="x <0"
end if.
        end if.</vip>
</vip>
 


===#IF #THEN #ELSE (DIRECTIVE FOR CONDITIONAL COMPILATION)===  
=== #if #then #else (directive for conditional compilation) ===


'''Examples'''
'''Examples'''
<vip>
constants
u64_con=1.
int_con=2.
real_con=3.


compile_big_con=u64_con. %change this and then recompile.
<vip>constants
    u64_con=1.
    int_con=2.
    real_con=3.
 
    compile_big_con=u64_con. %change this and then recompile.


#if compile_big_con=u64_con #then
#if compile_big_con=u64_con #then
predicates
    predicates
pred:()->unsigned64.
        pred:()->unsigned64.
clauses
    clauses
pred()=U64:-
        pred()=U64:-
U64=78766.
            U64=78766.


#elseif compile_big_con=int_con #then
#elseif compile_big_con=int_con #then
predicates
    predicates
pred:()->integer.
        pred:()->integer.
clauses
    clauses
pred()=Int:-
        pred()=Int:-
Int=20.
            Int=20.


#else
#else
predicates
    predicates
pred:(real [out]).
        pred:(real [out]).
clauses
    clauses
pred(0.766).
        pred(0.766).
 
#endif</vip>


#endif
</vip>
'''Note'''<br>
'''Note'''<br>
Code construct uses    '''if  - then''''''elseif  - then''''''else''''''end if''' <br>
Code construct uses    <vp>if  - then</vp><vp>elseif  - then</vp><vp>else</vp><vp>end if</vp> <br>
compiler directive uses '''#if - #then''', '''#elseif - #then''', '''#else''', '''#endif'''
compiler directive uses <vp>#if - #then</vp>, <vp>#elseif - #then</vp>, <vp>#else</vp>, <vp>#endif</vp>
<br>(just the "end if" is "different")
<br>(just the "end if" is "different")


===TRAP AND TRY/CATCH/FINALLY ===  
=== Trap and try/catch/finally ===
 
See [http://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms/Try-catch-finally Try-catch-finally]<br>
See [http://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms/Try-catch-finally Try-catch-finally]<br>
Note, in  
Note, in
[http://wiki.visual-prolog.com/index.php?title=Language_Reference/Built-in_entities/Predicates Built-in entities/Predicates]<br>
[http://wiki.visual-prolog.com/index.php?title=Language_Reference/Built-in_entities/Predicates Built-in entities/Predicates]<br>
suggests using '''try-end try''' instead of '''trap(_,_,_)'''
suggests using <vp>try-end try</vp> instead of <vp>trap(_,_,_)</vp>


'''Example 1'''
'''Example 1'''
<vip>
 
pred_all():-
<vip>clauses
try
    pred_all():-
call_pred_that_might_crash(0)
        try
catch ErrorNo do
            call_pred_that_might_crash(0)
call_own_exception_pred(ErrorNo)
        catch ErrorNo do
finally
            call_own_exception_pred(ErrorNo)
always_call_this_anyway()
        finally
end try.
            always_call_this_anyway()
        end try.


class predicates
class predicates
call_pred_that_might_crash:(integer).
    call_pred_that_might_crash:(integer).
call_own_exception_pred:(pointer ErrorNo).
    call_own_exception_pred:(pointer ErrorNo).
always_call_this_anyway:().
    always_call_this_anyway:().
clauses
clauses
call_pred_that_might_crash(X):-
    call_pred_that_might_crash(X):-
Y=9/X.
        Y=9/X.
 
    call_own_exception_pred(ErrorNo):-
        vpiCommonDialogs::note("crashed").


call_own_exception_pred(ErrorNo):-
    always_call_this_anyway():-
vpiCommonDialogs::note("crashed").
        vpiCommonDialogs::note("finally reached").</vip>


always_call_this_anyway():-
vpiCommonDialogs::note("finally reached").
</vip>
'''Example 2'''
'''Example 2'''
<vip>
 
pred_some():-
<vip>clauses
try
    pred_some():-
call_pred_that_might_crash(0)
        try
finally
            call_pred_that_might_crash(0)
always_call_this_anyway()
        finally
end try.
            always_call_this_anyway()
</vip>
        end try.</vip>
 
% in this case, VIP will automatically pop up its exception dialog
% in this case, VIP will automatically pop up its exception dialog


'''Example 3'''
'''Example 3'''
<vip>
pred_some():-
try
call_pred_that_might_crash(0)
catch ErrorNo do
call_own_exception_pred(ErrorNo)
end try.
</vip>
'''Example 4''' - illegal - it must have a "catch" or "finally"
<vip>
pred_illegal():-
try
call_pred_that_might_crash(0)
end try.
</vip>


===64 BIT NUMBERS===  
<vip>clauses
    pred_some():-
        try
            call_pred_that_might_crash(0)
        catch ErrorNo do
            call_own_exception_pred(ErrorNo)
        end try.</vip>
 
'''Example 4''' - illegal - it must have a <vp>catch</vp> or <vp>finally</vp>
 
<vip>clauses
    pred_illegal():-
        try
            call_pred_that_might_crash(0)
        end try.</vip>
 
 
=== 64 bit numbers ===




=== Properties ===


===PROPERTIES===
Properties in a class are used almost identically to fact variables, but properties can be set directly (from another class), without having to declare a public predicate to make the change.
Properties in a class are used almost identically to fact variables, but properties can be set directly (from another class), without having to declare a public predicate to make the change.


'''Example'''
'''Example'''
<vip>
 
interface fred
<vip>interface fred
domains
    domains
complete_dom=is_complete;
        complete_dom=is_complete;
not_complete.
                not_complete.
properties
    properties
prop_complete : complete_dom.
        prop_complete : complete_dom.
end interface fred
end interface fred</vip>
</vip>
 
In fred.pro:
In fred.pro:
<vip>
 
facts
<vip>facts
zz_complete:complete_dom:=erroneous.
    zz_complete:complete_dom:=erroneous.


clauses %for the property
clauses %for the property
prop_complete()=zz_complete. %get
    prop_complete()=zz_complete. %get
prop_complete(COMPLETE):-zz_complete:=COMPLETE. %set
    prop_complete(COMPLETE):-zz_complete:=COMPLETE. %set</vip>
</vip>
 


In some other class that calls class fred:
In some other class that calls class fred:
<vip>
 
implement other
<vip>implement other
open fred
    open fred
clauses
clauses
pred():-
    pred():-
Fred=fred::new(),
        Fred=fred::new(),
Fred:prop_complete:=is_complete,% to set the value
        Fred:prop_complete:=is_complete,% to set the value
Value=Fred:prop_complete,!. %get
        Value=Fred:prop_complete,!. %get</vip>
</vip>
 


===COMPARATORS AND COMPARE===
=== Comparators and compare ===


=== Anonymous predicates ===
''(under development)''
See [http://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms/Anonymous_Predicates Anonymous_Predicates]


===ANONYMOUS PREDICATES===
''(under development)''<br>
See [http://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms/Anonymous_Predicates Anonymous_Predicates] <br>
'''Examples'''
'''Examples'''
<vip>
 
run() :-
<vip>clauses
Anon={()=9},
    run() :-
K=Anon().
        Anon={()=9},
        K=Anon().
         %results in K=9
         %results in K=9
run() :-
    run() :-
Anon={=88},
        Anon={=88},
K=Anon().
        K=Anon().
%results in K=88.
        %results in K=88.
run():-
    run():-
Anon={(A,B)=A+B},
        Anon={(A,B)=A+B},
K=Anon(4,8),
        K=Anon(4,8),
%results in K=12.
        %results in K=12.
run():-
    run():-
Anon={
        Anon={
(A,B)=C:-
            (A,B)=C:-
R=math::random(7),
            R=math::random(7),
C=A+B+R,
            C=A+B+R,
stdio::wRite("RRRR=",R)
            stdio::wRite("RRRR=",R)
},
            },
K=Anon(4,8).
        K=Anon(4,8).
         %results in K=12 + a random number <7
         %results in K=12 + a random number <7


run():-
    run():-
Anon={=f_abc(3)},
        Anon={=f_abc(3)},
K=Anon(),
        K=Anon(),
stdio::write("\nI={=f_abc(3)} gives ",K),  
        stdio::write("\nI={=f_abc(3)} gives ",K),
fail.
        fail.
run().
    run().</vip>
</vip>
 
===THREADS===
 
=== Threads ===
 
To start a thread:
To start a thread:
<vip>
 
clauses
<vip>clauses
pred():-
    pred():-
_=thread::start(fred).
        _=thread::start(fred).


predicates
predicates
fred:().
    fred:().
clauses
clauses
fred():-.....
    fred():-.....</vip>
</vip>
 
fred can have no arguments, so no argument brackets are allowed:
fred can have no arguments, so no argument brackets are allowed:
<vip>
<vip>
_=thread::start(fred())
    _=thread::start(fred())</vip>
</vip>
 
is illegal. But the thread can access data prepared before it is started.
is illegal. But the thread can access data prepared before it is started.
<vip>
 
facts
<vip>facts
zz_int:integer:=erroneous.
    zz_int:integer:=erroneous.
clauses
clauses
pred():-
    pred():-
zz_int:=88,
        zz_int:=88,
_=thread::start(fred).
        _=thread::start(fred).


predicates
predicates
fred:().
    fred:().
clauses
clauses
fred():-
    fred():-
K=zz_int,
        K=zz_int,
...
        ...</vip>
</vip>

Revision as of 20:34, 15 March 2009

This page contains a collection of basic examples for (mainly) the newer constructs found in VIP7.

Fact variables

Fact variables are the only mutable types in VIP.

Examples - "reminder- ordinary facts"

facts
    ndb_int:(integer).  % a nondeterministic fact (0 to any number of values)
    db_int:(integer,string) determ. % a deterministic fact (0 to 1 value)

These types of fact are asserted and retracted.

Examples - "fact variables"

facts
    zz_int:integer:=0.
    zz_fred:integer:=erroneous.
 
domains
    dom=dom(string,chaindb:ref).
 
facts
    zz_dom:dom:=erroneous.
 
clauses
    pred():-
        zz_int:=7,
        stdio::write("\n zz_int = ",zz_int), %will write "zz_int=7"
        zz_int:=zz_int+20,
        stdio::write("\n zz_int = ",zz_int), %will write "zz_int=27"
        succeed.

A fact variable is great for counting eg.

predicates
    pred:()=integer Count.
clauses
    pred()=_:-
        zz_int:=0,
        some_nondeterm_fact_or_pred(),
            zz_int:=zz_int+1,
            fail.
    pred()=zz_int.

Lists - findall and list comprehension

See Lists and Recursion
findall() has been depricated, so use the list comprehension construct.

Examples

facts
    ndb:(integer). %default is nondeterm for a fact
clauses
    ndb(1).
    ndb(2).
    ndb(3).
    ....
    ndb(10).
clauses
    pred_old():-
        findall(X,ndb(X),List).
        %results in List = [1,2,3,4,5,6,7,8,9,10]
clauses
    pred_new():-
        List=[X||ndb(X)].
        %results in List = [1,2,3,4,5,6,7,8,9,10]
clauses
    pred_filter():-
        List=[X||ndb(X), X>6].
        %results in List = [7,8,9,10]
 
    pred_filter():-
        List=[X||ndb(X), X mod 2=0].
        %results in List = [2,4,6,8,10]
 
    pred_filter():-
        List=[X||ndb(X), X mod 2=0, X<7].
        %results in List = [2,4,6]
 
    pred_filter():-
        zz_int:=0,
        List=[X||ndb(X), X mod 2=0, X<7, zz_int:=zz_int+1].
        %results in List = [2,4,6] and zz_int=3.
 
    pred_filter():-
        List=[X||ndb(X), Y=pred2(X), Y>10].   %with pred2(X)=X^2.
        %results in List = [4,5,6,7,8,9,10]
        %reason - X=3 gives Y=9 which is < 10.
 
    pred_other():-
        L=[1,2,3,4,5,6,7,8,9,10],
        LIST=[ X || X = list::getMember_nd(L)].
        %results in List= L

if-then-else (code)

Examples

clauses
    pred(X):-
        if X>0 then
            stdio::write("\n X>0")  %the last line in each block has no comma
        else
            stdio::write("\n X<=0")
        end if.
 
    pred(X):-
        if X>0 then
            stdio::write("\n X>0")
        elseif X = 0 then
            stdio::write("\n X=0")
        else
            stdio::write("\n X<0")
        end if.
 
clauses
    pred(X,Y)=Z:-
        if X=0 then
            Z="x is zero"
        elseif X>0 then
            if pred3(Y)=true then
                Z="x>0 and pred(Y) is true"
            else
                Z="x>0 and pred(Y) is false"
            end if  %note, no comma here either
        else
            Z="x <0"
        end if.


#if #then #else (directive for conditional compilation)

Examples

constants
    u64_con=1.
    int_con=2.
    real_con=3.
 
    compile_big_con=u64_con. %change this and then recompile.
 
#if compile_big_con=u64_con #then
    predicates
        pred:()->unsigned64.
    clauses
        pred()=U64:-
            U64=78766.
 
#elseif compile_big_con=int_con #then
    predicates
        pred:()->integer.
    clauses
        pred()=Int:-
            Int=20.
 
#else
    predicates
        pred:(real [out]).
    clauses
        pred(0.766).
 
#endif

Note
Code construct uses if  - then, elseif  - then, else, end if
compiler directive uses #if - #then, #elseif - #then, #else, #endif
(just the "end if" is "different")

Trap and try/catch/finally

See Try-catch-finally
Note, in Built-in entities/Predicates
suggests using try-end try instead of trap(_,_,_)

Example 1

clauses
    pred_all():-
        try
            call_pred_that_might_crash(0)
        catch ErrorNo do
            call_own_exception_pred(ErrorNo)
        finally
            always_call_this_anyway()
        end try.
 
class predicates
    call_pred_that_might_crash:(integer).
    call_own_exception_pred:(pointer ErrorNo).
    always_call_this_anyway:().
clauses
    call_pred_that_might_crash(X):-
        Y=9/X.
 
    call_own_exception_pred(ErrorNo):-
        vpiCommonDialogs::note("crashed").
 
    always_call_this_anyway():-
        vpiCommonDialogs::note("finally reached").

Example 2

clauses
    pred_some():-
        try
            call_pred_that_might_crash(0)
        finally
            always_call_this_anyway()
        end try.

% in this case, VIP will automatically pop up its exception dialog

Example 3

clauses
    pred_some():-
        try
            call_pred_that_might_crash(0)
        catch ErrorNo do
            call_own_exception_pred(ErrorNo)
        end try.

Example 4 - illegal - it must have a catch or finally

clauses
    pred_illegal():-
        try
            call_pred_that_might_crash(0)
        end try.


64 bit numbers

Properties

Properties in a class are used almost identically to fact variables, but properties can be set directly (from another class), without having to declare a public predicate to make the change.

Example

interface fred
    domains
        complete_dom=is_complete;
                not_complete.
    properties
        prop_complete : complete_dom.
end interface fred

In fred.pro:

facts
    zz_complete:complete_dom:=erroneous.
 
clauses %for the property
    prop_complete()=zz_complete. %get
    prop_complete(COMPLETE):-zz_complete:=COMPLETE. %set


In some other class that calls class fred:

implement other
    open fred
clauses
    pred():-
        Fred=fred::new(),
        Fred:prop_complete:=is_complete,% to set the value
        Value=Fred:prop_complete,!. %get


Comparators and compare

Anonymous predicates

(under development) See Anonymous_Predicates

Examples

clauses
    run() :-
        Anon={()=9},
        K=Anon().
        %results in K=9
    run() :-
        Anon={=88},
        K=Anon().
        %results in K=88.
    run():-
        Anon={(A,B)=A+B},
        K=Anon(4,8),
        %results in K=12.
    run():-
        Anon={
            (A,B)=C:-
            R=math::random(7),
            C=A+B+R,
            stdio::wRite("RRRR=",R)
            },
        K=Anon(4,8).
        %results in K=12 + a random number <7
 
    run():-
        Anon={=f_abc(3)},
        K=Anon(),
        stdio::write("\nI={=f_abc(3)} gives ",K),
        fail.
    run().


Threads

To start a thread:

clauses
    pred():-
        _=thread::start(fred).
 
predicates
    fred:().
clauses
    fred():-.....

fred can have no arguments, so no argument brackets are allowed:

    _=thread::start(fred())

is illegal. But the thread can access data prepared before it is started.

facts
    zz_int:integer:=erroneous.
clauses
    pred():-
        zz_int:=88,
        _=thread::start(fred).
 
predicates
    fred:().
clauses
    fred():-
        K=zz_int,
        ...