instance for (Show ([(String, Int)] -> Int))

How do I code an 'instance' declaration for '(Show ([(String, Int)] -> Int))'. I am new to instance declarations and am following the Happy examples :- http://www.haskell.org/happy/doc/html/sec-using.html#sec-other-datatypes The compiler is requesting an instance declaration for Show :- expr-eval.hs:334:23: No instance for (Show ([(String, Int)] -> Int)) arising from a use of `print' at expr-eval.hs:334:23-27 Possible fix: add an instance declaration for (Show ([(String, Int)] -> Int)) In the first argument of `(.)', namely `print' In the second argument of `(>>=)', namely `print . calc . lexer' In the expression: getContents >>= print . calc . lexer Many thanks in advance, Aaron

On Fri, 24 Dec 2010, Aaron Gray wrote:
The compiler is requesting an instance declaration for Show :-
expr-eval.hs:334:23: No instance for (Show ([(String, Int)] -> Int)) arising from a use of `print' at expr-eval.hs:334:23-27 Possible fix: add an instance declaration for (Show ([(String, Int)] -> Int)) In the first argument of `(.)', namely `print' In the second argument of `(>>=)', namely `print . calc . lexer' In the expression: getContents >>= print . calc . lexer
... maybe 'calc' needs a further argument?

On 24 December 2010 18:24, Henning Thielemann wrote: On Fri, 24 Dec 2010, Aaron Gray wrote: The compiler is requesting an instance declaration for Show :- expr-eval.hs:334:23:
No instance for (Show ([(String, Int)] -> Int))
arising from a use of `print' at expr-eval.hs:334:23-27
Possible fix:
add an instance declaration for (Show ([(String, Int)] -> Int))
In the first argument of `(.)', namely `print'
In the second argument of `(>>=)', namely `print . calc . lexer'
In the expression: getContents >>= print . calc . lexer ... maybe 'calc' needs a further argument? I have attached what I have typed in so far.
Aaron

On Friday 24 December 2010 22:47:55, Aaron Gray wrote:
On 24 December 2010 18:24, Henning Thielemann
wrote:
On Fri, 24 Dec 2010, Aaron Gray wrote:
The compiler is requesting an instance declaration for Show :-
expr-eval.hs:334:23: No instance for (Show ([(String, Int)] -> Int)) arising from a use of `print' at expr-eval.hs:334:23-27 Possible fix: add an instance declaration for (Show ([(String, Int)] -> Int)) In the first argument of `(.)', namely `print' In the second argument of `(>>=)', namely `print . calc . lexer' In the expression: getContents >>= print . calc . lexer
... maybe 'calc' needs a further argument?
I have attached what I have typed in so far.
Well, *ExprEval> :t calc calc :: [Token] -> [(String, Int)] -> Int calc needs an environment (a dictionary of let-bound names), which you have to provide. main = print . flip calc [] . lexer works fine.
Aaron

On 24 December 2010 22:07, Daniel Fischer
On Friday 24 December 2010 22:47:55, Aaron Gray wrote:
On 24 December 2010 18:24, Henning Thielemann
wrote:
On Fri, 24 Dec 2010, Aaron Gray wrote:
The compiler is requesting an instance declaration for Show :-
expr-eval.hs:334:23: No instance for (Show ([(String, Int)] -> Int)) arising from a use of `print' at expr-eval.hs:334:23-27 Possible fix: add an instance declaration for (Show ([(String, Int)] -> Int)) In the first argument of `(.)', namely `print' In the second argument of `(>>=)', namely `print . calc . lexer' In the expression: getContents >>= print . calc . lexer
... maybe 'calc' needs a further argument?
I have attached what I have typed in so far.
Well,
*ExprEval> :t calc calc :: [Token] -> [(String, Int)] -> Int
calc needs an environment (a dictionary of let-bound names), which you have to provide.
main = print . flip calc [] . lexer
works fine.
Okay great, works this end too, but what does the 'flip' do ??? Now to get it to work as a REPL and to read from file :) Aaron

On Saturday 25 December 2010 00:32:38, Aaron Gray wrote:
Okay great, works this end too, but what does the 'flip' do ???
It flips the order of arguments to calc. You could also write main = getContents >>= print . (`calc` []) . lexer Generally, flip f = \x y -> f y x or flip f x = \y -> f y x flip f x y = f y x

On 24 December 2010 23:58, Daniel Fischer
On Saturday 25 December 2010 00:32:38, Aaron Gray wrote:
Okay great, works this end too, but what does the 'flip' do ???
It flips the order of arguments to calc. You could also write
main = getContents >>= print . (`calc` []) . lexer
Generally,
flip f = \x y -> f y x
or
flip f x = \y -> f y x
flip f x y = f y x
Thanks Daniel, Aaron

On 24 December 2010 23:58, Daniel Fischer
On Saturday 25 December 2010 00:32:38, Aaron Gray wrote:
Okay great, works this end too, but what does the 'flip' do ???
It flips the order of arguments to calc. You could also write
main = getContents >>= print . (`calc` []) . lexer
Generally,
flip f = \x y -> f y x
or
flip f x = \y -> f y x
flip f x y = f y x
Or :- calcExpr :: [Token] -> Int calcExpr tokens = calc tokens [] process = print . calcExpr . lexer makes things clearer. Aaron
participants (3)
-
Aaron Gray
-
Daniel Fischer
-
Henning Thielemann