You could use the stack-checker to infer the effect of the quotation and
then dispatch like this:
```
M: quotation |+|
dup infer in>> length {
{ 1 [ [ bi |+| ] 2curry ] }
{ 2 [ [ 2bi |+| ] 2curry ] }
{ 3 [ [ 3bi |+| ] 2curry ] }
} case ;
```
But you might start running into places where the compiler is trying to
dispatch on the run-time value of a quotation, and you would need to add a
``call( x y -- z )`` with static stack effects to tell it what to expect.
We have multiple dispatch implemented in the ``multi-methods`` vocabulary,
so you could do something like this:
http://re-factor.blogspot.com/2013/10/rock-paper-scissors.html
On Fri, Dec 5, 2014 at 2:10 AM, Andrea Ferretti <ferrettiand...@gmail.com>
wrote:
> Hi, I am trying to implement monoids in factor. A monoid is a set with
> a binary associative operation (and, depending on the defintiion, a
> neutral element for it).
>
> Typical examples are numbers (with either addition or multiplication)
> or sequences (with concatenation). Other examples can be derived from
> these, since hastables whose values are in a monoid are also naturally
> a monoid.
>
> My implementation is here
>
>
> https://github.com/andreaferretti/factor-work/blob/master/monoid/monoid.factor
>
> Using |+| as the word for the operation, one has for instance
>
> 3 5 |+|
> ! gives 8
>
> "hello " "world" |+|
> ! gives "hello world"
>
> H{ { 1 2 } { 3 4 } } H{ { 1 5 } { 5 6 } } |+|
> ! gives H{ { 1 7 } { 3 4 } { 5 6 } }
>
>
> The issue I have is with quotations. Functions with values in a monoid
> are also naturally a monoid - in order to sum two functions, apply
> both and sum the values.
>
> So I have the definition
>
> M: quotation |+| [ bi |+| ] 2curry ;
>
> The problem is that this only works for functions of one argument. For
> functions of 2 arguments one would have to use
>
> M: quotation |+| [ 2bi |+| ] 2curry ;
>
> and so on.
>
> Unfortunately, there is only a class quotation, which does not seem to
> distinguish the arity.
>
> For comparison, in scala functions have different types based on the
> arity (such a Function2[A1, A2, B]), and this allows to correctly
> define |+| - in fact it is a common method in scalaz.
>
> Is there anything I can do to make this work regardless of function
> arity? Also, what about dispatching on both arguments, so that the
> method only makes sense for a pair of numbers or a pair of quotations,
> but, say
>
> 3 "hi" |+|
>
> fails to dispatch?
>
>
> ------------------------------------------------------------------------------
> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
> with Interactivity, Sharing, Native Excel Exports, App Integration & more
> Get technology previously reserved for billion-dollar corporations, FREE
>
> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk