"Currying" in giac

Librairie C++ de calcul formel/ C++ symbolic computation library

Modérateur : xcasadmin

alexg
Messages : 11
Inscription : lun. janv. 24, 2011 9:56 pm

"Currying" in giac

Message par alexg » mar. janv. 25, 2011 4:33 pm

Hi

I wonder if there are any plans for currying support on giac.

At the moment, e.g., the statement below does not work in giac, although it is fine in Maple.
cfn := x->y->z->(x+y)*z
[ cfn(3)(4)(5) => 35 ]
the giac error is

Code : Tout sélectionner

// Error(s)
// End defining fn
undef
:1: syntax error, unexpected T_MAPSTO line 1 col 13 at ->
:1: syntax error, unexpected T_MAPSTO line 1 col 13 at ->
:1: syntax error, unexpected T_MAPSTO line 1 col 13 at ->
The best I can do with giac is:
cfn := x->(y->(z->(x+y)*z))

which (sort of) works, but gives the following warnings

Code : Tout sélectionner

// Warning: x,y, declared as global variable(s)
// Warning: z,x, declared as global variable(s)
// Warning: y,z, declared as global variable(s)
// End defining cfn
 (x)-> (y)-> (z)->(x+y)*z
cfn(3)(4)(5) now gives the correct answer, since x,y znd z appear to remain unbound globals, but if something is assigned to them, then obviously the thing screws up.

Any comments/ideas?

PS.
I need this since I am trying to adapt Sussman and Wisdom's SICM code to XCAS/giac, but I may end up just using scheme bindings to giac.

parisse
Messages : 5743
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: "Currying" in giac

Message par parisse » mar. janv. 25, 2011 8:20 pm

The user langage of giac was not designed for complex operations like that. It would certainly require someone with expertise in that kind of use to make the required changes, that's not me at least for now:-(

alexg
Messages : 11
Inscription : lun. janv. 24, 2011 9:56 pm

Re: "Currying" in giac

Message par alexg » mar. janv. 25, 2011 8:41 pm

Thanks for that feedback, at least I know I have to look for a workaround now.

I am looking forward to examining the source code, but that won't happen for a while yet...

alexg
Messages : 11
Inscription : lun. janv. 24, 2011 9:56 pm

Re: "Currying" in giac

Message par alexg » lun. mars 26, 2012 3:59 pm

Having been away from giac for a while, I recently revisited it, and found that currying CAN be done, in the following manner.
Since it seems that the system accepts this "workaround" correctly, it shouldn't be too difficult to adjust the language accordingly.

Using the example I gave above, once again, i.e. cfn := x->y->z->(x+y)*z
I can get the system to build it as follows:

1>> cfn := unapply(unapply(unapply((x+y)*z,z),y),x)
which correctly gives back
(x) -> (y) -> (z) -> (x+y)*z

Also,
2>> cfn(3)
(y) -> (z) -> (3+y)*z
3>> cfn(3)(4)
(z) -> (3+4)*z
4>> cfn(3)(4)(5)
35

Which are all absolutely correct.
I shall experiment with more complex ideas, but I am confident so far on the basis of what I've seen!
(Even Maple 13 doesn't handle this correctly!) :D

Of course the original problem remains, i.e. if I type
5>> cfn := (x) -> (y) -> (z) -> (x+y)*z
I get the same old T_MAPSTO and global variable warnings...

However, with a workaround this good, i can live with it.

alexg
Messages : 11
Inscription : lun. janv. 24, 2011 9:56 pm

Re: "Currying" in giac

Message par alexg » lun. mars 26, 2012 4:12 pm

Indeed, partial evaluation seems to be OK for multiple variables too, not just currying. eg:

1>> cfn := unapply(unapply((x^2+y^3)*z,z),(x,y))
(x,y)-> (z)->(x^2+y^3)*z
// Time 0
2>> cfn
(x,y)-> (z)->(x^2+y^3)*z
// Time 0
3>> cfn(3)
"[3] size() != [x,y] Error: Bad Argument Value"
// Time 0
4>> cfn(3,4)
(z)->(3^2+4^3)*z
// Time 0
5>> cfn(3,4)(5)
365

Répondre