add '$delete_all_attributes', use copy_term/3 as defined in #1272

This commit is contained in:
Mark Thom
2023-02-18 02:15:24 -07:00
parent a6e416f13d
commit 3f445c76be
6 changed files with 74 additions and 25 deletions

View File

@@ -566,6 +566,8 @@ enum SystemClauseType {
PutToAttributedVarList, PutToAttributedVarList,
#[strum_discriminants(strum(props(Arity = "3", Name = "$del_from_attr_list")))] #[strum_discriminants(strum(props(Arity = "3", Name = "$del_from_attr_list")))]
DeleteFromAttributedVarList, DeleteFromAttributedVarList,
#[strum_discriminants(strum(props(Arity = "1", Name = "$delete_all_attributes")))]
DeleteAllAttributes,
REPL(REPLCodePtr), REPL(REPLCodePtr),
} }
@@ -1627,6 +1629,7 @@ fn generate_instruction_preface() -> TokenStream {
&Instruction::CallGetFromAttributedVarList(_) | &Instruction::CallGetFromAttributedVarList(_) |
&Instruction::CallPutToAttributedVarList(_) | &Instruction::CallPutToAttributedVarList(_) |
&Instruction::CallDeleteFromAttributedVarList(_) | &Instruction::CallDeleteFromAttributedVarList(_) |
&Instruction::CallDeleteAllAttributes(_) |
&Instruction::CallFetchGlobalVar(_) | &Instruction::CallFetchGlobalVar(_) |
&Instruction::CallFirstStream(_) | &Instruction::CallFirstStream(_) |
&Instruction::CallFlushOutput(_) | &Instruction::CallFlushOutput(_) |
@@ -1842,6 +1845,7 @@ fn generate_instruction_preface() -> TokenStream {
&Instruction::ExecuteGetFromAttributedVarList(_) | &Instruction::ExecuteGetFromAttributedVarList(_) |
&Instruction::ExecutePutToAttributedVarList(_) | &Instruction::ExecutePutToAttributedVarList(_) |
&Instruction::ExecuteDeleteFromAttributedVarList(_) | &Instruction::ExecuteDeleteFromAttributedVarList(_) |
&Instruction::ExecuteDeleteAllAttributes(_) |
&Instruction::ExecuteFetchGlobalVar(_) | &Instruction::ExecuteFetchGlobalVar(_) |
&Instruction::ExecuteFirstStream(_) | &Instruction::ExecuteFirstStream(_) |
&Instruction::ExecuteFlushOutput(_) | &Instruction::ExecuteFlushOutput(_) |

View File

@@ -38,5 +38,5 @@ freeze(X, Goal) :-
attribute_goals(Var) --> attribute_goals(Var) -->
{ get_atts(Var, frozen(Goals)), { get_atts(Var, frozen(Goals)),
put_atts(Var, -frozen(_)) }, put_atts(Var, -frozen(_)) },
[freeze(Var, Goals)]. [freeze:freeze(Var, Goals)].

View File

@@ -11,7 +11,6 @@
current_module/1 current_module/1
]). ]).
:- use_module(library(error)). :- use_module(library(error)).
:- use_module(library(lists)). :- use_module(library(lists)).
:- use_module(library(pairs)). :- use_module(library(pairs)).
@@ -221,7 +220,12 @@ complete_partial_goal(N, HeadArg, InnerHeadArgs, SuppArgs, CompleteHeadArg) :-
integer(N), integer(N),
N >= 0, N >= 0,
HeadArg =.. [Functor | InnerHeadArgs], HeadArg =.. [Functor | InnerHeadArgs],
length(SuppArgs, N), % the next two lines are equivalent to length(SuppArgs, N) but
% avoid length/2 so that copy_term/3 (which is invoked by
% length/2) can be bootstrapped without self-reference.
functor(SuppArgsFunctor, '.', N),
SuppArgsFunctor =.. [_ | SuppArgs],
% length(SuppArgs, N),
append(InnerHeadArgs, SuppArgs, InnerHeadArgs0), append(InnerHeadArgs, SuppArgs, InnerHeadArgs0),
CompleteHeadArg =.. [Functor | InnerHeadArgs0]. CompleteHeadArg =.. [Functor | InnerHeadArgs0].

View File

@@ -5207,6 +5207,14 @@ impl Machine {
self.delete_from_attributed_variable_list(); self.delete_from_attributed_variable_list();
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallDeleteAllAttributes(_) => {
self.delete_all_attributes();
self.machine_st.p += 1;
}
&Instruction::ExecuteDeleteAllAttributes(_) => {
self.delete_all_attributes();
self.machine_st.p = self.machine_st.cp;
}
} }
} }

View File

