DeterministicLengthRundown,
#[strum_discriminants(strum(props(Arity = "7", Name = "$http_open")))]
HttpOpen,
+ #[strum_discriminants(strum(props(Arity = "3", Name = "$predicate_defined")))]
+ PredicateDefined,
REPL(REPLCodePtr),
}
&Instruction::CallCpuNow(_) |
&Instruction::CallDeterministicLengthRundown(_) |
&Instruction::CallHttpOpen(_) |
+ &Instruction::CallPredicateDefined(_) |
&Instruction::CallCurrentTime(_) |
&Instruction::CallQuotedToken(_) |
&Instruction::CallReadTermFromChars(_) |
&Instruction::ExecuteCpuNow(_) |
&Instruction::ExecuteDeterministicLengthRundown(_) |
&Instruction::ExecuteHttpOpen(_) |
+ &Instruction::ExecutePredicateDefined(_) |
&Instruction::ExecuteCurrentTime(_) |
&Instruction::ExecuteQuotedToken(_) |
&Instruction::ExecuteReadTermFromChars(_) |
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) ->
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))) ->
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;
+ }
}
}
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(