begin work on between library

This commit is contained in:
Mark Thom
2019-02-23 19:59:13 -07:00
parent 20b66e283f
commit 67ae47a01a
8 changed files with 83 additions and 49 deletions

View File

@@ -156,6 +156,8 @@ The following predicates are built-in to rusty-wam.
* `float/1` * `float/1`
* `freeze/2` * `freeze/2`
* `functor/3` * `functor/3`
* `gen_int/1`
* `gen_nat/1`
* `goal_expansion/2` * `goal_expansion/2`
* `ground/1` * `ground/1`
* `integer/1` * `integer/1`
@@ -170,12 +172,13 @@ The following predicates are built-in to rusty-wam.
* `nl/0` * `nl/0`
* `nonvar/1` * `nonvar/1`
* `numbervars/2` * `numbervars/2`
* `numlist/2`
* `once/1` * `once/1`
* `partial_string/2` * `partial_string/2`
* `phrase/{2,3}` * `phrase/{2,3}`
* `rational/1` * `rational/1`
* `read/1` * `read/1`
* `repeat/0` * `repeat/{0,1}`
* `reverse/2` * `reverse/2`
* `select/3` * `select/3`
* `setof/3` * `setof/3`

View File

@@ -4,7 +4,6 @@
'$add_to_list'/3, '$del_attr'/3, '$del_attr_step'/3, '$add_to_list'/3, '$del_attr'/3, '$del_attr_step'/3,
'$del_attr_buried'/4]). '$del_attr_buried'/4]).
:- use_module(library(control)).
:- use_module(library(dcgs)). :- use_module(library(dcgs)).
:- use_module(library(terms)). :- use_module(library(terms)).

48
src/prolog/lib/between.pl Normal file
View File

@@ -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)
).

View File

