Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an arbitrary arity function in Haskell, which includes an arity of 0?

My current approach to define a function of arbitrary arity is below, with A being an accumulator, E being the input argument type, and R being the result type.

combine :: A -> E -> A

class X r where
    foo :: A -> E -> r

instance X R where
    foo :: A -> E -> R


instance X r => X ( E -> r ) where
    foo :: A -> E -> E -> r
    foo ( a :: A ) ( x :: E ) =
        foo ( a `combine` e :: A )

doFoo = foo emptyA

But the minimum arity of foo is 1. The minimum for foo is still A -> E -> R, and doFoo is E -> R. I'd also like to have doFoo :: R. How?

like image 869
Dingfeng Quek Avatar asked Jul 11 '11 04:07

Dingfeng Quek


1 Answers

What about

class X r where
    foo :: A -> r

instance X r => X (E -> r) where
    foo :: A -> E -> r
    foo a e = foo (combine a e)

?

You may want to have a look at the PrintfType instances. It's only because of them that I was able to provide an answer.

like image 166
Rotsor Avatar answered Nov 15 '22 20:11

Rotsor