@@ -1,5 +1,6 @@
11module Type.Type where
22
3+import qualified Data.Char as Char
34import qualified Data.List as List
45import qualified Data.Map as Map
56import qualified Data.UnionFind.IO as UF
@@ -336,3 +337,31 @@ instance Crawl Descriptor where
336337 structure' <- traverse (crawl nextState) (structure desc)
337338 put state'
338339 return $ desc { name = name', structure = structure' }
340+
341+
342+toSrcType :: Variable -> IO Src.Type
343+toSrcType variable = do
344+ desc <- UF.descriptor =<< addNames variable
345+ case structure desc of
346+ Just term ->
347+ case term of
348+ App1 a b -> do
349+ Src.Data name ts <- toSrcType a
350+ b' <- toSrcType b
351+ return (Src.Data name (ts ++ [b']))
352+ Fun1 a b -> Src.Lambda <$> toSrcType a <*> toSrcType b
353+ Var1 a -> toSrcType a
354+ EmptyRecord1 -> return Src.EmptyRecord
355+ Record1 fs ext -> do
356+ fs' <- traverse (mapM toSrcType) fs
357+ let fs'' = concat [ map ((,) name) ts | (name,ts) <- Map.toList fs' ]
358+ Src.Record fs'' <$> toSrcType ext
359+ Nothing ->
360+ case name desc of
361+ Just x@(c:cs) | Char.isLower c -> return (Src.Var x)
362+ | otherwise -> return (Src.Data x [])
363+ Nothing ->
364+ error $ concat
365+ [ "Problem converting the following type "
366+ , "from a type-checker type to a source-syntax type:"
367+ , P.render (pretty Never variable) ]