Hello Kurt, Waldek,
thank you for those helpful links and the detailed example.

I have extended the example type domain so that I can use 'approximate'
(see below), which induces some further type constraints
that do not seem to be satisfied by just Expression(Integer).

While the code example below compiles fine, it seems that the retraction
from AnyFunctions1 fails:
   >> Error detected within library code:
   Cannot retract value.
The statements work fine with the FriCAS interpreter, so I assume that the
interpreter performs some additional type conversion
that I am missing here. What is the best way to debug something like this,
given that the interpreter seems to take some additional steps that make it
work.
Could, in this case, 'retract' not report the type wrapped by 'Any' for
debugging?

If 'Any' acts as a wrapper to any type and saves that information, why can
'retract' fail? Wouldn't it just have to always unwrap the internally saved
type?
Otherwise, how does the interpreter automatically unwrap it to the correct
type?

For example in the interpreter this works without any issues (with bottom
messages on), and apparently without any additional type conversions:

(31) -> fromany :=
retract(testseries)$AnyFunctions1(UnivariateTaylorSeries(FE,ex,ex0))

 Function Selection for retract
      Arguments: ANY
      Target type: UTS(EXPR(INT),x,x0)
      From:      ANY1(UTS(EXPR(INT),x,x0))

 [1]  signature:   ANY -> UTS(EXPR(INT),x,x0)
      implemented: slot (UnivariateTaylorSeries (Expression (Integer)) x
((1 #S(SPAD-KERNEL :OP #(|x0| 0 (((|%symbol|)))) :ARG NIL :NEST 1) (1 0 .
1)) 0 . 1))(Any) from ANY1(UTS(EXPR(INT),x,x0))

   (31)  f
                       Type:
UnivariateTaylorSeries(Expression(Integer),x,x0)


---- SNIP ----
)abbrev package MYTAYL MyTaylorExpansion

MyTaylorExpansion(R, FE) : Exports == Implementation where
    R  : Join(GcdDomain, Comparable, RetractableTo Integer, _
              LinearlyExplicitOver Integer)
    FE : Join(AlgebraicallyClosedField, TranscendentalFunctionCategory, _
              FunctionSpace R)

    Exports ==> with
        my_taylor : (FE, R) -> FE

    Implementation ==> add
        e_pak ==> ExpressionToUnivariatePowerSeries(R,FE)

        my_taylor(f, x0) ==
            ex := 'x :: FE
            ex0 := x0 :: FE
            eqn := equation(ex, ex0)$Equation(FE)
            testseries := taylor(f, eqn)$e_pak
            fromany :=
retract(testseries)$AnyFunctions1(UnivariateTaylorSeries(FE,ex,ex0))
            approx :=
approximate(fromany::UnivariateTaylorSeries(FE,ex,ex0),3)$UnivariateTaylorSeries(FE,ex,ex0)
            approx
---- SNIP ----

On Mon, Jan 25, 2021 at 6:06 PM Waldek Hebisch <[email protected]>
wrote:

