~jojo/Carth

ref: 55fb4f948f1f3797078b584dc60b4f7dd68b37ed Carth/src/Parsed.hs -rw-r--r-- 4.0 KiB
55fb4f94JoJo Check `cast` in Infer instead of Gen 4 months ago
                                                                                
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{-# LANGUAGE DataKinds #-}

module Parsed (module Parsed, Const (..), TPrim(..), TConst) where

import qualified Data.Set as Set
import Data.Set (Set)
import Control.Arrow ((>>>))

import SrcPos
import FreeVars
import TypeAst
import Lexd (Const (..))


data IdCase = Big | Small

newtype Id (case' :: IdCase) = Id (WithPos String)
    deriving (Show, Eq, Ord)

data TVar
    = TVExplicit (Id 'Small)
    | TVImplicit String
    deriving (Show, Eq, Ord)

data Type
    = TVar TVar
    | TPrim TPrim
    | TConst (TConst Type)
    -- TODO: Remove special case for these two? Is it really needed?
    | TFun Type Type
    | TBox Type
    deriving (Show, Eq, Ord)

data Scheme = Forall SrcPos (Set TVar) Type
    deriving (Show, Eq)

data Pat
    = PConstruction SrcPos (Id 'Big) [Pat]
    | PInt SrcPos Int
    | PStr SrcPos String
    | PVar (Id 'Small)
    | PBox SrcPos Pat
    -- TODO: Add special pattern for Lazy
    deriving Show

data Expr'
    = Lit Const
    | Var (Id 'Small)
    | App Expr Expr
    | If Expr Expr Expr
    | Let1 DefLike Expr
    | Let [DefLike] Expr
    | LetRec [Def] Expr
    | TypeAscr Expr Type
    | Match Expr [(Pat, Expr)]
    | FunMatch [(Pat, Expr)]
    | Ctor (Id 'Big)
    | Sizeof Type
    deriving (Show, Eq)

type Expr = WithPos Expr'

data Def = VarDef SrcPos (Id 'Small) (Maybe Scheme) Expr
         | FunDef SrcPos (Id 'Small) (Maybe Scheme) [Pat] Expr
    deriving (Show, Eq)

data DefLike = Def Def | Deconstr Pat Expr
    deriving (Show, Eq)

newtype ConstructorDefs = ConstructorDefs [(Id 'Big, [Type])]
    deriving (Show, Eq)

data TypeDef = TypeDef (Id 'Big) [Id 'Small] ConstructorDefs
    deriving (Show, Eq)

data Extern = Extern (Id 'Small) Type
    deriving (Show, Eq)

data Program = Program [Def] [TypeDef] [Extern]
    deriving (Show, Eq)


instance TypeAst Type where
    tprim = TPrim
    tconst = TConst
    tfun = TFun
    tbox = TBox

instance Eq Pat where
    (==) = curry $ \case
        (PConstruction _ x ps, PConstruction _ x' ps') -> x == x' && ps == ps'
        (PVar x, PVar x') -> x == x'
        _ -> False

instance FreeVars Def (Id 'Small) where
    freeVars = \case
        VarDef _ _ _ rhs -> freeVars rhs
        FunDef _ _ _ pats rhs ->
            Set.difference (freeVars rhs) (Set.unions (map bvPat pats))

instance FreeVars DefLike (Id 'Small) where
    freeVars = \case
        Def d -> freeVars d
        Deconstr _ matchee -> freeVars matchee

instance FreeVars Expr (Id 'Small) where
    freeVars = fvExpr

instance HasPos (Id a) where
    getPos (Id x) = getPos x

instance HasPos Pat where
    getPos = \case
        PConstruction p _ _ -> p
        PInt p _ -> p
        PStr p _ -> p
        PVar v -> getPos v
        PBox p _ -> p


fvExpr :: Expr -> Set (Id 'Small)
fvExpr = unpos >>> fvExpr'
  where
    fvExpr' = \case
        Lit _ -> Set.empty
        Var x -> Set.singleton x
        App f a -> fvApp f a
        If p c a -> fvIf p c a
        Let1 b e -> Set.union (freeVars b) (Set.difference (freeVars e) (bvDefLike b))
        Let bs e -> foldr
            (\b fvs -> Set.union (freeVars b) (Set.difference fvs (bvDefLike b)))
            (freeVars e)
            bs
        LetRec ds e -> fvLet (unzip (map (\d -> (defLhs d, d)) ds)) e
        TypeAscr e _t -> freeVars e
        Match e cs -> fvMatch e cs
        FunMatch cs -> fvCases cs
        Ctor _ -> Set.empty
        Sizeof _t -> Set.empty
    bvDefLike = \case
        Def d -> Set.singleton (defLhs d)
        Deconstr pat _ -> bvPat pat

defLhs :: Def -> Id 'Small
defLhs = \case
    VarDef _ lhs _ _ -> lhs
    FunDef _ lhs _ _ _ -> lhs

fvMatch :: Expr -> [(Pat, Expr)] -> Set (Id 'Small)
fvMatch e cs = Set.union (freeVars e) (fvCases cs)

fvCases :: [(Pat, Expr)] -> Set (Id 'Small)
fvCases = Set.unions . map (\(p, e) -> Set.difference (freeVars e) (bvPat p))

bvPat :: Pat -> Set (Id 'Small)
bvPat = \case
    PConstruction _ _ ps -> Set.unions (map bvPat ps)
    PInt _ _ -> Set.empty
    PStr _ _ -> Set.empty
    PVar x -> Set.singleton x
    PBox _ p -> bvPat p

idstr :: Id a -> String
idstr (Id (WithPos _ x)) = x