on master help sign in
Upgrade Type code from String to Text
commitb09bb0012c7b67e9b117b2e1860f786f75fb2784 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 6 Dec 2016, 15:29 (10 years ago) parent5b4fbc7
15 files changed+251−222
src/AST/Literal.hs+9−11 src/AST/Module/Name.hs−10 src/AST/Variable.hs−10 src/Elm/Package.hs−10 src/Nitpick/TopLevelTypes.hs+4−3 src/Type/Constrain/Effects.hs+7−1 src/Type/Constrain/Expression.hs+19−15 src/Type/Constrain/Literal.hs+8−2 src/Type/Constrain/Pattern.hs+8−9 src/Type/Environment.hs+116−90 src/Type/Inference.hs+14−11 src/Type/Solve.hs+8−6 src/Type/State.hs+9−6 src/Type/Type.hs+37−29 src/Type/Unify.hs+12−9
src/AST/Literal.hs+9−11
@@ -8,8 +8,6 @@ import Data.Text.Lazy.Builder (Builder, fromString)
88import Data.Text.Lazy.Builder.Int (decimal)
99import Data.Text.Lazy.Builder.RealFloat (realFloat)
1010
11−import qualified AST.Variable as Var
12−
1311
1412
1513-- LITERALS
@@ -49,16 +47,16 @@ data GLType
4947 deriving (Eq)
5048
5149
52−glTypeToVar :: GLType -> Var.Canonical
53−glTypeToVar glTipe =
50+glTypeToText :: GLType -> Text
51+glTypeToText glTipe =
5452 case glTipe of
55− V2 -> Var.inLinearAlgebra "Math.Vector2" "Vec2"
56− V3 -> Var.inLinearAlgebra "Math.Vector3" "Vec3"
57− V4 -> Var.inLinearAlgebra "Math.Vector4" "Vec4"
58− M4 -> Var.inLinearAlgebra "Math.Matrix4" "Mat4"
59− Int -> Var.builtin "Int"
60− Float -> Var.builtin "Float"
61− Texture -> Var.inWebGL "WebGL" "Texture"
53+ V2 -> "Math.Vector2.Vec2"
54+ V3 -> "Math.Vector3.Vec3"
55+ V4 -> "Math.Vector4.Vec4"
56+ M4 -> "Math.Matrix4.Mat4"
57+ Int -> "Int"
58+ Float -> "Float"
59+ Texture -> "WebGL.Texture"
6260
6361
6462data Shader =
src/AST/Module/Name.hs−10
@@ -34,16 +34,6 @@ inHtml raw =
3434 Canonical Package.html raw
3535
3636
37−inWebGL :: Raw -> Canonical
38−inWebGL raw =
39− Canonical Package.webgl raw
40−
41−
42−inLinearAlgebra :: Raw -> Canonical
43−inLinearAlgebra raw =
44− Canonical Package.linearAlgebra raw
45−
46−
4737toString :: Raw -> String
4838toString =
4939 Text.unpack
src/AST/Variable.hs−10
@@ -80,16 +80,6 @@ inHtml home name =
8080 Canonical (Module (ModuleName.inHtml home)) name
8181
8282
83−inWebGL :: ModuleName.Raw -> Text -> Canonical
84−inWebGL home name =
85− Canonical (Module (ModuleName.inWebGL home)) name
86−
87−
88−inLinearAlgebra :: ModuleName.Raw -> Text -> Canonical
89−inLinearAlgebra home name =
90− Canonical (Module (ModuleName.inLinearAlgebra home)) name
91−
92−
9383cmd :: Canonical
9484cmd =
9585 inCore "Platform.Cmd" "Cmd"
src/Elm/Package.hs−10
@@ -47,16 +47,6 @@ html =
4747 Name "elm-lang" "html"
4848
4949
50−webgl :: Name
51−webgl =
52− Name "elm-community" "webgl"
53−
54−
55−linearAlgebra :: Name
56−linearAlgebra =
57− Name "elm-community" "linear-algebra"
58−
59−
6050toString :: Name -> String
6151toString name =
6252 Text.unpack (toText name)
src/Nitpick/TopLevelTypes.hs+4−3
@@ -6,6 +6,7 @@ import qualified Data.Foldable as F
66import Data.Map ((!))
77import qualified Data.Map as Map
88import qualified Data.Text as Text
9+import Data.Text (Text)
910
1011import qualified AST.Expression.Canonical as Can
1112import qualified AST.Module.Name as ModuleName
@@ -26,7 +27,7 @@ type Result =
2627
2728
2829type TypeDict =
29− Map.Map Var.Canonical Type.Canonical
30+ Map.Map Text Type.Canonical
3031
3132
3233
@@ -64,7 +65,7 @@ checkAnnotation typeEnv (Can.Def _ (A.A region pattern) _ maybeType) =
6465 (P.Var name, Nothing) ->
6566 let
6667 warning =
67− Warning.MissingTypeAnnotation (Text.unpack name) (typeEnv ! Var.local name)
68+ Warning.MissingTypeAnnotation (Text.unpack name) (typeEnv ! name)
6869 in
6970 Result.warn region warning ()
7071
@@ -80,7 +81,7 @@ checkMain :: TypeDict -> Can.Def -> Result Can.Def
8081checkMain typeEnv (Can.Def facts pattern@(A.A region _) body maybeType) =
8182 let
8283 mainType =
83− typeEnv ! Var.local "main"
84+ typeEnv ! "main"
8485
8586 makeError tipe maybeMsg =
8687 A.A region (Error.BadFlags tipe maybeMsg)
src/Type/Constrain/Effects.hs+7−1
@@ -1,7 +1,9 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Type.Constrain.Effects (constrain) where
34
45import qualified Data.Map as Map
6+import Data.Monoid ((<>))
57
68import qualified AST.Effects as Effects
79import qualified AST.Module.Name as ModuleName
@@ -14,6 +16,10 @@ import Type.Type
1416 )
1517
1618
19+
20+-- CONSTRAIN EFFECTS
21+
22+
1723constrain :: Env.Env -> ModuleName.Canonical -> Effects.Canonical -> IO TypeConstraint
1824constrain env moduleName effects =
1925 case effects of
@@ -88,7 +94,7 @@ addEffectArgs
8894addEffectArgs env moduleName managerType msg result =
8995 let
9096 toTypeName (A.A _ name) =
91− ModuleName.canonicalToString moduleName ++ "." ++ name
97+ ModuleName.canonicalToText moduleName <> "." <> name
9298
9399 effectList fxName =
94100 Env.getType env "List" <|
src/Type/Constrain/Expression.hs+19−15
@@ -1,9 +1,12 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Type.Constrain.Expression where
34
45import Control.Arrow (second)
56import qualified Control.Monad as Monad
67import qualified Data.Map as Map
8+import qualified Data.Text as Text
9+import Data.Text (Text)
710
811import qualified AST.Expression.Canonical as C
912import qualified AST.Literal as Lit
@@ -45,20 +48,20 @@ constrain env annotatedExpr@(A.A region expression) tipe =
4548 C.SaveEnv moduleName effects ->
4649 Effects.constrain env moduleName effects
4750
48− C.GLShader _uid _src gltipe ->
51+ C.GLShader _uid _src glType ->
4952 exists $ \attr ->
5053 exists $ \unif ->
5154 let
5255 shaderTipe a u v =
5356 Env.getType env "WebGL.Shader" <| a <| u <| v
5457
55− glToType glTipe =
56− Env.getType env (Lit.glTipeName glTipe)
58+ glToType gl =
59+ Env.getType env (Lit.glTypeToText gl)
5760
5861 makeRec accessor baseRec =
59− let decls = accessor gltipe
62+ let decls = accessor glType
6063 in
61− if Map.size decls == 0 then
64+ if Map.null decls then
6265 baseRec
6366 else
6467 record (Map.map glToType decls) baseRec
@@ -70,7 +73,7 @@ constrain env annotatedExpr@(A.A region expression) tipe =
7073 return (CEqual Error.Shader region (shaderTipe attribute uniform varying) tipe)
7174
7275 C.Var var ->
73− return (CInstance region (V.toString var) tipe)
76+ return (CInstance region (V.toText var) tipe)
7477
7578 C.List exprs ->
7679 constrainList env region exprs tipe
@@ -103,7 +106,7 @@ constrain env annotatedExpr@(A.A region expression) tipe =
103106 do vars <- Monad.forM exprs $ \_ -> mkVar Nothing
104107 let pairs = zip exprs (map VarN vars)
105108 (ctipe, cs) <- Monad.foldM step (tipe, CTrue) (reverse pairs)
106− return $ ex vars (cs /\ CInstance region name ctipe)
109+ return $ ex vars (cs /\ CInstance region (V.toText name) ctipe)
107110 where
108111 step (t,c) (e,x) =
109112 do c' <- constrain env e x
@@ -270,7 +273,7 @@ constrainBinop env region op leftExpr@(A.A leftRegion _) rightExpr@(A.A rightReg
270273 ex [leftVar,rightVar,leftVar',rightVar',answerVar] $ CAnd $
271274 [ leftCon
272275 , rightCon
273− , CInstance region (V.toString op) opType
276+ , CInstance region (V.toText op) opType
274277 , CEqual (Error.BinopLeft op leftRegion) region (VarN leftVar') (VarN leftVar)
275278 , CEqual (Error.BinopRight op rightRegion) region (VarN rightVar') (VarN rightVar)
276279 , CEqual (Error.Binop op) region (VarN answerVar) tipe
@@ -460,7 +463,7 @@ expandPattern def@(C.Def facts lpattern expr maybeType) =
460463 P.boundVarList lpattern
461464
462465 combinedName =
463− "$" ++ concat vars
466+ Text.intercalate "$" ("" : vars)
464467
465468 pvar name =
466469 A.A patternRegion (P.Var name)
@@ -483,11 +486,12 @@ expandPattern def@(C.Def facts lpattern expr maybeType) =
483486-- CONSTRAIN DEFINITIONS
484487
485488
486−data Info = Info
489+data Info =
490+ Info
487491 { iSchemes :: [TypeScheme]
488492 , iRigid :: [Variable]
489493 , iFlex :: [Variable]
490− , iHeaders :: Map.Map String (A.Located Type)
494+ , iHeaders :: Map.Map Text (A.Located Type)
491495 , iC2 :: TypeConstraint
492496 , iC1 :: TypeConstraint
493497 }
@@ -509,7 +513,7 @@ constrainDef env info (C.Def defRegion (A.A patternRegion pattern) expr maybeTip
509513constrainUnannotatedDef
510514 :: Env.Env
511515 -> R.Region
512− -> String
516+ -> Text
513517 -> C.Expr
514518 -> Info
515519 -> IO Info
@@ -528,7 +532,7 @@ constrainAnnotatedDef
528532 :: Env.Env
529533 -> R.Region
530534 -> R.Region
531− -> String
535+ -> Text
532536 -> C.Expr
533537 -> ST.Canonical
534538 -> Info
@@ -554,7 +558,7 @@ constrainAnnotatedDef env defRegion region name expr tipe info =
554558
555559data ArgInfo =
556560 ArgInfo
557− { _name :: String
561+ { _name :: Text
558562 , _def :: R.Region
559563 , _env :: Env.Env
560564 , _args :: [(P.Canonical, Type)]
@@ -594,7 +598,7 @@ constrainAnnDefHelp expr tipe (ArgInfo name defRegion env args vars) =
594598 let sharedArity = length args
595599 let typeArity = sharedArity + length (ST.collectLambdas tipe) - 1
596600 let argsArity = sharedArity + length (fst (C.collectLambdas expr))
597− let hint = Error.ReturnType name typeArity argsArity region
601+ let hint = Error.ReturnType (Text.unpack name) typeArity argsArity region
598602 let resultCon =
599603 CEqual hint defRegion rigidType resultType
600604
src/Type/Constrain/Literal.hs+8−2
@@ -1,3 +1,5 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
13module Type.Constrain.Literal where
24
35import qualified AST.Literal as L
@@ -7,13 +9,17 @@ import qualified Type.Type as T
79import qualified Type.Environment as Env
810
911
12+
13+-- CONSTRAIN LITERALS
14+
15+
1016constrain :: Env.Env -> R.Region -> L.Literal -> T.Type -> IO T.TypeConstraint
1117constrain env region literal tipe =
1218 do definiteType <- litType
1319 return (T.CEqual (Error.Literal name) region definiteType tipe)
1420 where
15− prim name =
16− return (Env.getType env name)
21+ prim tipeName =
22+ return (Env.getType env tipeName)
1723
1824 (name, litType) =
1925 case literal of
src/Type/Constrain/Pattern.hs+8−9
@@ -3,11 +3,12 @@ module Type.Constrain.Pattern (Info(..), joinInfos, infoToScheme, constrain) whe
33
44import Control.Arrow (second)
55import qualified Control.Monad as Monad
6−import qualified Data.List as List
76import qualified Data.Map as Map
7+import qualified Data.List as List
8+import Data.Text (Text)
89
910import qualified AST.Pattern as P
10−import qualified AST.Variable as V
11+import qualified AST.Variable as Var
1112import qualified Reporting.Annotation as A
1213import qualified Reporting.Error.Type as Error
1314import qualified Type.Constrain.Literal as Literal
@@ -21,7 +22,7 @@ import Type.Type
2122
2223data Info =
2324 Info
24− { _headers :: Map.Map String (A.Located Type)
25+ { _headers :: Map.Map Text (A.Located Type)
2526 , _vars :: [Variable]
2627 , _cons :: TypeConstraint
2728 }
@@ -91,18 +92,16 @@ constrain env (A.A region pattern) tipe =
9192 /\ _cons info
9293 }
9394
94− P.Data name patterns ->
95− do let stringName = V.toString name
96−
97− (_kind, cvars, args, result) <-
98− Env.freshDataScheme env stringName
95+ P.Ctor name patterns ->
96+ do (_kind, cvars, args, result) <-
97+ Env.freshDataScheme env (Var.toText name)
9998
10099 fragList <- Monad.zipWithM (constrain env) patterns args
101100 let info = joinInfos fragList
102101 return $ info
103102 { _vars = cvars ++ _vars info
104103 , _cons =
105− equal (Error.PData stringName) result tipe
104+ equal (Error.PCtor name) result tipe
106105 /\ _cons info
107106 }
108107
src/Type/Environment.hs+116−90
@@ -1,147 +1,180 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Type.Environment
34 ( Env
5+ , Dict
46 , initialize
5− , getType, freshDataScheme, ctorNames
7+ , get, getType, freshDataScheme, ctorNames
68 , addValues
79 , instantiateType
810 )
911 where
1012
13+import Control.Monad (foldM)
1114import qualified Control.Monad.State as State
12−import Data.Map ((!))
15+import qualified Data.List as List
1316import qualified Data.Map as Map
1417import qualified Data.Set as Set
18+import qualified Data.Text as Text
19+import Data.Text (Text)
1520
1621import qualified AST.Type as T
17−import qualified AST.Variable as V
22+import qualified AST.Variable as Var
1823import qualified AST.Module as Module
1924import Type.Type
2025
2126
22−type TypeDict = Map.Map String Type
23−type VarDict = Map.Map String Variable
2427
28+-- ENVIRONMENT
2529
26−data Env = Env
27− { _constructor :: Map.Map String (IO (Int, [Variable], [Type], Type))
28− , _types :: TypeDict
29− , _value :: TypeDict
30+
31+data Env =
32+ Env
33+ { _ctor :: Dict Instantiator
34+ , _types :: Dict Type
35+ , _value :: Dict Type
3036 }
3137
3238
39+type Instantiator =
40+ IO (Int, [Variable], [Type], Type)
41+
42+
43+type Dict a =
44+ Map.Map Text a
45+
46+
3347initialize :: [Module.CanonicalUnion] -> IO Env
34−initialize datatypes =
35− do types <- makeTypes datatypes
48+initialize unions =
49+ do types <- makeTypes unions
3650 let env =
3751 Env
38− { _constructor = Map.empty
52+ { _ctor = Map.empty
3953 , _value = Map.empty
4054 , _types = types
4155 }
42− return $ env { _constructor = makeConstructors env datatypes }
56+ return $ env { _ctor = makeCtors env unions }
4357
4458
45−makeTypes :: [Module.CanonicalUnion] -> IO TypeDict
46−makeTypes datatypes =
47− do unions <- mapM makeImported datatypes
48− bs <- mapM makeBuiltin builtins
49− return (Map.fromList (unions ++ bs))
50− where
51− makeImported :: (V.Canonical, Module.UnionInfo V.Canonical) -> IO (String, Type)
52− makeImported (name, _) =
53− do tvar <- mkAtom name
54− return (V.toString name, VarN tvar)
5559
56− makeBuiltin :: (String, Int) -> IO (String, Type)
57− makeBuiltin (name, _) =
58− do name' <- mkAtom (V.builtin name)
59− return (name, VarN name')
60+-- TYPES
6061
61− builtins :: [(String, Int)]
62− builtins =
63− concat
64− [ map tuple [0..9]
65− , kind 1 ["List"]
66− , kind 0 ["Int","Float","Char","String","Bool"]
67− ]
68− where
69− tuple n = ("_Tuple" ++ show n, n)
70− kind n names = map (\name -> (name, n)) names
71−
72−
73−makeConstructors
74− :: Env
75− -> [Module.CanonicalUnion]
76− -> Map.Map String (IO (Int, [Variable], [Type], Type))
77−makeConstructors env datatypes =
78− Map.fromList builtins
62+
63+makeTypes :: [Module.CanonicalUnion] -> IO (Dict Type)
64+makeTypes unions =
65+ do builtinDict <- Map.fromList <$> mapM makeBuiltin builtinTypes
66+ foldM addImported builtinDict unions
67+
68+
69+addImported :: Dict Type -> (Var.Canonical, unionInfo) -> IO (Dict Type)
70+addImported dict ( var, _ ) =
71+ do atom <- mkAtom var
72+ return (Map.insert (Var.toText var) (VarN atom) dict)
73+
74+
75+makeBuiltin :: (Text, Int) -> IO (Text, Type)
76+makeBuiltin (name, _) =
77+ do atom <- mkAtom (Var.builtin name)
78+ return (name, VarN atom)
79+
80+
81+builtinTypes :: [(Text, Int)]
82+builtinTypes =
83+ concat
84+ [ map tuple [0..9]
85+ , kind 1 ["List"]
86+ , kind 0 ["Int","Float","Char","String","Bool"]
87+ ]
7988 where
89+ tuple n = ( Text.pack ("_Tuple" ++ show n), n )
90+ kind n names = map (\name -> (name, n)) names
91+
92+
93+
94+-- CONSTRUCTORS
95+
96+
97+makeCtors :: Env -> [Module.CanonicalUnion] -> Dict Instantiator
98+makeCtors env unions =
99+ let
80100 list t =
81− (_types env ! "List") <| t
101+ getType env "List" <| t
82102
83− inst :: Int -> ([Type] -> ([Type], Type)) -> IO (Int, [Variable], [Type], Type)
103+ inst :: Int -> ([Type] -> ([Type], Type)) -> Instantiator
84104 inst numTVars tipe =
85105 do vars <- mapM (\_ -> mkVar Nothing) [1..numTVars]
86106 let (args, result) = tipe (map (VarN) vars)
87107 return (length args, vars, args, result)
88108
89109 tupleCtor n =
90− let name = "_Tuple" ++ show n
91− in (name, inst n $ \vs -> (vs, foldl (<|) (_types env ! name) vs))
110+ let
111+ name =
112+ Text.pack ("_Tuple" ++ show n)
113+ in
114+ (name, inst n $ \vs -> (vs, foldl (<|) (getType env name) vs))
92115
93− builtins :: [ (String, IO (Int, [Variable], [Type], Type)) ]
116+ builtins :: Dict Instantiator
94117 builtins =
118+ Map.fromList $
95119 [ ("[]", inst 1 $ \ [t] -> ([], list t))
96120 , ("::", inst 1 $ \ [t] -> ([t, list t], list t))
97− ] ++ map tupleCtor [0..9]
98− ++ concatMap (ctorToType env) datatypes
121+ ]
122+ ++ map tupleCtor [0..9]
123+ in
124+ List.foldl' (makeCtorsHelp env) builtins unions
99125
100126
101−ctorToType
102− :: Env
103− -> (V.Canonical, Module.UnionInfo V.Canonical)
104− -> [(String, IO (Int, [Variable], [Type], Type))]
105−ctorToType env (name, (tvars, ctors)) =
106− zip (map (V.toString . fst) ctors) (map inst ctors)
107− where
108− inst :: (V.Canonical, [T.Canonical]) -> IO (Int, [Variable], [Type], Type)
109− inst ctor =
110− do ((args, tipe), dict) <- State.runStateT (go ctor) Map.empty
111− return (length args, Map.elems dict, args, tipe)
127+type VarDict =
128+ Map.Map Text Variable
112129
113130
114− go :: (V.Canonical, [T.Canonical]) -> State.StateT VarDict IO ([Type], Type)
115− go (_, args) =
131+makeCtorsHelp :: Env -> Dict Instantiator -> Module.CanonicalUnion -> Dict Instantiator
132+makeCtorsHelp env constructorDict ( var, (rawTypeVars, ctors) ) =
133+ let
134+ inst :: Dict Instantiator -> (Var.Canonical, [T.Canonical]) -> Dict Instantiator
135+ inst ctorDict (name, rawArgs) =
136+ Map.insert
137+ (Var.toText name)
138+ ( do ((args, tipe), dict) <- State.runStateT (go rawArgs) Map.empty
139+ return ( length args, Map.elems dict, args, tipe )
140+ )
141+ ctorDict
142+
143+ tvars =
144+ map T.Var rawTypeVars
145+
146+ go :: [T.Canonical] -> State.StateT VarDict IO ([Type], Type)
147+ go args =
116148 do types <- mapM (instantiator Flex env) args
117− returnType <- instantiator Flex env (T.App (T.Type name) (map T.Var tvars))
149+ returnType <- instantiator Flex env (T.App (T.Type var) tvars)
118150 return (types, returnType)
151+ in
152+ List.foldl' inst constructorDict ctors
119153
120154
121155
122156-- ACCESS TYPES
123157
124158
125−get :: (Env -> Map.Map String a) -> Env -> String -> a
126−get subDict env key =
127− Map.findWithDefault (error msg) key (subDict env)
128− where
129− msg = "Could not find type constructor `" ++ key ++ "` while checking types."
159+{-# INLINE get #-}
160+get :: Dict a -> Text -> a
161+get dict var =
162+ dict Map.! var
130163
131164
132−getType :: Env -> String -> Type
133−getType =
134− get _types
165+getType :: Env -> Text -> Type
166+getType env var =
167+ get (_types env) var
135168
136169
137−freshDataScheme :: Env -> String -> IO (Int, [Variable], [Type], Type)
138−freshDataScheme =
139− get _constructor
170+freshDataScheme :: Env -> Text -> Instantiator
171+freshDataScheme env var =
172+ get (_ctor env) var
140173
141174
142−ctorNames :: Env -> [String]
175+ctorNames :: Env -> [Text]
143176ctorNames env =
144− Map.keys (_constructor env)
177+ Map.keys (_ctor env)
145178
146179
147180
@@ -167,7 +200,7 @@ instantiator flex env sourceType =
167200 instantiatorHelp flex env Set.empty sourceType
168201
169202
170−instantiatorHelp :: Flex -> Env -> Set.Set String -> T.Canonical -> State.StateT VarDict IO Type
203+instantiatorHelp :: Flex -> Env -> Set.Set Text -> T.Canonical -> State.StateT VarDict IO Type
171204instantiatorHelp flex env aliasVars sourceType =
172205 let
173206 go =
@@ -198,7 +231,7 @@ instantiatorHelp flex env aliasVars sourceType =
198231 return (VarN variable)
199232
200233 T.Aliased name args aliasType ->
201− do targs <- mapM (\(arg,tipe) -> (,) arg <$> go tipe) args
234+ do targs <- traverse (traverse go) args
202235 realType <-
203236 case aliasType of
204237 T.Filled tipe ->
@@ -210,18 +243,11 @@ instantiatorHelp flex env aliasVars sourceType =
210243 return (AliasN name targs realType)
211244
212245 T.Type name ->
213− case Map.lookup (V.toString name) (_types env) of
214− Just tipe ->
215− return tipe
216−
217− Nothing ->
218− error $
219− "Could not find type constructor `" ++
220− V.toString name ++ "` while checking types."
246+ return (getType env (Var.toText name))
221247
222248 T.App func args ->
223249 do tfunc <- go func
224− targs <- mapM go args
250+ targs <- traverse go args
225251 return $ foldl (<|) tfunc targs
226252
227253 T.Record fields ext ->
src/Type/Inference.hs+14−11
@@ -1,9 +1,11 @@
1+{-# LANGUAGE OverloadedStrings #-}
12module Type.Inference where
23
34import Control.Arrow (first, second)
45import Control.Monad.Except (Except, forM, liftIO, runExceptT, throwError)
56import qualified Data.Map as Map
6−import qualified Data.Traversable as Traverse
7+import Data.Monoid ((<>))
8+import Data.Text (Text)
79
810import qualified AST.Module as Module
911import qualified AST.Module.Name as ModuleName
@@ -25,7 +27,7 @@ import System.IO.Unsafe
2527infer
2628 :: Module.Interfaces
2729 -> Module.Canonical
28− -> Except [A.Located Error.Error] (Map.Map String Type.Canonical)
30+ -> Except [A.Located Error.Error] (Map.Map Text Type.Canonical)
2931infer interfaces modul =
3032 either throwError return $ unsafePerformIO $ runExceptT $
3133 do (header, constraint) <-
@@ -36,15 +38,16 @@ infer interfaces modul =
3638 let header' = Map.delete "::" header
3739 let types = Map.map A.drop (Map.difference (TS.sSavedEnv state) header')
3840
39− liftIO (Traverse.traverse T.toSrcType types)
41+ liftIO (traverse T.toSrcType types)
4042
4143
4244genConstraints
4345 :: Module.Interfaces
4446 -> Module.Canonical
45− -> IO (Map.Map String T.Type, T.TypeConstraint)
47+ -> IO (Map.Map Text T.Type, T.TypeConstraint)
4648genConstraints interfaces modul =
47− do env <- Env.initialize (canonicalizeUnions interfaces modul)
49+ do env <-
50+ Env.initialize (canonicalizeUnions interfaces modul)
4851
4952 ctors <-
5053 forM (Env.ctorNames env) $ \name ->
@@ -70,12 +73,12 @@ genConstraints interfaces modul =
7073canonicalizeValues
7174 :: Env.Env
7275 -> (ModuleName.Canonical, Module.Interface)
73− -> IO [(String, ([T.Variable], T.Type))]
76+ -> IO [(Text, ([T.Variable], T.Type))]
7477canonicalizeValues env (moduleName, iface) =
7578 forM (Map.toList (Module.iTypes iface)) $ \(name, tipe) ->
7679 do (flexType, flexVars) <- Env.instantiateType T.Flex env tipe
7780 return
78− ( ModuleName.canonicalToString moduleName ++ "." ++ name
81+ ( ModuleName.canonicalToText moduleName <> "." <> name
7982 , ( Map.elems flexVars
8083 , flexType
8184 )
@@ -96,15 +99,15 @@ canonicalizeUnions interfaces (Module.Module name _ info) =
9699
97100
98101format :: (ModuleName.Canonical, Module.Unions) -> [Module.CanonicalUnion]
99−format (home, adts) =
100− map canonical (Map.toList adts)
102+format (home, unions) =
103+ map canonical (Map.toList unions)
101104 where
102− canonical :: (String, Module.UnionInfo String) -> Module.CanonicalUnion
105+ canonical :: (Text, Module.UnionInfo Text) -> Module.CanonicalUnion
103106 canonical (name, (tvars, ctors)) =
104107 ( toVar name
105108 , (tvars, map (first toVar) ctors)
106109 )
107110
108− toVar :: String -> Var.Canonical
111+ toVar :: Text -> Var.Canonical
109112 toVar name =
110113 Var.fromModule home name
src/Type/Solve.hs+8−6
@@ -1,12 +1,13 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Type.Solve (solve) where
34
45import Control.Monad
56import Control.Monad.State (execStateT, liftIO)
67import Control.Monad.Except (ExceptT, throwError)
7−import qualified Data.List as List
8−import qualified Data.Map as Map
98import qualified Data.Foldable as F
9+import qualified Data.Map as Map
10+import qualified Data.Text as Text
1011import qualified Data.UnionFind.IO as UF
1112
1213import qualified Reporting.Annotation as A
@@ -199,17 +200,17 @@ actuallySolve constraint =
199200 TS.makeInstance tipe
200201
201202 Nothing ->
202− if List.isPrefixOf "Native." name then
203+ if Text.isPrefixOf "Native." name then
203204 liftIO (mkVar Nothing)
204205
205206 else
206− error ("Could not find `" ++ name ++ "` when solving type constraints.")
207+ error ("Could not find `" ++ Text.unpack name ++ "` when solving type constraints.")
207208
208209 t <- TS.flatten term
209210 unify (Error.Instance name) region freshCopy t
210211
211212
212−solveScheme :: TypeScheme -> TS.Solver (Map.Map String (A.Located Variable))
213+solveScheme :: TypeScheme -> TS.Solver TS.Env
213214solveScheme scheme =
214215 let
215216 flatten (A.A region term) =
@@ -238,6 +239,7 @@ solveScheme scheme =
238239 return header'
239240
240241
242+
241243-- ADDITIONAL CHECKS
242244
243245
@@ -263,7 +265,7 @@ crash msg =
263265-- OCCURS CHECK
264266
265267
266−occurs :: (String, A.Located Variable) -> TS.Solver ()
268+occurs :: (Text.Text, A.Located Variable) -> TS.Solver ()
267269occurs (name, A.A region variable) =
268270 do hasOccurred <- liftIO $ occursHelp [] variable
269271 case hasOccurred of
src/Type/State.hs+9−6
@@ -2,8 +2,9 @@
22module Type.State where
33
44import qualified Control.Monad.State as State
5−import Data.Map ((!))
65import qualified Data.Map as Map
6+import Data.Map ((!))
7+import Data.Text (Text)
78import qualified Data.UnionFind.IO as UF
89
910import qualified Reporting.Annotation as A
@@ -17,7 +18,8 @@ import Type.Type
1718-- The rank of each variable is less than or equal to the pool's "maxRank"
1819-- The young pool exists to make it possible to identify these vars in constant time.
1920
20−data Pool = Pool
21+data Pool =
22+ Pool
2123 { maxRank :: Int
2224 , inhabitants :: [Variable]
2325 }
@@ -25,20 +27,21 @@ data Pool = Pool
2527
2628emptyPool :: Pool
2729emptyPool =
28− Pool
30+ Pool
2931 { maxRank = outermostRank
3032 , inhabitants = []
3133 }
3234
3335
34−type Env = Map.Map String (A.Located Variable)
36+type Env = Map.Map Text (A.Located Variable)
3537
3638
3739type Solver = State.StateT SolverState IO
3840
3941
4042-- Keeps track of the environment, type variable pool, and a list of errors
41−data SolverState = SS
43+data SolverState =
44+ SS
4245 { sEnv :: Env
4346 , sSavedEnv :: Env
4447 , sPool :: Pool
@@ -126,7 +129,7 @@ flatten term =
126129 flattenHelp Map.empty term
127130
128131
129−flattenHelp :: Map.Map String Variable -> Type -> Solver Variable
132+flattenHelp :: Map.Map Text Variable -> Type -> Solver Variable
130133flattenHelp aliasDict termN =
131134 case termN of
132135 PlaceHolder name ->
src/Type/Type.hs+37−29
@@ -1,13 +1,16 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Type.Type where
34
45import Control.Monad (when)
56import Control.Monad.State (StateT, liftIO)
67import qualified Control.Monad.State as State
78import qualified Data.Char as Char
8−import qualified Data.List as List
99import qualified Data.Map as Map
10+import Data.Monoid ((<>))
11+import qualified Data.Text as Text
1012import qualified Data.UnionFind.IO as UF
13+import Data.Text (Text)
1114
1215import qualified AST.Type as T
1316import qualified AST.Variable as Var
@@ -44,17 +47,17 @@ data Term1 a
4447 = App1 a a
4548 | Fun1 a a
4649 | EmptyRecord1
47− | Record1 (Map.Map String a) a
50+ | Record1 (Map.Map Text a) a
4851
4952
5053data TermN a
51− = PlaceHolder String
52− | AliasN Var.Canonical [(String, TermN a)] (TermN a)
54+ = PlaceHolder Text
55+ | AliasN Var.Canonical [(Text, TermN a)] (TermN a)
5356 | VarN a
5457 | TermN (Term1 (TermN a))
5558
5659
57−record :: Map.Map String (TermN a) -> TermN a -> TermN a
60+record :: Map.Map Text (TermN a) -> TermN a -> TermN a
5861record fs rec =
5962 TermN (Record1 fs rec)
6063
@@ -63,7 +66,8 @@ record fs rec =
6366-- DESCRIPTORS
6467
6568
66−data Descriptor = Descriptor
69+data Descriptor =
70+ Descriptor
6771 { _content :: Content
6872 , _rank :: Int
6973 , _mark :: Int
@@ -74,8 +78,8 @@ data Descriptor = Descriptor
7478data Content
7579 = Structure (Term1 Variable)
7680 | Atom Var.Canonical
77− | Var Flex (Maybe Super) (Maybe String)
78− | Alias Var.Canonical [(String,Variable)] Variable
81+ | Var Flex (Maybe Super) (Maybe Text)
82+ | Alias Var.Canonical [(Text,Variable)] Variable
7983 | Error
8084
8185
@@ -131,14 +135,14 @@ data Constraint a b
131135 | CInstance R.Region SchemeName a
132136
133137
134−type SchemeName = String
138+type SchemeName = Text
135139
136140
137141data Scheme a b = Scheme
138142 { _rigidQuantifiers :: [b]
139143 , _flexibleQuantifiers :: [b]
140144 , _constraint :: Constraint a b
141− , _header :: Map.Map String (A.Located a)
145+ , _header :: Map.Map Text (A.Located a)
142146 }
143147
144148
@@ -183,20 +187,20 @@ mkVar maybeSuper =
183187 UF.fresh $ mkDescriptor (Var Flex maybeSuper Nothing)
184188
185189
186−mkNamedVar :: Flex -> String -> IO Variable
190+mkNamedVar :: Flex -> Text -> IO Variable
187191mkNamedVar flex name =
188192 UF.fresh $ mkDescriptor $ Var flex (toSuper name) (Just name)
189193
190194
191−toSuper :: String -> Maybe Super
195+toSuper :: Text -> Maybe Super
192196toSuper name =
193− if List.isPrefixOf "number" name then
197+ if Text.isPrefixOf "number" name then
194198 Just Number
195199
196− else if List.isPrefixOf "comparable" name then
200+ else if Text.isPrefixOf "comparable" name then
197201 Just Comparable
198202
199− else if List.isPrefixOf "appendable" name then
203+ else if Text.isPrefixOf "appendable" name then
200204 Just Appendable
201205
202206 else
@@ -207,13 +211,14 @@ toSuper name =
207211-- CONSTRAINT HELPERS
208212
209213
210−monoscheme :: Map.Map String (A.Located a) -> Scheme a b
214+monoscheme :: Map.Map Text (A.Located a) -> Scheme a b
211215monoscheme headers =
212216 Scheme [] [] CTrue headers
213217
214218
215219infixl 8 /\
216220
221+
217222(/\) :: Constraint a b -> Constraint a b -> Constraint a b
218223(/\) c1 c2 =
219224 case (c1, c2) of
@@ -349,7 +354,7 @@ data NameState = NameState
349354 }
350355
351356
352−type TakenNames = Map.Map String Variable
357+type TakenNames = Map.Map Text Variable
353358
354359
355360makeNameState :: TakenNames -> NameState
@@ -357,7 +362,7 @@ makeNameState taken =
357362 NameState taken 0 0 0 0 0
358363
359364
360−getFreshName :: (Monad m) => Maybe Super -> StateT NameState m String
365+getFreshName :: (Monad m) => Maybe Super -> StateT NameState m Text
361366getFreshName maybeSuper =
362367 case maybeSuper of
363368 Nothing ->
@@ -380,14 +385,17 @@ getFreshName maybeSuper =
380385 getFreshSuper "compappend" _compAppends (\index state -> state { _compAppends = index })
381386
382387
383−getFreshNormal :: Int -> TakenNames -> (String, Int)
388+getFreshNormal :: Int -> TakenNames -> (Text, Int)
384389getFreshNormal index taken =
385390 let
386391 (postfix, letter) =
387392 quotRem index 26
388393
394+ char =
395+ Char.chr (97 + letter)
396+
389397 name =
390− Char.chr (97 + letter) : if postfix <= 0 then "" else show postfix
398+ Text.pack (if postfix <= 0 then [char] else char : show postfix)
391399 in
392400 if Map.member name taken then
393401 getFreshNormal (index + 1) taken
@@ -398,10 +406,10 @@ getFreshNormal index taken =
398406
399407getFreshSuper
400408 :: (Monad m)
401− => String
409+ => Text
402410 -> (NameState -> Int)
403411 -> (Int -> NameState -> NameState)
404− -> StateT NameState m String
412+ -> StateT NameState m Text
405413getFreshSuper name getter setter =
406414 do index <- State.gets getter
407415 taken <- State.gets _taken
@@ -410,11 +418,11 @@ getFreshSuper name getter setter =
410418 return uniqueName
411419
412420
413−getFreshSuperHelp :: String -> Int -> TakenNames -> (String, Int)
421+getFreshSuperHelp :: Text -> Int -> TakenNames -> (Text, Int)
414422getFreshSuperHelp name index taken =
415423 let
416424 newName =
417− if index <= 0 then name else name ++ show index
425+ if index <= 0 then name else name <> Text.pack (show index)
418426 in
419427 if Map.member newName taken then
420428 getFreshSuperHelp name (index + 1) taken
@@ -471,7 +479,7 @@ getVarNames var =
471479 getVarNames extension
472480
473481
474−addVarName :: Int -> String -> Variable -> Flex -> Maybe Super -> TakenNames -> IO TakenNames
482+addVarName :: Int -> Text -> Variable -> Flex -> Maybe Super -> TakenNames -> IO TakenNames
475483addVarName index givenName var flex maybeSuper taken =
476484 let
477485 name =
@@ -490,13 +498,13 @@ addVarName index givenName var flex maybeSuper taken =
490498 else addVarName (index + 1) givenName var flex maybeSuper taken
491499
492500
493−makeIndexedName :: String -> Int -> String
501+makeIndexedName :: Text -> Int -> Text
494502makeIndexedName name index =
495503 if index <= 0 then
496504 name
497505
498− else if Char.isDigit (last name) then
499− name ++ '_' : show index
506+ else if Char.isDigit (Text.last name) then
507+ name <> Text.pack ('_' : show index)
500508
501509 else
502− name ++ show index
510+ name <> Text.pack (show index)
src/Type/Unify.hs+12−9
@@ -1,10 +1,13 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Type.Unify (unify) where
34
45import Control.Monad (zipWithM_)
56import Control.Monad.Except (ExceptT, lift, liftIO, throwError, runExceptT)
67import qualified Data.Map as Map
78import qualified Data.Maybe as Maybe
9+import qualified Data.Text as Text
10+import Data.Text (Text)
811import qualified Data.UnionFind.IO as UF
912
1013import qualified AST.Variable as Var
@@ -103,7 +106,7 @@ mismatchHelp orientation maybeReason =
103106 Error.flipReason <$> maybeReason
104107
105108
106−badRigid :: Maybe String -> Error.Reason
109+badRigid :: Maybe Text -> Error.Reason
107110badRigid maybeName =
108111 Error.BadVar (Just (Error.Rigid maybeName)) Nothing
109112
@@ -248,7 +251,7 @@ unifyFlex context otherContent =
248251-- UNIFY RIGID VARIABLES
249252
250253
251−unifyRigid :: Context -> Maybe Super -> Maybe String -> Content -> Unify ()
254+unifyRigid :: Context -> Maybe Super -> Maybe Text -> Content -> Unify ()
252255unifyRigid context maybeSuper maybeName otherContent =
253256 case otherContent of
254257 Error ->
@@ -353,7 +356,7 @@ combineSupers firstSuper secondSuper =
353356 Left $ doubleBad (errorSuper firstSuper) (errorSuper secondSuper)
354357
355358
356−isPrimitiveFrom :: [String] -> Var.Canonical -> Bool
359+isPrimitiveFrom :: [Text] -> Var.Canonical -> Bool
357360isPrimitiveFrom prims var =
358361 any (\p -> Var.isPrim p var) prims
359362
@@ -531,7 +534,7 @@ isIntFloat name otherName =
531534-- UNIFY ALIASES
532535
533536
534−unifyAlias :: Context -> Var.Canonical -> [(String, Variable)] -> Variable -> Content -> Unify ()
537+unifyAlias :: Context -> Var.Canonical -> [(Text, Variable)] -> Variable -> Content -> Unify ()
535538unifyAlias context name args realVar otherContent =
536539 case otherContent of
537540 Error ->
@@ -668,7 +671,7 @@ unifyRecord context firstStructure secondStructure =
668671
669672
670673
671−unifySharedFieldsHelp :: Context -> Map.Map String (Variable, Variable) -> Unify ()
674+unifySharedFieldsHelp :: Context -> Map.Map Text (Variable, Variable) -> Unify ()
672675unifySharedFieldsHelp context sharedFields =
673676 do maybeBadFields <- traverse (unifyField context) (Map.toList sharedFields)
674677 case Maybe.catMaybes maybeBadFields of
@@ -679,7 +682,7 @@ unifySharedFieldsHelp context sharedFields =
679682 mismatch context (Just (Error.BadFields (reverse badFields)))
680683
681684
682−unifyField :: Context -> (String, (Variable, Variable)) -> Unify (Maybe (String, Maybe Error.Reason))
685+unifyField :: Context -> (Text, (Variable, Variable)) -> Unify (Maybe (String, Maybe Error.Reason))
683686unifyField context (field, (expected, actual)) =
684687 do result <- lift $ runExceptT $ subUnify context expected actual
685688 case result of
@@ -687,7 +690,7 @@ unifyField context (field, (expected, actual)) =
687690 return $ Nothing
688691
689692 Left (Mismatch maybeReason) ->
690− return $ Just (field, maybeReason)
693+ return $ Just (Text.unpack field, maybeReason)
691694
692695
693696
@@ -695,7 +698,7 @@ unifyField context (field, (expected, actual)) =
695698
696699
697700data RecordStructure = RecordStructure
698− { _fields :: Map.Map String Variable
701+ { _fields :: Map.Map Text Variable
699702 , _extVar :: Variable
700703 , _extStruct :: ExtensionStructure
701704 }
@@ -706,7 +709,7 @@ data ExtensionStructure
706709 | Extension
707710
708711
709−gatherFields :: Context -> Map.Map String Variable -> Variable -> Unify RecordStructure
712+gatherFields :: Context -> Map.Map Text Variable -> Variable -> Unify RecordStructure
710713gatherFields context fields variable =
711714 do desc <- liftIO (UF.descriptor variable)
712715 case _content desc of