From 67ae47a01ae1018adb85c3733638dcb678839fa1 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sat, 23 Feb 2019 19:59:13 -0700 Subject: [PATCH] begin work on between library --- README.md | 5 ++- src/prolog/{lib => examples}/minatotask.pl | 0 src/prolog/lib/atts.pl | 1 - src/prolog/lib/between.pl | 48 ++++++++++++++++++++++ src/prolog/lib/builtins.pl | 38 ++++++++++------- src/prolog/lib/control.pl | 24 ----------- src/prolog/machine/mod.rs | 4 +- src/tests.rs | 12 +++--- 8 files changed, 83 insertions(+), 49 deletions(-) rename src/prolog/{lib => examples}/minatotask.pl (100%) create mode 100644 src/prolog/lib/between.pl delete mode 100644 src/prolog/lib/control.pl diff --git a/README.md b/README.md index 5816829b..a0aec782 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,8 @@ The following predicates are built-in to rusty-wam. * `float/1` * `freeze/2` * `functor/3` +* `gen_int/1` +* `gen_nat/1` * `goal_expansion/2` * `ground/1` * `integer/1` @@ -170,12 +172,13 @@ The following predicates are built-in to rusty-wam. * `nl/0` * `nonvar/1` * `numbervars/2` +* `numlist/2` * `once/1` * `partial_string/2` * `phrase/{2,3}` * `rational/1` * `read/1` -* `repeat/0` +* `repeat/{0,1}` * `reverse/2` * `select/3` * `setof/3` diff --git a/src/prolog/lib/minatotask.pl b/src/prolog/examples/minatotask.pl similarity index 100% rename from src/prolog/lib/minatotask.pl rename to src/prolog/examples/minatotask.pl diff --git a/src/prolog/lib/atts.pl b/src/prolog/lib/atts.pl index 528c5065..58555a27 100644 --- a/src/prolog/lib/atts.pl +++ b/src/prolog/lib/atts.pl @@ -4,7 +4,6 @@ '$add_to_list'/3, '$del_attr'/3, '$del_attr_step'/3, '$del_attr_buried'/4]). -:- use_module(library(control)). :- use_module(library(dcgs)). :- use_module(library(terms)). diff --git a/src/prolog/lib/between.pl b/src/prolog/lib/between.pl new file mode 100644 index 00000000..9dce74da --- /dev/null +++ b/src/prolog/lib/between.pl @@ -0,0 +1,48 @@ +:- module(between, [between/3, gen_int/1, gen_nat/1, numlist/2, repeat/1]). + +%% TODO: numlist/3, numlist/5. + +:- use_module(library(lists), [length/2]). + +between(Lower, Upper, Lower) :- + Lower =< Upper. +between(Lower1, Upper, X) :- + Lower1 < Upper, + Lower2 is Lower1 + 1, + between(Lower2, Upper, X). + +enumerate_nats(I, I). +enumerate_nats(I0, N) :- + I1 is I0 + 1, + enumerate_nats(I1, N). + +gen_nat(N) :- + integer(N), !, N >= 0. +gen_nat(N) :- + var(N), enumerate_nats(0, N). + +enumerate_ints(I, I). +enumerate_ints(I0, N) :- + I0 > 0, + N is -I0. +enumerate_ints(I0, N) :- + I1 is I0 + 1, + enumerate_ints(I1, N). + +gen_int(N) :- + integer(N), !. +gen_int(N) :- + var(N), enumerate_ints(0, N). + +repeat_integer(N) :- + N > 0. +repeat_integer(N0) :- + N0 > 0, N1 is N0 - 1, repeat_integer(N1). + +repeat(N) :- + integer(N), N > 0, repeat_integer(N). + +numlist(Upper, List) :- + ( integer(Upper) -> findall(X, between(1, Upper, X), List) + ; List = [_|_], length(List, Upper), findall(X, between(1, Upper, X), List) + ). diff --git a/src/prolog/lib/builtins.pl b/src/prolog/lib/builtins.pl index 4496fc01..7239783d 100644 --- a/src/prolog/lib/builtins.pl +++ b/src/prolog/lib/builtins.pl @@ -1,13 +1,14 @@ :- op(400, yfx, /). -:- module(builtins, [(=)/2, (+)/1, (+)/2, (**)/2, (*)/2, (-)/1, (-)/2, - (/)/2, (/\)/2, (\/)/2, (is)/2, (xor)/2, (div)/2, (//)/2, - (rdiv)/2, (<<)/2, (>>)/2, (mod)/2, (rem)/2, (>)/2, (<)/2, - (=\=)/2, (=:=)/2, (-)/1, (>=)/2, (=<)/2, (,)/2, (->)/2, (;)/2, - (=..)/2, (==)/2, (\==)/2, (@=<)/2, (@>=)/2, (@<)/2, (@>)/2, - (=@=)/2, (\=@=)/2, (:)/2, bagof/3, call_with_inference_limit/3, - catch/3, current_prolog_flag/2, expand_goal/2, expand_term/2, - findall/3, findall/4, set_prolog_flag/2, setof/3, setup_call_cleanup/3, +:- module(builtins, [(=)/2, (\=)/2, (\+)/1, (+)/1, (+)/2, (**)/2, + (*)/2, (-)/1, (-)/2, (/)/2, (/\)/2, (\/)/2, (is)/2, (xor)/2, + (div)/2, (//)/2, (rdiv)/2, (<<)/2, (>>)/2, (mod)/2, (rem)/2, + (>)/2, (<)/2, (=\=)/2, (=:=)/2, (-)/1, (>=)/2, (=<)/2, (,)/2, + (->)/2, (;)/2, (=..)/2, (==)/2, (\==)/2, (@=<)/2, (@>=)/2, + (@<)/2, (@>)/2, (=@=)/2, (\=@=)/2, (:)/2, bagof/3, + call_with_inference_limit/3, catch/3, current_prolog_flag/2, + expand_goal/2, expand_term/2, findall/3, findall/4, once/1, + repeat/0, set_prolog_flag/2, setof/3, setup_call_cleanup/3, term_variables/2, throw/1, true/0, false/0, write/1, write_canonical/1, writeq/1, write_term/2]). @@ -44,7 +45,7 @@ expand_op_list([Op | OtherOps], Pred, Spec, [(:- op(Pred, Spec, Op)) | OtherResu :- op(1100, xfy, ;). % control. -:- op(700, xfx, [=, =..]). +:- op(700, xfx, [=, =.., \=]). :- op(900, fy, \+). % term comparison. @@ -116,6 +117,17 @@ set_prolog_flag(Flag, _) :- % control operators. +\+ G :- G, !, false. +\+ _. + +X \= X :- !, false. +_ \= _. + +once(G) :- G, !. + +repeat. +repeat :- repeat. + ','(G1, G2) :- '$get_b_value'(B), '$call_with_default_policy'(comma_errors(G1, G2, B)). :- non_counted_backtracking comma_errors/3. @@ -154,9 +166,6 @@ G1 -> G2 :- '$get_b_value'(B), '$call_with_default_policy'(->(G1, G2, B)). % univ. -\+ G :- G, !, false. -\+ _. - :- non_counted_backtracking univ_errors/3. univ_errors(Term, List, N) :- '$skip_max_list'(N, -1, List, R), @@ -363,9 +372,8 @@ throw(Ball) :- '$set_ball'(Ball), '$unwind_stack'. truncate_lh_to(LhLength) :- '$truncate_lh_to'(LhLength). -check_for_compat_list(L, PI) :- - '$skip_max_list'(_, -1, L, R), - ( nonvar(R), R \== [], throw(error(type_error(list, L), PI)) +check_for_compat_list(L, PI) :- + ( nonvar(L), L \= [_|_], throw(error(type_error(list, L), PI)) ; true ). diff --git a/src/prolog/lib/control.pl b/src/prolog/lib/control.pl deleted file mode 100644 index 6e896b3b..00000000 --- a/src/prolog/lib/control.pl +++ /dev/null @@ -1,24 +0,0 @@ -:- module(control, [(\=)/2, (\+)/1, between/3, call_cleanup/2, once/1, repeat/0]). - -:- op(900, fy, \+). -:- op(700, xfx, \=). - -once(G) :- G, !. - -\+ G :- G, !, false. -\+ _. - -X \= X :- !, false. -_ \= _. - -call_cleanup(G, C) :- setup_call_cleanup(true, G, C). - -between(Lower, Upper, Lower) :- - Lower =< Upper. -between(Lower1, Upper, X) :- - Lower1 < Upper, - Lower2 is Lower1 + 1, - between(Lower2, Upper, X). - -repeat. -repeat :- repeat. diff --git a/src/prolog/machine/mod.rs b/src/prolog/machine/mod.rs index 762ce15e..b0eeb266 100644 --- a/src/prolog/machine/mod.rs +++ b/src/prolog/machine/mod.rs @@ -312,8 +312,8 @@ impl SubModuleUser for IndexStore { } static BUILTINS: &str = include_str!("../lib/builtins.pl"); +static BETWEEN: &str = include_str!("../lib/between.pl"); static LISTS: &str = include_str!("../lib/lists.pl"); -static CONTROL: &str = include_str!("../lib/control.pl"); static QUEUES: &str = include_str!("../lib/queues.pl"); static ERROR: &str = include_str!("../lib/error.pl"); static TERMS: &str = include_str!("../lib/terms.pl"); @@ -344,7 +344,7 @@ impl Machine { fn compile_libraries(&mut self) { compile_user_module(self, LISTS.as_bytes()); - compile_user_module(self, CONTROL.as_bytes()); + compile_user_module(self, BETWEEN.as_bytes()); compile_user_module(self, QUEUES.as_bytes()); compile_user_module(self, ERROR.as_bytes()); compile_user_module(self, TERMS.as_bytes()); diff --git a/src/tests.rs b/src/tests.rs index 95e7502d..a9fe8958 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1715,7 +1715,7 @@ fn test_queries_on_builtins() assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 2), S).", [["S = [1, 2]", "X = _0"]]); assert_prolog_success!(&mut wam, "?- findall(X+Y, (X = 1), S).", - [["S = [1+_35]", "X = _1", "Y = _2"]]); + [["S = [1+_33]", "X = _1", "Y = _2"]]); assert_prolog_success!(&mut wam, "?- findall(X, false, S).", [["S = []", "X = _0"]]); assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 1), S).", @@ -1744,12 +1744,12 @@ fn test_queries_on_builtins() assert_prolog_success!(&mut wam, "?- bagof(X, (X=Y; X=Z; Y=1), L).", [["L = [_3, _6]", "X = _0", "Y = _3", "Z = _6"], - ["L = [_182]", "X = _0", "Y = 1", "Z = _6"]]); + ["L = [_178]", "X = _0", "Y = 1", "Z = _6"]]); submit(&mut wam, "a(1, f(_)). a(2, f(_))."); assert_prolog_success!(&mut wam, "?- bagof(X, a(X, Y), L).", - [["L = [1, 2]", "X = _0", "Y = f(_148)"]]); + [["L = [1, 2]", "X = _0", "Y = f(_144)"]]); assert_prolog_success!(&mut wam, "?- setof(X, (X = 1 ; X = 2), S).", [["S = [1, 2]", "X = _0"]]); @@ -1761,7 +1761,7 @@ fn test_queries_on_builtins() ["L = [1]", "Y = 2"]]); assert_prolog_success!(&mut wam, "?- setof(X, (X=Y; X=Z; Y=1), L).", [["L = [_3, _6]", "X = _0", "Y = _3", "Z = _6"], - ["L = [_182]", "X = _0", "Y = 1", "Z = _6"]]); + ["L = [_178]", "X = _0", "Y = 1", "Z = _6"]]); assert_prolog_failure!(&mut wam, "?- setof(X, member(X, [f(U,b),f(V,c)]), [f(a,c),f(a,b)])."); assert_prolog_success!(&mut wam, "?- setof(X, member(X, [f(U,b),f(V,c)]), [f(a,b),f(a,c)]).", [["U = a", "V = a", "X = _0"]]); @@ -1773,7 +1773,7 @@ fn test_queries_on_builtins() assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 2), S0, S1).", [["S0 = [1, 2 | _11]", "S1 = _11", "X = _0"]]); assert_prolog_success!(&mut wam, "?- findall(X+Y, (X = 1), S0, S1).", - [["S0 = [1+_42 | _7]", "S1 = _7", "X = _1", "Y = _2"]]); + [["S0 = [1+_38 | _7]", "S1 = _7", "X = _1", "Y = _2"]]); assert_prolog_success!(&mut wam, "?- findall(X, false, S, _).", [["S = []", "X = _0"]]); assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 1), S0, S1).", @@ -2256,7 +2256,7 @@ fn test_queries_on_attributed_variables() put_atts(V, my_mod, -dif(A)), get_atts(V, my_mod, Ls).", [["A = _98", "Ls = [frozen(a), frozen(b)]", "V = _31"]]); - submit(&mut wam, include_str!("./prolog/lib/minatotask.pl")); + submit(&mut wam, include_str!("./prolog/examples/minatotask.pl")); submit(&mut wam, ":- use_module(library(zdd))."); assert_prolog_failure!(&mut wam, "?- ZDD = ( X -> b(true) ; ( Y -> b(true) ; b(false) ) ), -- 2.54.0