hermann on Tue, 04 Jul 2023 13:24:55 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Question on "assert()" implementation in GP


I learned from Bill that execution of GP script has to be done with "gp < script":
https://pari.math.u-bordeaux.fr/archives/pari-users-2306/msg00004.html


That was fine sofar but now I want to implement "assert()" function that raises message and then immediately stops.

Here is a small sample implementation which has a problem:

$ cat asst.gp
assert(b, v, s) = { if(!(b), error(Str(v) " " Str(s))); quit }
x = 4;
assert(x==5, x, "wrong");
print("done.");
$


In gp session it works as expected, raises error and stops, "done." is not printed:

$ gp -q
? \r asst
(b,v,s)->if(!(b),error(Str(v)" "Str(s)));quit
  ***   at top-level: assert(x==5,x,"wrong")
  ***                 ^----------------------
  ***   in function assert: if(!(b),error(Str(v)" "Str(s)));quit
  ***                               ^----------------------------
  ***   user error: 4 wrong
  ***   Break loop: type 'break' to go back to GP prompt
break>



But when executed with "gp < script", each line is executed separately if I did understand that correctly. And "done." gets output, no stop before:

$ gp -q < asst.gp
(b,v,s)->if(!(b),error(Str(v)" "Str(s)));quit
  ***   at top-level: assert(x==5,x,"wrong")
  ***                 ^----------------------
  ***   in function assert: if(!(b),error(Str(v)" "Str(s)));quit
  ***                               ^----------------------------
  ***   user error: 4 wrong
done.
$


Is there some other mechanism than "quit()" that can be used to achieve stopping in "gp < script" mode?


Regards,

Hermann.