2013-07-14 23:06:00 +00:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
2013-07-07 16:13:40 +00:00
|
|
|
module SourceSyntax.Type where
|
|
|
|
|
2013-07-14 23:06:00 +00:00
|
|
|
import Data.Data
|
2013-07-07 16:13:40 +00:00
|
|
|
import qualified Data.Map as Map
|
|
|
|
import SourceSyntax.PrettyPrint
|
|
|
|
import Text.PrettyPrint as P
|
|
|
|
|
|
|
|
data Type = Lambda Type Type
|
|
|
|
| Var String
|
|
|
|
| Data String [Type]
|
|
|
|
| EmptyRecord
|
|
|
|
| Record (Map.Map String [Type]) Type
|
2013-07-14 23:06:00 +00:00
|
|
|
deriving (Eq, Show, Data, Typeable)
|
2013-07-07 16:13:40 +00:00
|
|
|
|
|
|
|
fieldMap :: [(String,Type)] -> Map.Map String [Type]
|
|
|
|
fieldMap fields =
|
|
|
|
foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
|
|
|
|
|
|
|
|
recordOf :: [(String,Type)] -> Type
|
|
|
|
recordOf fields = Record (fieldMap fields) EmptyRecord
|
|
|
|
|
|
|
|
listOf :: Type -> Type
|
|
|
|
listOf t = Data "List" [t]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
instance Pretty Type where
|
|
|
|
pretty tipe =
|
|
|
|
case tipe of
|
|
|
|
Lambda t1 t2 -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
|
|
|
|
where t:ts = collectLambdas tipe
|
|
|
|
Var x -> P.text x
|
|
|
|
Data name tipes -> P.hang (P.text name) 2 (P.sep $ map prettyParens tipes)
|
|
|
|
EmptyRecord -> P.braces P.empty
|
|
|
|
Record fields ext -> error "not done yet"
|
|
|
|
|
|
|
|
collectLambdas tipe =
|
|
|
|
case tipe of
|
|
|
|
Lambda arg@(Lambda _ _) body -> P.parens (pretty arg) : collectLambdas body
|
|
|
|
Lambda arg body -> pretty arg : collectLambdas body
|
|
|
|
_ -> [pretty tipe]
|
|
|
|
|
|
|
|
prettyParens tipe = parensIf needed (pretty tipe)
|
|
|
|
where
|
|
|
|
needed =
|
|
|
|
case tipe of
|
|
|
|
Lambda _ _ -> True
|
|
|
|
Data _ _ -> True
|
|
|
|
_ -> False
|