From 7edbb540bdf1c7d18ce43299f06884a6e0545f82 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 24 Feb 2019 13:10:31 -0700 Subject: [PATCH] use can_be and must_be in library(between) --- src/prolog/lib/between.pl | 20 +++++++++++++------- src/prolog/machine/mod.rs | 6 +++--- src/prolog/write.rs | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/prolog/lib/between.pl b/src/prolog/lib/between.pl index e7167ca7..730e590e 100644 --- a/src/prolog/lib/between.pl +++ b/src/prolog/lib/between.pl @@ -3,6 +3,7 @@ %% TODO: numlist/5. :- use_module(library(lists), [length/2]). +:- use_module(library(error)). between(Lower, Upper, Lower) :- Lower =< Upper. @@ -19,7 +20,9 @@ enumerate_nats(I0, N) :- gen_nat(N) :- integer(N), !, N >= 0. gen_nat(N) :- - var(N), enumerate_nats(0, N). + var(N), !, enumerate_nats(0, N). +gen_nat(N) :- + throw(error(type_error(integer, N), gen_nat/1)). enumerate_ints(I, I). enumerate_ints(I0, N) :- @@ -32,7 +35,9 @@ enumerate_ints(I0, N) :- gen_int(N) :- integer(N), !. gen_int(N) :- - var(N), enumerate_ints(0, N). + var(N), !, enumerate_ints(0, N). +gen_int(N) :- + throw(error(type_error(integer, N), gen_int/1)). repeat_integer(N) :- N > 0. @@ -40,7 +45,7 @@ repeat_integer(N0) :- N0 > 0, N1 is N0 - 1, repeat_integer(N1). repeat(N) :- - integer(N), N > 0, repeat_integer(N). + must_be(integer, N), N > 0, repeat_integer(N). numlist(Upper, List) :- ( integer(Upper) -> findall(X, between(1, Upper, X), List) @@ -48,14 +53,14 @@ numlist(Upper, List) :- ). diag_nats(M, N, M, N). +diag_nats(M, 0, M1, N1) :- + !, + M0 is M+1, + diag_nats(0, M0, M1, N1). diag_nats(M, N, M1, N1) :- - N > 0, !, M0 is M+1, N0 is N-1, diag_nats(M0, N0, M1, N1). -diag_nats(M, 0, M1, N1) :- - M0 is M+1, - diag_nats(0, M0, M1, N1). diag_nats(0, 0). diag_nats(M, N) :- @@ -82,6 +87,7 @@ diag_ints(M, N) :- diag_ints(M0, N0, M, N). gen_ints(L, U) :- + can_be(integer, L), can_be(integer, U), ( integer(L), integer(U), ! ; integer(L) -> gen_int(U) ; integer(U) -> gen_int(L) diff --git a/src/prolog/machine/mod.rs b/src/prolog/machine/mod.rs index b0eeb266..c2567c4e 100644 --- a/src/prolog/machine/mod.rs +++ b/src/prolog/machine/mod.rs @@ -312,10 +312,10 @@ 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 QUEUES: &str = include_str!("../lib/queues.pl"); static ERROR: &str = include_str!("../lib/error.pl"); +static BETWEEN: &str = include_str!("../lib/between.pl"); static TERMS: &str = include_str!("../lib/terms.pl"); static DCGS: &str = include_str!("../lib/dcgs.pl"); static ATTS: &str = include_str!("../lib/atts.pl"); @@ -343,10 +343,10 @@ impl Machine { } fn compile_libraries(&mut self) { - compile_user_module(self, LISTS.as_bytes()); - compile_user_module(self, BETWEEN.as_bytes()); + compile_user_module(self, LISTS.as_bytes()); compile_user_module(self, QUEUES.as_bytes()); compile_user_module(self, ERROR.as_bytes()); + compile_user_module(self, BETWEEN.as_bytes()); compile_user_module(self, TERMS.as_bytes()); compile_user_module(self, DCGS.as_bytes()); compile_user_module(self, ATTS.as_bytes()); diff --git a/src/prolog/write.rs b/src/prolog/write.rs index 6b39d382..206fc87c 100644 --- a/src/prolog/write.rs +++ b/src/prolog/write.rs @@ -389,7 +389,7 @@ pub fn print(wam: &mut Machine, result: EvalSession) { let attr_goals = wam.attribute_goals(&heap_locs); if !attr_goals.is_empty() { - write!(raw_stdout, "\r\n{}\r", attr_goals).unwrap(); + write!(raw_stdout, "\r\n{}\r\n", attr_goals).unwrap(); } if !wam.or_stack_is_empty() { -- 2.54.0