on 5fab52a help sign in
elk/src/AST/Pattern.hs
112 lines 1.8 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module AST.Pattern
4 ( Raw
5 , Raw_(..)
6 , Canonical
7 , Canonical_(..)
8 , boundVars
9 , member
10 , boundVarSet
11 , boundVarList
12 )
13 where
16import qualified Data.Set as Set
17import Data.Text (Text)
19import qualified AST.Literal as L
20import qualified AST.Variable as Var
21import qualified Reporting.Annotation as A
25-- RAW
28type Raw = A.Located Raw_
31data Raw_
32 = RCtor (Maybe Text) Text [Raw]
33 | RRecord [Text]
34 | RUnit
35 | RTuple Raw Raw [Raw]
36 | RList [Raw]
37 | RCons Raw Raw
38 | RAlias Text Raw
39 | RVar Text
40 | RAnything
41 | RLiteral L.Literal
45-- CANONICAL
48type Canonical = A.Located Canonical_
51data Canonical_
52 = Ctor Var.Canonical [Canonical]
53 | Record [Text]
54 | Unit
55 | Tuple Canonical Canonical [Canonical]
56 | Alias Text Canonical
57 | Var Text
58 | Anything
59 | Literal L.Literal
63-- FIND VARIABLES
66boundVars :: Raw -> [A.Located Text]
67boundVars (A.A region pattern) =
68 case pattern of
69 RVar name ->
70 [ A.A region name ]
72 RAlias name realPattern ->
73 A.A region name : boundVars realPattern
75 RCtor _ _ patterns ->
76 concatMap boundVars patterns
78 RRecord fields ->
79 map (A.A region) fields
81 RUnit ->
82 []
84 RTuple a b cs ->
85 concatMap boundVars (a:b:cs)
87 RList xs ->
88 concatMap boundVars xs
90 RCons a b ->
91 boundVars a ++ boundVars b
93 RAnything ->
94 []
96 RLiteral _ ->
97 []
100member :: Text -> Raw -> Bool
101member name pattern =
102 elem name (map A.drop (boundVars pattern))
105boundVarSet :: Raw -> Set.Set Text
106boundVarSet pattern =
107 Set.fromList (map A.drop (boundVars pattern))
110boundVarList :: Raw -> [Text]
111boundVarList pattern =
112 Set.toList (boundVarSet pattern)