aboutsummaryrefslogtreecommitdiff
path: root/ast/CC/Typed.hs
blob: 535fd31530c67a6cb181787df57f038450bef0a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module CC.Typed(
    module CC.Typed,
    module CC.Types
) where

import CC.Pretty
import CC.Types


data ProgramT = ProgramT [DefT]
  deriving (Show, Read)

data DefT = DefT Name ExprT
  deriving (Show, Read)

data TypeT = TFunT TypeT TypeT
           | TIntT
           | TyVar Int
  deriving (Show, Read)

data ExprT = LamT TypeT Occ ExprT
           | CallT TypeT ExprT ExprT
           | IntT Int
           | VarT Occ
  deriving (Show, Read)

data Occ = Occ Name TypeT
  deriving (Show, Read)

exprType :: ExprT -> TypeT
exprType (LamT typ _ _) = typ
exprType (CallT typ _ _) = typ
exprType (IntT _) = TIntT
exprType (VarT (Occ _ typ)) = typ

instance Pretty TypeT where
    prettyPrec _ TIntT = "Int"
    prettyPrec p (TFunT a b) =
        precParens p 2 (prettyPrec 3 a ++ " -> " ++ prettyPrec 2 b)
    prettyPrec _ (TyVar i) = 't' : show i

instance Pretty ExprT where
    prettyPrec p (LamT ty (Occ n t) e) =
        precParens p 2 $
            "(\\(" ++ n ++ " :: " ++ pretty t ++ ") -> "
                ++ prettyPrec 2 e ++ ") :: " ++ pretty ty
    prettyPrec p (CallT ty e1 e2) =
        precParens p 2 $
            prettyPrec 3 e1 ++ " " ++ prettyPrec 3 e2 ++ " :: " ++ pretty ty
    prettyPrec _ (IntT i) = show i
    prettyPrec p (VarT (Occ n t)) =
        precParens p 2 $
            show n ++ " :: " ++ pretty t

instance Pretty DefT where
    pretty (DefT n e) = n ++ " = " ++ pretty e

instance Pretty ProgramT where
    pretty (ProgramT defs) = concatMap ((++ "\n") . pretty) defs