[Haskell-cafe] Can't figure out how to "show" for type "Int -> (Int, Int)"

Typing up and running (via Hugs) the examples in Wadler's excellent "Monads for functional programming" (here: http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf) I hit the inevitable "show" function error: ERROR - Cannot find "show" function for: *** Expression : eval answer *** Of type : Int -> (Int,Int) I can find lots of nice text about extending data for "show" (including the miraculous "Deriving") but... I'm showing my Haskell newbie roots by utterly failing to understand how to do this of a "higher-order" type (my best guess for the right term for the type Int -> (Int, Int)). It'd be a big help if somebody could tell me either: a) It's obvious, you moron, just <insert Haskell code here> or b) It's impossible, you moron, you're trying to violate the <insert Haskell rule here> Here' my code: data Term = Con Int | Div Term Term type M a = State -> (a, State) -- higher-order type, e.g. function type type State = Int -- type synonym eval :: Term -> M Int eval (Con a) x = (a, x) eval (Div t u) x = let (a, y) = eval t x in let (b, z) = eval u y in (a `div` b, z + 1) answer, error :: Term answer = (Div(Div(Con 1972)(Con 2))(Con 23)) error = (Div(Con 1)(Con 0)) I get the "ERROR" message when I type "eval answer" at the Hugs prompt. Thanks! Tom Titchener

On Thu, 11 Jan 2007, Tom Titchener wrote:
Here' my code:
data Term = Con Int | Div Term Term type M a = State -> (a, State) -- higher-order type, e.g. function type type State = Int -- type synonym eval :: Term -> M Int eval (Con a) x = (a, x) eval (Div t u) x = let (a, y) = eval t x in let (b, z) = eval u y in (a `div` b, z + 1) answer, error :: Term answer = (Div(Div(Con 1972)(Con 2))(Con 23)) error = (Div(Con 1)(Con 0))
I get the "ERROR" message when I type "eval answer" at the Hugs prompt.
'eval' requires two arguments, the second one is hidden in (M Int) which expands to (State -> (a, State)). That is, you must call eval answer 42 or so.

Em Qui, 2007-01-11 às 06:37 -0800, Tom Titchener escreveu:
ERROR - Cannot find "show" function for:
*** Expression : eval answer
*** Of type : Int -> (Int,Int)
type M a = State -> (a, State) -- higher-order type, e.g. function type
type State = Int -- type synonym
So M a = Int -> (a, Int)
eval :: Term -> M Int
So eval :: Term -> Int -> (Int, Int)
answer, error :: Term
I get the “ERROR” message when I type “eval answer” at the Hugs prompt.
eval answer :: M Int eval answer :: Int -> (Int, Int) Then you got to the error: there's no instance Show (Int -> (Int, Int)) To call eval (x :: Term) you must pass a initial State, say 0. eval answer 0 :: (Int, Int) -- malebria Marco Túlio Gontijo e Silva Correio (MSN): malebria@riseup.net Jabber (GTalk): malebria@jabber.org Telefone: 33346720 Celular: 98116720 Endereço: Rua Paula Cândido, 257/201 Gutierrez 30430-260 Belo Horizonte/MG Brasil
participants (3)
-
Henning Thielemann
-
Marco Túlio Gontijo e Silva
-
Tom Titchener