Yrogirg wrote:
> 
> To make things clear all that time I was trying to rewrite your
> example from MathAction in a functional way. However each time I think
> I've managed to do so a new problem arises. Maybe it would be easier
> just to show me that code written functionally (i.e. operations over
> lists instead of the for loop (recursion is usually too bulky in
> syntax to use instead of loops)).
> 
> My current attempt still fails:
> 
> )abbrev package MYEXP MyExp
> 
> MyExp(F: Ring): with
>     myexp: (F, PositiveInteger) -> F
>  == add
>     myexp(x, n) ==
>         a(i : PositiveInteger) : F == x^i
>         myList : List(PositiveInteger) := [i::PositiveInteger for i in
> 1..10]
>         reduce(_+, map(a, myList))
>

The following works:

)abbrev package MYEXP MyExp

MyExp(F: Ring): with
    myexp: (F, PositiveInteger) -> F
 == add
    Map2 ==> ListFunctions2(PositiveInteger, F)
    myexp(x, n) ==
        a(i : PositiveInteger) : F == x^i
        myList : List(PositiveInteger) := [i::PositiveInteger for i in 1..10]
        reduce(_+@((F, F) -> F), map(a, myList)$Map2)

Basic problem is that 'map', 'reduce' and '+' are very unspecific,
you need to say something more to force correct type.  Above
I wrote that I want function '+' of given type.  The second problem is
that there is quite a lot of functions 'map' and most of them
was invisible (not imported).  In particular the 'List' domain
contains only homogeneous 'map' functions, that is 'map' from
List(PositiveInteger) to List(PositiveInteger).  The
'map' from List(PositiveInteger) to List(F) is in ListFunctions2.
Alternatively, in the last line one can write:

        reduce(_+, map(a, myList)$Map2)$List(F)

Once it is specified that 'reduce' is from List(F) compiler sees that
there is only one two-argument 'reduce' in List(F) and it requires
something of type '(F, F) -> F' so it can choose correct '+'.

> 
> So far I've learned something about local functions. In local
> functions one cannot
> 1) use "myfunc : A -> B" syntax to prescribe types before the
> implementation of the myfunc. One should use C-like type notion.
> 2) use functions without arguments (== won't do, only :=)
> 
> Anything else?
>

Message like:

$x:= +
$m:= $EmptyMode

means that FriCAS can not determine type for '+'.  Typical
reason is missing declaration.  Also frequently appropriate
definition is not imported (and hence invisible) or it
is too complex for FriCAS to find matching definition.  In
such cases try to make things more explicit by saying where
to find functions (by using $) and maybe also expected type
(needed is there are multiple functions which differ only
in return type).

The same applies when FriCAS says that it wants type T1 but
the offending expression has type T2.

> I wonder how do people learn Spad at all. I've looked through volume
> 0, 2 (that one was a bit strange), axiom wiki, but nothing devoted to
> Spad itself. At least nothing concerning the differences
> between .input and .spad syntax or local functions technicalities.
> Maybe I've overlooked something.
> 

Actually chapters 11, 12 and 13 in Axiom Book (you can see it via
HyperDoc) contain main part different than interpreter.  Chapter 2,
6 and 10 contain material about interpreter but are intended to
apply also to Spad.  Yes, that is rather sparse reference, but
AFAICS all Spad contructs are there.

Note that "interpreter" and Spad compiler are two different
compilers which intend to implement essentially the same
language.  One intended difference is that "interpreter"
tries hard to "guess" types while compiler signals errors.
Most of differences are considered bugs and we try to elliminate
them if possible.

Differences between .input and .spad syntax or local functions
technicalities are rather specialised knowledge.  And they
change, for example '+->' in Spad files is from March 2009
which given Axiom/FriCAS history is quite recent (before that
Spad compiler used quite different notation).

We certainly need more material on Spad programming.  And
please, keep asking questions -- that way we know what
needs to be explained.  Also, real questions is the
best source of examples.

-- 
                              Waldek Hebisch
[email protected] 

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/fricas-devel?hl=en.

Reply via email to