on master help sign in
Improve explanation on infinite type aliases
commit903973dcd57063756b78566db1b8c7de4bff7e02 authorEvan Czaplicki <info@elm-lang.org> dateSun 23 Nov 2014, 22:01 (12 years ago) parent393337f
2 files changed+34−28
src/Transform/Canonicalize/Setup.hs+22−14 src/Transform/Check.hs+12−14
src/Transform/Canonicalize/Setup.hs+22−14
@@ -1,5 +1,9 @@
11{-# OPTIONS_GHC -Wall #-}
2−module Transform.Canonicalize.Setup (environment) where
2+module Transform.Canonicalize.Setup
3+ ( environment
4+ , typeAliasErrorSegue
5+ , typeAliasErrorExplanation
6+ ) where
37
48import Control.Arrow (first)
59import Control.Monad (foldM)
@@ -178,11 +182,11 @@ addTypeAlias moduleName env scc =
178182 aliases ->
179183 throwError
180184 [ P.vcat
181− [ P.text (eightyCharLines 0 msg1)
185+ [ P.text (eightyCharLines 0 mutuallyRecursiveMessage)
182186 , indented (map typeAlias aliases)
183− , P.text (eightyCharLines 0 msg2)
187+ , P.text (eightyCharLines 0 typeAliasErrorSegue)
184188 , indented (map datatype aliases)
185− , P.text (eightyCharLines 0 msg3)
189+ , P.text (eightyCharLines 0 typeAliasErrorExplanation)
186190 ]
187191 ]
188192
@@ -207,18 +211,22 @@ indented decls = P.vcat (map prty decls) <> P.text "\n"
207211 prty decl = P.text "\n " <> pretty decl
208212
209213
210−msg1 :: String
211−msg1 = "The following type aliases are mutually recursive, forming an \
212− \infinite type. When you expand them, they just keep getting bigger:"
214+mutuallyRecursiveMessage :: String
215+mutuallyRecursiveMessage =
216+ "The following type aliases are mutually recursive, forming an \
217+ \infinite type. When you expand them, they just keep getting bigger:"
213218
214−msg2 :: String
215−msg2 = "Instead, you can try something like this:"
216219
217−msg3 :: String
218−msg3 =
219− "It looks very similar, but the 'type' keyword creates a brand new type. \
220− \By giving the type an explicit name, we can avoid infinitely expanding \
221− \it during type inference."
220+typeAliasErrorSegue :: String
221+typeAliasErrorSegue =
222+ "Try this instead:"
223+
224+
225+typeAliasErrorExplanation :: String
226+typeAliasErrorExplanation =
227+ "It looks very similar, but the 'type' keyword creates a brand new type, \
228+ \not just an alias for an existing one. This lets us avoid infinitely \
229+ \expanding it during type inference."
222230
223231
224232-- When canonicalizing, all _values should be Local, but all _adts and _patterns
src/Transform/Check.hs+12−14
@@ -12,6 +12,7 @@ import qualified AST.Pattern as Pattern
1212import qualified AST.Type as T
1313import qualified AST.Variable as Var
1414import qualified Transform.Expression as Expr
15+import qualified Transform.Canonicalize.Setup as Setup
1516
1617import AST.PrettyPrint
1718import Elm.Utils ((|>))
@@ -162,18 +163,15 @@ infiniteTypeAliases decls =
162163 indented decl = P.text "\n " <> pretty decl <> P.text "\n"
163164
164165 report name args tipe =
165− P.vcat [ P.text $ eightyCharLines 0 msg1
166− , indented $ D.TypeAlias name args tipe
167− , P.text $ eightyCharLines 0 msg2
168− , indented $ D.Datatype name args [(name,[tipe])]
169− , P.text $ eightyCharLines 0 msg3 ++ "\n"
170− ]
166+ P.vcat
167+ [ P.text $ eightyCharLines 0 msg1
168+ , indented $ D.TypeAlias name args tipe
169+ , P.text $ eightyCharLines 0 Setup.typeAliasErrorSegue
170+ , indented $ D.Datatype name args [(name,[tipe])]
171+ , P.text $ eightyCharLines 0 Setup.typeAliasErrorExplanation ++ "\n"
172+ ]
171173 where
172− msg1 = "Type alias '" ++ name ++ "' is an infinite type. " ++
173− "Notice that it appears in its own definition, so when \
174− \you expand it, it just keeps getting bigger:"
175− msg2 = "Try this instead:"
176− msg3 = "It looks very similar, but an algebraic data type (ADT) \
177− \actually creates a new type. Unlike with a type alias, this \
178− \freshly created type is meaningful on its own, so an ADT \
179− \does not need to be expanded."
174+ msg1 =
175+ "Type alias '" ++ name ++ "' is an infinite type. " ++
176+ "Notice that it appears in its own definition, so when \
177+ \you expand it, it just keeps getting bigger:"