/* Runs a Picat program via the shell command line in Picat. This works for simple goals: $ picat run_program.pi queens go $ picat run_program.pi queens "time(go)" If time2 is defined in queens.pi, then this also works $ picat run_program.pi queens "time2(go)" And we can use the predicates defined in this program (here timeX/1) $ picat run_program.pi queens "timeX(go)" However, it don't work for more complex goals, e.g. calling a predicate with a variable: $ picat run_program.pi queens 'queens3(100,Q),writeln(Q)' *** Undefined procedure: throw/2 Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ timeX(Goal) => statistics(runtime,_), statistics(backtracks, Backtracks1), (call(Goal); true), statistics(backtracks, Backtracks2), statistics(runtime, [_,End]), T = End / 1000.0, Backtracks = Backtracks2 - Backtracks1, printf("CPU time %2.5f seconds. Backtracks: %d\n", T, Backtracks). % timeout(Goal, Limit in s) timeout(Goal, Limit) => println($timeout(Goal, Limit)), if Limit == 0 then call(Goal) else time_out(Goal, Limit*1000, Result) end, println(Result). time_out(Goal,Limit) => timeout(Goal, Limit). % Run Goal N times, % and report times and their average bench(Goal,N) => Times = [], statistics(runtime,_), foreach(_I in 1..N) call(Goal), statistics(runtime, [_,End]), T = End / 1000.0, Times := Times ++ [T] end, println([times=Times, avg=avg(Times)]). bench(Goal) => bench(Goal,1). eval(Expr) = parse_term(Expr.flatten()).apply(). main([Program|Goals]) => println(program=Program), cl(Program), % compile and load the program foreach(Goal in Goals) nl, G = parse_term(Goal), % convert the goal string to an atom println(goal=Goal), % G.call() time2(G.call()) % time(G.call()) end.