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))
deriving (Eq, Ord, Show)
tyUnit :: Ty
tyUnit = TyRecord M.empty
displayTy :: Ty -> String
displayTy ty = go ty "" where
go :: Ty -> ShowS
go TyBool = showString "Bool"
go (TyString _) = showString "String"
go (TyList _ t) = showChar '[' . go 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 '}'
showPair n t = showString n . showString ": " . go t