2013-07-03 12:35:51 +00:00
|
|
|
module Type.Type where
|
|
|
|
|
2013-07-29 18:23:42 +00:00
|
|
|
import qualified Data.Char as Char
|
2013-07-08 14:47:44 +00:00
|
|
|
import qualified Data.List as List
|
2013-07-03 12:35:51 +00:00
|
|
|
import qualified Data.Map as Map
|
|
|
|
import qualified Data.UnionFind.IO as UF
|
2013-07-19 15:48:41 +00:00
|
|
|
import Type.PrettyPrint
|
2013-07-08 14:47:44 +00:00
|
|
|
import Text.PrettyPrint as P
|
2013-07-03 12:35:51 +00:00
|
|
|
import System.IO.Unsafe
|
2013-07-08 14:47:44 +00:00
|
|
|
import Control.Applicative ((<$>),(<*>))
|
|
|
|
import Control.Monad.State
|
2013-08-26 03:23:49 +00:00
|
|
|
import Control.Monad.Error
|
2013-07-08 14:47:44 +00:00
|
|
|
import Data.Traversable (traverse)
|
2013-07-30 23:01:20 +00:00
|
|
|
import SourceSyntax.Location
|
2013-07-19 17:17:51 +00:00
|
|
|
import SourceSyntax.Helpers (isTuple)
|
2013-07-21 04:08:08 +00:00
|
|
|
import qualified SourceSyntax.Type as Src
|
2013-07-03 12:35:51 +00:00
|
|
|
|
|
|
|
data Term1 a
|
|
|
|
= App1 a a
|
|
|
|
| Fun1 a a
|
|
|
|
| Var1 a
|
|
|
|
| EmptyRecord1
|
|
|
|
| Record1 (Map.Map String [a]) a
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
data TermN a
|
|
|
|
= VarN a
|
|
|
|
| TermN (Term1 (TermN a))
|
2013-07-08 14:47:44 +00:00
|
|
|
deriving Show
|
2013-07-03 12:35:51 +00:00
|
|
|
|
2013-07-26 22:14:38 +00:00
|
|
|
record :: Map.Map String [TermN a] -> TermN a -> TermN a
|
2013-07-03 12:35:51 +00:00
|
|
|
record fs rec = TermN (Record1 fs rec)
|
|
|
|
|
2013-07-21 04:08:08 +00:00
|
|
|
type Type = TermN Variable
|
|
|
|
type Variable = UF.Point Descriptor
|
|
|
|
|
2013-07-03 12:35:51 +00:00
|
|
|
type SchemeName = String
|
|
|
|
type TypeName = String
|
|
|
|
|
2013-07-30 23:01:20 +00:00
|
|
|
type Constraint a b = Located (BasicConstraint a b)
|
|
|
|
data BasicConstraint a b
|
2013-07-03 12:35:51 +00:00
|
|
|
= CTrue
|
2013-07-19 15:48:41 +00:00
|
|
|
| CSaveEnv
|
2013-07-03 12:35:51 +00:00
|
|
|
| CEqual a a
|
|
|
|
| CAnd [Constraint a b]
|
|
|
|
| CLet [Scheme a b] (Constraint a b)
|
|
|
|
| CInstance SchemeName a
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
data Scheme a b = Scheme {
|
|
|
|
rigidQuantifiers :: [b],
|
|
|
|
flexibleQuantifiers :: [b],
|
|
|
|
constraint :: Constraint a b,
|
2013-07-08 14:47:44 +00:00
|
|
|
header :: Map.Map String a
|
2013-07-03 12:35:51 +00:00
|
|
|
} deriving Show
|
|
|
|
|
2013-07-21 04:08:08 +00:00
|
|
|
type TypeConstraint = Constraint Type Variable
|
|
|
|
type TypeScheme = Scheme Type Variable
|
|
|
|
|
2013-08-03 18:41:33 +00:00
|
|
|
monoscheme headers = Scheme [] [] (noneNoDocs CTrue) headers
|
2013-07-03 17:51:38 +00:00
|
|
|
|
2013-07-25 12:54:21 +00:00
|
|
|
infixl 8 /\
|
|
|
|
|
|
|
|
(/\) :: Constraint a b -> Constraint a b -> Constraint a b
|
2014-01-04 15:16:21 +00:00
|
|
|
a@(L _ c1) /\ b@(L _ c2) =
|
2013-07-30 23:01:20 +00:00
|
|
|
case (c1, c2) of
|
|
|
|
(CTrue, _) -> b
|
|
|
|
(_, CTrue) -> a
|
2013-08-03 18:41:33 +00:00
|
|
|
_ -> mergeOldDocs a b (CAnd [a,b])
|
2013-07-25 12:54:21 +00:00
|
|
|
|
|
|
|
infixr 9 ==>
|
|
|
|
(==>) :: Type -> Type -> Type
|
|
|
|
a ==> b = TermN (Fun1 a b)
|
|
|
|
|
|
|
|
f <| a = TermN (App1 f a)
|
|
|
|
|
2013-07-03 12:35:51 +00:00
|
|
|
data Descriptor = Descriptor {
|
|
|
|
structure :: Maybe (Term1 Variable),
|
|
|
|
rank :: Int,
|
|
|
|
flex :: Flex,
|
2013-07-07 10:52:48 +00:00
|
|
|
name :: Maybe TypeName,
|
2013-07-09 19:52:05 +00:00
|
|
|
copy :: Maybe Variable,
|
2013-07-07 10:52:48 +00:00
|
|
|
mark :: Int
|
2013-07-08 14:47:44 +00:00
|
|
|
} deriving Show
|
2013-07-03 12:35:51 +00:00
|
|
|
|
2013-07-07 10:52:48 +00:00
|
|
|
noRank = -1
|
|
|
|
outermostRank = 0 :: Int
|
|
|
|
|
2013-07-09 19:52:05 +00:00
|
|
|
noMark = 0
|
|
|
|
initialMark = 1
|
|
|
|
|
2013-07-25 16:07:04 +00:00
|
|
|
data Flex = Rigid | Flexible | Constant | Is SuperType
|
2013-07-24 23:24:16 +00:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
data SuperType = Number | Comparable | Appendable
|
2013-07-07 10:52:48 +00:00
|
|
|
deriving (Show, Eq)
|
2013-07-03 12:35:51 +00:00
|
|
|
|
2014-01-04 15:16:21 +00:00
|
|
|
namedVar :: Flex -> String -> IO Variable
|
2013-07-19 15:48:41 +00:00
|
|
|
namedVar flex name = UF.fresh $ Descriptor {
|
2013-07-03 12:35:51 +00:00
|
|
|
structure = Nothing,
|
|
|
|
rank = noRank,
|
2013-07-19 15:48:41 +00:00
|
|
|
flex = flex,
|
2013-07-07 10:52:48 +00:00
|
|
|
name = Just name,
|
2013-07-09 19:52:05 +00:00
|
|
|
copy = Nothing,
|
|
|
|
mark = noMark
|
2013-07-03 12:35:51 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 15:16:21 +00:00
|
|
|
var :: Flex -> IO Variable
|
2013-07-25 16:07:04 +00:00
|
|
|
var flex = UF.fresh $ Descriptor {
|
2013-07-03 12:35:51 +00:00
|
|
|
structure = Nothing,
|
|
|
|
rank = noRank,
|
2013-07-25 16:07:04 +00:00
|
|
|
flex = flex,
|
2013-07-07 10:52:48 +00:00
|
|
|
name = Nothing,
|
2013-07-09 19:52:05 +00:00
|
|
|
copy = Nothing,
|
|
|
|
mark = noMark
|
2013-07-03 12:35:51 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 15:16:21 +00:00
|
|
|
structuredVar :: Term1 Variable -> IO Variable
|
2013-07-26 17:04:43 +00:00
|
|
|
structuredVar structure = UF.fresh $ Descriptor {
|
|
|
|
structure = Just structure,
|
|
|
|
rank = noRank,
|
|
|
|
flex = Flexible,
|
|
|
|
name = Nothing,
|
|
|
|
copy = Nothing,
|
|
|
|
mark = noMark
|
|
|
|
}
|
|
|
|
|
2013-07-03 17:51:38 +00:00
|
|
|
|
2013-07-03 12:35:51 +00:00
|
|
|
-- ex qs constraint == exists qs. constraint
|
|
|
|
ex :: [Variable] -> TypeConstraint -> TypeConstraint
|
2013-07-30 23:01:20 +00:00
|
|
|
ex fqs constraint@(L s _) = L s $ CLet [Scheme [] fqs constraint Map.empty] (L s CTrue)
|
2013-07-03 12:35:51 +00:00
|
|
|
|
|
|
|
-- fl qs constraint == forall qs. constraint
|
|
|
|
fl :: [Variable] -> TypeConstraint -> TypeConstraint
|
2013-07-30 23:01:20 +00:00
|
|
|
fl rqs constraint@(L s _) = L s $ CLet [Scheme rqs [] constraint Map.empty] (L s CTrue)
|
2013-07-03 12:35:51 +00:00
|
|
|
|
2013-08-26 03:23:49 +00:00
|
|
|
exists :: Error e => (Type -> ErrorT e IO TypeConstraint) -> ErrorT e IO TypeConstraint
|
2013-07-03 12:35:51 +00:00
|
|
|
exists f = do
|
2013-08-26 03:23:49 +00:00
|
|
|
v <- liftIO $ var Flexible
|
2013-07-03 12:35:51 +00:00
|
|
|
ex [v] <$> f (VarN v)
|
|
|
|
|
2013-07-17 17:26:42 +00:00
|
|
|
|
2013-07-03 12:35:51 +00:00
|
|
|
instance Show a => Show (UF.Point a) where
|
|
|
|
show point = unsafePerformIO $ fmap show (UF.descriptor point)
|
|
|
|
|
2013-07-08 14:47:44 +00:00
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
instance PrettyType a => PrettyType (UF.Point a) where
|
|
|
|
pretty when point = unsafePerformIO $ fmap (pretty when) (UF.descriptor point)
|
|
|
|
|
|
|
|
|
2013-07-30 23:01:20 +00:00
|
|
|
instance PrettyType a => PrettyType (Located a) where
|
|
|
|
pretty when (L _ e) = pretty when e
|
|
|
|
|
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
instance PrettyType a => PrettyType (Term1 a) where
|
|
|
|
pretty when term =
|
|
|
|
let prty = pretty Never in
|
2013-07-08 14:47:44 +00:00
|
|
|
case term of
|
2013-07-19 17:17:51 +00:00
|
|
|
App1 f x | P.render px == "_List" -> P.brackets (pretty Never x)
|
|
|
|
| otherwise -> parensIf needed (px <+> pretty App x)
|
2013-07-09 19:59:58 +00:00
|
|
|
where
|
2013-07-19 17:17:51 +00:00
|
|
|
px = prty f
|
2013-07-19 15:48:41 +00:00
|
|
|
needed = case when of
|
|
|
|
App -> True
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
Fun1 arg body ->
|
|
|
|
parensIf needed (pretty Fn arg <+> P.text "->" <+> prty body)
|
|
|
|
where
|
|
|
|
needed = case when of
|
|
|
|
Never -> False
|
|
|
|
_ -> True
|
|
|
|
|
|
|
|
Var1 x -> prty x
|
|
|
|
|
2013-07-08 14:47:44 +00:00
|
|
|
EmptyRecord1 -> P.braces P.empty
|
2013-07-19 15:48:41 +00:00
|
|
|
|
2013-07-26 17:04:43 +00:00
|
|
|
Record1 fields ext -> P.braces (extend <+> commaSep prettyFields)
|
2013-07-08 14:47:44 +00:00
|
|
|
where
|
2013-07-26 17:04:43 +00:00
|
|
|
prettyExt = prty ext
|
|
|
|
extend | P.render prettyExt == "{}" = P.empty
|
|
|
|
| otherwise = prettyExt <+> P.text "|"
|
2013-08-14 07:44:40 +00:00
|
|
|
mkPretty f t = P.text (reprime f) <+> P.text ":" <+> prty t
|
2013-07-08 14:47:44 +00:00
|
|
|
prettyFields = concatMap (\(f,ts) -> map (mkPretty f) ts) (Map.toList fields)
|
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
|
|
|
|
instance PrettyType a => PrettyType (TermN a) where
|
|
|
|
pretty when term =
|
2013-07-08 14:47:44 +00:00
|
|
|
case term of
|
2013-07-19 15:48:41 +00:00
|
|
|
VarN x -> pretty when x
|
|
|
|
TermN t1 -> pretty when t1
|
2013-07-08 14:47:44 +00:00
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
|
|
|
|
instance PrettyType Descriptor where
|
|
|
|
pretty when desc =
|
2013-07-08 14:47:44 +00:00
|
|
|
case (structure desc, name desc) of
|
2013-07-19 15:48:41 +00:00
|
|
|
(Just term, _) -> pretty when term
|
2013-08-14 07:44:40 +00:00
|
|
|
(_, Just name) -> if not (isTuple name) then P.text (reprime name) else
|
2013-07-19 17:17:51 +00:00
|
|
|
P.parens . P.text $ replicate (read (drop 6 name) - 1) ','
|
2013-07-08 14:47:44 +00:00
|
|
|
_ -> P.text "?"
|
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
|
2013-07-30 23:01:20 +00:00
|
|
|
instance (PrettyType a, PrettyType b) => PrettyType (BasicConstraint a b) where
|
2013-07-19 15:48:41 +00:00
|
|
|
pretty _ constraint =
|
|
|
|
let prty = pretty Never in
|
2013-07-08 14:47:44 +00:00
|
|
|
case constraint of
|
|
|
|
CTrue -> P.text "True"
|
2013-07-19 15:48:41 +00:00
|
|
|
CSaveEnv -> P.text "SaveTheEnvironment!!!"
|
|
|
|
CEqual a b -> prty a <+> P.text "=" <+> prty b
|
2013-07-08 14:47:44 +00:00
|
|
|
CAnd [] -> P.text "True"
|
|
|
|
|
2013-07-11 21:30:18 +00:00
|
|
|
CAnd cs ->
|
2013-07-19 15:48:41 +00:00
|
|
|
P.parens . P.sep $ P.punctuate (P.text " and") (map (pretty Never) cs)
|
2013-07-08 14:47:44 +00:00
|
|
|
|
2013-07-30 23:01:20 +00:00
|
|
|
CLet [Scheme [] fqs constraint header] (L _ CTrue) | Map.null header ->
|
2013-07-19 15:48:41 +00:00
|
|
|
P.sep [ binder, pretty Never c ]
|
2013-07-08 14:47:44 +00:00
|
|
|
where
|
2013-07-30 23:01:20 +00:00
|
|
|
mergeExists vs (L _ c) =
|
2013-07-11 21:30:18 +00:00
|
|
|
case c of
|
2013-07-30 23:01:20 +00:00
|
|
|
CLet [Scheme [] fqs' c' _] (L _ CTrue) -> mergeExists (vs ++ fqs') c'
|
2013-07-11 21:30:18 +00:00
|
|
|
_ -> (vs, c)
|
|
|
|
|
|
|
|
(fqs', c) = mergeExists fqs constraint
|
|
|
|
|
|
|
|
binder = if null fqs' then P.empty else
|
2013-07-19 15:48:41 +00:00
|
|
|
P.text "\x2203" <+> P.hsep (map (pretty Never) fqs') <> P.text "."
|
2013-07-08 14:47:44 +00:00
|
|
|
|
|
|
|
CLet schemes constraint ->
|
2013-07-19 15:48:41 +00:00
|
|
|
P.fsep [ P.hang (P.text "let") 4 (P.brackets . commaSep $ map (pretty Never) schemes)
|
|
|
|
, P.text "in", pretty Never constraint ]
|
2013-07-08 14:47:44 +00:00
|
|
|
|
|
|
|
CInstance name tipe ->
|
2013-07-19 15:48:41 +00:00
|
|
|
P.text name <+> P.text "<" <+> prty tipe
|
2013-07-08 14:47:44 +00:00
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
instance (PrettyType a, PrettyType b) => PrettyType (Scheme a b) where
|
2013-07-30 23:01:20 +00:00
|
|
|
pretty _ (Scheme rqs fqs (L _ constraint) headers) =
|
2013-07-11 21:30:18 +00:00
|
|
|
P.sep [ forall, cs, headers' ]
|
2013-07-08 14:47:44 +00:00
|
|
|
where
|
2013-07-19 15:48:41 +00:00
|
|
|
prty = pretty Never
|
|
|
|
|
2013-07-11 21:30:18 +00:00
|
|
|
forall = if null rqs && null fqs then P.empty else
|
2013-07-19 15:48:41 +00:00
|
|
|
P.text "\x2200" <+> frees <+> rigids
|
2013-07-11 21:30:18 +00:00
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
frees = P.hsep $ map prty fqs
|
|
|
|
rigids = if null rqs then P.empty else P.braces . P.hsep $ map prty rqs
|
2013-07-11 21:30:18 +00:00
|
|
|
|
2013-07-08 14:47:44 +00:00
|
|
|
cs = case constraint of
|
|
|
|
CTrue -> P.empty
|
|
|
|
CAnd [] -> P.empty
|
2013-07-19 15:48:41 +00:00
|
|
|
_ -> P.brackets (pretty Never constraint)
|
2013-07-11 21:30:18 +00:00
|
|
|
|
2013-07-08 14:47:44 +00:00
|
|
|
headers' = if Map.size headers > 0 then dict else P.empty
|
2013-07-19 15:48:41 +00:00
|
|
|
dict = P.parens . commaSep . map prettyPair $ Map.toList headers
|
|
|
|
prettyPair (n,t) = P.text n <+> P.text ":" <+> pretty Never t
|
|
|
|
|
2013-07-08 14:47:44 +00:00
|
|
|
|
2013-07-19 15:48:41 +00:00
|
|
|
extraPretty :: (PrettyType t, Crawl t) => t -> IO Doc
|
2013-07-21 04:08:08 +00:00
|
|
|
extraPretty t = pretty Never <$> addNames t
|
|
|
|
|
|
|
|
addNames :: (Crawl t) => t -> IO t
|
|
|
|
addNames value = do
|
2013-07-25 16:07:04 +00:00
|
|
|
(rawVars, _, _, _) <- execStateT (crawl getNames value) ([], 0, 0, 0)
|
2013-07-08 14:47:44 +00:00
|
|
|
let vars = map head . List.group $ List.sort rawVars
|
2013-07-19 15:48:41 +00:00
|
|
|
suffix s = map (++s) (map (:[]) ['a'..'z'])
|
|
|
|
allVars = concatMap suffix $ ["","'","_"] ++ map show [0..]
|
2013-07-08 14:47:44 +00:00
|
|
|
okayVars = filter (`notElem` vars) allVars
|
2013-07-25 16:07:04 +00:00
|
|
|
runStateT (crawl rename value) (okayVars, 0, 0, 0)
|
2013-07-21 04:08:08 +00:00
|
|
|
return value
|
2013-07-08 14:47:44 +00:00
|
|
|
where
|
2013-07-25 16:07:04 +00:00
|
|
|
getNames desc state@(vars, a, b, c) =
|
|
|
|
let name' = name desc in
|
|
|
|
case name' of
|
|
|
|
Just var -> (name', (var:vars, a, b, c))
|
|
|
|
Nothing -> (name', state)
|
|
|
|
|
|
|
|
rename desc state@(vars, a, b, c) =
|
|
|
|
case name desc of
|
|
|
|
Just var -> (Just var, state)
|
|
|
|
Nothing ->
|
|
|
|
case flex desc of
|
|
|
|
Is Number -> (Just $ "number" ++ replicate a '\'', (vars, a+1, b, c))
|
|
|
|
Is Comparable -> (Just $ "comparable" ++ replicate b '\'', (vars, a, b+1, c))
|
|
|
|
Is Appendable -> (Just $ "appendable" ++ replicate c '\'', (vars, a, b, c+1))
|
2013-08-10 21:18:59 +00:00
|
|
|
other -> (Just $ head vars, (tail vars, a, b, c))
|
|
|
|
where mark = case other of
|
|
|
|
Flexible -> ""
|
|
|
|
Rigid -> "!"
|
|
|
|
Constant -> "#"
|
2013-07-08 14:47:44 +00:00
|
|
|
|
2013-07-21 04:08:08 +00:00
|
|
|
|
2013-07-25 16:07:04 +00:00
|
|
|
type CrawlState = ([String], Int, Int, Int)
|
2013-07-10 22:31:56 +00:00
|
|
|
|
|
|
|
-- Code for traversing all the type data-structures and giving
|
|
|
|
-- names to the variables embedded deep in there.
|
|
|
|
class Crawl t where
|
2013-07-25 16:07:04 +00:00
|
|
|
crawl :: (Descriptor -> CrawlState -> (Maybe TypeName, CrawlState))
|
2013-07-10 22:31:56 +00:00
|
|
|
-> t
|
2013-07-25 16:07:04 +00:00
|
|
|
-> StateT CrawlState IO t
|
2013-07-10 22:31:56 +00:00
|
|
|
|
2013-07-30 23:01:20 +00:00
|
|
|
instance Crawl a => Crawl (Located a) where
|
|
|
|
crawl nextState (L s e) = L s <$> crawl nextState e
|
|
|
|
|
|
|
|
instance (Crawl t, Crawl v) => Crawl (BasicConstraint t v) where
|
2013-07-10 22:31:56 +00:00
|
|
|
crawl nextState constraint =
|
|
|
|
let rnm = crawl nextState in
|
|
|
|
case constraint of
|
|
|
|
CTrue -> return CTrue
|
2013-07-19 15:48:41 +00:00
|
|
|
CSaveEnv -> return CSaveEnv
|
2013-07-10 22:31:56 +00:00
|
|
|
CEqual a b -> CEqual <$> rnm a <*> rnm b
|
|
|
|
CAnd cs -> CAnd <$> crawl nextState cs
|
|
|
|
CLet schemes c -> CLet <$> crawl nextState schemes <*> crawl nextState c
|
|
|
|
CInstance name tipe -> CInstance name <$> rnm tipe
|
|
|
|
|
|
|
|
instance Crawl a => Crawl [a] where
|
|
|
|
crawl nextState list = mapM (crawl nextState) list
|
|
|
|
|
|
|
|
instance (Crawl t, Crawl v) => Crawl (Scheme t v) where
|
|
|
|
crawl nextState (Scheme rqs fqs c headers) =
|
|
|
|
let rnm = crawl nextState in
|
|
|
|
Scheme <$> rnm rqs <*> rnm fqs <*> crawl nextState c <*> return headers
|
|
|
|
|
|
|
|
instance Crawl t => Crawl (TermN t) where
|
|
|
|
crawl nextState tipe =
|
|
|
|
case tipe of
|
|
|
|
VarN x -> VarN <$> crawl nextState x
|
|
|
|
TermN term -> TermN <$> crawl nextState term
|
|
|
|
|
|
|
|
instance Crawl t => Crawl (Term1 t) where
|
|
|
|
crawl nextState term =
|
|
|
|
let rnm = crawl nextState in
|
|
|
|
case term of
|
|
|
|
App1 a b -> App1 <$> rnm a <*> rnm b
|
|
|
|
Fun1 a b -> Fun1 <$> rnm a <*> rnm b
|
|
|
|
Var1 a -> Var1 <$> rnm a
|
|
|
|
EmptyRecord1 -> return EmptyRecord1
|
|
|
|
Record1 fields ext ->
|
|
|
|
Record1 <$> traverse (mapM rnm) fields <*> rnm ext
|
|
|
|
|
|
|
|
instance Crawl a => Crawl (UF.Point a) where
|
|
|
|
crawl nextState point = do
|
|
|
|
desc <- liftIO $ UF.descriptor point
|
|
|
|
desc' <- crawl nextState desc
|
|
|
|
liftIO $ UF.setDescriptor point desc'
|
|
|
|
return point
|
|
|
|
|
|
|
|
instance Crawl Descriptor where
|
|
|
|
crawl nextState desc = do
|
|
|
|
state <- get
|
2013-07-25 16:07:04 +00:00
|
|
|
let (name', state') = nextState desc state
|
2013-07-10 22:31:56 +00:00
|
|
|
structure' <- traverse (crawl nextState) (structure desc)
|
|
|
|
put state'
|
|
|
|
return $ desc { name = name', structure = structure' }
|
2013-07-29 18:23:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
toSrcType :: Variable -> IO Src.Type
|
|
|
|
toSrcType variable = do
|
|
|
|
desc <- UF.descriptor =<< addNames variable
|
|
|
|
case structure desc of
|
|
|
|
Just term ->
|
|
|
|
case term of
|
|
|
|
App1 a b -> do
|
|
|
|
Src.Data name ts <- toSrcType a
|
|
|
|
b' <- toSrcType b
|
|
|
|
return (Src.Data name (ts ++ [b']))
|
|
|
|
Fun1 a b -> Src.Lambda <$> toSrcType a <*> toSrcType b
|
|
|
|
Var1 a -> toSrcType a
|
2014-01-13 08:23:23 +00:00
|
|
|
EmptyRecord1 -> return $ Src.Record [] Nothing
|
|
|
|
Record1 tfields extension -> do
|
|
|
|
fields' <- traverse (mapM toSrcType) tfields
|
|
|
|
let fields = concat [ map ((,) name) ts | (name,ts) <- Map.toList fields' ]
|
|
|
|
ext' <- toSrcType extension
|
|
|
|
return $ case ext' of
|
|
|
|
Src.Record fs ext -> Src.Record (fs ++ fields) ext
|
|
|
|
Src.Var x -> Src.Record fields (Just x)
|
|
|
|
_ -> error "Used toSrcType on a type that is not well-formed"
|
2013-07-29 18:23:42 +00:00
|
|
|
Nothing ->
|
|
|
|
case name desc of
|
2014-01-04 15:16:21 +00:00
|
|
|
Just x@(c:_) | Char.isLower c -> return (Src.Var x)
|
|
|
|
| otherwise -> return (Src.Data x [])
|
2013-11-04 19:57:00 +00:00
|
|
|
_ -> error $ concat
|
2013-07-29 18:23:42 +00:00
|
|
|
[ "Problem converting the following type "
|
|
|
|
, "from a type-checker type to a source-syntax type:"
|
|
|
|
, P.render (pretty Never variable) ]
|
2013-07-30 15:29:52 +00:00
|
|
|
|
|
|
|
data AppStructure = List Variable | Tuple [Variable] | Other
|
|
|
|
|
|
|
|
collectApps :: Variable -> IO AppStructure
|
|
|
|
collectApps variable = go [] variable
|
|
|
|
where
|
|
|
|
go vars variable = do
|
|
|
|
desc <- UF.descriptor variable
|
|
|
|
case (structure desc, vars) of
|
2014-01-04 15:16:21 +00:00
|
|
|
(Nothing, [v]) -> case name desc of
|
2013-07-30 18:57:58 +00:00
|
|
|
Just "_List" -> return (List v)
|
|
|
|
_ -> return Other
|
2014-01-04 15:16:21 +00:00
|
|
|
(Nothing, vs) -> case name desc of
|
2013-07-30 18:57:58 +00:00
|
|
|
Just ctor | isTuple ctor -> return (Tuple vs)
|
|
|
|
_ -> return Other
|
2014-01-04 15:16:21 +00:00
|
|
|
(Just term, _) -> case term of
|
|
|
|
App1 a b -> go (vars ++ [b]) a
|
|
|
|
_ -> return Other
|