begin work on between library
This commit is contained in:
@@ -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)).
|
||||
|
||||
|
||||
48
src/prolog/lib/between.pl
Normal file
48
src/prolog/lib/between.pl
Normal 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)
|
||||
).
|
||||
@@ -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
|
||||
).
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user