From 88f1160e2b6199ee613d3501a834a0fb5de54a44 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 01:22:13 -0700 Subject: [PATCH 01/13] use find_inner_choice_instr when appending an indexed clause to an indexed subsequence --- src/machine/compile.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/machine/compile.rs b/src/machine/compile.rs index 13ac32c5..2d05ae34 100644 --- a/src/machine/compile.rs +++ b/src/machine/compile.rs @@ -1001,22 +1001,22 @@ fn append_compiled_clause( AppendOrPrepend::Append, ); - if lower_bound + 1 == target_pos { - let lower_bound_clause_start = find_inner_choice_instr( - code, - skeleton.clauses[lower_bound].clause_start, - index_loc, - ); + let target_pos_clause_start = find_inner_choice_instr( + code, + skeleton.clauses[target_pos - 1].clause_start, + index_loc, + ); + if lower_bound + 1 == target_pos { set_switch_var_offset( code, index_loc, - lower_bound_clause_start - index_loc, + target_pos_clause_start - index_loc, retraction_info, ); } - skeleton.clauses[target_pos - 1].clause_start + target_pos_clause_start // skeleton.clauses[target_pos - 1].clause_start } _ => { skeleton.clauses[target_pos].opt_arg_index_key += clause_loc; From f5d808e68f0763cbe86f5b7c82e29a4cc6ee7823 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 11:11:22 -0700 Subject: [PATCH 02/13] do not traverse data structures when binding to local variables in unify_with_occurs_check (#781) --- src/machine/machine_state_impl.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/machine/machine_state_impl.rs b/src/machine/machine_state_impl.rs index 5de07140..f7cda917 100644 --- a/src/machine/machine_state_impl.rs +++ b/src/machine/machine_state_impl.rs @@ -201,6 +201,14 @@ impl MachineState { } fn bind_with_occurs_check(&mut self, r: Ref, addr: Addr) { + if let Ref::StackCell(..) = r { + // local variable optimization -- r cannot occur in the + // data structure bound to addr, so don't bother + // traversing it. + self.bind(r, addr); + return; + } + let mut fail = false; for addr in self.acyclic_pre_order_iter(addr) { From 5749d43fb6b575f47a73b03bc8cacc86d39c8a8f Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 11:19:33 -0700 Subject: [PATCH 03/13] restore prolog_parser test files (#782) --- crates/prolog_parser/Cargo.lock | 265 +++++++++++++++++++++ crates/prolog_parser/tests/bom.rs | 43 ++++ crates/prolog_parser/tests/parse_tokens.rs | 114 +++++++++ 3 files changed, 422 insertions(+) create mode 100644 crates/prolog_parser/Cargo.lock create mode 100644 crates/prolog_parser/tests/bom.rs create mode 100644 crates/prolog_parser/tests/parse_tokens.rs diff --git a/crates/prolog_parser/Cargo.lock b/crates/prolog_parser/Cargo.lock new file mode 100644 index 00000000..f90f0d69 --- /dev/null +++ b/crates/prolog_parser/Cargo.lock @@ -0,0 +1,265 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "arrayvec" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" +dependencies = [ + "nodrop", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "az" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d84e1d907bfc5795a6addb95ef8666141ee73c8f2f5250ff2a46bf4e4f4aec8a" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "gmp-mpfr-sys" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a57fdb339d49833021b1fded600ed240ae907e33909d5511a61dff884df7f16e" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "lexical" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e0d09e60c187a6d0a3fa418aec8587c6a4ae9de872f6126f2134f319b5ed10d" +dependencies = [ + "cfg-if", + "lexical-core", + "rustc_version", +] + +[[package]] +name = "lexical-core" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14" +dependencies = [ + "arrayvec", + "cfg-if", + "rustc_version", + "ryu", + "static_assertions", +] + +[[package]] +name = "libc" +version = "0.2.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" + +[[package]] +name = "nodrop" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" + +[[package]] +name = "num-bigint" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" +dependencies = [ + "autocfg", + "num-integer", + "num-traits 0.2.14", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits 0.2.14", +] + +[[package]] +name = "num-rational" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits 0.2.14", +] + +[[package]] +name = "num-rug-adapter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7470b6acf85abce0771203112db4181d03f7b8a6be49f0e842a78030192f8a58" +dependencies = [ + "libc", + "num-bigint", + "num-integer", + "num-rational", + "num-traits 0.2.14", +] + +[[package]] +name = "num-traits" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" +dependencies = [ + "num-traits 0.2.14", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "ordered-float" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eb5259643245d3f292c7a146b2df53bba24d7eab159410e648eb73dc164669d" +dependencies = [ + "num-traits 0.1.43", + "unreachable", +] + +[[package]] +name = "prolog_parser_rebis" +version = "0.8.68" +dependencies = [ + "lexical", + "num-rug-adapter", + "ordered-float", + "rug", + "unicode_reader", +] + +[[package]] +name = "rug" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e538d00da450a8e48aac7e6322e67b2dc86ec71a1feeac0e3954c4f07f01bc45" +dependencies = [ + "az", + "gmp-mpfr-sys", + "libc", +] + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "smallvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" + +[[package]] +name = "static_assertions" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3" + +[[package]] +name = "unicode-segmentation" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" + +[[package]] +name = "unicode_reader" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b639121690b27acd92c97ed2b52c5e5e8d3d39482e943b4559695cef62f771a" +dependencies = [ + "smallvec", + "unicode-segmentation", +] + +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +dependencies = [ + "void", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/crates/prolog_parser/tests/bom.rs b/crates/prolog_parser/tests/bom.rs new file mode 100644 index 00000000..25b57104 --- /dev/null +++ b/crates/prolog_parser/tests/bom.rs @@ -0,0 +1,43 @@ +extern crate prolog_parser_rebis; + +use prolog_parser_rebis::ast::*; +use prolog_parser_rebis::lexer::{Lexer, Token}; +use prolog_parser_rebis::tabled_rc::TabledData; + +use std::rc::Rc; + +#[test] +fn valid_token() { + let stream = parsing_stream("valid text".as_bytes()); + assert!(stream.is_ok()); +} + +#[test] +fn empty_stream() { + let bytes: &[u8] = &[]; + assert!(parsing_stream(bytes).is_ok()); +} + +#[test] +fn skip_utf8_bom() { + let atom_tbl = TabledData::new(Rc::new("my_module".to_string())); + let flags = MachineFlags::default(); + let bytes: &[u8] = &[0xEF, 0xBB, 0xBF, '4' as u8, '\n' as u8]; + let mut stream = parsing_stream(bytes).expect("valid stream"); + let mut lexer = Lexer::new(atom_tbl, flags, &mut stream); + match lexer.next_token() { + Ok(Token::Constant(Constant::Fixnum(4))) => (), + _ => assert!(false) + } +} + +#[test] +fn invalid_utf16_bom() { + let bytes: &[u8] = &[0xFF, 0xFE, 'a' as u8, '\n' as u8]; + let stream = parsing_stream(bytes); + match stream { + Err(ParserError::Utf8Error(0, 0)) => (), + _ => assert!(false) + } +} + diff --git a/crates/prolog_parser/tests/parse_tokens.rs b/crates/prolog_parser/tests/parse_tokens.rs new file mode 100644 index 00000000..3afb9164 --- /dev/null +++ b/crates/prolog_parser/tests/parse_tokens.rs @@ -0,0 +1,114 @@ +extern crate prolog_parser_rebis; + +use prolog_parser_rebis::ast::*; +use prolog_parser_rebis::lexer::{Lexer, Token}; +use prolog_parser_rebis::tabled_rc::TabledData; + +use std::rc::Rc; + +fn read_all_tokens(text: &str) -> Result, ParserError> { + let atom_tbl = TabledData::new(Rc::new("my_module".to_string())); + let flags = MachineFlags::default(); + let mut stream = parsing_stream(text.as_bytes())?; + let mut lexer = Lexer::new(atom_tbl, flags, &mut stream); + + let mut tokens = Vec::new(); + while !lexer.eof()? { + let token = lexer.next_token()?; + tokens.push(token); + } + Ok(tokens) +} + +#[test] +fn empty_multiline_comment() -> Result<(), ParserError> { + let tokens = read_all_tokens("/**/ 4\n")?; + assert_eq!(tokens, [Token::Constant(Constant::Fixnum(4))]); + Ok(()) +} + +#[test] +fn any_char_multiline_comment() -> Result<(), ParserError> { + let tokens = read_all_tokens("/* █╗╚═══╝ © */ 4\n")?; + assert_eq!(tokens, [Token::Constant(Constant::Fixnum(4))]); + Ok(()) +} + +#[test] +fn simple_char() -> Result<(), ParserError> { + let tokens = read_all_tokens("'a'\n")?; + assert_eq!(tokens, [Token::Constant(Constant::Char('a'))]); + Ok(()) +} + +#[test] +fn char_with_meta_seq() -> Result<(), ParserError> { + let tokens = read_all_tokens(r#"'\\' '\'' '\"' '\`' "#)?; // use literal string so \ are escaped + assert_eq!( + tokens, + [ + Token::Constant(Constant::Char('\\')), + Token::Constant(Constant::Char('\'')), + Token::Constant(Constant::Char('"')), + Token::Constant(Constant::Char('`')) + ] + ); + Ok(()) +} + +#[test] +fn char_with_control_seq() -> Result<(), ParserError> { + let tokens = read_all_tokens(r"'\a' '\b' '\r' '\f' '\t' '\n' '\v' ")?; + assert_eq!( + tokens, + [ + Token::Constant(Constant::Char('\u{07}')), + Token::Constant(Constant::Char('\u{08}')), + Token::Constant(Constant::Char('\r')), + Token::Constant(Constant::Char('\u{0c}')), + Token::Constant(Constant::Char('\t')), + Token::Constant(Constant::Char('\n')), + Token::Constant(Constant::Char('\u{0b}')), + ] + ); + Ok(()) +} + +#[test] +fn char_with_octseq() -> Result<(), ParserError> { + let tokens = read_all_tokens(r"'\60433\' ")?; + assert_eq!(tokens, [Token::Constant(Constant::Char('愛'))]); // Japanese character + Ok(()) +} + +#[test] +fn char_with_octseq_0() -> Result<(), ParserError> { + let tokens = read_all_tokens(r"'\0\' ")?; + assert_eq!(tokens, [Token::Constant(Constant::Char('\u{0000}'))]); + Ok(()) +} + +#[test] +fn char_with_hexseq() -> Result<(), ParserError> { + let tokens = read_all_tokens(r"'\x2124\' ")?; + assert_eq!(tokens, [Token::Constant(Constant::Char('ℤ'))]); // Z math symbol + Ok(()) +} + +#[test] +fn char_with_hexseq_invalid() { + assert!(read_all_tokens(r"'\x\' ").is_err()); +} + +#[test] +fn empty() -> Result<(), ParserError> { + let tokens = read_all_tokens("")?; + assert!(tokens.is_empty()); + Ok(()) +} + +#[test] +fn comment_then_eof() -> Result<(), ParserError> { + assert!(read_all_tokens("% only a comment").is_err()); + Ok(()) +} From 48f3f4ca37966c2f748d12c95e71e464a0776697 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 12:46:33 -0700 Subject: [PATCH 04/13] add meta-predicate declarations to reif library --- src/lib/reif.pl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib/reif.pl b/src/lib/reif.pl index 0e958356..092f93ec 100644 --- a/src/lib/reif.pl +++ b/src/lib/reif.pl @@ -4,6 +4,8 @@ :- use_module(library(dif)). +:- meta_predicate if_(0, 0, 0). + if_(If_1, Then_0, Else_0) :- call(If_1, T), ( T == true -> call(Then_0) @@ -26,6 +28,8 @@ dif(X, Y, T) :- non(true, false). non(false, true). +:- meta_predicate tfilter(1, ?, ?). + tfilter(C_2, Es, Fs) :- i_tfilter(Es, C_2, Fs). @@ -34,6 +38,8 @@ i_tfilter([E|Es], C_2, Fs0) :- if_(call(C_2, E), Fs0 = [E|Fs], Fs0 = Fs), i_tfilter(Es, C_2, Fs). +:- meta_predicate tpartition(1, ?, ?). + tpartition(P_2, Xs, Ts, Fs) :- i_tpartition(Xs, P_2, Ts, Fs). @@ -44,12 +50,18 @@ i_tpartition([X|Xs], P_2, Ts0, Fs0) :- , ( Fs0 = [X|Fs], Ts0 = Ts ) ), i_tpartition(Xs, P_2, Ts, Fs). +:- meta_predicate ','(0, 0, ?). + ','(A_1, B_1, T) :- if_(A_1, call(B_1, T), T = false). +:- meta_predicate ';'(0, 0, ?). + ';'(A_1, B_1, T) :- if_(A_1, T = true, call(B_1, T)). +:- meta_predicate cond_t(0, 0, ?). + cond_t(If_1, Then_0, T) :- if_(If_1, ( Then_0, T = true ), T = false ). @@ -60,8 +72,12 @@ i_memberd_t([], _, false). i_memberd_t([X|Xs], E, T) :- if_( X = E, T = true, i_memberd_t(Xs, E, T) ). +:- meta_predicate tmember(1, ?). + tmember(P_2, [X|Xs]) :- if_( call(P_2, X), true, tmember(P_2, Xs) ). +:- meta_predicate tmember_t(1, ?). + tmember_t(P_2, [X|Xs], T) :- if_( call(P_2, X), T = true, tmember_t(P_2, Xs, T) ). From 88ad2ee103c5f55564c4b8f67e93fc434042a162 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 15:18:38 -0700 Subject: [PATCH 05/13] restore cut after dcg_constr check (#784) --- src/lib/dcgs.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/dcgs.pl b/src/lib/dcgs.pl index f9410d99..05f41cb0 100644 --- a/src/lib/dcgs.pl +++ b/src/lib/dcgs.pl @@ -19,7 +19,7 @@ phrase(GRBody, S0, S) :- ( var(GRBody) -> throw(error(instantiation_error, phrase/3)) ; strip_module(GRBody, Module, GRBody0), - dcg_constr(GRBody0), + dcg_constr(GRBody0) -> ( var(Module) -> phrase_(GRBody0, S0, S) ; phrase_(Module:GRBody0, S0, S) From ae66e299e6867fc8f5cb63cede17bdcb54f990fc Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Fri, 8 Jan 2021 17:28:29 +0100 Subject: [PATCH 06/13] Use term expansion for generated predicates. --- src/lib/clpz.pl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/lib/clpz.pl b/src/lib/clpz.pl index a4f66256..fda2f309 100644 --- a/src/lib/clpz.pl +++ b/src/lib/clpz.pl @@ -7763,11 +7763,10 @@ zo_t(1, true). Generated predicates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -generated_clauses(Cs) :- - make_parse_clpz(Cs1), - make_parse_reified(Cs2), - make_matches(Cs3), - append([Cs1,Cs2,Cs3], Cs). +term_expansion(make_parse_clpz, Clauses) :- make_parse_clpz(Clauses). +term_expansion(make_parse_reified, Clauses) :- make_parse_reified(Clauses). +term_expansion(make_matches, Clauses) :- make_matches(Clauses). -:- initialization((generated_clauses(Cs), - maplist(assertz, Cs))). +make_parse_clpz. +make_parse_reified. +make_matches. From cab0e4395ab3c5478d344aa9393cf44499228ead Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 15:37:16 -0700 Subject: [PATCH 07/13] manually expand module names in higher-order predicates of lists.pl and give call_residue_vars/2 a meta-predicate definition --- src/lib/atts.pl | 2 ++ src/lib/lists.pl | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib/atts.pl b/src/lib/atts.pl index 8fa16e11..fcd5b2c1 100644 --- a/src/lib/atts.pl +++ b/src/lib/atts.pl @@ -156,6 +156,8 @@ user:goal_expansion(Term, M:get_atts(Var, Attr)) :- nonvar(Term), Term = get_atts(Var, M, Attr). +:- meta_predicate call_residue_vars(0, ?). + call_residue_vars(Goal, Vars) :- '$get_attr_var_queue_delim'(B), call(Goal), diff --git a/src/lib/lists.pl b/src/lib/lists.pl index c6160e55..5549a061 100644 --- a/src/lib/lists.pl +++ b/src/lib/lists.pl @@ -126,7 +126,7 @@ maplist(Cont, [E1|E1s], [E2|E2s], [E3|E3s], [E4|E4s], [E5|E5s], [E6|E6s], [E7|E7 sum_list(Ls, S) :- - foldl(sum_, Ls, 0, S). + foldl(lists:sum_, Ls, 0, S). sum_(L, S0, S) :- S is S0 + L. @@ -164,13 +164,13 @@ lists_transpose([L|Ls], Ts) :- foldl(transpose_, L, Ts, [L|Ls], _). transpose_(_, Fs, Lists0, Lists) :- - maplist(list_first_rest, Lists0, Fs, Lists). + maplist(lists:list_first_rest, Lists0, Fs, Lists). list_first_rest([L|Ls], L, Ls). list_to_set(Ls0, Ls) :- - maplist(with_var, Ls0, LVs0), + maplist(lists:with_var, Ls0, LVs0), keysort(LVs0, LVs), same_elements(LVs), pick_firsts(LVs0, Ls). @@ -188,7 +188,7 @@ with_var(E, E-_). same_elements([]). same_elements([EV|EVs]) :- - foldl(unify_same, EVs, EV, _). + foldl(lists:unify_same, EVs, EV, _). unify_same(E-V, Prev-Var, E-V) :- ( Prev == E -> @@ -221,13 +221,13 @@ nth0_search(N0, N, [_|Es], E) :- list_max([N|Ns], Max) :- - foldl(list_max_, Ns, N, Max). + foldl(lists:list_max_, Ns, N, Max). list_max_(N, Max0, Max) :- Max is max(N, Max0). list_min([N|Ns], Min) :- - foldl(list_min_, Ns, N, Min). + foldl(lists:list_min_, Ns, N, Min). list_min_(N, Min0, Min) :- Min is min(N, Min0). From 24e6c31c4429d52b11b5ed7a4e798c63b27d854e Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Wed, 3 Feb 2021 23:45:06 +0100 Subject: [PATCH 08/13] add meta_predicate declaration for with_local_attributes/3 This is needed for all_distinct/1 etc. Example: ?- all_distinct([X,Y,Z]). clpz:all_distinct([X,Y,Z]) ; false. --- src/lib/clpz.pl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/clpz.pl b/src/lib/clpz.pl index fda2f309..e72a33c1 100644 --- a/src/lib/clpz.pl +++ b/src/lib/clpz.pl @@ -6047,6 +6047,8 @@ remove_attr(Var, Attr) :- functor(Term, Attr, 1), put_atts(Var, -Term). +:- meta_predicate with_local_attributes(?, 0, ?). + with_local_attributes(Vars, Goal, Result) :- catch((Goal, maplist(del_all_attrs, Vars), From 94392f248c9a00556a0fcc6d95e7f7fabe1a0853 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 15:48:09 -0700 Subject: [PATCH 09/13] enable phrase_ rules for module resolved grammars (#785) --- src/lib/dcgs.pl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib/dcgs.pl b/src/lib/dcgs.pl index 05f41cb0..a33c870b 100644 --- a/src/lib/dcgs.pl +++ b/src/lib/dcgs.pl @@ -31,6 +31,8 @@ phrase(GRBody, S0, S) :- phrase_([], S, S). phrase_(!, S, S). +phrase_(_:[], S, S). +phrase_(_:!, S, S). phrase_((A, B), S0, S) :- phrase(A, S0, S1), phrase(B, S1, S). phrase_(M:(A, B), S0, S) :- @@ -57,16 +59,24 @@ phrase_(M:(A | B), S0, S) :- ( phrase(M:A, S0, S) ; phrase(M:B, S0, S) ). phrase_({G}, S0, S) :- ( call(G), S0 = S ). +phrase_(M:{G}, S0, S) :- + ( call(M:G), S0 = S ). phrase_(call(G), S0, S) :- call(G, S0, S). +phrase_(M:call(G), S0, S) :- + call(M:G, S0, S). phrase_((A -> B), S0, S) :- phrase((A -> B ; fail), S0, S). phrase_(M:(A -> B), S0, S) :- phrase((M:A -> M:B ; fail), S0, S). phrase_(phrase(NonTerminal), S0, S) :- phrase(NonTerminal, S0, S). +phrase_(M:phrase(NonTerminal), S0, S) :- + phrase(M:NonTerminal, S0, S). phrase_([T|Ts], S0, S) :- append([T|Ts], S, S0). +phrase_(_:[T|Ts], S0, S) :- + append([T|Ts], S, S0). % The same version of the below two dcg_rule clauses, but with module scoping. From b96ff781bc806ff37b4abe9e3b7d986728473b2a Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Wed, 3 Feb 2021 23:49:28 +0100 Subject: [PATCH 10/13] add meta_predicate declaration for must_succeed/1 --- src/lib/clpz.pl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib/clpz.pl b/src/lib/clpz.pl index e72a33c1..b7fa0ea7 100644 --- a/src/lib/clpz.pl +++ b/src/lib/clpz.pl @@ -5884,10 +5884,12 @@ difference_arcs([V|Vs], FL0) --> writeln(T) :- write(T), nl. +:- meta_predicate must_succeed(0). + must_succeed(G) :- - (G -> true - ;write(failed-G), halt - ). + ( G -> true + ; throw(failed-G) + ). enumerate([], _) --> []. enumerate([N|Ns], V) --> From a8a82e45a01fd49cc6876e8e7caf18dcc6af393f Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 16:03:12 -0700 Subject: [PATCH 11/13] introduce cuts over indexed (:)/2 of dcgs:phrase_/3 (#786) --- src/lib/dcgs.pl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib/dcgs.pl b/src/lib/dcgs.pl index a33c870b..07b82b95 100644 --- a/src/lib/dcgs.pl +++ b/src/lib/dcgs.pl @@ -31,11 +31,12 @@ phrase(GRBody, S0, S) :- phrase_([], S, S). phrase_(!, S, S). -phrase_(_:[], S, S). -phrase_(_:!, S, S). +phrase_(_:[], S, S) :- !. +phrase_(_:!, S, S) :- !. phrase_((A, B), S0, S) :- phrase(A, S0, S1), phrase(B, S1, S). phrase_(M:(A, B), S0, S) :- + !, phrase(M:A, S0, S1), phrase(M:B, S1, S). phrase_((A -> B ; C), S0, S) :- !, @@ -52,26 +53,32 @@ phrase_(M:(A -> B ; C), S0, S) :- phrase_((A ; B), S0, S) :- ( phrase(A, S0, S) ; phrase(B, S0, S) ). phrase_(M:(A ; B), S0, S) :- + !, ( phrase(M:A, S0, S) ; phrase(M:B, S0, S) ). phrase_((A | B), S0, S) :- ( phrase(A, S0, S) ; phrase(B, S0, S) ). phrase_(M:(A | B), S0, S) :- + !, ( phrase(M:A, S0, S) ; phrase(M:B, S0, S) ). phrase_({G}, S0, S) :- ( call(G), S0 = S ). phrase_(M:{G}, S0, S) :- + !, ( call(M:G), S0 = S ). phrase_(call(G), S0, S) :- call(G, S0, S). phrase_(M:call(G), S0, S) :- + !, call(M:G, S0, S). phrase_((A -> B), S0, S) :- phrase((A -> B ; fail), S0, S). phrase_(M:(A -> B), S0, S) :- + !, phrase((M:A -> M:B ; fail), S0, S). phrase_(phrase(NonTerminal), S0, S) :- phrase(NonTerminal, S0, S). phrase_(M:phrase(NonTerminal), S0, S) :- + !, phrase(M:NonTerminal, S0, S). phrase_([T|Ts], S0, S) :- append([T|Ts], S, S0). From 2fe55e47153432563a0664921ecf10a03caebb0c Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 16:38:44 -0700 Subject: [PATCH 12/13] resolve calls to tranpose_ and same_length in lists.pl (#790) --- src/lib/lists.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/lists.pl b/src/lib/lists.pl index 5549a061..0a799672 100644 --- a/src/lib/lists.pl +++ b/src/lib/lists.pl @@ -160,8 +160,8 @@ transpose(Ls, Ts) :- lists_transpose([], []). lists_transpose([L|Ls], Ts) :- - maplist(same_length(L), Ls), - foldl(transpose_, L, Ts, [L|Ls], _). + maplist(lists:same_length(L), Ls), + foldl(lists:transpose_, L, Ts, [L|Ls], _). transpose_(_, Fs, Lists0, Lists) :- maplist(lists:list_first_rest, Lists0, Fs, Lists). From e17eb01f7617b72805f6c6123264485444329e42 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 3 Feb 2021 16:47:04 -0700 Subject: [PATCH 13/13] respect module context in expand_module_names (#788) --- src/loader.pl | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/loader.pl b/src/loader.pl index b16150c6..06fe2ef2 100644 --- a/src/loader.pl +++ b/src/loader.pl @@ -423,7 +423,7 @@ expand_module_names(Goals, MetaSpecs, Module, ExpandedGoals, HeadVars) :- Goals =.. [GoalFunctor | SubGoals], ( GoalFunctor == (:), SubGoals = [M, SubGoal] -> - expand_module_names(SubGoal, MetaSpecs, Module, ExpandedSubGoal, HeadVars), + expand_module_names(SubGoal, MetaSpecs, M, ExpandedSubGoal, HeadVars), ExpandedGoals = M:ExpandedSubGoal ; expand_meta_predicate_subgoals(SubGoals, MetaSpecs, Module, ExpandedGoalList, HeadVars), ExpandedGoals =.. [GoalFunctor | ExpandedGoalList] @@ -434,6 +434,28 @@ expand_goal(UnexpandedGoals, Module, ExpandedGoals) :- expand_goal(UnexpandedGoals, Module, ExpandedGoals, []), !. +expand_goal_cases((Goal0, Goals0), Module, ExpandedGoals, HeadVars) :- + ( expand_goal(Goal0, Module, Goal1, HeadVars) -> + expand_goal(Goals0, Module, Goals1, HeadVars), + thread_goals(Goal1, ExpandedGoals, Goals1, (',')) + ; expand_goal(Goals0, Module, Goals1, HeadVars), + ExpandedGoals = (Goal0, Goals1) + ). +expand_goal_cases((Goals0 -> Goals1), Module, ExpandedGoals, HeadVars) :- + expand_goal(Goals0, Module, ExpandedGoals0, HeadVars), + expand_goal(Goals1, Module, ExpandedGoals1, HeadVars), + ExpandedGoals = (ExpandedGoals0 -> ExpandedGoals1). +expand_goal_cases((Goals0 ; Goals1), Module, ExpandedGoals, HeadVars) :- + expand_goal(Goals0, Module, ExpandedGoals0, HeadVars), + expand_goal(Goals1, Module, ExpandedGoals1, HeadVars), + ExpandedGoals = (ExpandedGoals0 ; ExpandedGoals1). +expand_goal_cases((\+ Goals0), Module, ExpandedGoals, HeadVars) :- + expand_goal(Goals0, Module, Goals1, HeadVars), + ExpandedGoals = (\+ Goals1). +expand_goal_cases((Module:Goals0), _, ExpandedGoals, HeadVars) :- + expand_goal(Goals0, Module, Goals1, HeadVars), + ExpandedGoals = (Module:Goals1). + expand_goal(UnexpandedGoals, Module, ExpandedGoals, HeadVars) :- ( var(UnexpandedGoals) -> UnexpandedGoals = ExpandedGoals @@ -442,24 +464,8 @@ expand_goal(UnexpandedGoals, Module, ExpandedGoals, HeadVars) :- goal_expansion(UnexpandedGoals1, user, Goals) ; Goals = UnexpandedGoals1 ), - ( Goals = (Goal0, Goals0) -> - ( expand_goal(Goal0, Module, Goal1, HeadVars) -> - expand_goal(Goals0, Module, Goals1, HeadVars), - thread_goals(Goal1, ExpandedGoals, Goals1, (',')) - ; expand_goal(Goals0, Module, Goals1, HeadVars), - ExpandedGoals = (Goal0, Goals1) - ) - ; Goals = (Goals0 -> Goals1) -> - expand_goal(Goals0, Module, ExpandedGoals0, HeadVars), - expand_goal(Goals1, Module, ExpandedGoals1, HeadVars), - ExpandedGoals = (ExpandedGoals0 -> ExpandedGoals1) - ; Goals = (Goals0 ; Goals1) -> - expand_goal(Goals0, Module, ExpandedGoals0, HeadVars), - expand_goal(Goals1, Module, ExpandedGoals1, HeadVars), - ExpandedGoals = (ExpandedGoals0 ; ExpandedGoals1) - ; Goals = (\+ Goals0) -> - expand_goal(Goals0, Module, Goals1, HeadVars), - ExpandedGoals = (\+ Goals1) + ( expand_goal_cases(Goals, Module, ExpandedGoals, HeadVars) -> + true ; predicate_property(Module:Goals, meta_predicate(MetaSpecs)) -> expand_module_names(Goals, MetaSpecs, Module, ExpandedGoals, HeadVars) ; thread_goals(Goals, ExpandedGoals, (','))