on master help sign in
Update a couple Type/ modules for AST shuffle
commit6e2f87e7c2581d645e5cc5d4a475c2a7d3e5f170 authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 8 Nov 2017, 19:49 (9 years ago) parente34add9
4 files changed+70−62
src/Type/Constrain/Effects.hs+2−2 src/Type/Constrain/Pattern.hs+1−1 src/Type/Instantiate.hs+21−21 src/Type/Type.hs+46−38
src/Type/Constrain/Effects.hs+2−2
@@ -9,7 +9,7 @@ module Type.Constrain.Effects
99import Control.Monad (replicateM)
1010import qualified Data.Map.Strict as Map
1111
12−import qualified AST.Expression.Canonical as Can
12+import qualified AST.Canonical as Can
1313import qualified AST.Module.Name as ModuleName
1414import qualified AST.Type as T
1515import qualified Elm.Name as N
@@ -122,7 +122,7 @@ constrainEffects r0 r1 r2 toFinalArgs =
122122 , CEqual Error.SelfMsg r2 (VarN selfMsg1) (VarN selfMsg2)
123123 ]
124124
125− return $ CLet [ Scheme [] vars (CAnd constrains) Map.empty ] CSaveEnv
125+ return $ CLet [ Scheme [] vars Map.empty (CAnd constrains) ] CSaveEnv
126126
127127
128128{-# NOINLINE never #-}
src/Type/Constrain/Pattern.hs+1−1
@@ -10,7 +10,7 @@ import Control.Arrow (second)
1010import Control.Monad (foldM)
1111import qualified Data.Map.Strict as Map
1212
13−import qualified AST.Expression.Canonical as Can
13+import qualified AST.Canonical as Can
1414import qualified AST.Module.Name as ModuleName
1515import qualified Data.Bag as Bag
1616import qualified Data.Index as Index
src/Type/Instantiate.hs+21−21
@@ -11,7 +11,7 @@ module Type.Instantiate
1111import qualified Data.Map.Strict as Map
1212import Data.Map.Strict ((!))
1313
14−import qualified AST.Type as T
14+import qualified AST.Canonical as Can
1515import qualified Elm.Name as N
1616import Type.Type
1717
@@ -22,7 +22,7 @@ import Type.Type
2222
2323-- TODO should the freeFlexVars be ranked a certain way?
2424-- How does this interact with `TS.flatten` exactly?
25−flexible :: T.Canonical -> IO ( Type, Map.Map N.Name Variable )
25+flexible :: Can.Type -> IO ( Type, Map.Map N.Name Variable )
2626flexible srcType =
2727 do let freeVars = gatherFreeVars srcType Map.empty
2828 freeFlexVars <- Map.traverseWithKey (\name () -> nameToFlex name) freeVars
@@ -30,7 +30,7 @@ flexible srcType =
3030 return ( tipe, freeFlexVars )
3131
3232
33−rigid :: T.Canonical -> IO ( Type, Map.Map N.Name Variable )
33+rigid :: Can.Type -> IO ( Type, Map.Map N.Name Variable )
3434rigid srcType =
3535 do let freeVars = gatherFreeVars srcType Map.empty
3636 freeRigidVars <- Map.traverseWithKey (\name () -> nameToRigid name) freeVars
@@ -42,22 +42,22 @@ rigid srcType =
4242-- GATHER FREE VARS
4343
4444
45−gatherFreeVars :: T.Canonical -> Map.Map N.Name () -> Map.Map N.Name ()
45+gatherFreeVars :: Can.Type -> Map.Map N.Name () -> Map.Map N.Name ()
4646gatherFreeVars tipe dict =
4747 case tipe of
48− T.Lambda arg result ->
48+ Can.TLambda arg result ->
4949 gatherFreeVars result (gatherFreeVars arg dict)
5050
51− T.Var name ->
51+ Can.TVar name ->
5252 Map.insert name () dict
5353
54− T.Type _ _ args ->
54+ Can.TType _ _ args ->
5555 foldr gatherFreeVars dict args
5656
57− T.Aliased _ _ args _ ->
57+ Can.TAlias _ _ args _ ->
5858 foldr gatherFreeVars dict (map snd args)
5959
60− T.Tuple a b maybeC ->
60+ Can.TTuple a b maybeC ->
6161 gatherFreeVars a $ gatherFreeVars b $
6262 case maybeC of
6363 Nothing ->
@@ -66,10 +66,10 @@ gatherFreeVars tipe dict =
6666 Just c ->
6767 gatherFreeVars c dict
6868
69− T.Unit ->
69+ Can.TUnit ->
7070 dict
7171
72− T.Record fields maybeExt ->
72+ Can.TRecord fields maybeExt ->
7373 case maybeExt of
7474 Nothing ->
7575 Map.foldr gatherFreeVars dict fields
@@ -82,40 +82,40 @@ gatherFreeVars tipe dict =
8282-- FROM SOURCE TYPE
8383
8484
85−fromSrcType :: Map.Map N.Name Type -> T.Canonical -> IO Type
85+fromSrcType :: Map.Map N.Name Type -> Can.Type -> IO Type
8686fromSrcType freeVars sourceType =
8787 case sourceType of
88− T.Lambda arg result ->
88+ Can.TLambda arg result ->
8989 FunN
9090 <$> fromSrcType freeVars arg
9191 <*> fromSrcType freeVars result
9292
93− T.Var name ->
93+ Can.TVar name ->
9494 return (freeVars ! name)
9595
96− T.Type home name args ->
96+ Can.TType home name args ->
9797 AppN home name <$> traverse (fromSrcType freeVars) args
9898
99− T.Aliased home name args aliasedType ->
99+ Can.TAlias home name args aliasedType ->
100100 do targs <- traverse (traverse (fromSrcType freeVars)) args
101101 AliasN home name targs <$>
102102 case aliasedType of
103− T.Filled realType ->
103+ Can.Filled realType ->
104104 fromSrcType freeVars realType
105105
106− T.Holey realType ->
106+ Can.Holey realType ->
107107 fromSrcType (Map.fromList targs) realType
108108
109− T.Tuple a b maybeC ->
109+ Can.TTuple a b maybeC ->
110110 TupleN
111111 <$> fromSrcType freeVars a
112112 <*> fromSrcType freeVars b
113113 <*> traverse (fromSrcType freeVars) maybeC
114114
115− T.Unit ->
115+ Can.TUnit ->
116116 return UnitN
117117
118− T.Record fields maybeExt ->
118+ Can.TRecord fields maybeExt ->
119119 RecordN
120120 <$> traverse (fromSrcType freeVars) fields
121121 <*>
src/Type/Type.hs+46−38
@@ -35,8 +35,9 @@ import qualified Data.Text as Text
3535import Data.Text (Text)
3636import Data.Word (Word32)
3737
38+import qualified AST.Canonical as Can
3839import qualified AST.Module.Name as ModuleName
39−import qualified AST.Type as T
40+import qualified AST.Utils.Type as Type
4041import qualified Elm.Name as N
4142import qualified Type.UnionFind as UF
4243
@@ -85,14 +86,21 @@ data Descriptor =
8586
8687data Content
8788 = FlexVar (Maybe N.Name)
88− | FlexSuper T.Super (Maybe N.Name)
89+ | FlexSuper SuperType (Maybe N.Name)
8990 | RigidVar N.Name
90− | RigidSuper T.Super N.Name
91+ | RigidSuper SuperType N.Name
9192 | Structure FlatType
9293 | Alias ModuleName.Canonical N.Name [(N.Name,Variable)] Variable
9394 | Error Text
9495
9596
97+data SuperType
98+ = Number
99+ | Comparable
100+ | Appendable
101+ | CompAppend
102+
103+
96104makeDescriptor :: Content -> Descriptor
97105makeDescriptor content =
98106 Descriptor content noRank noMark Nothing
@@ -245,10 +253,10 @@ mkFlexNumber =
245253{-# NOINLINE flexNumberDescriptor #-}
246254flexNumberDescriptor :: Descriptor
247255flexNumberDescriptor =
248− makeDescriptor (unnamedFlexSuper T.Number)
256+ makeDescriptor (unnamedFlexSuper Number)
249257
250258
251−unnamedFlexSuper :: T.Super -> Content
259+unnamedFlexSuper :: SuperType -> Content
252260unnamedFlexSuper super =
253261 FlexSuper super Nothing
254262
@@ -269,19 +277,19 @@ nameToRigid name =
269277 maybe RigidVar RigidSuper (toSuper name) name
270278
271279
272−toSuper :: N.Name -> Maybe T.Super
280+toSuper :: N.Name -> Maybe SuperType
273281toSuper name =
274282 if Text.isPrefixOf "number" name then
275− Just T.Number
283+ Just Number
276284
277285 else if Text.isPrefixOf "comparable" name then
278− Just T.Comparable
286+ Just Comparable
279287
280288 else if Text.isPrefixOf "appendable" name then
281− Just T.Appendable
289+ Just Appendable
282290
283291 else if Text.isPrefixOf "compappend" name then
284− Just T.CompAppend
292+ Just CompAppend
285293
286294 else
287295 Nothing
@@ -293,19 +301,19 @@ toSuper name =
293301
294302-- TODO: Attach resulting type to the descriptor so that you
295303-- never have to do extra work, particularly nice for aliased types
296−toSrcType :: Variable -> IO T.Canonical
304+toSrcType :: Variable -> IO Can.Type
297305toSrcType variable =
298306 do takenNames <- getVarNames variable Map.empty
299307 State.evalStateT (variableToSrcType variable) (makeNameState takenNames)
300308
301309
302−variableToSrcType :: Variable -> StateT NameState IO T.Canonical
310+variableToSrcType :: Variable -> StateT NameState IO Can.Type
303311variableToSrcType variable =
304312 do descriptor <- liftIO $ UF.descriptor variable
305313 let mark = _mark descriptor
306314 if mark == occursMark
307315 then
308− return (T.Var "∞")
316+ return (Can.TVar "∞")
309317
310318 else
311319 do liftIO $ UF.modifyDescriptor variable (\desc -> desc { _mark = occursMark })
@@ -314,7 +322,7 @@ variableToSrcType variable =
314322 return srcType
315323
316324
317−contentToSrcType :: Variable -> Content -> StateT NameState IO T.Canonical
325+contentToSrcType :: Variable -> Content -> StateT NameState IO Can.Type
318326contentToSrcType variable content =
319327 case content of
320328 Structure term ->
@@ -323,71 +331,71 @@ contentToSrcType variable content =
323331 FlexVar maybeName ->
324332 case maybeName of
325333 Just name ->
326− return (T.Var name)
334+ return (Can.TVar name)
327335
328336 Nothing ->
329337 do name <- getFreshVarName
330338 liftIO $ UF.modifyDescriptor variable (\desc -> desc { _content = FlexVar (Just name) })
331− return (T.Var name)
339+ return (Can.TVar name)
332340
333341 FlexSuper super maybeName ->
334342 case maybeName of
335343 Just name ->
336− return (T.Var name)
344+ return (Can.TVar name)
337345
338346 Nothing ->
339347 do name <- getFreshSuperName super
340348 liftIO $ UF.modifyDescriptor variable (\desc -> desc { _content = FlexSuper super (Just name) })
341− return (T.Var name)
349+ return (Can.TVar name)
342350
343351 RigidVar name ->
344− return (T.Var name)
352+ return (Can.TVar name)
345353
346354 RigidSuper _ name ->
347− return (T.Var name)
355+ return (Can.TVar name)
348356
349357 Alias home name args realVariable ->
350358 do srcArgs <- traverse (traverse variableToSrcType) args
351359 srcType <- variableToSrcType realVariable
352− return (T.Aliased home name srcArgs (T.Filled srcType))
360+ return (Can.TAlias home name srcArgs (Can.Filled srcType))
353361
354362 Error name ->
355− return (T.Var name)
363+ return (Can.TVar name)
356364
357365
358−termToSrcType :: FlatType -> StateT NameState IO T.Canonical
366+termToSrcType :: FlatType -> StateT NameState IO Can.Type
359367termToSrcType term =
360368 case term of
361369 App1 home name args ->
362− T.Type home name <$> traverse variableToSrcType args
370+ Can.TType home name <$> traverse variableToSrcType args
363371
364372 Fun1 a b ->
365− T.Lambda
373+ Can.TLambda
366374 <$> variableToSrcType a
367375 <*> variableToSrcType b
368376
369377 EmptyRecord1 ->
370− return $ T.Record Map.empty Nothing
378+ return $ Can.TRecord Map.empty Nothing
371379
372380 Record1 fields extension ->
373381 do srcFields <- traverse variableToSrcType fields
374− srcExt <- T.iteratedDealias <$> variableToSrcType extension
382+ srcExt <- Type.iteratedDealias <$> variableToSrcType extension
375383 return $
376384 case srcExt of
377− T.Record subFields subExt ->
378− T.Record (Map.union subFields srcFields) subExt
385+ Can.TRecord subFields subExt ->
386+ Can.TRecord (Map.union subFields srcFields) subExt
379387
380− T.Var _ ->
381− T.Record srcFields (Just srcExt)
388+ Can.TVar _ ->
389+ Can.TRecord srcFields (Just srcExt)
382390
383391 _ ->
384392 error "Used toSrcType on a type that is not well-formed"
385393
386394 Unit1 ->
387− return T.Unit
395+ return Can.TUnit
388396
389397 Tuple1 a b maybeC ->
390− T.Tuple
398+ Can.TTuple
391399 <$> variableToSrcType a
392400 <*> variableToSrcType b
393401 <*> traverse variableToSrcType maybeC
@@ -425,19 +433,19 @@ getFreshVarName =
425433 return uniqueName
426434
427435
428−getFreshSuperName :: (Monad m) => T.Super -> StateT NameState m Text
436+getFreshSuperName :: (Monad m) => SuperType -> StateT NameState m Text
429437getFreshSuperName super =
430438 case super of
431− T.Number ->
439+ Number ->
432440 getFreshSuper "number" _numbers (\index state -> state { _numbers = index })
433441
434− T.Comparable ->
442+ Comparable ->
435443 getFreshSuper "comparable" _comparables (\index state -> state { _comparables = index })
436444
437− T.Appendable ->
445+ Appendable ->
438446 getFreshSuper "appendable" _appendables (\index state -> state { _appendables = index })
439447
440− T.CompAppend ->
448+ CompAppend ->
441449 getFreshSuper "compappend" _compAppends (\index state -> state { _compAppends = index })
442450
443451