on master help sign in
Improve explanation on infinite type aliases
903973dEvan Czaplicki
src/Transform/Canonicalize/Setup.hs+22−14
@@ -1,5 +1,9 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2−module Transform.Canonicalize.Setup (environment) where
2+module Transform.Canonicalize.Setup
3+ ( environment
4+ , typeAliasErrorSegue
5+ , typeAliasErrorExplanation
6+ ) where
3
7
4import Control.Arrow (first)
8import Control.Arrow (first)
5import Control.Monad (foldM)
9import Control.Monad (foldM)
@@ -178,11 +182,11 @@ addTypeAlias moduleName env scc =
178 aliases ->
182 aliases ->
179 throwError
183 throwError
180 [ P.vcat
184 [ P.vcat
181− [ P.text (eightyCharLines 0 msg1)
185+ [ P.text (eightyCharLines 0 mutuallyRecursiveMessage)
182 , indented (map typeAlias aliases)
186 , indented (map typeAlias aliases)
183− , P.text (eightyCharLines 0 msg2)
187+ , P.text (eightyCharLines 0 typeAliasErrorSegue)
184 , indented (map datatype aliases)
188 , indented (map datatype aliases)
185− , P.text (eightyCharLines 0 msg3)
189+ , P.text (eightyCharLines 0 typeAliasErrorExplanation)
186 ]
190 ]
187 ]
191 ]
188
192
@@ -207,18 +211,22 @@ indented decls = P.vcat (map prty decls) <> P.text "\n"
207 prty decl = P.text "\n " <> pretty decl
211 prty decl = P.text "\n " <> pretty decl
208
212
209
213
210−msg1 :: String
214+mutuallyRecursiveMessage :: String
215+mutuallyRecursiveMessage =
211−msg1 = "The following type aliases are mutually recursive, forming an \
216+ "The following type aliases are mutually recursive, forming an \
212− \infinite type. When you expand them, they just keep getting bigger:"
217+ \infinite type. When you expand them, they just keep getting bigger:"
213
218
214−msg2 :: String
215−msg2 = "Instead, you can try something like this:"
216
219
217−msg3 :: String
220+typeAliasErrorSegue :: String
218−msg3 =
221+typeAliasErrorSegue =
222+ "Try this instead:"
223+
224+
225+typeAliasErrorExplanation :: String
226+typeAliasErrorExplanation =
219− "It looks very similar, but the 'type' keyword creates a brand new type. \
227+ "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 \
228+ \not just an alias for an existing one. This lets us avoid infinitely \
221− \it during type inference."
229+ \expanding it during type inference."
222
230
223
231
224-- When canonicalizing, all _values should be Local, but all _adts and _patterns
232-- 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
12import qualified AST.Type as T
12import qualified AST.Type as T
13import qualified AST.Variable as Var
13import qualified AST.Variable as Var
14import qualified Transform.Expression as Expr
14import qualified Transform.Expression as Expr
15+import qualified Transform.Canonicalize.Setup as Setup
15
16
16import AST.PrettyPrint
17import AST.PrettyPrint
17import Elm.Utils ((|>))
18import Elm.Utils ((|>))
@@ -162,18 +163,15 @@ infiniteTypeAliases decls =
162 indented decl = P.text "\n " <> pretty decl <> P.text "\n"
163 indented decl = P.text "\n " <> pretty decl <> P.text "\n"
163
164
164 report name args tipe =
165 report name args tipe =
166+ P.vcat
165− P.vcat [ P.text $ eightyCharLines 0 msg1
167+ [ P.text $ eightyCharLines 0 msg1
166− , indented $ D.TypeAlias name args tipe
168+ , indented $ D.TypeAlias name args tipe
167− , P.text $ eightyCharLines 0 msg2
169+ , P.text $ eightyCharLines 0 Setup.typeAliasErrorSegue
168− , indented $ D.Datatype name args [(name,[tipe])]
170+ , indented $ D.Datatype name args [(name,[tipe])]
169− , P.text $ eightyCharLines 0 msg3 ++ "\n"
171+ , P.text $ eightyCharLines 0 Setup.typeAliasErrorExplanation ++ "\n"
170− ]
172+ ]
171 where
173 where
174+ msg1 =
172− msg1 = "Type alias '" ++ name ++ "' is an infinite type. " ++
175+ "Type alias '" ++ name ++ "' is an infinite type. " ++
173− "Notice that it appears in its own definition, so when \
176+ "Notice that it appears in its own definition, so when \
174− \you expand it, it just keeps getting bigger:"
177+ \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."