[Haskell-beginners] Haskell interpretation of names produces error?

trent shipley trent.shipley at gmail.com
Fri Sep 21 10:26:02 UTC 2018


data Prop = Const Bool
          | Var Char
          | Not Prop
          | And Prop Prop
          | If Prop Prop
          | Or Prop Prop
          | Yff Prop Prop -- Why do I get errors if "Yff" is replaced with
"Iff"?
          | Xor Prop Prop

type Assoc k v = [(k, v)]

-- Hutton, Graham. Programming in Haskell (p. 93). Cambridge University
Press. Kindle Edition.

find :: Eq k => k -> Assoc k v -> v
find k t = head [v | (k', v) <- t, k == k']

-- Hutton, Graham. Programming in Haskell (p. 93). Cambridge University
Press. Kindle Edition.

type Subst = Assoc Char Bool

-- Hutton, 2016, Ch 8.6

eval :: Subst -> Prop -> Bool
eval _ (Const b) = b
eval s (Var x)   = find x s
eval s (Not p)   = not (eval s p)
eval s (And p q) = eval s p && eval s q
eval s (If p q)  = eval s p <= eval s q
eval s (Or p q)  = eval s p || eval s q
eval s (Yff p q) = eval s p == eval s q  -- Iff produces error here
eval s (Xor p q) = eval s p /= eval s q

-- Hutton 2016 Ch 8.6

vars :: Prop -> [Char]
vars (Const _)  = []
vars (Var x)    = [x]
vars (Not p)    = vars p
vars (And p q)  = vars p ++ vars q
vars (If p q)   = vars p ++ vars q
vars (Or p q)   = vars p ++ vars q
vars (Yff p q)  = vars p ++ vars q -- Iff produces error here
vars (Xor p q)  = vars p ++ vars q

-- Hutton 2016 Ch 8.6
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180921/b91846f9/attachment.html>


More information about the Beginners mailing list