# function types as instances of Num

6 messages
Open this post in threaded view
|

## function types as instances of Num

 Let's say we've got a little stack language, where you compute things by transformations of stacks, using compositions of functions from stacks to stacks (represented here as nested tuples). (See also Chris Okasaki's "Techniques for Embedding Postfix Languages in Haskell"  www.eecs.harvard.edu/~nr/ cs252r/archive/chris-okasaki/hw02.ps )   For example, the simple program below calculates the square of 4... > {-# OPTIONS -fglasgow-exts #-} > > main = print \$ test () > > test  = square . (lit 4) > > lit :: Integer -> a -> (Integer,a) > lit val stack    = (val, stack) > > dup  (a, b)      = (a, (a, b)) > mult (a, (b, c)) = (b*a, c) > square = mult . dup ...now let's say I find that using the function "lit" to annotation numeric literals ugly.  What I really want is something like... > test' = square . 4 ...Seems simple enough, I'll just make an appropriate instance of Num and I'll be able to use fromInteger... > instance Eq   (a -> (Integer, a)) > instance Show (a -> (Integer, a)) > instance Num  (a -> (Integer, a)) where >     fromInteger = lit ...but now when I try it, GHC complains...     No instance for (Num (a -> (Integer, t)))       arising from the literal `4' at final.hs:15:17     Possible fix:       add an instance declaration for (Num (a -> (Integer, t)))     In the second argument of `(.)', namely `4'     In the expression: square . 4     In the definition of `test'': test' = square . 4 ...so it seems that (a -> (Integer, t)) can't be unified with (a -> (Integer, a)), or somesuch.  Any thoughts on how to get this to work? Thanks, Greg Buchholz _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: function types as instances of Num

Open this post in threaded view
|

## Re: function types as instances of Num

 Dan Weston wrote: > How about:     Hmm.  I'm probably being dense today, but when I add the following definitions to your program... main = print \$ (square . 4) () square (a,b) = (a*a,b) ...I still get the same error...     No instance for (Num (() -> (t, t1)))       arising from the literal `4' at weston.hs:5:25     Possible fix: add an instance declaration for (Num (() -> (t, t1)))     In the second argument of `(.)', namely `4'     In the second argument of `(\$)', namely `(square . 4) ()'     In the expression: print \$ ((square . 4) ()) ...maybe you could show me your implementation of "main" and "square" to help nudge me in the right direction.  (I'm using ghc-6.6) Thanks, Greg Buchholz _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|