On Sunday, March 19, 2006 5:54 AM Francois Maltey wrote: fricas (1) -> resFloat : Expression Float := cos (4.0 * x)
Type: Expression(Float)
fricas resInteger : Expression Integer := cos (4 * x)
Type: Expression(Integer)
fricas resComplex : Expression Complex Integer := exp (3+4*%i)
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 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: fricas )set function compile on Type: Void
To use the function, coerce the input to Any, such as: fricas Myfunction(resInteger::Any) fricas Compiling function Myfunction with type Any -> String
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.
fricas 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, Type: Void
fricas MyFunc2(resFloat) fricas Compiling function MyFunc2 with type Any -> OutputForm
Type: OutputForm?
fricas MyFunc2(resInteger)
Type: OutputForm?
fricas MyFunc2(resComplex)
Type: OutputForm?
fricas MyFunc2(1)
Type: OutputForm?
fricas MyFunc2(x::EXPR FRAC INT)
Type: OutputForm?
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 Unfortunately it is not possible to do this in the Axiom interpreter. Often you can do what you want to do by using theUnion 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':
fricas EI ==> Expression Integer Type: Void
fricas EF ==> Expression Float Type: Void
fricas ECI ==> Expression Complex Integer Type: Void
fricas MyFunc3(a:Union(EI, Type: Void
fricas MyFunc3(resFloat) fricas Compiling function MyFunc3 with type Union(Expression(Integer),
Type: String
fricas MyFunc3(resInteger)
Type: String
fricas MyFunc3(resComplex)
Type: String
fricas MyFunc3(n::EXPR FRAC INT)
Type: String
We don't need those complicated looking S-expressions. It
is easier just to generate them from a local variable.
fricas 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" Type: Void
fricas MyFunc4(resFloat) fricas Compiling function MyFunc4 with type Any -> String
Type: String
fricas MyFunc4(resInteger)
Type: String
fricas MyFunc4(resComplex)
Type: String
fricas MyFunc4(1)
Type: String
fricas MyFunc4(x::EXPR FRAC INT)
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
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/lib/zope2.10/instance/axiom-wiki/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.02 SEC. fricas MyFunc(resInteger)$MyDomain
Type: String
fricas MyFunc(x::EXPR FRAC INT)$MyDomain
Type: String
|