[Haskell-cafe] Re: distinguish functions from non-functions in
a class/instances
Dan Weston
westondan at imageworks.com
Fri Dec 7 13:21:24 EST 2007
This is great! Two questions:
1) I want to make sure the function arity matches the list length (as a
runtime check). I think I can do this with an arity function using
Data.Typeable. I came up with:
arity f = a (typeOf f) where
a tr | typeRepTyCon tr /= mkTyCon "->" = 0
| otherwise = 1 + (a . fromJust . funResultTy tr . head
. typeRepArgs $ tr)
This looks awful. Is there a better way to get the function arity?
2) Once I have say arity (+) == 2 at runtime, how can I get it reified
into Succ (Succ Zero)) at compile time to be able to use it as the first
argument in your nary function? Can/should I use Template Haskell for this?
Dan
Victor Nazarov wrote:
>
>> {-# OPTIONS -fglasgow-exts #-}
>> {-# OPTIONS -fallow-undecidable-instances #-}
>
> data Zero
> data Succ a
>
> class Nary n x y | n x -> y where
> nary :: n -> x -> [String] -> y
>
> instance Nary Zero x x where
> nary _ x [] = x
>
> instance (Nary n y z, Read x) => Nary (Succ n) (x->y) z where
> nary _ f (x:xs) = nary (undefined::n) (f $ read x) xs
>
More information about the Haskell-Cafe
mailing list