login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

On Sunday, March 19, 2006 5:54 AM Francois Maltey wrote:

axiom
resFloat   : Expression Float := cos (4.0 * x)

\label{eq1}\cos \left({{4.0}\  x}\right)(1)
Type: Expression(Float)
axiom
resInteger : Expression Integer := cos (4 * x)

\label{eq2}\cos \left({4 \  x}\right)(2)
Type: Expression(Integer)
axiom
resComplex : Expression Complex Integer := exp (3+4*%i)

\label{eq3}e^{\left(3 +{4 \  i}\right)}(3)
Type: Expression(Complex(Integer))

How can I get Float from resFloat ? Integer from resInteger, etc. in a *.input file ?

I don't try to get the sub-argument, but the type of the subargument.

Imagine I know that a is an Expression... My function don't know if a is an Expression Float, or an Expression Integer...

I want to do as below in an *.input file:

  Myfunction (a : Expression ...) : String ==
    is a an Expression Integer => "Integer"   -- or an other calculus.
    is a an Expression Franction Integer => "Rationnal"
    is a an Expression Complex ... => "Complex"   -- so I can use real and imag.
    is a an Expression Float => "Float"
    "A rare domain"

On Monday, March 20, 2006 7:53 PM William Sit wrote:

This is a bit tricky, since any function in Axiom must have a specific domain as source. The Axiom domain ANY is a coverall. Here is a try:

axiom
)set function compile on
Myfunction(a:Any):String == aDom := unparse ((domainOf a)::InputForm) substring?("Expression",aDom,1)=>delete(aDom, 1..11) "A rare domain"
Function declaration Myfunction : Any -> String has been added to workspace.
Type: Void

To use the function, coerce the input to Any, such as:

axiom
Myfunction(resInteger::Any)
axiom
Compiling function Myfunction with type Any -> String

\label{eq4}\mbox{\tt "Integer)"}(4)
Type: String

building on William Sit's ideas --Bill Page, Mon, 20 Mar 2006 20:36:07 -0600 reply
Based on what William suggested above, here is the way I might write the kind of function you want.

dom returns the domain of any expression as an "SExpression?" (SEX). An S-expression is the form used by Lisp and it can represent any expression as a special kind of list.

axiom
MyFunc2(a:ANY):OutputForm==
  if dom(a).1 = "Expression"::Symbol::SEX then
    dom(a).2 = ["Integer"::Symbol::SEX]::SEX =>
      "Integer"
    dom(a).2 = ["Float"::Symbol::SEX]::SEX =>
      "Float"
    dom(a).2 = ["Complex"::Symbol::SEX,["Integer"::
Symbol::SEX]::SEX]::SEX =>
      "Complex"
    "A rare domain"
  else
    return hconcat(message("Can not handle: ")$OutputForm,(dom
a)::OutputForm)
Function declaration MyFunc2 : Any -> OutputForm has been added to workspace.
Type: Void

axiom
MyFunc2(resFloat)
axiom
Compiling function MyFunc2 with type Any -> OutputForm

\label{eq5}\mbox{\tt "Float"}(5)
Type: OutputForm
axiom
MyFunc2(resInteger)

\label{eq6}\mbox{\tt "Integer"}(6)
Type: OutputForm
axiom
MyFunc2(resComplex)

\label{eq7}\mbox{\tt "Complex"}(7)
Type: OutputForm
axiom
MyFunc2(1)

\label{eq8}\mbox{\rm \hbox{\axiomType{Can}\ } not handle :}{\left(\hbox{\axiomType{PositiveInteger}\ } \right)}(8)
Type: OutputForm
axiom
MyFunc2(x::EXPR FRAC INT)

\label{eq9}\mbox{\tt "A rare domain"}(9)
Type: OutputForm

But, now a word of caution
Is this really what you want to do?

In generally, in Axiom we would like to use automatic polymorphism to select the appropriate function. In SPAD the "same" function can be defined many times using different signatures. So for example:

  plus(x::Expression Float):Expression Float == ...

and:

  plus(x::Expression Integer):Expression Integer == ...

can define different behaviours for the function plus depending on the type of the argument.

Unfortunately it is not possible to do this in the Axiom interpreter.

Here is another way using Union and case --Bill Page, Mon, 20 Mar 2006 21:12:25 -0600 reply
Often you can do what you want to do by using the Union type. In some ways it is similar to Any but more specific. This allows you to use the case operation to test membership in the 'Union':
axiom
EI ==> Expression Integer
Type: Void
axiom
EF ==> Expression Float
Type: Void
axiom
ECI ==> Expression Complex Integer
Type: Void
axiom
MyFunc3(a:Union(EI, EF, ECI)):String ==
  a case EI => "Integer";
  a case EF => "Float";
  "Complex Integer"
Function declaration MyFunc3 : Union(Expression(Integer),Expression( Float),Expression(Complex(Integer))) -> String has been added to workspace.
Type: Void

