@@ -1,7 +1,7 @@
11{-# OPTIONS_GHC -Wall #-}
22module Canonicalize.Pattern
3− ( canonicalizeArgs
4− , canonicalizeMatch
3+ ( canonicalize
4+ , canonicalizeArgs
55 , canonicalizeBindings
66 , Binding(..)
77 )
@@ -35,51 +35,50 @@ type Result i =
3535
3636
3737
38−-- CANONICALIZE ARGS
38+-- CANONICALIZE
3939
4040
41−canonicalizeArgs :: Env.Env -> [Src.Pattern] -> Result () Can.Args
42−canonicalizeArgs env patterns =
43− case Index.indexedTraverse (canonicalizeArg env) patterns of
44− Result.Result bag warnings (Result.Ok args) ->
41+canonicalize :: Env.Env -> Index.ZeroBased -> Src.Pattern -> Result () (Can.Pattern, Can.Destructors)
42+canonicalize env index pattern =
43+ case process env (Can.DRoot index) pattern of
44+ Result.Result bag warnings (Result.Ok cpattern) ->
4545 do let toError name () () = Error.DuplicateArg name
4646 destructors <- Dups.detect toError (Bag.toList bag)
47− Result.Result () warnings $ Result.Ok $
48− Can.Args args destructors
47+ Result.Result () warnings $ Result.Ok (cpattern, destructors)
4948
5049 Result.Result _ warnings (Result.Err err) ->
5150 Result.Result () warnings (Result.Err err)
5251
5352
54−canonicalizeArg :: Env.Env -> Index.ZeroBased -> Src.Pattern -> Result Bag Can.Arg
55−canonicalizeArg env index pattern =
56− Can.Arg index <$> canonicalize env (Can.DRoot index) pattern
57−
58−
5953
60−-- CANONICALIZE MATCH
54+-- CANONICALIZE ARGS
6155
6256
63−canonicalizeMatch :: Env.Env -> Src.Pattern -> Result () Can.Match
64−canonicalizeMatch env pattern =
65− case canonicalize env (Can.DRoot Index.first) pattern of
66− Result.Result bag warnings (Result.Ok cpattern) ->
57+canonicalizeArgs :: Env.Env -> [Src.Pattern] -> Result () Can.Args
58+canonicalizeArgs env patterns =
59+ case Index.indexedTraverse (canonicalizeArg env) patterns of
60+ Result.Result bag warnings (Result.Ok args) ->
6761 do let toError name () () = Error.DuplicateArg name
6862 destructors <- Dups.detect toError (Bag.toList bag)
6963 Result.Result () warnings $ Result.Ok $
70− Can.Match cpattern destructors
64+ Can.Args args destructors
7165
7266 Result.Result _ warnings (Result.Err err) ->
7367 Result.Result () warnings (Result.Err err)
7468
7569
70+canonicalizeArg :: Env.Env -> Index.ZeroBased -> Src.Pattern -> Result Bag Can.Arg
71+canonicalizeArg env index pattern =
72+ Can.Arg index <$> process env (Can.DRoot index) pattern
73+
74+
7675
7776-- CANONICALIZE BINDINGS
7877
7978
8079data Binding
8180 = Define R.Region Int (A.Located N.Name) [Src.Pattern] Valid.Expr (Maybe Src.Type)
82− | Destruct R.Region Int Can.Match Valid.Expr
81+ | Destruct R.Region Int Can.Pattern Can.Destructors Valid.Expr
8382
8483
8584canonicalizeBindings :: Env.Env -> [Valid.Def] -> Result () ( [Binding], Map.Map N.Name (A.Located Int) )
@@ -107,12 +106,12 @@ canonicalizeBinding env def index =
107106 Define region index aname args body maybeType
108107
109108 Valid.Destruct region pattern body ->
110− case canonicalize env (Can.DRoot Index.first) pattern of
109+ case process env (Can.DRoot Index.first) pattern of
111110 Result.Result bag warnings (Result.Ok cpattern) ->
112111 do let toError name () () = Error.DuplicateBindingName name
113112 destructors <- Result.untracked (Dups.detect toError (Bag.toList bag))
114113 Result.Result (toKeyBag index bag) warnings $ Result.Ok $
115− Destruct region index (Can.Match cpattern destructors) body
114+ Destruct region index cpattern destructors body
116115
117116 Result.Result bag warnings (Result.Err err) ->
118117 Result.Result (toKeyBag index bag) warnings (Result.Err err)
@@ -138,8 +137,8 @@ type Bag =
138137 Bag.Bag (N.Name, [Dups.Info () (A.Located Can.Destructor)])
139138
140139
141−canonicalize :: Env.Env -> Can.Destructor -> Src.Pattern -> Result Bag Can.Pattern
142−canonicalize env destructor (A.A region pattern) =
140+process :: Env.Env -> Can.Destructor -> Src.Pattern -> Result Bag Can.Pattern
141+process env destructor (A.A region pattern) =
143142 A.A region <$>
144143 case pattern of
145144 Src.PAnything ->
@@ -164,15 +163,15 @@ canonicalize env destructor (A.A region pattern) =
164163
165164 Src.PTuple a b cs ->
166165 Can.PTuple
167− <$> canonicalize env (Can.DIndex Index.first destructor) a
168− <*> canonicalize env (Can.DIndex Index.second destructor) b
169− <*> canonicalizeTuple region env destructor cs
166+ <$> process env (Can.DIndex Index.first destructor) a
167+ <*> process env (Can.DIndex Index.second destructor) b
168+ <*> processTuple region env destructor cs
170169
171170 Src.PCtor nameRegion maybePrefix name patterns ->
172171 let
173172 toCanonicalArg index ptrn tipe =
174173 Can.PatternCtorArg index tipe
175− <$> canonicalize env (Can.DIndex index destructor) ptrn
174+ <$> process env (Can.DIndex index destructor) ptrn
176175 in
177176 do (Env.Pattern home tipe vars args) <- Env.findPattern nameRegion env maybePrefix name
178177 verifiedList <- Index.indexedZipWithA toCanonicalArg patterns args
@@ -184,15 +183,15 @@ canonicalize env destructor (A.A region pattern) =
184183 Result.throw region (error "TODO" actualLength expectedLength)
185184
186185 Src.PList patterns ->
187− Can.PList <$> canonicalizeList env destructor patterns
186+ Can.PList <$> processList env destructor patterns
188187
189188 Src.PCons first rest ->
190189 Can.PCons
191− <$> canonicalize env (Can.DIndex Index.first destructor) first
192− <*> canonicalize env (Can.DIndex Index.second destructor) rest
190+ <$> process env (Can.DIndex Index.first destructor) first
191+ <*> process env (Can.DIndex Index.second destructor) rest
193192
194193 Src.PAlias ptrn (A.A reg name) ->
195− do cpattern <- canonicalize env destructor ptrn
194+ do cpattern <- process env destructor ptrn
196195 let info = Dups.info name reg () (A.A reg destructor)
197196 Result.accumulate (Bag.one info) (Can.PAlias cpattern name)
198197
@@ -206,27 +205,27 @@ canonicalize env destructor (A.A region pattern) =
206205 Result.ok (Can.PInt int)
207206
208207
209−canonicalizeTuple :: R.Region -> Env.Env -> Can.Destructor -> [Src.Pattern] -> Result Bag (Maybe Can.Pattern)
210−canonicalizeTuple tupleRegion env destructor extras =
208+processTuple :: R.Region -> Env.Env -> Can.Destructor -> [Src.Pattern] -> Result Bag (Maybe Can.Pattern)
209+processTuple tupleRegion env destructor extras =
211210 case extras of
212211 [] ->
213212 Result.ok Nothing
214213
215214 [three] ->
216− Just <$> canonicalize env (Can.DIndex Index.third destructor) three
215+ Just <$> process env (Can.DIndex Index.third destructor) three
217216
218217 _ : others ->
219218 let (A.A r1 _, A.A r2 _) = (head others, last others) in
220219 Result.throw tupleRegion (Error.TupleLargerThanThree (R.merge r1 r2))
221220
222221
223−canonicalizeList :: Env.Env -> Can.Destructor -> [Src.Pattern] -> Result Bag [Can.Pattern]
224−canonicalizeList env destructor list =
222+processList :: Env.Env -> Can.Destructor -> [Src.Pattern] -> Result Bag [Can.Pattern]
223+processList env destructor list =
225224 case list of
226225 [] ->
227226 Result.ok []
228227
229228 pattern : otherPatterns ->
230229 (:)
231− <$> canonicalize env (Can.DIndex Index.first destructor) pattern
232− <*> canonicalizeList env (Can.DIndex Index.second destructor) otherPatterns
230+ <$> process env (Can.DIndex Index.first destructor) pattern
231+ <*> processList env (Can.DIndex Index.second destructor) otherPatterns