VIP7 Construct examples

From wiki.visual-prolog.com

Revision as of 13:56, 14 March 2009 by Steve Lympany (talk | contribs) (→‎FACT VARIABLES: spelling)

This page contains a collection of 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

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 (COMILER 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

Note, in
http://wiki.visual-prolog.com/index.php?title=Language_Reference/Built-in_entities/Predicates use try-end try instead of trap

Example 1

	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

	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

	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"

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

64 BIT NUMBERS

PROPERTIES

COMPARATORS AND COMPARE

ANONYMOUS PREDICATES