@@ -3,7 +3,6 @@
33module Reporting.Error.Type
44 ( Error(..)
55 -- expectations
6− , Origin(..)
76 , Expected(..)
87 , Context(..)
98 , SubContext(..)
@@ -14,10 +13,6 @@ module Reporting.Error.Type
1413 , PCategory(..)
1514 , typeReplace
1615 , ptypeReplace
17− -- error types
18− , Type(..)
19− , Extension(..)
20− , Localizer
2116 -- make reports
2217 , toReport
2318 )
@@ -29,12 +24,12 @@ import Data.Monoid ((<>))
2924
3025import qualified AST.Module.Name as ModuleName
3126import qualified Data.Index as Index
32−import qualified Data.Map as Map
3327import qualified Elm.Name as N
3428import qualified Reporting.Helpers as H
3529import qualified Reporting.Region as R
3630import qualified Reporting.Render.Code as Code
3731import qualified Reporting.Report as Report
32+import qualified Type.Error as T
3833
3934
4035
@@ -42,21 +37,15 @@ import qualified Reporting.Report as Report
4237
4338
4439data Error
45− = Mismatch Origin (Expected Type)
46− | BadPattern R.Region PCategory Type (PExpected Type)
47− | InfiniteType Type
40+ = Mismatch R.Region Category T.Type (Expected T.Type)
41+ | BadPattern R.Region PCategory T.Type (PExpected T.Type)
42+ | InfiniteType T.Type
4843
4944
5045
5146-- EXPRESSION EXPECTATIONS
5247
5348
54−data Origin
55− = BadExpr R.Region Category Type
56− | BadLocal R.Region N.Name Type
57− | BadForeign R.Region N.Name Type
58−
59−
6049data Expected tipe
6150 = NoExpectation tipe
6251 | FromContext R.Region Context tipe
@@ -100,14 +89,16 @@ data Category
10089 | If
10190 | Case
10291 | CallResult MaybeName
103− | LambdaExpr
92+ | Lambda
10493 | Accessor N.Name
10594 | Access N.Name
106− | RecordExpr
107− | TupleExpr
108− | UnitExpr
95+ | Record
96+ | Tuple
97+ | Unit
10998 | Shader
11099 | Effects
100+ | Local N.Name
101+ | Foreign N.Name
111102
112103
113104
@@ -166,47 +157,14 @@ ptypeReplace expectation tipe =
166157
167158
168159
169−-- ERROR TYPES
170−
171−
172−data Type
173− = Lambda Type [Type]
174− | Infinite
175− | Error
176− | FlexVar N.Name
177− | FlexSuper N.Name
178− | RigidVar N.Name
179− | RigidSuper N.Name
180− | Type ModuleName.Canonical N.Name [Type]
181− | Record (Map.Map N.Name Type) Extension
182− | Unit
183− | Tuple Type Type (Maybe Type)
184− | Alias ModuleName.Canonical N.Name [(N.Name, Type)] Type
185−
186−
187−data Extension
188− = Closed
189− | FlexOpen N.Name
190− | RigidOpen N.Name
191−
192−
193−
194160-- TO REPORT
195161
196162
197−toReport :: Code.Source -> Localizer -> Error -> Report.Report
163+toReport :: Code.Source -> T.Localizer -> Error -> Report.Report
198164toReport source localizer err =
199165 case err of
200− Mismatch origin expected ->
201− case origin of
202− BadForeign region name tipe ->
203− toMismatchReport source localizer region (Var name) tipe expected
204−
205− BadLocal region name tipe ->
206− toMismatchReport source localizer region (Var name) tipe expected
207−
208− BadExpr region category tipe ->
209− toMismatchReport source localizer region (Category category) tipe expected
166+ Mismatch region category actualType expected ->
167+ toMismatchReport source localizer region category actualType expected
210168
211169 BadPattern region category tipe expected ->
212170 error "TODO" region category tipe expected
@@ -219,48 +177,40 @@ toReport source localizer err =
219177-- HELPERS
220178
221179
222−data Thing
223− = Var N.Name
224− | Category Category
225−
226−
227−toDescription :: Thing -> String -> String
228−toDescription thing thatThingIs =
229− case thing of
230− Var name ->
231− "This `" <> N.toString name <> "` value has type:"
232−
233− Category category ->
234− case category of
235− List -> thatThingIs <> " a list of type:"
236− Number -> thatThingIs <> " a number of type:"
237− Float -> thatThingIs <> " a float of type:"
238− String -> thatThingIs <> " a string of type:"
239− Char -> thatThingIs <> " a character of type:"
240− If -> "This `if` expression produces a value of type:"
241− Case -> "This `case` expression produces a value of type:"
242− LambdaExpr -> thatThingIs <> " an anonymous function of type:"
243− Accessor field -> "This ." <> N.toString field <> " field access function has type:"
244− Access field -> "The value at ." <> N.toString field <> " has type:"
245− RecordExpr -> thatThingIs <> " a record of type:"
246− TupleExpr -> thatThingIs <> " a tuple of type:"
247− UnitExpr -> thatThingIs <> " a unit value:"
248− Shader -> thatThingIs <> " a GLSL shader of type:"
249− Effects -> thatThingIs <> " a thing for CORE LIBRARIES ONLY."
250− CallResult maybeName ->
251− case maybeName of
252− NoName -> thatThingIs <> ":"
253− FuncName name -> "This `" <> N.toString name <> "` call produces:"
254− CtorName name -> "This `" <> N.toString name <> "` call produces:"
255− OpName _ -> thatThingIs <> ":"
180+toDescription :: Category -> String -> String
181+toDescription category thatThingIs =
182+ case category of
183+ Local name -> "This `" <> N.toString name <> "` value has type:"
184+ Foreign name -> "This `" <> N.toString name <> "` value has type:"
185+ Access field -> "The value at ." <> N.toString field <> " has type:"
186+ Accessor field -> "This ." <> N.toString field <> " field access function has type:"
187+ If -> "This `if` expression produces a value of type:"
188+ Case -> "This `case` expression produces a value of type:"
189+ List -> thatThingIs <> " a list of type:"
190+ Number -> thatThingIs <> " a number of type:"
191+ Float -> thatThingIs <> " a float of type:"
192+ String -> thatThingIs <> " a string of type:"
193+ Char -> thatThingIs <> " a character of type:"
194+ Lambda -> thatThingIs <> " an anonymous function of type:"
195+ Record -> thatThingIs <> " a record of type:"
196+ Tuple -> thatThingIs <> " a tuple of type:"
197+ Unit -> thatThingIs <> " a unit value:"
198+ Shader -> thatThingIs <> " a GLSL shader of type:"
199+ Effects -> thatThingIs <> " a thing for CORE LIBRARIES ONLY."
200+ CallResult maybeName ->
201+ case maybeName of
202+ NoName -> thatThingIs <> ":"
203+ FuncName name -> "This `" <> N.toString name <> "` call produces:"
204+ CtorName name -> "This `" <> N.toString name <> "` call produces:"
205+ OpName _ -> thatThingIs <> ":"
256206
257207
258208
259209-- TO MISMATCH REPORT
260210
261211
262−toMismatchReport :: Code.Source -> Localizer -> R.Region -> Thing -> Type -> Expected Type -> Report.Report
263−toMismatchReport source localizer exprRegion thing tipe expected =
212+toMismatchReport :: Code.Source -> T.Localizer -> R.Region -> Category -> T.Type -> Expected T.Type -> Report.Report
213+toMismatchReport source localizer exprRegion category tipe expected =
264214 Report.Report "TYPE MISMATCH" exprRegion [] $
265215 case expected of
266216 NoExpectation expectedType ->
@@ -270,7 +220,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
270220 ,
271221 typeComparison localizer tipe expectedType
272222 (
273− toDescription thing "It is"
223+ toDescription category "It is"
274224 ,
275225 "But you are trying to use it as:"
276226 ,
@@ -296,7 +246,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
296246 ,
297247 typeComparison localizer tipe expectedType
298248 (
299− toDescription thing $
249+ toDescription category $
300250 case subContext of
301251 TypedIfBranch index ->
302252 "The " <> H.ordinalize (Index.toHuman index) <> " branch is"
@@ -328,7 +278,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
328278 ,
329279 typeComparison localizer tipe expectedType
330280 (
331− toDescription thing $
281+ toDescription category $
332282 "The " <> H.ordinalize (Index.toHuman index) <> " element is"
333283 ,
334284 "But all the previous elements in the list are:"
@@ -349,7 +299,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
349299 "I do not know how to negate this type of value:"
350300 ,
351301 H.stack
352− [ H.reflow $ toDescription thing "It is"
302+ [ H.reflow $ toDescription category "It is"
353303 , indentType localizer tipe
354304 , H.fillSep
355305 ["But","I","only","now","how","to","negate"
@@ -361,10 +311,10 @@ toMismatchReport source localizer exprRegion thing tipe expected =
361311
362312 OpLeft op ->
363313 Report.toCodeSnippet source region (Just exprRegion) $
364− opLeftToDocs localizer thing op tipe expectedType
314+ opLeftToDocs localizer category op tipe expectedType
365315
366316 OpRight op ->
367− case opRightToDocs localizer thing op tipe expectedType of
317+ case opRightToDocs localizer category op tipe expectedType of
368318 EmphBoth details ->
369319 Report.toCodeSnippet source region Nothing details
370320
@@ -377,7 +327,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
377327 "This `if` condition does not evaluate to a boolean value, True or False."
378328 ,
379329 H.stack
380− [ H.reflow $ toDescription thing "It is"
330+ [ H.reflow $ toDescription category "It is"
381331 , indentType localizer tipe
382332 , H.fillSep ["But","I","need","this","`if`","condition","to","be","a",H.dullyellow "Bool","value."]
383333 , H.toSimpleHint $
@@ -398,7 +348,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
398348 ,
399349 typeComparison localizer tipe expectedType
400350 (
401− toDescription thing $
351+ toDescription category $
402352 "The " <> H.ordinalize (Index.toHuman index) <> " branch is"
403353 ,
404354 "But all the previous branches result in:"
@@ -424,7 +374,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
424374 ,
425375 typeComparison localizer tipe expectedType
426376 (
427− toDescription thing $
377+ toDescription category $
428378 "The " <> H.ordinalize (Index.toHuman index) <> " branch is"
429379 ,
430380 "But all the previous branches result in:"
@@ -463,7 +413,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
463413 ,
464414 typeComparison localizer tipe expectedType
465415 (
466− toDescription thing "This argument is"
416+ toDescription category "This argument is"
467417 ,
468418 "But " <> thisFunction <> " needs the " <> H.ordinalize (Index.toHuman index)
469419 <> " argument to be:"
@@ -559,7 +509,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
559509 RecordAccess field ->
560510 Report.toCodeSnippet source region (Just exprRegion) $
561511 case error "TODO figure out record things" tipe of
562− Record _ _ ->
512+ T.Record _ _ ->
563513 (
564514 H.reflow $
565515 "This record does not have a `" <> N.toString field <> "` field:"
@@ -593,7 +543,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
593543 ]
594544 )
595545
596− Lambda _ _ ->
546+ T.Lambda _ _ _ ->
597547 (
598548 H.reflow $
599549 "I cannot access field `" <> N.toString field <> "` of this " <> whatever <> ":"
@@ -620,7 +570,7 @@ toMismatchReport source localizer exprRegion thing tipe expected =
620570
621571 RecordUpdate ->
622572 case error "TODO figure out record things" tipe of
623− Record _ _ ->
573+ T.Record _ _ ->
624574 Report.toCodeSnippet source region Nothing
625575 (
626576 H.reflow $
@@ -649,11 +599,11 @@ toMismatchReport source localizer exprRegion thing tipe expected =
649599-- COUNT ARGS
650600
651601
652−countArgs :: Type -> Int
602+countArgs :: T.Type -> Int
653603countArgs tipe =
654604 case tipe of
655− Lambda _ stuff ->
656− length stuff
605+ T.Lambda _ _ stuff ->
606+ 1 + length stuff
657607
658608 _ ->
659609 0
@@ -663,8 +613,8 @@ countArgs tipe =
663613-- OP LEFT
664614
665615
666−opLeftToDocs :: Localizer -> Thing -> N.Name -> Type -> Type -> (H.Doc, H.Doc)
667−opLeftToDocs localizer thing op tipe expectedType =
616+opLeftToDocs :: T.Localizer -> Category -> N.Name -> T.Type -> T.Type -> (H.Doc, H.Doc)
617+opLeftToDocs localizer category op tipe expectedType =
668618 case op of
669619 "+"
670620 | isString tipe -> badStringAdd
@@ -681,10 +631,10 @@ opLeftToDocs localizer thing op tipe expectedType =
681631 "//" -> badIDiv localizer "left" tipe
682632 "&&" -> badBool localizer "&&" "left" tipe
683633 "||" -> badBool localizer "||" "left" tipe
684− "<" -> badCompLeft localizer thing "<" "left" tipe
685− ">" -> badCompLeft localizer thing ">" "left" tipe
686− "<=" -> badCompLeft localizer thing "<=" "left" tipe
687− ">=" -> badCompLeft localizer thing ">=" "left" tipe
634+ "<" -> badCompLeft localizer category "<" "left" tipe
635+ ">" -> badCompLeft localizer category ">" "left" tipe
636+ "<=" -> badCompLeft localizer category "<=" "left" tipe
637+ ">=" -> badCompLeft localizer category ">=" "left" tipe
688638
689639 "<|" ->
690640 ( "The left side of (<|) needs to be a function so I can pipe arguments to it!"
@@ -712,7 +662,7 @@ opLeftToDocs localizer thing op tipe expectedType =
712662 ,
713663 typeComparison localizer tipe expectedType
714664 (
715− toDescription thing "The left argument is"
665+ toDescription category "The left argument is"
716666 ,
717667 "But (" <> N.toString op <> ") needs the left argument to be:"
718668 ,
@@ -730,8 +680,8 @@ data RightDocs
730680 | EmphRight (H.Doc, H.Doc)
731681
732682
733−opRightToDocs :: Localizer -> Thing -> N.Name -> Type -> Type -> RightDocs
734−opRightToDocs localizer thing op tipe expectedType =
683+opRightToDocs :: T.Localizer -> Category -> N.Name -> T.Type -> T.Type -> RightDocs
684+opRightToDocs localizer category op tipe expectedType =
735685 case op of
736686 "+"
737687 | isInt tipe -> badCast op FloatInt
@@ -778,7 +728,7 @@ opRightToDocs localizer thing op tipe expectedType =
778728 ,
779729 typeComparison localizer tipe expectedType
780730 (
781− toDescription thing "You are providing"
731+ toDescription category "You are providing"
782732 ,
783733 "But (<|) is piping it a function that expects:"
784734 ,
@@ -790,9 +740,10 @@ opRightToDocs localizer thing op tipe expectedType =
790740
791741 "|>" ->
792742 case (tipe, expectedType) of
793− (Lambda expectedArgType _, Lambda argType _) ->
743+ (T.Lambda expectedArgType _ _, T.Lambda argType _ _) ->
794744 EmphRight
795745 (
746+ -- TODO try to say the function name instead of "this function"
796747 "I cannot pipe the argument through to this function:"
797748 ,
798749 typeComparison localizer argType expectedArgType
@@ -847,7 +798,7 @@ opRightToDocs localizer thing op tipe expectedType =
847798 ,
848799 typeComparison localizer tipe expectedType
849800 (
850− toDescription thing "The right argument is"
801+ toDescription category "The right argument is"
851802 ,
852803 "But (" <> N.toString op <> ") needs the right argument to be:"
853804 ,
@@ -862,40 +813,40 @@ opRightToDocs localizer thing op tipe expectedType =
862813
863814
864815
865−isInt :: Type -> Bool
816+isInt :: T.Type -> Bool
866817isInt tipe =
867818 case tipe of
868− Type home name [] ->
819+ T.Type home name [] ->
869820 home == ModuleName.basics && name == N.int
870821
871822 _ ->
872823 False
873824
874825
875−isFloat :: Type -> Bool
826+isFloat :: T.Type -> Bool
876827isFloat tipe =
877828 case tipe of
878− Type home name [] ->
829+ T.Type home name [] ->
879830 home == ModuleName.basics && name == N.float
880831
881832 _ ->
882833 False
883834
884835
885−isString :: Type -> Bool
836+isString :: T.Type -> Bool
886837isString tipe =
887838 case tipe of
888− Type home name [] ->
839+ T.Type home name [] ->
889840 home == ModuleName.string && name == N.string
890841
891842 _ ->
892843 False
893844
894845
895−isList :: Type -> Bool
846+isList :: T.Type -> Bool
896847isList tipe =
897848 case tipe of
898− Type home name [_] ->
849+ T.Type home name [_] ->
899850 home == ModuleName.list && name == N.list
900851
901852 _ ->
@@ -969,7 +920,7 @@ badStringAdd =
969920 )
970921
971922
972−badListAdd :: Localizer -> String -> Type -> (H.Doc, H.Doc)
923+badListAdd :: T.Localizer -> String -> T.Type -> (H.Doc, H.Doc)
973924badListAdd localizer direction tipe =
974925 (
975926 "I cannot do addition with lists:"
@@ -988,7 +939,7 @@ badListAdd localizer direction tipe =
988939 )
989940
990941
991−badListMul :: Localizer -> String -> Type -> (H.Doc, H.Doc)
942+badListMul :: T.Localizer -> String -> T.Type -> (H.Doc, H.Doc)
992943badListMul localizer direction tipe =
993944 badMath localizer "Multiplication" direction "*" tipe
994945 [
@@ -1000,7 +951,7 @@ badListMul localizer direction tipe =
1000951 ]
1001952
1002953
1003−badMath :: Localizer -> String -> String -> String -> Type -> [H.Doc] -> (H.Doc, H.Doc)
954+badMath :: T.Localizer -> String -> String -> String -> T.Type -> [H.Doc] -> (H.Doc, H.Doc)
1004955badMath localizer operation direction op tipe otherHints =
1005956 (
1006957 H.reflow $
@@ -1020,7 +971,7 @@ badMath localizer operation direction op tipe otherHints =
1020971 )
1021972
1022973
1023−badFDiv :: Localizer -> H.Doc -> Type -> (H.Doc, H.Doc)
974+badFDiv :: T.Localizer -> H.Doc -> T.Type -> (H.Doc, H.Doc)
1024975badFDiv localizer direction tipe =
1025976 (
1026977 H.reflow $
@@ -1053,7 +1004,7 @@ badFDiv localizer direction tipe =
10531004 )
10541005
10551006
1056−badIDiv :: Localizer -> H.Doc -> Type -> (H.Doc, H.Doc)
1007+badIDiv :: T.Localizer -> H.Doc -> T.Type -> (H.Doc, H.Doc)
10571008badIDiv localizer direction tipe =
10581009 (
10591010 H.reflow $
@@ -1092,7 +1043,7 @@ badIDiv localizer direction tipe =
10921043-- BAD BOOLS
10931044
10941045
1095−badBool :: Localizer -> H.Doc -> H.Doc -> Type -> (H.Doc, H.Doc)
1046+badBool :: T.Localizer -> H.Doc -> H.Doc -> T.Type -> (H.Doc, H.Doc)
10961047badBool localizer op direction tipe =
10971048 (
10981049 H.reflow $
@@ -1115,14 +1066,14 @@ badBool localizer op direction tipe =
11151066-- BAD COMPARISON
11161067
11171068
1118−badCompLeft :: Localizer -> Thing -> String -> String -> Type -> (H.Doc, H.Doc)
1119−badCompLeft localizer thing op direction tipe =
1069+badCompLeft :: T.Localizer -> Category -> String -> String -> T.Type -> (H.Doc, H.Doc)
1070+badCompLeft localizer category op direction tipe =
11201071 (
11211072 H.reflow $
11221073 "I cannot do a comparison with this value:"
11231074 ,
11241075 H.stack
1125− [ H.reflow $ toDescription thing $
1076+ [ H.reflow $ toDescription category $
11261077 "The " <> direction <> " side of (" <> op <> ") is"
11271078 , indentType localizer tipe
11281079 , H.fillSep
@@ -1139,7 +1090,7 @@ badCompLeft localizer thing op direction tipe =
11391090 )
11401091
11411092
1142−badCompRight :: Localizer -> String -> Type -> Type -> RightDocs
1093+badCompRight :: T.Localizer -> String -> T.Type -> T.Type -> RightDocs
11431094badCompRight localizer op tipe expectedType =
11441095 EmphBoth
11451096 (
@@ -1163,7 +1114,7 @@ badCompRight localizer op tipe expectedType =
11631114-- BAD EQUALITY
11641115
11651116
1166−badEquality :: Localizer -> String -> Type -> Type -> RightDocs
1117+badEquality :: T.Localizer -> String -> T.Type -> T.Type -> RightDocs
11671118badEquality localizer op tipe expectedType =
11681119 EmphBoth
11691120 (
@@ -1195,10 +1146,7 @@ badEquality localizer op tipe expectedType =
11951146-- DIFF TYPES
11961147
11971148
1198−type Localizer = Map.Map N.Name ()
1199−
1200−
1201−typeComparison :: Localizer -> Type -> Type -> ( String, String, [H.Doc] ) -> H.Doc
1149+typeComparison :: T.Localizer -> T.Type -> T.Type -> ( String, String, [H.Doc] ) -> H.Doc
12021150typeComparison localizer actual expected ( iAmSeeing, insteadOf, soHereAreSomeThoughts ) =
12031151 let
12041152 (actualDoc, expectedDoc) =
@@ -1213,7 +1161,7 @@ typeComparison localizer actual expected ( iAmSeeing, insteadOf, soHereAreSomeTh
12131161 ++ soHereAreSomeThoughts
12141162
12151163
1216−indentType :: Localizer -> Type -> H.Doc
1164+indentType :: T.Localizer -> T.Type -> H.Doc
12171165indentType localizer tipe =
12181166 H.indent 4 (H.dullyellow (error "TODO Type.toDoc" localizer tipe))
12191167