Re: [fricas-devel] Re: InputForm

Ralf Hemmecke <[email protected]> Wed, 03 Jun 2009 20:44:58 +0200
Newsgroups gmane.comp.mathematics.axiom.user
Message-ID <[email protected]>
Dear Gaby

Looks like r881 is the one you meant. Can you confirm?

Martin, is this in your interest? I somehow had the impression that you 
have other ideas of what InputForm should be used for.

Ralf
----------------------------------------------------------------------
r2496 (orig r881):  dos-reis | 2008-10-15 05:44:37 +0200
Changed paths:
   M  /openaxiom/1.2/src/ChangeLog
   M  /openaxiom/1.2/src/algebra/mkfunc.spad.pamphlet
   M  /openaxiom/1.2/src/interp/i-output.boot
   M  /openaxiom/trunk/src/ChangeLog
   M  /openaxiom/trunk/src/algebra/mkfunc.spad.pamphlet
   M  /openaxiom/trunk/src/interp/i-output.boot

	* algebra/mkfunc.spad.pamphlet (coerce$InputForm): Display
	InputForm as a one-dimensional stream of characters suitable
	for input to the interpreter.
	* interp/i-output.boot: Implement conversion of InputForm to
	displayed OutputForm.

=============================================================
svn diff -r880:881 
https://open-axiom.svn.sourceforge.net/svnroot/open-axiom/trunk > r881.patch
==== r881.patch =============================================
Index: src/ChangeLog
===================================================================
--- src/ChangeLog	(revision 880)
+++ src/ChangeLog	(revision 881)
@@ -1,5 +1,13 @@
  2008-10-14  Gabriel Dos Reis  <[email protected]>

+	* algebra/mkfunc.spad.pamphlet (coerce$InputForm): Display
+	InputForm as a one-dimensional stream of characters suitable
+	for input to the interpreter.
+	* interp/i-output.boot: Implement conversion of InputForm to
+	displayed OutputForm.
+
+2008-10-14  Gabriel Dos Reis  <[email protected]>
+
  	* interp/i-spec2.boot (upQUOTE): Quoted forms belong to InputForm.
  	* interp/sys-constants.boot ($InputForm): New.

Index: src/algebra/mkfunc.spad.pamphlet
===================================================================
--- src/algebra/mkfunc.spad.pamphlet	(revision 880)
+++ src/algebra/mkfunc.spad.pamphlet	(revision 881)
@@ -1,21 +1,24 @@
  \documentclass{article}
  \usepackage{axiom}
+
+\title{src/algebra mkfunc.spad}
+\author{Manuel Bronstein}
+
  \begin{document}
-\title{\$SPAD/src/algebra mkfunc.spad}
-\author{Manuel Bronstein}
  \maketitle
  \begin{abstract}
  \end{abstract}
-\eject
  \tableofcontents
  \eject
+
  \section{domain INFORM InputForm}
+
  <<domain INFORM InputForm>>=
  )abbrev domain INFORM InputForm
  ++ Parser forms
  ++ Author: Manuel Bronstein
  ++ Date Created: ???
-++ Date Last Updated: September 18, 2008
+++ Date Last Updated: October 14, 2008
  ++ Description:
  ++   Domain of parsed forms which can be passed to the interpreter.
  ++   This is also the interface between algebra code and facilities
@@ -191,6 +194,11 @@
        s2 = 1 => s1
        conv [convert("/"::Symbol), s1, s2]$List(%)

+    -- A displayed form of an InputForm should be suitable as
+    -- input to the interpreter parser.
+    coerce(x: %): OutputForm ==
+      inputForm2OutputForm(x)$Lisp
+
  @
  \section{package INFORM1 InputFormFunctions1}
  <<package INFORM1 InputFormFunctions1>>=
Index: src/interp/i-output.boot
===================================================================
--- src/interp/i-output.boot	(revision 880)
+++ src/interp/i-output.boot	(revision 881)
@@ -2581,3 +2581,99 @@
      form
    if ^$collectOutput then PRETTYPRINT(u,$algebraOutputStream)
    nil
+
+
+--% Rendering of InputForm
+
+$allClassicOps ==
+  ["~","#","-","**","^","*","/","rem","quo","+","-",
+    "@","::","$", "pretend"]
+
+isUnaryPrefix op ==
+  op in '(_~ _# _-)
+
+primaryForm2String x ==
+  x = nil => '""
+  STRINGP x => x
+  x = $EmptyMode => specialChar 'quad
+  IDENTP x => SYMBOL_-NAME x
+  atom x => WRITE_-TO_-STRING x
+  strconc('"(",inputForm2String x, '")")
+
+callForm2String x ==
+  atom x => primaryForm2String x
+  [op,:args] := x
+
+  op in $allClassicOps => primaryForm2String x
+
+  #args = 0 =>
+    op = "Zero" => '"0"
+    op = "One" => '"1"
+    constructor? op => primaryForm2String op
+    strconc(inputForm2String op, '"()")
+
+  "strconc"/[inputForm2String op, '"(",:args','")"] where
+    args' := [toString(a,i) for a in args for i in 0..]
+    toString(a,i) ==
+      i = 0 => inputForm2String a
+      strconc('",",inputForm2String a)
+
+typedForm2String(s,x,t) ==
+  s = "pretend" =>
+    strconc(primaryForm2String x, '" pretend ", callForm2String t)
+  strconc(primaryForm2String x, SYMBOL_-NAME s, callForm2String t)
+
+expForm2String x ==
+  x is [op,lhs,rhs] and op in '(** _^) =>
+    strconc(expForm2String lhs,'"^", primaryForm2String rhs)
+  callForm2String x
+
+unaryForm2String x ==
+  x is [op,arg] and isUnaryPrefix op =>
+    strconc(inputForm2String op, inputForm2String arg)
+  expForm2String x
+
+multForm2String x ==
+  x isnt ["*",lhs,rhs] => unaryForm2String x
+  strconc(multForm2String lhs,'"*", multForm2String rhs)
+
+divForm2String x ==
+  x isnt ["/",lhs,rhs] => multForm2String x
+  strconc(divForm2String lhs,'"/", expForm2String rhs)
+
+remForm2String x ==
+  x isnt ["rem",lhs,rhs] => divForm2String x
+  strconc(divForm2String lhs,'" rem ", multForm2String rhs)
+
+quoForm2String x ==
+  x isnt ["quo",lhs,rhs] => remForm2String x
+  strconc(quoForm2String lhs,'" quo ", remForm2String rhs)
+
+plusForm2String x ==
+  x isnt ["+",lhs,rhs] => quoForm2String x
+  strconc(plusForm2String lhs,'" + ", plusForm2String rhs)
+
+minusForm2String x ==
+  x isnt ["-",lhs,rhs] => plusForm2String x
+  strconc(minusForm2String lhs,'" - ", minusForm2String rhs)
+
+inputForm2String x ==
+  atom x => primaryForm2String x
+  [op,:args] := x
+  isUnaryPrefix op and #args = 1 => unaryForm2String x
+  #args = 2 =>
+    op in '(** _^) => expForm2String x
+    op = "*" => multForm2String x
+    op = "/" => divForm2String x
+    op = "rem" => remForm2String x
+    op = "quo" => quoForm2String x
+    op = "+" => plusForm2String x
+    op = "-" => minusForm2String x
+    op in '(_@ _:_: $ pretend) =>
+      typedForm2String(op, first args, second args)
+    callForm2String x
+  callForm2String x
+
+inputForm2OutputForm x ==
+  INTERN inputForm2String x
+