the compiler cannot guess your human assumptions and conclusions:

Yes, and it is good that it doesn't!

The reason, why this

    myexp(x: F, n: PositiveInteger): F == reduce(+,[1,2,3,4])

doesn't work is the following.

Obviously you want F as the result type. So try

)display operations reduce

on the command line or rather use HyperDoc for a nicer form of output.
(For HD enter "reduce" in the browse field and click operations and then Signatures.)

In order to figure out which functions you actually meant in your
reduce(+,[1,2,3,4]) expression, the compiler collects every possible function with name "reduce" and tries to match types with the given arguments.

Your use of reduce has 2 arguments. So you can rule out every reduce function with just one or more than 2 arguments. So what remains is

  reduce: ((S, S) -> S, X) -> S

For some yet unknown types S and X.

Since you want reduce to return something of type F, the compiler already knows that it must look for a function

  reduce: ((F, F) -> F, X) -> F

The first argument + must now be of type (F,F)->F. That's now simple, because the domain F exports such a function.

If you click in hyperdoc on the respective reduce: ((S,S)->S,%)->S, you'll see more explanation. In particular you find "Origin: Collection(S).

Now your second argument looks like a List. And a List is a Collection.
But it also means, it is Collection(S), and bringing all together, i.e. S=F in your case, means, that in order to find a reduce function, the second argument [1,2,3,4] must be of type List(F).

But [1,2,3,4] is (without any implicit conversion) not of type List(F), it rather looks like being of type List(PositiveInteger).

So the compiler does not find anything appropriate and thus must complain to the user.

As Johannes sugested, you have to provide *explicit* coercions using "::F" or you simply do as I have done in my original code.

    myexp(x: F, n: PositiveInteger): F ==
        y: F := 1
        z: F := 1
        for i in 1..n repeat
            z := z * x
            y := y + z
        y

I was careful to give y and z the type F from the beginning.

Ralf

--
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