on master help sign in
improve style, start improving infinite type alias error message
966c381Evan Czaplicki
src/Transform/Canonicalize/Setup.hs+68−39
@@ -149,47 +149,76 @@ node name tvars alias = ((name, tvars, alias), name, edges alias)
149 Type.Record fs ext -> maybe [] edges ext ++ concatMap (edges . snd) fs
149 Type.Record fs ext -> maybe [] edges ext ++ concatMap (edges . snd) fs
150 Type.Aliased _ t -> edges t
150 Type.Aliased _ t -> edges t
151
151
152+
153+addTypeAliases
154+ :: Module.Name
155+ -> [Node]
156+ -> Environment
152−addTypeAliases :: Module.Name -> [Node] -> Environment -> Canonicalizer [Doc] Environment
157+ -> Canonicalizer [Doc] Environment
153addTypeAliases moduleName nodes environ =
158addTypeAliases moduleName nodes environ =
154− foldM addTypeAlias environ (Graph.stronglyConnComp nodes)
159+ foldM (addTypeAlias moduleName) environ (Graph.stronglyConnComp nodes)
155− where
160+
161+
162+addTypeAlias
163+ :: Module.Name
164+ -> Environment
156− addTypeAlias :: Environment -> Graph.SCC (String, [String], Type.RawType)
165+ -> Graph.SCC (String, [String], Type.RawType)
157− -> Canonicalizer [Doc] Environment
166+ -> Canonicalizer [Doc] Environment
158− addTypeAlias env scc =
167+addTypeAlias moduleName env scc =
159− case Graph.flattenSCC scc of
168+ case Graph.flattenSCC scc of
160− [(name, tvars, alias)] ->
169+ [(name, tvars, alias)] ->
161− do alias' <- Env.onError throw (Canonicalize.tipe env alias)
170+ do alias' <- Env.onError throw (Canonicalize.tipe env alias)
162− let value = (Var.Canonical (Var.Module moduleName) name, tvars, alias')
171+ let value = (Var.Canonical (Var.Module moduleName) name, tvars, alias')
163− return $ env { _aliases = insert name value (_aliases env) }
172+ return $ env { _aliases = insert name value (_aliases env) }
164− where
165− throw err =
166− let msg = "Problem with type alias '" ++ name ++ "':"
167− in P.vcat [ P.text msg, P.text err ]
168−
169− aliases ->
170− throwError [ P.vcat [ P.text (eightyCharLines 0 msg1)
171− , indented (map typeAlias aliases)
172− , P.text (eightyCharLines 0 msg2)
173− , indented (map datatype aliases)
174− , P.text (eightyCharLines 0 msg3)
175− ]
176− ]
177−
178− typeAlias (n,ts,t) = D.TypeAlias n ts t
179− datatype (n,ts,t) = D.Datatype n ts [(n,[t])]
180−
181− indented :: [D.ValidDecl] -> Doc
182− indented decls = P.vcat (map prty decls) <> P.text "\n"
183 where
173 where
174+ throw err =
175+ let msg = "Problem with type alias '" ++ name ++ "':"
176+ in P.vcat [ P.text msg, P.text err ]
177+
178+ aliases ->
179+ throwError
180+ [ P.vcat
181+ [ P.text (eightyCharLines 0 msg1)
182+ , indented (map typeAlias aliases)
183+ , P.text (eightyCharLines 0 msg2)
184+ , indented (map datatype aliases)
185+ , P.text (eightyCharLines 0 msg3)
186+ ]
187+ ]
188+
189+
190+typeAlias
191+ :: (String, [String], Type.Type var)
192+ -> D.Declaration' port def var
193+typeAlias (n,ts,t) =
194+ D.TypeAlias n ts t
195+
196+
197+datatype
198+ :: (String, [String], Type.Type var)
199+ -> D.Declaration' port def var
200+datatype (n,ts,t) =
201+ D.Datatype n ts [(n,[t])]
202+
203+
204+indented :: [D.ValidDecl] -> Doc
205+indented decls = P.vcat (map prty decls) <> P.text "\n"
206+ where
184− prty decl = P.text "\n " <> pretty decl
207+ prty decl = P.text "\n " <> pretty decl
185−
208+
209+
210+msg1 :: String
186− msg1 = "The following type aliases are mutually recursive, forming an \
211+msg1 = "The following type aliases are mutually recursive, forming an \
187− \infinite type. When you expand them, they just keep getting bigger:"
212+ \infinite type. When you expand them, they just keep getting bigger:"
213+
214+msg2 :: String
188− msg2 = "Instead, you can try something like this:"
215+msg2 = "Instead, you can try something like this:"
216+
217+msg3 :: String
218+msg3 =
189− msg3 = "It looks very similar, but an algebraic data type (ADT) \
219+ "It looks very similar, but the 'type' keyword creates a brand new type. \
190− \actually creates a new type. Unlike with a type alias, this \
220+ \By giving the type an explicit name, we can avoid infinitely expanding \
191− \freshly created type is meaningful on its own, so an ADT \
221+ \it during type inference."
192− \does not need to be expanded."
193
222
194
223
195-- When canonicalizing, all _values should be Local, but all _adts and _patterns
224-- When canonicalizing, all _values should be Local, but all _adts and _patterns