1{-# OPTIONS_GHC -Wall #-}
2module SourceSyntax.Pattern where
4import qualified SourceSyntax.Helpers as Help
5import SourceSyntax.PrettyPrint
6import Text.PrettyPrint as PP
7import qualified Data.Set as Set
8import SourceSyntax.Literal as Literal
11 = Data String [Pattern]
13 | Alias String Pattern
16 | Literal Literal.Literal
17 deriving (Eq, Ord, Show)
19cons :: Pattern -> Pattern -> Pattern
20cons h t = Data "::" [h,t]
25list :: [Pattern] -> Pattern
28tuple :: [Pattern] -> Pattern
29tuple es = Data ("_Tuple" ++ show (length es)) es
31boundVarList :: Pattern -> [String]
32boundVarList = Set.toList . boundVars
34boundVars :: Pattern -> Set.Set String
37 Var x -> Set.singleton x
38 Alias x p -> Set.insert x (boundVars p)
39 Data _ ps -> Set.unions (map boundVars ps)
40 Record fields -> Set.fromList fields
42 Literal _ -> Set.empty
45instance Pretty Pattern where
49 Literal lit -> pretty lit
50 Record fs -> PP.braces (commaCat $ map variable fs)
51 Alias x p -> prettyParens p <+> PP.text "as" <+> variable x
52 Anything -> PP.text "_"
53 Data "::" [hd,tl] -> parensIf isCons (pretty hd) <+> PP.text "::" <+> pretty tl
54 where isCons = case hd of
58 if Help.isTuple name then
59 PP.parens . commaCat $ map pretty ps
60 else hsep (PP.text name : map prettyParens ps)
62prettyParens :: Pattern -> Doc
63prettyParens pattern = parensIf needsThem (pretty pattern)
67 Data name (_:_) | not (Help.isTuple name) -> True
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list