On Thu, Apr 3, 2008 at 11:13 AM, Gabriel Dos Reis wrote:
>
> Bill Page writes:
>
> | On Thu, Apr 3, 2008 at 10:58 AM, Gabriel Dos Reis wrote:
> | >
> | > Bill Page writes:
> | >
> | > | I don't think 'reduce(+,[])' requires too much magic - at least in
> | > | the interpreter. Try:
> | > |
> | > | (1) -> myreduce(f,x)==(#x>1 => f(first x,myreduce(f,rest x)); #x=1 =>
> first x; f=_+ => 0; f=_* => 1;error "use: reduce(op,list,id)")
Type: Void
> | > ...
> | > | (5) -> myreduce(+,[])
> | > | Compiling function myreduce with type (Variable +,List None) ->
> | > | NonNegativeInteger
> | > |
> | > | (5) 0
> | > | Type:
> NonNegativeInteger
> | >
> | > I'm very surprised you find that result OK.
> | >
> |
> | I expect that
> |
> | reduce(+,A) + reduce(+,B) == reduce(+,concat(A,B))
> |
> | so what else could it be?
>
> So, you expect that reducing addition operator over a List None should
> yield a NonNegativeInteger? I'm awfully surprised.
>
Well I *am* quite surprised by the signature that the interpreter
chooses for this function:
(Variable +,List None) -> NonNegativeInteger
and as I said previously, I find it a little "magic" that the
expression 'f(first x,myreduce(f,rest x))' apparently compiles as the
naive user might expect. Really should have written '_+$Integer'
instead of just '+'
(2) -> myreduce(_+$Integer,[])
Compiling function red with type (((Integer,Integer) -> Integer),
List None) -> Integer
(2) 0
Type: NonNegativeInteger
But now perhaps equally "magic" is that 'f=_+ => 0' again naively just works ...
In the library code what I really wanted was something like this
modification to ListAggregate:
reduce(f, x) ==
if empty? x then
if S has AbelianMonoid then
f=_+$S => 0$S
if S has Monoid then
f=_*$S => 1$S
error "reducing over an empty list needs the 3 argument form"
reduce(f, rest x, first x)
since presumably then we are checking the specific algebraic
properties of the operation. But this does not compile in Spad.
Regards,
Bill Page.
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
open-axiom-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/open-axiom-devel