]> Repositorios git - scryer-prolog.git/commitdiff
ENHANCED: Delay toplevel output until after the goal succeeds.
authorMarkus Triska <[email protected]>
Sat, 18 Apr 2020 23:28:50 +0000 (01:28 +0200)
committerMarkus Triska <[email protected]>
Sat, 18 Apr 2020 23:34:03 +0000 (01:34 +0200)
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

index fd143fc7fb43057bbc4f82edc7e8b15c72b19be9..d826b1b5dbbc4046fb24bc1b57aeb8f8b0b05998 100644 (file)
@@ -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