Project goals of query variables (#362)

This commit is contained in:
Mark Thom
2020-05-02 22:27:32 -06:00
parent 989bed88fe
commit 848425418b
4 changed files with 100 additions and 59 deletions

View File

@@ -44,8 +44,7 @@ make_new_var_name(VarType, V, VarName, N, N1, VarList) :-
N1 is N + 1 N1 is N + 1
). ).
extend_var_list(Value, VarList, NewVarList, VarType) :- extend_var_list(Vars, VarList, NewVarList, VarType) :-
term_variables(Value, Vars),
extend_var_list_(Vars, 0, VarList, NewVarList0, VarType), extend_var_list_(Vars, 0, VarList, NewVarList0, VarType),
append(VarList, NewVarList0, NewVarList). append(VarList, NewVarList0, NewVarList).
@@ -140,5 +139,6 @@ write_term_to_chars(Term, Options, Chars) :-
builtins:inst_member_or(Options, quoted(Quoted), quoted(false)), builtins:inst_member_or(Options, quoted(Quoted), quoted(false)),
builtins:inst_member_or(Options, variable_names(VarNames), variable_names([])), builtins:inst_member_or(Options, variable_names(VarNames), variable_names([])),
builtins:inst_member_or(Options, max_depth(MaxDepth), max_depth(0)), builtins:inst_member_or(Options, max_depth(MaxDepth), max_depth(0)),
extend_var_list(Term, VarNames, NewVarNames, numbervars), term_variables(Term, Vars),
extend_var_list(Vars, VarNames, NewVarNames, numbervars),
'$write_term_to_chars'(Term, IgnoreOps, NumberVars, Quoted, NewVarNames, MaxDepth, Chars). '$write_term_to_chars'(Term, IgnoreOps, NumberVars, Quoted, NewVarNames, MaxDepth, Chars).

View File

