PRODUCT : TURBO C NUMBER : 400 VERSION : 1.0/1.1 OS : PC-DOS DATE : January 6, 1988 PAGE : 1/4 TITLE : SIMULATING NON-DETERMINISTIC CALLS IN C When writing Turbo C functions to interface with Turbo Prolog, it is often desirable to have functions which behave non- deterministically. Following is an example program which shows how to write C functions which simulate non-deterministic Prolog predicates. Type the Turbo Prolog program and compile the code to an .OBJ file. After this is done, type the Turbo C program and compile it to an .OBJ file as well. Use the following command from the DOS prompt to compile the C module: TCC -u- -r- -ml -c -O NONDETC.C Once both source code files are successfully compiled into object code, issue the following link command from the DOS command line to create the executable file NONDET.EXE. TLINK INIT+CPINIT+NONDET+NONDETC+NONDET.SYM,NONDET,,PROLOG Here is the Turbo Prolog source code: /*-------------------------------------------------------------- * NONDET.PRO -- Prolog program to demonstrate non-deterministic * calls of C functions. *-------------------------------------------------------------- */ global predicates c_range(integer,integer,integer,integer) - (i,i,o,i) language c cpinit - language c backtrack(integer) - (i) language c genenv(integer) - (o) language c predicates continue(integer) range(integer,integer,integer). PRODUCT : TURBO C NUMBER : 400 VERSION : 1.0/1.1 OS : PC-DOS DATE : January 6, 1988 PAGE : 2/4 TITLE : SIMULATING NON-DETERMINISTIC CALLS IN C clauses /* Succeeds if the C call with environment Env can be re- * satisfied. */ continue(_). continue(Env) :- backtrack(Env), continue(Env). /* This predicate is equivilent to the predicate: * range(Start,End,Start) :- Start <= End. * range(Start,End,X) :- Start < End, Y = Start + 1, * range(Y,End,X).*/ range(Start,End,X) :- genenv(Env), continue(Env), c_range(Start,End,X,Env). /* Display the numbers from 1 to 100. */ goal cpinit, clearwindow, range(1,100,X), write(X), readchar(_), nl, fail. Following is the Turbo C source code: /* Implements a C function equivilent to the Prolog predicate: * range(Start,End,Start) :- Start <= End. * range(Start,End,X) :- Start < End, Y = Start + 1, * range(Y,End,X)..*/ static int env = 0; void fail_cc(); PRODUCT : TURBO C NUMBER : 400 VERSION : 1.0/1.1 OS : PC-DOS DATE : January 6, 1988 PAGE : 3/4 TITLE : SIMULATING NON-DETERMINISTIC CALLS IN C #define MAX_ENV 128 static int environ[MAX_ENV]; /* Returns the index to an unused environment cell */ void genenv_0(int *x) { int i; /* If a cell is 0, it is unused */ for (i=0; i