module Zinza.Type (
Ty (..),
tyUnit,
displayTy,
) where
import qualified Data.Map as M
import Zinza.Var
data Ty
= TyBool
| TyString (Maybe Selector)
| TyList (Maybe Selector) Ty
| TyRecord (M.Map Var (Selector, Ty))
| TyFun Ty Ty
deriving (Eq, Ord, Show)
tyUnit :: Ty
tyUnit = TyRecord M.empty
displayTy :: Ty -> String
displayTy ty = go 0 ty "" where
go :: Int -> Ty -> ShowS
go _ TyBool = showString "Bool"
go _ (TyString _) = showString "String"
go _ (TyList _ t) = showChar '[' . go 0 t . showChar ']'
go _ (TyRecord m) = case M.toList m of
[] -> showString "{}"
((n,(_,t)) : nts) -> foldl
(\acc (n',(_,t')) -> acc . showString ", " . showPair n' t')
(showChar '{' . showPair n t)
nts
. showChar '}'
go d (TyFun a b) = showParen (d > 10) $
go 11 a . showString " -> " . go 10 b
showPair n t = showString n . showString ": " . go 0 t