@@ -436,7 +436,6 @@ impl Machine {
) )
); );
compile_user_module(&mut wam, compile_user_module(&mut wam,
Stream::from(CHARSIO), Stream::from(CHARSIO),
true, true,
@@ -446,6 +445,15 @@ 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() { if wam.compile_top_level().is_err() {
panic!("Loading '$toplevel' module failed"); panic!("Loading '$toplevel' module failed");
} }

View File

@@ -101,7 +101,7 @@ call_attribute_goals_with_module_prefix([Module | Modules], GoalCaller, AttrVars
call_attribute_goals_with_module_prefix(Modules, GoalCaller, AttrVars, Gs). call_attribute_goals_with_module_prefix(Modules, GoalCaller, AttrVars, Gs).
copy_term(Source, Dest, Goals) :- copy_term(Source, Dest, Goals) :-
term_variables(Source, Vars), '$term_attributed_variables'(Source, Vars),
gather_modules(Vars, Modules0, _), gather_modules(Vars, Modules0, _),
sort(Modules0, Modules), sort(Modules0, Modules),
call_attribute_goals_with_module_prefix(Modules, call_query_var_goals, Vars, Goals0), call_attribute_goals_with_module_prefix(Modules, call_query_var_goals, Vars, Goals0),

View File

@@ -1,9 +1,9 @@
:- module('$toplevel', ['$repl'/1, consult/1, use_module/1, use_module/2, :- module('$toplevel', ['$repl'/1, consult/1, use_module/1, use_module/2,
argv/1]). argv/1]).
:- use_module(library(charsio)). :- use_module(library(charsio)).
:- use_module(library(lists)). :- use_module(library(lists)).
:- use_module(library(ordsets)).
:- use_module(library(si)). :- use_module(library(si)).
:- dynamic(argv/1). :- dynamic(argv/1).
@@ -71,13 +71,13 @@ arg_type(g(_)).
arg_type(t(_)). arg_type(t(_)).
ends_with_dot(Ls0) :- ends_with_dot(Ls0) :-
reverse(Ls0, Ls), reverse(Ls0, Ls),
layout_and_dot(Ls). layout_and_dot(Ls).
layout_and_dot(['.'|_]). layout_and_dot(['.'|_]).
layout_and_dot([C|Cs]) :- layout_and_dot([C|Cs]) :-
char_type(C, layout), char_type(C, layout),
layout_and_dot(Cs). layout_and_dot(Cs).
run_goals([]). run_goals([]).
run_goals([g(Gs0)|Goals]) :- run_goals([g(Gs0)|Goals]) :-
@@ -116,19 +116,19 @@ instruction_match(Term, VarList) :-
( var(Term) -> ( var(Term) ->
throw(error(instantiation_error, repl/0)) throw(error(instantiation_error, repl/0))
; ;
Term = [Item] -> !, Term = [Item] -> !,
( atom(Item) -> ( atom(Item) ->
( Item == user -> ( Item == user ->
catch(compile_batch, E, print_exception_with_check(E)) catch(compile_batch, E, print_exception_with_check(E))
; consult(Item) ; consult(Item)
) )
; ;
catch(throw(error(type_error(atom, Item), repl/0)), catch(throw(error(type_error(atom, Item), repl/0)),
E, E,
print_exception_with_check(E)) print_exception_with_check(E))
) )
; ;
submit_query_and_print_results(Term, VarList) submit_query_and_print_results(Term, VarList)
). ).
:- use_module(library(iso_ext)). :- use_module(library(iso_ext)).
@@ -152,10 +152,10 @@ submit_query_and_print_results(Term0, VarList) :-
needs_bracketing(Value, Op) :- needs_bracketing(Value, Op) :-
catch((functor(Value, F, _), catch((functor(Value, F, _),
current_op(EqPrec, EqSpec, Op), current_op(EqPrec, EqSpec, Op),
current_op(FPrec, _, F)), current_op(FPrec, _, F)),
_, _,
false), false),
( EqPrec < FPrec -> true ( EqPrec < FPrec -> true
; '$quoted_token'(F) -> true ; '$quoted_token'(F) -> true
; atom_length(F, 1), graphic_token_char(F) -> true ; atom_length(F, 1), graphic_token_char(F) -> true
@@ -166,7 +166,7 @@ needs_bracketing(Value, Op) :-
write_goal(G, VarList, MaxDepth) :- write_goal(G, VarList, MaxDepth) :-
( G = (Var = Value) -> ( G = (Var = Value) ->
( var(Value) -> ( var(Value) ->
select((Var = _), VarList, NewVarList) select((Var = _), VarList, NewVarList)
; VarList = NewVarList ; VarList = NewVarList
), ),
write(Var), write(Var),
@@ -185,20 +185,20 @@ write_goal(G, VarList, MaxDepth) :-
write_last_goal(G, VarList, MaxDepth) :- write_last_goal(G, VarList, MaxDepth) :-
( G = (Var = Value) -> ( G = (Var = Value) ->
( var(Value) -> ( var(Value) ->
select((Var = _), VarList, NewVarList) select((Var = _), VarList, NewVarList)
; VarList = NewVarList ; VarList = NewVarList
), ),
write(Var), write(Var),
write(' = '), write(' = '),
( needs_bracketing(Value, (=)) -> ( needs_bracketing(Value, (=)) ->
write('('), write('('),
write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)]), write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)]),
write(')') write(')')
; write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)]), ; write_term(Value, [quoted(true), variable_names(NewVarList), max_depth(MaxDepth)]),
( trailing_period_is_ambiguous(Value) -> ( trailing_period_is_ambiguous(Value) ->
write(' ') write(' ')
; true ; true
) )
) )
; G == [] -> ; G == [] ->
write('true') write('true')
@@ -229,9 +229,12 @@ trailing_period_is_ambiguous(Value) :-
graphic_token_char(Char). graphic_token_char(Char).
write_eqs_and_read_input(B, VarList) :- write_eqs_and_read_input(B, VarList) :-
charsio:extend_var_list(VarList, VarList, NewVarList, fabricated), term_variables(VarList, Vars0),
'$term_attributed_variables'(VarList, AttrVars),
append(Vars0, AttrVars, Vars),
charsio:extend_var_list(Vars, VarList, NewVarList, fabricated),
'$get_b_value'(B0), '$get_b_value'(B0),
gather_goals(NewVarList, NewVarList, Goals), gather_goals(NewVarList, NewVarList, VarList, Goals),
( bb_get('$first_answer', true) -> ( bb_get('$first_answer', true) ->
write(' '), write(' '),
bb_put('$first_answer', false) bb_put('$first_answer', false)
@@ -239,11 +242,11 @@ write_eqs_and_read_input(B, VarList) :-
), ),
( B0 == B -> ( B0 == B ->
( Goals == [] -> ( Goals == [] ->
write('true.'), nl write('true.'), nl
; thread_goals(Goals, ThreadedGoals, (',')), ; thread_goals(Goals, ThreadedGoals, (',')),
write_eq(ThreadedGoals, NewVarList, 20), write_eq(ThreadedGoals, NewVarList, 20),
write('.'), write('.'),
nl nl
) )
; thread_goals(Goals, ThreadedGoals, (',')), ; thread_goals(Goals, ThreadedGoals, (',')),
write_eq(ThreadedGoals, NewVarList, 20), write_eq(ThreadedGoals, NewVarList, 20),
@@ -293,23 +296,53 @@ is_a_different_variable([_ = Binding | Pairs], Value) :-
; is_a_different_variable(Pairs, Value) ; is_a_different_variable(Pairs, Value)
). ).
gather_goals([], VarList, Goals) :- filter_goals([Goal|Goals], FGoals, QueryVars) :-
gather_query_vars(VarList, QueryVars), term_variables(Goal, GoalVars0),
copy_term(QueryVars, QueryVars, Goals). sort(GoalVars0, GoalVars),
gather_goals([Var = Value | Pairs], VarList, Goals) :- ( 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) :-
( ( nonvar(Value) ( ( nonvar(Value)
; is_a_different_variable(Pairs, Value) ; is_a_different_variable(Pairs, Value)
) -> ) ->
Goals = [Var = Value | Goals0], Goals = [Var = Value | Goals0],
gather_goals(Pairs, VarList, Goals0) gather_goals(Pairs, VarList, QueryVarList, Goals0)
; gather_goals(Pairs, VarList, Goals) ; gather_goals(Pairs, VarList, QueryVarList, Goals)
). ).
/*
gather_goals([], VarList, QueryVarList, Goals) :-
gather_query_vars(VarList, Vars),
copy_term(Vars, Vars, Goals).
% filter_goals(Goals0, Goals, Vars).
gather_goals([Var = Value | Pairs], VarList, QueryVarList, Goals) :-
( ( nonvar(Value)
; is_a_different_variable(Pairs, Value)
) ->
Goals = [Var = Value | Goals0],
gather_goals(Pairs, VarList, QueryVarList, Goals0)
; gather_goals(Pairs, VarList, QueryVarList, Goals)
).
*/
print_exception(E) :- print_exception(E) :-
( E == error('$interrupt_thrown', repl) -> nl % print the ( E == error('$interrupt_thrown', repl) -> nl % print the
% exception on a % exception on a
% newline to evade % newline to evade
% "^C". % "^C".
; true ; true
), ),
write_term('caught: ', [quoted(false), max_depth(20)]), write_term('caught: ', [quoted(false), max_depth(20)]),
@@ -318,8 +351,8 @@ print_exception(E) :-
print_exception_with_check(E) :- print_exception_with_check(E) :-
( E = error(_, _:_) -> true % if the error source contains a line ( E = error(_, _:_) -> true % if the error source contains a line
% number, a GNU-style error message % number, a GNU-style error message
% is expected to be printed instead. % is expected to be printed instead.
; print_exception(E) ; print_exception(E)
). ).
@@ -370,12 +403,12 @@ use_module(Module, QualifiedExports) :-
( nonvar(Module) -> ( nonvar(Module) ->
( list_si(QualifiedExports) -> ( list_si(QualifiedExports) ->
maplist('$module_export'(use_module/2), QualifiedExports) -> maplist('$module_export'(use_module/2), QualifiedExports) ->
( Module = library(Filename) -> ( Module = library(Filename) ->
'$use_qualified_module'(Filename, QualifiedExports) '$use_qualified_module'(Filename, QualifiedExports)
; atom(Module) -> ; atom(Module) ->
'$use_qualified_module_from_file'(Module, QualifiedExports) '$use_qualified_module_from_file'(Module, QualifiedExports)
; throw(error(invalid_module_specifier, use_module/2)) ; throw(error(invalid_module_specifier, use_module/2))
) )
; throw(error(type_error(list, QualifiedExports), use_module/2)) ; throw(error(type_error(list, QualifiedExports), use_module/2))
) )
; throw(error(instantiation_error, use_module/2)) ; throw(error(instantiation_error, use_module/2))