From 8900df6f1330f82909f6a56ba13b69efcccdd99c Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Tue, 2 Feb 2021 15:03:14 -0700 Subject: [PATCH] add built_in to predicate_property --- src/clause_types.rs | 3 +++ src/lib/dcgs.pl | 7 +++++-- src/loader.pl | 16 ++++++++------- src/machine/loader.rs | 37 ++++++++++++++++++++++++++++++++++ src/machine/machine_indices.rs | 1 + src/machine/mod.rs | 3 +++ src/write.rs | 2 ++ 7 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/clause_types.rs b/src/clause_types.rs index aded0bb2..4c39d5d6 100644 --- a/src/clause_types.rs +++ b/src/clause_types.rs @@ -398,6 +398,8 @@ impl SystemClauseType { clause_name!("$prolog_lc_stream"), &SystemClauseType::REPL(REPLCodePtr::MetaPredicateProperty) => clause_name!("$cpp_meta_predicate_property"), + &SystemClauseType::REPL(REPLCodePtr::BuiltInProperty) => + clause_name!("$cpp_built_in_property"), &SystemClauseType::REPL(REPLCodePtr::CompilePendingPredicates) => clause_name!("$compile_pending_predicates"), &SystemClauseType::Close => clause_name!("$close"), @@ -770,6 +772,7 @@ impl SystemClauseType { ("$prolog_lc_module", 1) => Some(SystemClauseType::REPL(REPLCodePtr::LoadContextModule)), ("$prolog_lc_stream", 1) => Some(SystemClauseType::REPL(REPLCodePtr::LoadContextStream)), ("$cpp_meta_predicate_property", 4) => Some(SystemClauseType::REPL(REPLCodePtr::MetaPredicateProperty)), + ("$cpp_built_in_property", 2) => Some(SystemClauseType::REPL(REPLCodePtr::BuiltInProperty)), ("$compile_pending_predicates", 1) => Some(SystemClauseType::REPL(REPLCodePtr::CompilePendingPredicates)), _ => None, } diff --git a/src/lib/dcgs.pl b/src/lib/dcgs.pl index b4e2fafc..6918342e 100644 --- a/src/lib/dcgs.pl +++ b/src/lib/dcgs.pl @@ -16,11 +16,13 @@ phrase(GRBody, S0) :- phrase(GRBody, S0, S) :- - ( var(GRBody) -> throw(error(instantiation_error, phrase/3)) + ( var(GRBody) -> + throw(error(instantiation_error, phrase/3)) ; strip_module(GRBody, _, GRBody0), dcg_constr(GRBody0) -> phrase_(GRBody0, S0, S) - ; functor(GRBody, _, _) -> call(GRBody, S0, S) + ; functor(GRBody, _, _) -> + call(GRBody, S0, S) ; throw(error(type_error(callable, GRBody), phrase/3)) ). @@ -49,6 +51,7 @@ phrase_(phrase(NonTerminal), S0, S) :- phrase_([T|Ts], S0, S) :- append([T|Ts], S, S0). + % The same version of the below two dcg_rule clauses, but with module scoping. dcg_rule(( M:NonTerminal, Terminals --> GRBody ), ( M:Head :- Body )) :- dcg_non_terminal(NonTerminal, S0, S, Head), diff --git a/src/loader.pl b/src/loader.pl index d0b24f37..972fe367 100644 --- a/src/loader.pl +++ b/src/loader.pl @@ -112,7 +112,7 @@ load_loop(Stream, Evacuable) :- ). -inner_meta_specs((:), HeadArg, InnerHeadArgs, InnerMetaSpecs) :- +inner_meta_specs(0, HeadArg, InnerHeadArgs, InnerMetaSpecs) :- !, predicate_property(HeadArg, meta_predicate(InnerMetaSpecs)), HeadArg =.. [_ | InnerHeadArgs]. @@ -334,9 +334,9 @@ use_module(Module, Exports, Evacuable) :- check_predicate_property(meta_predicate, Module, Name, Arity, MetaPredicateTerm) :- - must_be(atom, Name), - must_be(integer, Arity), '$cpp_meta_predicate_property'(Module, Name, Arity, MetaPredicateTerm). +check_predicate_property(built_in, _, Name, Arity, built_in) :- + '$cpp_built_in_property'(Name, Arity). extract_predicate_property(Property, PropertyType) :- @@ -355,7 +355,10 @@ predicate_property(Callable, Property) :- check_predicate_property(PropertyType, Module, Name, Arity, Property) ; functor(Callable, Name, Arity), extract_predicate_property(Property, PropertyType), - prolog_load_context(module, Module), + ( prolog_load_context(module, Module) -> + true + ; Module = user + ), check_predicate_property(PropertyType, Module, Name, Arity, Property) ). @@ -392,15 +395,14 @@ expand_subgoal(UnexpandedGoals, MS, Module, ExpandedGoals, HeadVars) :- expand_module_name(ESG0, M, ESG) :- ( var(ESG0) -> ESG = M:ESG0 - ; ESG0 = _:ESG1 -> + ; ESG0 = _:_ -> ESG = ESG0 ; ESG = M:ESG0 ). expand_meta_predicate_subgoals([SG | SGs], [MS | MSs], M, [ESG | ESGs], HeadVars) :- - ( ( MS == (:) - ; integer(MS), + ( ( integer(MS), MS >= 0 ) -> ( var(SG), diff --git a/src/machine/loader.rs b/src/machine/loader.rs index 4560b682..286fa167 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -1530,6 +1530,43 @@ impl Machine { } } + pub(crate) + fn builtin_property(&mut self) { + let key = + self.machine_st.read_predicate_key( + self.machine_st[temp_v!(1)], + self.machine_st[temp_v!(2)], + ); + + match ClauseType::from(key.0, key.1, None) { + ClauseType::BuiltIn(_) | ClauseType::Inlined(..) | ClauseType::CallN => { + return; + } + ClauseType::Named(ref name, arity, _) => { + if let Some(module) = self.indices.modules.get(&(clause_name!("builtins"))) { + self.machine_st.fail = !module.code_dir.contains_key( + &(name.clone(), arity), + ); + + return; + } + } + ClauseType::Op(ref name, ref op_desc, _) => { + if let Some(module) = self.indices.modules.get(&(clause_name!("builtins"))) { + self.machine_st.fail = !module.code_dir.contains_key( + &(name.clone(), op_desc.arity()), + ); + + return; + } + } + _ => { + } + } + + self.machine_st.fail = true; + } + pub(crate) fn compile_pending_predicates(&mut self) { let (mut loader, evacuable_h) = self.loader_from_heap_evacuable(temp_v!(1)); diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index 21effa5d..869d4c92 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -505,6 +505,7 @@ pub enum REPLCodePtr { AddDynamicPredicate, AddGoalExpansionClause, AddTermExpansionClause, + BuiltInProperty, ClauseToEvacuable, ConcludeLoad, DeclareModule, diff --git a/src/machine/mod.rs b/src/machine/mod.rs index a2486380..51af9c13 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -463,6 +463,9 @@ impl Machine { REPLCodePtr::MetaPredicateProperty => { self.meta_predicate_property(); } + REPLCodePtr::BuiltInProperty => { + self.builtin_property(); + } REPLCodePtr::CompilePendingPredicates => { self.compile_pending_predicates(); } diff --git a/src/write.rs b/src/write.rs index b0dd010a..bbb28e30 100644 --- a/src/write.rs +++ b/src/write.rs @@ -26,6 +26,8 @@ impl fmt::Display for REPLCodePtr { write!(f, "REPLCodePtr::AddGoalExpansionClause"), REPLCodePtr::AddTermExpansionClause => write!(f, "REPLCodePtr::AddTermExpansionClause"), + REPLCodePtr::BuiltInProperty => + write!(f, "REPLCodePtr::BuiltInProperty"), REPLCodePtr::UserAssertz => write!(f, "REPLCodePtr::UserAssertz"), REPLCodePtr::UserAsserta => -- 2.54.0