@@ -1,7 +1,11 @@
:- module('$project_atts', [copy_term/3]). :- module('$project_atts', [copy_term/3]).
:- use_module(library(dcgs)).
:- use_module(library(lambda)).
:- use_module(library(lists), [foldl/4]).
project_attributes(QueryVars, AttrVars) :- project_attributes(QueryVars, AttrVars) :-
gather_attr_modules(AttrVars, Modules0), phrase(gather_attr_modules(AttrVars), Modules0),
sort(Modules0, Modules), sort(Modules0, Modules),
call_project_attributes(Modules, QueryVars, AttrVars). call_project_attributes(Modules, QueryVars, AttrVars).
@@ -17,9 +21,9 @@ project_attributes(QueryVars, AttrVars) :-
call_project_attributes([], _, _). call_project_attributes([], _, _).
call_project_attributes([Module|Modules], QueryVars, AttrVars) :- call_project_attributes([Module|Modules], QueryVars, AttrVars) :-
( catch(Module:project_attributes(QueryVars, AttrVars), ( catch(Module:project_attributes(QueryVars, AttrVars),
E, E,
'$project_atts':'$print_project_attributes_exception'(Module, E) '$project_atts':'$print_project_attributes_exception'(Module, E)
) )
-> true -> true
; true ; true
), ),
@@ -72,25 +76,33 @@ call_attribute_goals_with_module_prefix([Module | Modules], GoalCaller, AttrVars
module_prefixed_goals(Goals0, Module, Goals, Gs), module_prefixed_goals(Goals0, Module, Goals, Gs),
call_attribute_goals_with_module_prefix(Modules, GoalCaller, AttrVars, Gs). call_attribute_goals_with_module_prefix(Modules, GoalCaller, AttrVars, Gs).
gather_attr_modules([]) --> [].
gather_attr_modules([AttrVar|AttrVars]) -->
{ '$get_attr_list'(AttrVar, Attrs) },
copy_attribute_modules(Attrs),
gather_attr_modules(AttrVars).
gather_attr_modules([], []). copy_attribute_modules(Attrs) -->
gather_attr_modules([AttrVar|AttrVars], Modules) :- { var(Attrs) },
'$get_attr_list'(AttrVar, Attrs), !.
copy_attribute_modules(Attrs, Modules, Modules0), copy_attribute_modules([Module:_|Attrs]) -->
gather_attr_modules(AttrVars, Modules0). [Module],
copy_attribute_modules(Attrs).
copy_attribute_modules(Attrs, Ls, Ls) :- gather_residual_goals([]) --> [].
var(Attrs), !. gather_residual_goals([V|Vs]) -->
copy_attribute_modules([Module:_|Attrs], [Module|Modules0], Modules1) :- { '$get_attr_list'(V, Attrs),
copy_attribute_modules(Attrs, Modules0, Modules1). phrase(copy_attribute_modules(Attrs), Modules0),
sort(Modules0, Modules) },
foldl(V+\M^phrase(M:attribute_goals(V)), Modules),
gather_residual_goals(Vs).
delete_all_attributes(Term) :- '$delete_all_attributes'(Term).
copy_term(Source, Dest, Goals) :- copy_term(Term, Copy, Gs) :-
'$term_attributed_variables'(Source, AttrVars), '$term_attributed_variables'(Term, Vs),
gather_attr_modules(AttrVars, Modules0), findall(Term-Gs,
sort(Modules0, Modules), ( phrase(gather_residual_goals(Vs), Gs),
call_attribute_goals_with_module_prefix(Modules, '$project_atts':call_query_var_goals, delete_all_attributes(Term)
AttrVars, Goals0), ),
sort(Goals0, Goals1), [Copy-Gs]).
!,
'$copy_term_without_attr_vars'([Source | Goals1], [Dest | Goals]).

View File

@@ -1036,6 +1036,27 @@ impl MachineState {
} }
impl Machine { impl Machine {
#[inline(always)]
pub(crate) fn delete_all_attributes(&mut self) {
let h = self.machine_st.heap.len();
self.machine_st.heap.push(heap_loc_as_cell!(h));
self.machine_st.registers[2] = heap_loc_as_cell!(h);
self.term_attributed_variables();
let mut list_of_attr_vars = self.deref_register(2);
while let HeapCellValueTag::Lis = list_of_attr_vars.get_tag() {
let attr_var_loc = list_of_attr_vars.get_value();
self.machine_st.heap[attr_var_loc] = heap_loc_as_cell!(attr_var_loc);
self.machine_st.trail(TrailRef::Ref(Ref::attr_var(attr_var_loc)));
list_of_attr_vars = self.machine_st.heap[attr_var_loc + 1];
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn get_clause_p(&self, module_name: Atom) -> (usize, usize) { pub(crate) fn get_clause_p(&self, module_name: Atom) -> (usize, usize) {
use crate::machine::loader::CompilationTarget; use crate::machine::loader::CompilationTarget;