on master help sign in
Provide a show instance for Patterns.
commit001d620f3eec6efdc539cc1eb8294d99ec006854 authorevancz <info@elm-lang.org> dateSun 10 Jun 2012, 01:48 (14 years ago) parentf710b07
2 files changed+19−2
elm/src/Ast.hs+18−1 elm/src/Parse/Parser.hs+1−1
elm/src/Ast.hs+18−1
@@ -1,8 +1,11 @@
11
22module Ast where
33
4+import Data.Char (isDigit)
5+import Data.List (intercalate)
6+
47data Pattern = PData String [Pattern] | PVar String | PAnything
5− deriving (Show, Eq)
8+ deriving (Eq)
69
710data Expr = Number Int
811 | Chr Char
@@ -37,3 +40,17 @@ pcons h t = PData "Cons" [h,t]
3740pnil = PData "Nil" []
3841plist = foldr pcons pnil
3942ptuple 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
5454term = choice [ numTerm, strTerm, chrTerm
5555 , accessible varTerm
5656 , listTerm, parensTerm ]
57− <?> "basic term (number, variable, etc.)"
57+ <?> "basic term (4, x, 'c', etc.)"
5858
5959-------- Applications --------
6060