2014-01-14 11:48:54 +00:00
|
|
|
{-# OPTIONS_GHC -W #-}
|
2013-07-07 16:13:40 +00:00
|
|
|
module SourceSyntax.Type where
|
|
|
|
|
2013-07-21 04:08:08 +00:00
|
|
|
import Data.Binary
|
2013-07-07 16:13:40 +00:00
|
|
|
import qualified Data.Map as Map
|
2013-07-19 15:45:16 +00:00
|
|
|
import qualified SourceSyntax.Helpers as Help
|
2013-07-21 04:08:08 +00:00
|
|
|
import Control.Applicative ((<$>), (<*>))
|
2013-07-07 16:13:40 +00:00
|
|
|
import SourceSyntax.PrettyPrint
|
|
|
|
import Text.PrettyPrint as P
|
|
|
|
|
|
|
|
data Type = Lambda Type Type
|
|
|
|
| Var String
|
|
|
|
| Data String [Type]
|
2014-01-13 08:23:23 +00:00
|
|
|
| Record [(String,Type)] (Maybe String)
|
2014-01-15 00:33:40 +00:00
|
|
|
deriving (Eq)
|
2013-07-07 16:13:40 +00:00
|
|
|
|
2013-07-26 22:14:38 +00:00
|
|
|
fieldMap :: [(String,a)] -> Map.Map String [a]
|
2013-07-07 16:13:40 +00:00
|
|
|
fieldMap fields =
|
|
|
|
foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
|
|
|
|
|
|
|
|
recordOf :: [(String,Type)] -> Type
|
2014-01-13 08:23:23 +00:00
|
|
|
recordOf fields = Record fields Nothing
|
2013-07-07 16:13:40 +00:00
|
|
|
|
|
|
|
listOf :: Type -> Type
|
2013-07-17 17:24:56 +00:00
|
|
|
listOf t = Data "_List" [t]
|
2013-07-07 16:13:40 +00:00
|
|
|
|
|
|
|
tupleOf :: [Type] -> Type
|
2013-07-14 17:52:50 +00:00
|
|
|
tupleOf ts = Data ("_Tuple" ++ show (length ts)) ts
|
2013-07-07 16:13:40 +00:00
|
|
|
|
2014-01-15 00:33:40 +00:00
|
|
|
instance Show Type where
|
|
|
|
show = render . pretty
|
2013-07-07 16:13:40 +00:00
|
|
|
|
|
|
|
instance Pretty Type where
|
|
|
|
pretty tipe =
|
|
|
|
case tipe of
|
2014-01-16 00:46:41 +00:00
|
|
|
Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
|
2014-01-14 11:48:54 +00:00
|
|
|
where
|
|
|
|
t:ts = map prettyLambda (collectLambdas tipe)
|
|
|
|
prettyLambda t = case t of
|
|
|
|
Lambda _ _ -> P.parens (pretty t)
|
|
|
|
_ -> pretty t
|
|
|
|
|
2013-07-07 16:13:40 +00:00
|
|
|
Var x -> P.text x
|
2013-07-19 15:45:16 +00:00
|
|
|
Data "_List" [t] -> P.brackets (pretty t)
|
|
|
|
Data name tipes
|
2013-07-22 12:52:57 +00:00
|
|
|
| Help.isTuple name -> P.parens . P.sep . P.punctuate P.comma $ map pretty tipes
|
2013-07-19 15:45:16 +00:00
|
|
|
| otherwise -> P.hang (P.text name) 2 (P.sep $ map prettyParens tipes)
|
2014-01-13 08:23:23 +00:00
|
|
|
Record fields ext ->
|
|
|
|
P.braces $ case ext of
|
|
|
|
Nothing -> prettyFields
|
|
|
|
Just x -> P.hang (P.text x <+> P.text "|") 4 prettyFields
|
2013-07-25 15:09:10 +00:00
|
|
|
where
|
2013-07-26 16:20:57 +00:00
|
|
|
prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
|
|
|
|
prettyFields = commaSep . map prettyField $ fields
|
2013-07-07 16:13:40 +00:00
|
|
|
|
2014-01-14 11:48:54 +00:00
|
|
|
collectLambdas :: Type -> [Type]
|
2013-07-07 16:13:40 +00:00
|
|
|
collectLambdas tipe =
|
|
|
|
case tipe of
|
2014-01-14 11:48:54 +00:00
|
|
|
Lambda arg body -> arg : collectLambdas body
|
|
|
|
_ -> [tipe]
|
2013-07-07 16:13:40 +00:00
|
|
|
|
2014-01-04 09:14:36 +00:00
|
|
|
prettyParens :: Type -> Doc
|
2013-07-07 16:13:40 +00:00
|
|
|
prettyParens tipe = parensIf needed (pretty tipe)
|
|
|
|
where
|
|
|
|
needed =
|
|
|
|
case tipe of
|
|
|
|
Lambda _ _ -> True
|
2013-08-22 02:44:57 +00:00
|
|
|
Data "_List" [_] -> False
|
2013-07-22 12:52:57 +00:00
|
|
|
Data _ [] -> False
|
2013-07-07 16:13:40 +00:00
|
|
|
Data _ _ -> True
|
2013-07-21 04:08:08 +00:00
|
|
|
_ -> False
|
|
|
|
|
|
|
|
instance Binary Type where
|
|
|
|
put tipe =
|
|
|
|
case tipe of
|
|
|
|
Lambda t1 t2 ->
|
|
|
|
putWord8 0 >> put t1 >> put t2
|
|
|
|
Var x ->
|
|
|
|
putWord8 1 >> put x
|
|
|
|
Data ctor tipes ->
|
|
|
|
putWord8 2 >> put ctor >> put tipes
|
|
|
|
Record fs ext ->
|
2014-01-13 08:23:23 +00:00
|
|
|
putWord8 3 >> put fs >> put ext
|
2013-07-21 04:08:08 +00:00
|
|
|
|
|
|
|
get = do
|
|
|
|
n <- getWord8
|
|
|
|
case n of
|
|
|
|
0 -> Lambda <$> get <*> get
|
|
|
|
1 -> Var <$> get
|
|
|
|
2 -> Data <$> get <*> get
|
2014-01-13 08:23:23 +00:00
|
|
|
3 -> Record <$> get <*> get
|
2013-11-04 18:57:43 +00:00
|
|
|
_ -> error "Error reading a valid type from serialized string"
|