> On Mon, Jan 25, 2021 at 11:11:20AM -0800, Tobias Neumann wrote:
> > Hello!
> >
> > What is the best resource for SPAD? Aldor has a really excellent book
> > ( https://www.aldor.org/docs/aldorug.pdf ) and I don't suppose that
> SPAD
> > has something similar? Presumably the FriCAS book (
> > https://fricas.github.io/book.pdf ) and the FriCAS source are the best
> > resources? The FriCAS book covers SPAD in chapters 12 and 13, but I feel
> > it's not sufficient for me, especially when it comes to compiler errors.
> >
> > I am currently struggling to implement functions around series
> expansions.
> > It's probably one of the most complicated situations to start with due
> to
> > the dependent types, but it's what I need :)
>
> You will be looking at darkest corners of Spad compiler.  There
> are bugs and limitations affecting series.  You can look at
> existing algebra code to see how it deals with problems.
>
> > My ultimate goal is to write a function (in a package) that takes an
> > expression for series expansion and the expansion point and returns the
> > series expansion. So basically as a first step I would just like to
> write a
> > simple wrapper around series/taylor/puiseux with fixed input/output
> types.
>
> It is not clear for me what you really want to do (in FriCAS there
> are already several different functions fitting your description
> above).  For calling existing expanders you may look at
> function 'expr_to_series' in 'src/algebra/mrv_limit.spad'.
>
> BTW: 'taylor' already is a wrapper, so to see how wrappers works
> you can look up code of 'taylor' in 'src/algebra/expr2ups.spad'.
>
> BTW2: If you look up 'taylor' in HyperDoc you will see several
> signatures, you need to tell Spad compiler which one you want
> (for example by importing appropriate packege or via explicit
> qualification that I did below).  Once you found correct
> signature in HyperDoc 'descriptions' view you can click at
> 'Origin' field to see package description.  In package
> description you can click at 'Source File' field to see
> source file.  At least on my system title bar of source
> window contains file name, so if you prefer you can use
> different viewer to see the file (or fiddle a but with
> environment variables so that HyperDoc invokes your
> preffered viewer).
>
> > I've tried this in a SPAD function:
> >            x := 'x
> >            ex := x :: Expression(Integer)
> >            eqn := (ex = 0)
> >            testseries := taylor(x,eqn)
> >
> > But the compiler complains about
> >    >> Apparent user error:
> >    NoValueMode
> >     is an unknown mode
> >
> > What does such an error mean?
>
> Spad compiler has to deal with potentialy infinte set of types
> and with overloading.  Also Spad lets you omit most type
> annotations.  This means that Spad has to reconstruct missing
> information.  Errors like above mean that Spad is unable to
> find consistent types.  Spad can not be much more accurate
> than this, partially due to internal limitations, partially
> because huge (potentially infinte) number of possible
> errors and fixes.  And yes, adding type/package annotations
> is right thing to do.
>
> > I tried to get away with a minimal number of
> > type annotations, but adding additional for the taylor function doesn't
> > seem to help this error.
>
> You need to be persistent.  AFAICS the following works:
>
> -------------<cut here>----------------
>
> )abbrev package MYTAYL MyTaylorExpansion
>
> MyTaylorExpansion : with
>     my_taylor : Expression(Integer) -> Any
>   == add
>
>     e_pak ==> ExpressionToUnivariatePowerSeries(Integer,
>                   Expression(Integer))
>
>     my_taylor(f) ==
>         x := 'x
>         ex := x :: Expression(Integer)
>         eqn := equation(ex, 0)$Equation(Expression(Integer))
>         testseries := taylor(f, eqn)$e_pak
>
> -------------<cut here>----------------
>
> BTW: Error messages typically point to offending part, first
> error pointed out to 'taylor' so it was easy to deduce that
> I need to specify the package.  Then I got:
>
> error in function my_taylor
>
> (SEQ (|:=| |x| '|x|) (|:=| |ex| (|::| |x| (|Expression| (|Integer|))))
>      (|:=| |eqn| (= |ex| 0))
>      (|exit| 1
>       (|:=| |testseries|
>        ((|Sel|
>          (|ExpressionToUnivariatePowerSeries| (|Integer|)
>                                               (|Expression| (|Integer|)))
>          |taylor|)
>         |f| | << eqn >> |))))
> ****** level 5  ******
> $x:= eqn
> $m:= (Equation (Expression (Integer)))
> $f:=
> ((((|eqn| #) (|ex| #) (|x| #) (|f| # #) ...)))
>
>    >> Apparent user error:
>    Cannot coerce eqn
>       of mode (Boolean)
>       to mode (Equation (Expression (Integer)))
>
> This told me that Spad took wrong type for 'ex = 0'.  So
> I replaced '=' with 'equation'.  Then I got another
> error about 'equation(ex, 0)' so I made it more specific.
>
> > Furthermore, suppose I want to write a wrapper around taylor, could I
> write
> > the function with dependent types like this (I think something like this
> > would work in Aldor):
> >
> > wrapper : (Expression(Integer), p:Expression(Integer), x0:Integer) ->
> > UnivariateTaylorSeries(Expression(Integer), p, x0)
> > wrapper (expr, x, x0) == series(expr,x=x0)
>
> No.  You can get similar effect to dependent types using extra
> indirection via helper packages/domains, but in this case
> extra indirection would defeat your purpose.
>
> --
>                               Waldek Hebisch
>
> --
> You received this message because you are subscribed to the Google Groups
> "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/fricas-devel/20210125230626.GA11077%40math.uni.wroc.pl
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/CANL0awb_2YHsHZsyAhmUJuvuq3QFCaWe4VYz63wqANTU1ZQ-Tg%40mail.gmail.com.

Reply via email to