From 605c2337530fdd262add7f1ab894b1798e4a922f Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Mon, 4 May 2020 00:54:43 +0200 Subject: [PATCH 1/4] do not project queue/2 attributes to residual goals --- src/prolog/lib/clpz.pl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/prolog/lib/clpz.pl b/src/prolog/lib/clpz.pl index 54c4b457..61d3e4b6 100644 --- a/src/prolog/lib/clpz.pl +++ b/src/prolog/lib/clpz.pl @@ -7424,6 +7424,10 @@ intervals_to_drep([A0-B0|Rest], Drep0, Drep) :- ), intervals_to_drep(Rest, Drep0 \/ D1, Drep). +attribute_goals(X) --> + { get_atts(X, queue(_,_)) }, + !, + { put_atts(X, -queue(_,_)) }. attribute_goals(X) --> % { get_attr(X, clpz, Attr), format("A: ~w\n", [Attr]) }, { get_attr(X, clpz, clpz_attr(_,_,_,Dom,fd_props(Gs,Bs,Os),_)), From e2a3db1b825b544d4f3308e759dd53bde31c2210 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 3 May 2020 19:34:18 -0600 Subject: [PATCH 2/4] use copy_term/3 as source of attribute goals --- src/prolog/machine/mod.rs | 9 --------- src/prolog/toplevel.pl | 32 ++++++++------------------------ 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/src/prolog/machine/mod.rs b/src/prolog/machine/mod.rs index da14cc80..03deda01 100644 --- a/src/prolog/machine/mod.rs +++ b/src/prolog/machine/mod.rs @@ -445,15 +445,6 @@ impl Machine { ) ); - compile_user_module(&mut wam, - Stream::from(ORDSETS), - true, - ListingSource::from_file_and_path( - clause_name!("si"), - lib_path.clone(), - ) - ); - if wam.compile_top_level().is_err() { panic!("Loading '$toplevel' module failed"); } diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index 461e7195..a939a40d 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -3,7 +3,6 @@ :- use_module(library(charsio)). :- use_module(library(lists)). -:- use_module(library(ordsets)). :- use_module(library(si)). :- dynamic(argv/1). @@ -231,10 +230,12 @@ trailing_period_is_ambiguous(Value) :- write_eqs_and_read_input(B, VarList) :- term_variables(VarList, Vars0), '$term_attributed_variables'(VarList, AttrVars), - append(Vars0, AttrVars, Vars), + copy_term(AttrVars, AttrVars, AttrGoals), + term_variables(AttrGoals, AttrGoalVars), + append([Vars0, AttrVars, AttrGoalVars], Vars), charsio:extend_var_list(Vars, VarList, NewVarList, fabricated), '$get_b_value'(B0), - gather_goals(NewVarList, NewVarList, VarList, Goals), + gather_equations(NewVarList, Goals, AttrGoals), ( bb_get('$first_answer', true) -> write(' '), bb_put('$first_answer', false) @@ -296,31 +297,14 @@ is_a_different_variable([_ = Binding | Pairs], Value) :- ; is_a_different_variable(Pairs, Value) ). -filter_goals([Goal|Goals], FGoals, QueryVars) :- - term_variables(Goal, GoalVars0), - sort(GoalVars0, GoalVars), - ( ord_intersect(GoalVars, QueryVars) -> - append(GoalVars, QueryVars, QueryVars0), - sort(QueryVars0, QueryVars1), - FGoals = [Goal | FGoals0], - filter_goals(Goals, FGoals0, QueryVars1) - ; - filter_goals(Goals, FGoals, QueryVars) - ). -filter_goals([], [], _). - -gather_goals([], VarList, QueryVarList, Goals) :- - gather_query_vars(VarList, Vars), - term_variables(QueryVarList, QueryVars), - copy_term(Vars, Vars, Goals0), - filter_goals(Goals0, Goals, QueryVars). -gather_goals([Var = Value | Pairs], VarList, QueryVarList, Goals) :- +gather_equations([], Goals, Goals). +gather_equations([Var = Value | Pairs], Goals, Goals1) :- ( ( nonvar(Value) ; is_a_different_variable(Pairs, Value) ) -> Goals = [Var = Value | Goals0], - gather_goals(Pairs, VarList, QueryVarList, Goals0) - ; gather_goals(Pairs, VarList, QueryVarList, Goals) + gather_equations(Pairs, Goals0, Goals1) + ; gather_equations(Pairs, Goals, Goals1) ). print_exception(E) :- From e5903d8d6eee8da6924334f1d6aad2a8f80d1097 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 3 May 2020 20:31:12 -0600 Subject: [PATCH 3/4] use select/3 in gather_equations for detecting whether variables are distinct. --- src/prolog/machine/compile.rs | 17 +++++++++-------- src/prolog/toplevel.pl | 15 ++++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/prolog/machine/compile.rs b/src/prolog/machine/compile.rs index 10bc4d22..64f56a30 100644 --- a/src/prolog/machine/compile.rs +++ b/src/prolog/machine/compile.rs @@ -763,14 +763,14 @@ impl ListingCompiler { } fn generate_init_goal_code( - &mut self, + &mut self, ) -> Result { - let query_terms = mem::replace(&mut self.initialization_goals.0, vec![]); - let queue = mem::replace(&mut self.initialization_goals.1, VecDeque::new()); + let query_terms = mem::replace(&mut self.initialization_goals.0, vec![]); + let queue = mem::replace(&mut self.initialization_goals.1, VecDeque::new()); - compile_query(query_terms, queue) - .map(|(code, _)| code) - .map_err(SessionError::from) + compile_query(query_terms, queue) + .map(|(code, _)| code) + .map_err(SessionError::from) } fn set_code_index( @@ -1143,7 +1143,8 @@ impl ListingCompiler { result } - pub(crate) fn gather_items( + pub(crate) + fn gather_items( &mut self, wam: &mut Machine, src: &mut ParsingStream, @@ -1339,7 +1340,7 @@ fn compile_work_impl( let init_goal_code = compiler.generate_init_goal_code()?; if init_goal_code.len() > 0 { - if !wam.run_init_code(init_goal_code) { + if !wam.run_init_code(init_goal_code) { println!("Warning: initialization goal for {} failed", compiler.listing_src.name()); } diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index a939a40d..ed17f80a 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -235,7 +235,7 @@ write_eqs_and_read_input(B, VarList) :- append([Vars0, AttrVars, AttrGoalVars], Vars), charsio:extend_var_list(Vars, VarList, NewVarList, fabricated), '$get_b_value'(B0), - gather_equations(NewVarList, Goals, AttrGoals), + gather_equations(NewVarList, NewVarList, Goals, AttrGoals), ( bb_get('$first_answer', true) -> write(' '), bb_put('$first_answer', false) @@ -294,17 +294,18 @@ gather_query_vars([], []). is_a_different_variable([_ = Binding | Pairs], Value) :- ( Value == Binding, ! - ; is_a_different_variable(Pairs, Value) + ; is_a_different_variable(Pairs, Var) ). -gather_equations([], Goals, Goals). -gather_equations([Var = Value | Pairs], Goals, Goals1) :- +gather_equations([], MasterList, Goals, Goals). +gather_equations([Var = Value | Pairs], MasterList, Goals, Goals1) :- + select((Var = _), MasterList, MasterPairs), ( ( nonvar(Value) - ; is_a_different_variable(Pairs, Value) + ; is_a_different_variable(MasterPairs, Value) ) -> Goals = [Var = Value | Goals0], - gather_equations(Pairs, Goals0, Goals1) - ; gather_equations(Pairs, Goals, Goals1) + gather_equations(Pairs, MasterList, Goals0, Goals1) + ; gather_equations(Pairs, MasterList, Goals, Goals1) ). print_exception(E) :- From d04abc20215571f317aeb0abffa5be8fd49d2679 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 3 May 2020 23:08:53 -0600 Subject: [PATCH 4/4] improve answer substitutions at top-level --- src/prolog/toplevel.pl | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index ed17f80a..8c3c6941 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -235,7 +235,8 @@ write_eqs_and_read_input(B, VarList) :- append([Vars0, AttrVars, AttrGoalVars], Vars), charsio:extend_var_list(Vars, VarList, NewVarList, fabricated), '$get_b_value'(B0), - gather_equations(NewVarList, NewVarList, Goals, AttrGoals), + gather_query_vars(VarList, OrigVars), + gather_equations(NewVarList, OrigVars, Goals, AttrGoals), ( bb_get('$first_answer', true) -> write(' '), bb_put('$first_answer', false) @@ -286,8 +287,8 @@ help_message :- gather_query_vars([_ = Var | Vars], QueryVars) :- ( var(Var) -> - QueryVars = [Var | QueryVars1], - gather_query_vars(Vars, QueryVars1) + QueryVars = [Var | QueryVars0], + gather_query_vars(Vars, QueryVars0) ; gather_query_vars(Vars, QueryVars) ). gather_query_vars([], []). @@ -297,16 +298,38 @@ is_a_different_variable([_ = Binding | Pairs], Value) :- ; is_a_different_variable(Pairs, Var) ). +eq_member(X, [Y|_]) :- X == Y, !. +eq_member(X, [_|Ys]) :- eq_member(X, Ys). + +gather_equations([], _, Goals, Goals). +gather_equations([Var = Value | Pairs], OrigVarList, Goals, Goals1) :- + ( var(Value) -> + eq_member(Value, OrigVarList), + ( ( Pairs == [], NewPairs = [] + ; ( select((OtherVar = OtherValue), Pairs, NewPairs), + Value == OtherValue, Var \== OtherVar + ) + ) -> + Goals = [Var = Value | Goals0], + gather_equations(NewPairs, OrigVarList, Goals0, Goals1) + ; gather_equations(Pairs, OrigVarList, Goals, Goals1) + ) + ; Goals = [Var = Value | Goals0], + gather_equations(Pairs, OrigVarList, Goals0, Goals1) + ). + +/* gather_equations([], MasterList, Goals, Goals). gather_equations([Var = Value | Pairs], MasterList, Goals, Goals1) :- - select((Var = _), MasterList, MasterPairs), ( ( nonvar(Value) - ; is_a_different_variable(MasterPairs, Value) + ; select((Var = _), MasterList, MasterPairs), + is_a_different_variable(MasterPairs, Value) ) -> Goals = [Var = Value | Goals0], gather_equations(Pairs, MasterList, Goals0, Goals1) ; gather_equations(Pairs, MasterList, Goals, Goals1) ). +*/ print_exception(E) :- ( E == error('$interrupt_thrown', repl) -> nl % print the