From 075ca78dd2d733932292a27e1350f9433e469da5 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sun, 19 Apr 2020 01:28:50 +0200 Subject: [PATCH] ENHANCED: Delay toplevel output until after the goal succeeds. This is to accommodate goals that are used for their side-effects, when we are interested in their output. Examples: ?- portray_clause((a :- a)). a :- a. true ; false. ?- format("hello~w~n", [!]). hello! true ; false. --- src/prolog/toplevel.pl | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index fd143fc7..d826b1b5 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -46,16 +46,24 @@ instruction_match(Term, VarList) :- submit_query_and_print_results(Term, VarList) ). +:- use_module(library(iso_ext)). + +% auxiliary predicates, so that using them in setup_call_cleanup/3 works +get_b_value(B) :- '$get_b_value'(B). +clear_attribute_goals :- '$clear_attribute_goals'. + submit_query_and_print_results(Term0, VarList) :- ( expand_goals(Term0, Term) -> true ; Term0 = Term ), - ( '$get_b_value'(B), write(' '), call(Term), write_eqs_and_read_input(B, VarList), - ! - % clear attribute goal lists, which may be populated by - % copy_term/3 prior to failure. - ; '$clear_attribute_goals', write('false.'), nl - ). + setup_call_cleanup(bb_put('$first_answer', true), + ( get_b_value(B), call(Term), write_eqs_and_read_input(B, VarList), + ! + ; % clear attribute goal lists, which may be populated by + % copy_term/3 prior to failure. + clear_attribute_goals, write('false.'), nl + ), + bb_put('$first_answer', false)). needs_bracketing(Value, Op) :- catch((functor(Value, F, _), @@ -132,6 +140,11 @@ write_eqs_and_read_input(B, VarList) :- sort(NewVarList, SortedVarList), '$get_b_value'(B0), gather_goals(SortedVarList, SortedVarList, Goals), + ( bb_get('$first_answer', true) -> + write(' '), + bb_put('$first_answer', false) + ; true + ), ( B0 == B -> ( Goals == [] -> write('true.'), nl -- 2.54.0