vikingr12
vikingr12

Reputation: 23

Haskell Function returning its input

I'm currently writing a function for substitution (Lambda Calculus) based in Haskell.

data Term = Var String 
          | Application Term Term
          | Lambda String Term

The function takes in a Term t1, String s and a Term t2. The function should replace every occurrence of s in t1 with t2.

subst :: Term -> String -> Term -> Term
subst (Lambda y e) x v = if x == y then (Lambda y e) else let e' = (subst e x v) in (Lambda y e')
subst (Var y) x v = if x == y then v else (Var y)
subst (Application e1 e2) x v = Application (subst e1 x v) (subst e2 x v)

When I try to call the function, I receive the following:

Input

 subst (Lambda "x" (Application (Var "x") (Var "y"))) "y" (Var "f")

Output

subst (Lambda "x" (Application (Var "x") (Var "y"))) "y" (Var "f")
  :: Term

Am I making an input error or is there a problem with my function?

Upvotes: 1

Views: 198

Answers (1)

amalloy
amalloy

Reputation: 91837

The problem is that your Term type lacks a Show instance, and cannot be shown. The interpreter is telling you that the expression foo yielded a result of type SomeType, but it can't print any more details than that because SomeType does not support Show. Your function has therefore apparently returned some value successfully, but you don't know what value. You could inspect it by pattern-matching on the result. But you probably just want to add deriving Show to the type definition, so that the repl can print the value it got.

Now, the most popular repl, GHCI, doesn't normally behave this way: if you try to show a value without a Show instance, you just get an error. But some other GHCI-like tools do this compromise of printing the expression again and its type. One that I know of is interactive-haskell-mode in Emacs; there may well be others.

Upvotes: 1

Related Questions