assertz/1, asserta/1, retract/1, retractall/1 are meta-predicates (#902, #903)

This commit is contained in:
Mark Thom
2021-04-14 15:10:45 -06:00
parent 5452b55e38
commit b08442b46f
3 changed files with 34 additions and 11 deletions

View File

@@ -37,7 +37,7 @@ impl OptArgIndexKey {
match (self, key_type) {
(OptArgIndexKey::Constant(..), OptArgIndexKeyType::Constant)
| (OptArgIndexKey::Structure(..), OptArgIndexKeyType::Structure)
// | (OptArgIndexKey::List(..), OptArgIndexKeyType::List)
// | (OptArgIndexKey::List(..), OptArgIndexKeyType::List)
=> true,
_ => false,
}
@@ -116,7 +116,12 @@ impl<'a> IndexingCodeMergingPtr<'a> {
constants.insert(constant.clone(), constant_ptr);
}
_ => {
unreachable!()
if let IndexingCodePtr::DynamicExternal(_) = constant_ptr {
// this must be a defunct clause, because it's been deleted
// from the skeleton.
} else {
unreachable!()
}
}
}
@@ -378,7 +383,12 @@ impl<'a> IndexingCodeMergingPtr<'a> {
structures.insert((name.clone(), *arity), structure_ptr);
}
_ => {
unreachable!()
if let IndexingCodePtr::DynamicExternal(_) = structure_ptr {
// this must be a defunct clause, because it's been deleted
// from the skeleton.
} else {
unreachable!()
}
}
}

View File

@@ -818,6 +818,8 @@ module_asserta_clause(Head, Body, Module) :-
; callable(Head), functor(Head, Name, Arity) ->
( '$head_is_dynamic'(Module, Head) ->
call_asserta(Head, Body, Name, Arity, Module)
; '$no_such_predicate'(Module, Head) ->
call_asserta(Head, Body, Name, Arity, Module)
; throw(error(permission_error(modify, static_procedure, Name/Arity), asserta/1))
)
; throw(error(type_error(callable, Head), asserta/1))
@@ -840,6 +842,8 @@ asserta_clause(Head, Body) :-
; throw(error(type_error(callable, Head), asserta/1))
).
:- meta_predicate asserta(0).
asserta(Clause) :-
( Clause \= (_ :- _) ->
Head = Clause,
@@ -888,6 +892,8 @@ assertz_clause(Head, Body) :-
; throw(error(type_error(callable, Head), assertz/1))
).
:- meta_predicate assertz(0).
assertz(Clause) :-
( Clause \= (_ :- _) ->
Head = Clause,
@@ -973,6 +979,8 @@ retract_clause(Head, Body) :-
; throw(error(type_error(callable, Head), retract/1))
).
:- meta_predicate retract(0).
retract(Clause) :-
( Clause \= (_ :- _) ->
Head = Clause,
@@ -983,6 +991,14 @@ retract(Clause) :-
).
:- meta_predicate retractall(0).
retractall(Head) :-
retract((Head :- _)),
false.
retractall(_).
module_abolish(Pred, Module) :-
( var(Pred) ->
throw(error(instantiation_error), abolish/1)
@@ -996,7 +1012,7 @@ module_abolish(Pred, Module) :-
throw(error(domain_error(not_less_than_zero, Arity), abolish/1))
; max_arity(N), Arity > N ->
throw(error(representation_error(max_arity), abolish/1))
; callable(Head), functor(Head, Name, Arity) ->
; functor(Head, Name, Arity) ->
( '$head_is_dynamic'(Module, Head) ->
'$abolish_clause'(Module, Name, Arity)
; throw(error(permission_error(modify, static_procedure, Pred), abolish/1))
@@ -1007,6 +1023,9 @@ module_abolish(Pred, Module) :-
; throw(error(type_error(predicate_indicator, Module:Pred), abolish/1))
).
:- meta_predicate abolish(0).
abolish(Pred) :-
( var(Pred) ->
throw(error(instantiation_error), abolish/1)
@@ -1024,7 +1043,7 @@ abolish(Pred) :-
throw(error(domain_error(not_less_than_zero, Arity), abolish/1))
; max_arity(N), Arity > N ->
throw(error(representation_error(max_arity), abolish/1))
; callable(Head), functor(Head, Name, Arity) ->
; functor(Head, Name, Arity) ->
( '$head_is_dynamic'(user, Head) ->
'$abolish_clause'(user, Name, Arity)
; '$no_such_predicate'(user, Head) ->
@@ -1585,8 +1604,3 @@ callable(X) :-
true
; false
).
retractall(Head) :-
retract((Head :- _)),
false.
retractall(_).

View File

@@ -365,7 +365,6 @@ compile_declaration(discontiguous(Name/Arity), Evacuable) :-
'$add_discontiguous_predicate'(Module, Name, Arity, Evacuable).
compile_declaration(initialization(Goal), Evacuable) :-
prolog_load_context(module, Module),
'$add_dynamic_predicate'(Module, '$initialization_goals', 1, Evacuable),
assertz(Module:'$initialization_goals'(Goal)).
compile_declaration(set_prolog_flag(Flag, Value), _) :-
set_prolog_flag(Flag, Value).