@@ -1,13 +1,14 @@
:- op(400, yfx, /). :- op(400, yfx, /).
:- module(builtins, [(=)/2, (+)/1, (+)/2, (**)/2, (*)/2, (-)/1, (-)/2, :- module(builtins, [(=)/2, (\=)/2, (\+)/1, (+)/1, (+)/2, (**)/2,
(/)/2, (/\)/2, (\/)/2, (is)/2, (xor)/2, (div)/2, (//)/2, (*)/2, (-)/1, (-)/2, (/)/2, (/\)/2, (\/)/2, (is)/2, (xor)/2,
(rdiv)/2, (<<)/2, (>>)/2, (mod)/2, (rem)/2, (>)/2, (<)/2, (div)/2, (//)/2, (rdiv)/2, (<<)/2, (>>)/2, (mod)/2, (rem)/2,
(=\=)/2, (=:=)/2, (-)/1, (>=)/2, (=<)/2, (,)/2, (->)/2, (;)/2, (>)/2, (<)/2, (=\=)/2, (=:=)/2, (-)/1, (>=)/2, (=<)/2, (,)/2,
(=..)/2, (==)/2, (\==)/2, (@=<)/2, (@>=)/2, (@<)/2, (@>)/2, (->)/2, (;)/2, (=..)/2, (==)/2, (\==)/2, (@=<)/2, (@>=)/2,
(=@=)/2, (\=@=)/2, (:)/2, bagof/3, call_with_inference_limit/3, (@<)/2, (@>)/2, (=@=)/2, (\=@=)/2, (:)/2, bagof/3,
catch/3, current_prolog_flag/2, expand_goal/2, expand_term/2, call_with_inference_limit/3, catch/3, current_prolog_flag/2,
findall/3, findall/4, set_prolog_flag/2, setof/3, setup_call_cleanup/3, 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, term_variables/2, throw/1, true/0, false/0, write/1,
write_canonical/1, writeq/1, write_term/2]). 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, ;). :- op(1100, xfy, ;).
% control. % control.
:- op(700, xfx, [=, =..]). :- op(700, xfx, [=, =.., \=]).
:- op(900, fy, \+). :- op(900, fy, \+).
% term comparison. % term comparison.
@@ -116,6 +117,17 @@ set_prolog_flag(Flag, _) :-
% control operators. % 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)). ','(G1, G2) :- '$get_b_value'(B), '$call_with_default_policy'(comma_errors(G1, G2, B)).
:- non_counted_backtracking comma_errors/3. :- non_counted_backtracking comma_errors/3.
@@ -154,9 +166,6 @@ G1 -> G2 :- '$get_b_value'(B), '$call_with_default_policy'(->(G1, G2, B)).
% univ. % univ.
\+ G :- G, !, false.
\+ _.
:- non_counted_backtracking univ_errors/3. :- non_counted_backtracking univ_errors/3.
univ_errors(Term, List, N) :- univ_errors(Term, List, N) :-
'$skip_max_list'(N, -1, List, R), '$skip_max_list'(N, -1, List, R),
@@ -364,8 +373,7 @@ throw(Ball) :- '$set_ball'(Ball), '$unwind_stack'.
truncate_lh_to(LhLength) :- '$truncate_lh_to'(LhLength). truncate_lh_to(LhLength) :- '$truncate_lh_to'(LhLength).
check_for_compat_list(L, PI) :- check_for_compat_list(L, PI) :-
'$skip_max_list'(_, -1, L, R), ( nonvar(L), L \= [_|_], throw(error(type_error(list, L), PI))
( nonvar(R), R \== [], throw(error(type_error(list, L), PI))
; true ; true
). ).

View File

@@ -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.

View File

@@ -312,8 +312,8 @@ impl SubModuleUser for IndexStore {
} }
static BUILTINS: &str = include_str!("../lib/builtins.pl"); 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 LISTS: &str = include_str!("../lib/lists.pl");
static CONTROL: &str = include_str!("../lib/control.pl");
static QUEUES: &str = include_str!("../lib/queues.pl"); static QUEUES: &str = include_str!("../lib/queues.pl");
static ERROR: &str = include_str!("../lib/error.pl"); static ERROR: &str = include_str!("../lib/error.pl");
static TERMS: &str = include_str!("../lib/terms.pl"); static TERMS: &str = include_str!("../lib/terms.pl");
@@ -344,7 +344,7 @@ impl Machine {
fn compile_libraries(&mut self) { fn compile_libraries(&mut self) {
compile_user_module(self, LISTS.as_bytes()); 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, QUEUES.as_bytes());
compile_user_module(self, ERROR.as_bytes()); compile_user_module(self, ERROR.as_bytes());
compile_user_module(self, TERMS.as_bytes()); compile_user_module(self, TERMS.as_bytes());

View File

@@ -1715,7 +1715,7 @@ fn test_queries_on_builtins()
assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 2), S).", assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 2), S).",
[["S = [1, 2]", "X = _0"]]); [["S = [1, 2]", "X = _0"]]);
assert_prolog_success!(&mut wam, "?- findall(X+Y, (X = 1), S).", 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).", assert_prolog_success!(&mut wam, "?- findall(X, false, S).",
[["S = []", "X = _0"]]); [["S = []", "X = _0"]]);
assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 1), S).", 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).", assert_prolog_success!(&mut wam, "?- bagof(X, (X=Y; X=Z; Y=1), L).",
[["L = [_3, _6]", "X = _0", "Y = _3", "Z = _6"], [["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(_))."); submit(&mut wam, "a(1, f(_)). a(2, f(_)).");
assert_prolog_success!(&mut wam, "?- bagof(X, a(X, Y), L).", 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).", assert_prolog_success!(&mut wam, "?- setof(X, (X = 1 ; X = 2), S).",
[["S = [1, 2]", "X = _0"]]); [["S = [1, 2]", "X = _0"]]);
@@ -1761,7 +1761,7 @@ fn test_queries_on_builtins()
["L = [1]", "Y = 2"]]); ["L = [1]", "Y = 2"]]);
assert_prolog_success!(&mut wam, "?- setof(X, (X=Y; X=Z; Y=1), L).", assert_prolog_success!(&mut wam, "?- setof(X, (X=Y; X=Z; Y=1), L).",
[["L = [_3, _6]", "X = _0", "Y = _3", "Z = _6"], [["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_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)]).", 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"]]); [["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).", assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 2), S0, S1).",
[["S0 = [1, 2 | _11]", "S1 = _11", "X = _0"]]); [["S0 = [1, 2 | _11]", "S1 = _11", "X = _0"]]);
assert_prolog_success!(&mut wam, "?- findall(X+Y, (X = 1), S0, S1).", 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, _).", assert_prolog_success!(&mut wam, "?- findall(X, false, S, _).",
[["S = []", "X = _0"]]); [["S = []", "X = _0"]]);
assert_prolog_success!(&mut wam, "?- findall(X, (X = 1 ; X = 1), S0, S1).", 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).", put_atts(V, my_mod, -dif(A)), get_atts(V, my_mod, Ls).",
[["A = _98", "Ls = [frozen(a), frozen(b)]", "V = _31"]]); [["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))."); submit(&mut wam, ":- use_module(library(zdd)).");
assert_prolog_failure!(&mut wam, "?- ZDD = ( X -> b(true) ; ( Y -> b(true) ; b(false) ) ), assert_prolog_failure!(&mut wam, "?- ZDD = ( X -> b(true) ; ( Y -> b(true) ; b(false) ) ),