on master help sign in
Provide a show instance for Patterns.
001d620evancz
elm/src/Ast.hs+18−1
@@ -1,8 +1,11 @@
1
1
2module Ast where
2module Ast where
3
3
4+import Data.Char (isDigit)
5+import Data.List (intercalate)
6+
4data Pattern = PData String [Pattern] | PVar String | PAnything
7data Pattern = PData String [Pattern] | PVar String | PAnything
5− deriving (Show, Eq)
8+ deriving (Eq)
6
9
7data Expr = Number Int
10data Expr = Number Int
8 | Chr Char
11 | Chr Char
@@ -37,3 +40,17 @@ pcons h t = PData "Cons" [h,t]
37pnil = PData "Nil" []
40pnil = PData "Nil" []
38plist = foldr pcons pnil
41plist = foldr pcons pnil
39ptuple es = PData ("Tuple" ++ show (length es)) es
42ptuple es = PData ("Tuple" ++ show (length es)) es
43+
44+parens s = "(" ++ s ++ ")"
45+
46+instance Show Pattern where
47+ show (PVar x) = x
48+ show PAnything = "_"
49+ show (PData "Cons" [hd@(PData "Cons" _),tl]) =
50+ parens (show hd) ++ " : " ++ show tl
51+ show (PData "Cons" [hd,tl]) = show hd ++ " : " ++ show tl
52+ show (PData "Nil" []) = "[]"
53+ show (PData name ps) =
54+ if take 5 name == "Tuple" && all isDigit (drop 5 name) then
55+ parens . intercalate ", " $ map show ps
56+ else (if null ps then id else parens) $ unwords (name : map show ps)
elm/src/Parse/Parser.hs+1−1
@@ -54,7 +54,7 @@ parensTerm = parens $ choice
54term = choice [ numTerm, strTerm, chrTerm
54term = choice [ numTerm, strTerm, chrTerm
55 , accessible varTerm
55 , accessible varTerm
56 , listTerm, parensTerm ]
56 , listTerm, parensTerm ]
57− <?> "basic term (number, variable, etc.)"
57+ <?> "basic term (4, x, 'c', etc.)"
58
58
59-------- Applications --------
59-------- Applications --------
60
60