simplify detecting undefined goal_ and term_expansion

This commit is contained in:
Mark Thom
2022-06-07 21:45:26 -06:00
parent 3d6fbabc86
commit a68394b6f2
4 changed files with 50 additions and 14 deletions

View File

@@ -542,6 +542,8 @@ enum SystemClauseType {
DeterministicLengthRundown,
#[strum_discriminants(strum(props(Arity = "7", Name = "$http_open")))]
HttpOpen,
#[strum_discriminants(strum(props(Arity = "3", Name = "$predicate_defined")))]
PredicateDefined,
REPL(REPLCodePtr),
}
@@ -1665,6 +1667,7 @@ fn generate_instruction_preface() -> TokenStream {
&Instruction::CallCpuNow(_) |
&Instruction::CallDeterministicLengthRundown(_) |
&Instruction::CallHttpOpen(_) |
&Instruction::CallPredicateDefined(_) |
&Instruction::CallCurrentTime(_) |
&Instruction::CallQuotedToken(_) |
&Instruction::CallReadTermFromChars(_) |
@@ -1869,6 +1872,7 @@ fn generate_instruction_preface() -> TokenStream {
&Instruction::ExecuteCpuNow(_) |
&Instruction::ExecuteDeterministicLengthRundown(_) |
&Instruction::ExecuteHttpOpen(_) |
&Instruction::ExecutePredicateDefined(_) |
&Instruction::ExecuteCurrentTime(_) |
&Instruction::ExecuteQuotedToken(_) |
&Instruction::ExecuteReadTermFromChars(_) |

View File

@@ -34,23 +34,13 @@ write_error(Error) :-
write('.').
'$print_message_and_fail'(Error) :-
( ( Error = error(existence_error(procedure, Expansion), Expansion)
; Error = error(evaluation_error((_:_)/_),Expansion)
) ->
( ( Expansion = goal_expansion/2
; Expansion = term_expansion/2
) ->
true
; write_error(Error),
nl
)
; write_error(Error),
nl
),
write_error(Error),
nl,
'$fail'.
expand_term(Term, ExpandedTerm) :-
( catch('$call'(user:term_expansion(Term, ExpandedTerm0)),
( '$predicate_defined'(user, term_expansion, 2),
catch('$call'(user:term_expansion(Term, ExpandedTerm0)),
E,
'$call'(loader:'$print_message_and_fail'(E))) ->
( var(ExpandedTerm0) ->
@@ -77,6 +67,7 @@ term_expansion_list([Term|Terms], ExpandedTermsHead, ExpandedTermsTail) :-
goal_expansion(Goal, Module, ExpandedGoal) :-
( atom(Module),
'$predicate_defined'(Module, goal_expansion, 2),
catch('$call'(Module:goal_expansion(Goal, ExpandedGoal0)),
E,
'$call'(loader:'$print_message_and_fail'(E))) ->

View File

@@ -4851,6 +4851,14 @@ impl Machine {
try_or_throw!(self.machine_st, self.add_non_counted_backtracking());
self.machine_st.p = self.machine_st.cp;
}
&Instruction::CallPredicateDefined(_) => {
self.machine_st.fail = !self.predicate_defined();
self.machine_st.p += 1;
}
&Instruction::ExecutePredicateDefined(_) => {
self.machine_st.fail = !self.predicate_defined();
self.machine_st.p = self.machine_st.cp;
}
}
}

View File

@@ -3771,6 +3771,39 @@ impl Machine {
self.machine_st.fail = !self.indices.modules.contains_key(&module_name);
}
pub(crate) fn predicate_defined(&self) -> bool {
let module_name = cell_as_atom!(self.machine_st.store(self.machine_st.deref(
self.machine_st.registers[1]
)));
let name = cell_as_atom!(self.machine_st.store(self.machine_st.deref(
self.machine_st.registers[2]
)));
let arity = match Number::try_from(self.machine_st.store(self.machine_st.deref(
self.machine_st.registers[3]
))) {
Ok(Number::Fixnum(n)) => n.get_num() as usize,
Ok(Number::Integer(n)) => {
if let Some(n) = n.to_usize() {
n
} else {
return false;
}
}
_ => {
unreachable!()
}
};
self.indices.get_predicate_code_index(
name,
arity,
module_name,
).map(|index| index.local().is_some())
.unwrap_or(false)
}
#[inline(always)]
pub(crate) fn no_such_predicate(&mut self) -> CallResult {
let module_name = cell_as_atom!(self.machine_st.store(self.machine_st.deref(