axiom
MyFunc3(resFloat)
axiom
Compiling function MyFunc3 with type Union(Expression(Integer),
      Expression(Float),Expression(Complex(Integer))) -> String

\label{eq10}\mbox{\tt "Float"}(10)
Type: String
axiom
MyFunc3(resInteger)

\label{eq11}\mbox{\tt "Integer"}(11)
Type: String
axiom
MyFunc3(resComplex)

\label{eq12}\mbox{\tt "Complex Integer"}(12)
Type: String
axiom
MyFunc3(n::EXPR FRAC INT)

\label{eq13}\mbox{\tt "Integer"}(13)
Type: String

simpler version using ANY --Bill Page, Tue, 21 Mar 2006 00:48:57 -0600 reply
We don't need those complicated looking S-expressions. It is easier just to generate them from a local variable.
axiom
MyFunc4(a:ANY):String==
  myEI:Expression Integer
  myEF:Expression Float
  myECI:Expression Complex Integer
  myEFI:Expression Fraction Integer
  dom(a) = dom(myEI) => "Integer"
  dom(a) = dom(myEF) => "Float"
  dom(a) = dom(myEFI) => "Rational"
  dom(a) = dom(myECI) => "Complex"
  "A rare domain"
Function declaration MyFunc4 : Any -> String has been added to workspace.
Type: Void

axiom
MyFunc4(resFloat)
axiom
Compiling function MyFunc4 with type Any -> String

\label{eq14}\mbox{\tt "Float"}(14)
Type: String
axiom
MyFunc4(resInteger)

\label{eq15}\mbox{\tt "Integer"}(15)
Type: String
axiom
MyFunc4(resComplex)

\label{eq16}\mbox{\tt "Complex"}(16)
Type: String
axiom
MyFunc4(1)

\label{eq17}\mbox{\tt "A rare domain"}(17)
Type: String
axiom
MyFunc4(x::EXPR FRAC INT)

\label{eq18}\mbox{\tt "Rational"}(18)
Type: String

By studying

http://wiki.axiom-developer.org/axiom--test--1/src/algebra/AnySpad

we can see how to write this in Spad. Note that the function devaluate returns an S-expression that represents a domain.

spad
)abbrev package MYDOM MyDomain
MyDomain: with
    MyFunc:Any->String
  == add
    MyFunc(a:Any):String==
      myEI==>Expression Integer
      myEF==>Expression Float
      myECI==>Expression Complex Integer
      myEFI==>Expression Fraction Integer
      dom(a) = devaluate(myEI)$Lisp => "Integer"
      dom(a) = devaluate(myEF)$Lisp => "Float"
      dom(a) = devaluate(myEFI)$Lisp => "Rational"
      dom(a) = devaluate(myECI)$Lisp => "Complex"
      "A rare domain"
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/7109084728131837944-25px010.spad using 
      old system compiler.
   MYDOM abbreviates package MyDomain 
------------------------------------------------------------------------
   initializing NRLIB MYDOM for MyDomain 
   compiling into NRLIB MYDOM 
   compiling exported MyFunc : Any -> String
   processing macro definition myEI ==> Expression Integer 
   processing macro definition myEF ==> Expression Float 
   processing macro definition myECI ==> Expression Complex Integer 
   processing macro definition myEFI ==> Expression Fraction Integer 
Time: 0.44 SEC.
(time taken in buildFunctor: 0)
;;; *** |MyDomain| REDEFINED
;;; *** |MyDomain| REDEFINED Time: 0.01 SEC.
Cumulative Statistics for Constructor MyDomain Time: 0.45 seconds
finalizing NRLIB MYDOM Processing MyDomain for Browser database: --->-->MyDomain((MyFunc ((String) (Any)))): Not documented!!!! --->-->MyDomain(constructor): Not documented!!!! --->-->MyDomain(): Missing Description ; compiling file "/var/zope2/var/LatexWiki/MYDOM.NRLIB/MYDOM.lsp" (written 15 APR 2010 12:23:47 AM): ; compiling (/VERSIONCHECK 2) ; compiling (DEFUN |MYDOM;MyFunc;AS;1| ...) ; compiling (DEFUN |MyDomain| ...) ; compiling (DEFUN |MyDomain;| ...) ; compiling (MAKEPROP (QUOTE |MyDomain|) ...) ; compiling (MAKEPROP (QUOTE |MyDomain|) ...)
; /var/zope2/var/LatexWiki/MYDOM.NRLIB/MYDOM.fasl written ; compilation finished in 0:00:00.053 ------------------------------------------------------------------------ MyDomain is now explicitly exposed in frame initial MyDomain will be automatically loaded when needed from /var/zope2/var/LatexWiki/MYDOM.NRLIB/MYDOM

axiom
MyFunc(resInteger)$MyDomain

\label{eq19}\mbox{\tt "Integer"}(19)
Type: String
axiom
MyFunc(x::EXPR FRAC INT)$MyDomain

\label{eq20}\mbox{\tt "Rational"}(20)
Type: String




  Subject:   Be Bold !!
  ( 8 subscribers )  
Please rate this page: