summaryrefslogtreecommitdiff
path: root/Yhc/Core/Type.hs
blob: 711ea7ece74f769a2fa6535bc5dd31438a0cb11c (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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
{-# OPTIONS_DERIVE --module=Yhc.Core.Binary --derive=BinaryOld --output=Binary.hs #-}
{-# OPTIONS_DERIVE --import --import=Yhc.Core.Internal.Binary --import=Control.Monad #-}

module Yhc.Core.Type where

-- while it may seem tempting to add type signatures to Core
-- it won't work - by this stage all the type signatures are
-- wrong because of desugarring

import Control.Monad(liftM)
import Data.Maybe(fromMaybe, listToMaybe, mapMaybe)
import Data.Char(isSpace)
import Data.List(intersperse)
import qualified Data.Map as Map


{-! global: GhcBinary !-}

type CoreVarName = String
type CoreFuncName = String
type CoreDataName = String
type CoreCtorName = String
type CoreFieldName = String

-- module name, imports, items in the module
data Core = Core {coreName :: String, coreImports :: [String],
                  coreDatas :: [CoreData], coreFuncs :: [CoreFunc]}
            deriving (Eq,Ord)

data CoreData = CoreData {coreDataName :: CoreDataName, coreDataTypes :: [String], coreDataCtors :: [CoreCtor]}
                deriving (Eq,Ord)

-- Name, then list of maybe field names
data CoreCtor = CoreCtor {coreCtorName :: CoreCtorName, coreCtorFields :: [(String, Maybe CoreFieldName)]}
                deriving (Eq,Ord)

data CoreFunc = CoreFunc {coreFuncName :: CoreFuncName, coreFuncArgs :: [CoreVarName], coreFuncBody :: CoreExpr}
              | CorePrim {
                    coreFuncName :: CoreFuncName,
                    corePrimArity :: Int,
                    corePrimExternal :: String,
                    corePrimConv :: String,
                    corePrimImport :: Bool,
                    corePrimTypes :: [String]
                }
                deriving (Eq,Ord)

isCoreFunc, isCorePrim :: CoreFunc -> Bool
isCoreFunc (CoreFunc{}) = True; isCoreFunc _ = False
isCorePrim (CorePrim{}) = True; isCorePrim _ = False


coreFuncArity :: CoreFunc -> Int
coreFuncArity (CorePrim{corePrimArity=x}) = x
coreFuncArity x = length $ coreFuncArgs x

-- An universal replacement for coreFuncArgs that now does not match in all cases

coreFuncArgList :: CoreFunc -> [CoreVarName]
coreFuncArgList (CorePrim{coreFuncName=n,corePrimArity=x}) = take x $ map (("__" ++ n ++ "_") ++) (map show [1..])
coreFuncArgList x = coreFuncArgs x

type CoreFuncMap = Map.Map CoreFuncName CoreFunc



data CoreExpr = CoreCon CoreCtorName
              | CoreVar CoreVarName
              | CoreFun CoreFuncName
              | CoreApp CoreExpr [CoreExpr]
              | CoreLam [CoreVarName] CoreExpr
              | CoreCase CoreExpr [(CorePat,CoreExpr)]
              | CoreLet [(CoreVarName,CoreExpr)] CoreExpr
              | CorePos String CoreExpr
              | CoreLit CoreLit
                deriving (Ord,Eq)


data CoreLit = CoreInt Int
             | CoreInteger Integer
             | CoreChr Char
             | CoreStr String
             | CoreFloat Float
             | CoreDouble Double
               deriving (Ord,Eq,Show)


data CorePat = PatCon {patCon :: CoreCtorName, patVars :: [CoreVarName]}
             | PatLit {patLit :: CoreLit}
             | PatDefault
               deriving (Ord,Eq,Show)


-- smart constructors
coreApp :: CoreExpr -> [CoreExpr] -> CoreExpr
coreApp x [] = x
coreApp x xs = CoreApp x xs

coreLet :: [(CoreVarName,CoreExpr)] -> CoreExpr -> CoreExpr
coreLet [] x = x
coreLet xs x = CoreLet xs x

coreLam :: [CoreVarName] -> CoreExpr -> CoreExpr
coreLam [] x = x
coreLam xs x = CoreLam xs x


fromCoreLit :: CoreExpr -> CoreLit
fromCoreLit (CoreLit x) = x
fromCoreLit x = error $ "Yhc.Core.fromCoreLit on a non-literal"

fromCoreCon, fromCoreVar, fromCoreFun :: CoreExpr -> String
fromCoreCon  (CoreCon  x) = x
fromCoreVar  (CoreVar  x) = x
fromCoreFun  (CoreFun  x) = x

fromCoreApp :: CoreExpr -> (CoreExpr,[CoreExpr])
fromCoreApp (CoreApp x y) = (x,y)
fromCoreApp x = (x,[])

fromCoreLet :: CoreExpr -> ([(CoreVarName,CoreExpr)],CoreExpr)
fromCoreLet (CoreLet x y) = (x,y)
fromCoreLet x = ([],x)

fromCoreLam :: CoreExpr -> ([CoreVarName],CoreExpr)
fromCoreLam (CoreLam x y) = (x,y)
fromCoreLam x = ([],x)

isCoreCon, isCoreVar, isCoreFun, isCoreLam :: CoreExpr -> Bool
isCorePos, isCoreLet, isCoreCase, isCoreLit :: CoreExpr -> Bool
isCoreCon  x = case x of {CoreCon{}  -> True; _ -> False}
isCoreVar  x = case x of {CoreVar{}  -> True; _ -> False}
isCoreFun  x = case x of {CoreFun{}  -> True; _ -> False}
isCoreLam  x = case x of {CoreLam{}  -> True; _ -> False}
isCorePos  x = case x of {CorePos{}  -> True; _ -> False}
isCoreLet  x = case x of {CoreLet{}  -> True; _ -> False}
isCoreCase x = case x of {CoreCase{} -> True; _ -> False}
isCoreLit  x = case x of {CoreLit{}  -> True; _ -> False}

isCoreStr, isCoreChr, isCoreInt :: CoreLit -> Bool
isCoreStr  x = case x of {CoreStr{}  -> True; _ -> False}
isCoreChr  x = case x of {CoreChr{}  -> True; _ -> False}
isCoreInt  x = case x of {CoreInt{}  -> True; _ -> False}

isPatDefault, isPatLit, isPatCon :: CorePat -> Bool
isPatDefault x = case x of {PatDefault{} -> True; _ -> False}
isPatLit     x = case x of {PatLit{}     -> True; _ -> False}
isPatCon     x = case x of {PatCon{}     -> True; _ -> False}


{-# DEPRECATED fromPatLit "use patLit instead" #-}
fromPatLit = patLit

patVariables (PatCon _ xs) = xs
patVariables _ = []

patToExpr :: CorePat -> CoreExpr
patToExpr (PatCon c xs) = coreApp (CoreCon c) (map CoreVar xs)
patToExpr (PatLit x) = CoreLit x
patToExpr PatDefault = CoreVar "_"

exprToPat :: CoreExpr -> CorePat
exprToPat (CoreApp (CoreCon c) vs) = PatCon c (map fromCoreVar vs)
exprToPat (CoreCon c) = PatCon c []
exprToPat (CoreLit x) = PatLit x
exprToPat (CoreVar _) = PatDefault


-- | Returns true for constants that take a small, bounded
-- amount of space
isCoreLitSmall :: CoreLit -> Bool
isCoreLitSmall x = isCoreInt x || isCoreChr x


remCorePos :: CoreExpr -> CoreExpr
remCorePos (CorePos _ x) = x
remCorePos x = x


-- | drop a module from a Core declaration
dropModule :: String -> String
dropModule x = f x False x
    where
        f x False (';':_) = x
        f _ True  (';':x) = f x False x
        f x _ (_:xs) = f x True xs
        f x _ [] = x


-- | Get a function from a Core type
--   crashes if the function does not exist
coreFunc :: Core -> CoreFuncName -> CoreFunc
coreFunc core name = fromMaybe (error msg) (coreFuncMaybe core name)
    where msg = "Yhc.Core.Type.coreFunc, function not found: " ++ name


-- | A non-crashing version of 'coreFunc'
--   returns Nothing if the function does not exist.
--   If multiple functions with the same name exist, this crashes.
coreFuncMaybe :: Core -> CoreFuncName -> Maybe CoreFunc
coreFuncMaybe core name =
    case [x | x <- coreFuncs core, coreFuncName x == name] of
        [] -> Nothing
        [x] -> Just x
        xs -> error $ "Yhc.Core.Type.mbCoreFunc, found found " ++ show (length xs) ++ " times: " ++ name



-- | Get a 'CoreData' from a field (the snd element of 'coreCtorFields')
coreFieldDataMaybe :: Core -> CoreFieldName -> Maybe CoreData
coreFieldDataMaybe core name = coreFieldCtorMaybe core name >>= coreCtorDataMaybe core . coreCtorName

-- | Get a 'CoreData' from a ctor name
coreCtorDataMaybe :: Core -> CoreCtorName -> Maybe CoreData
coreCtorDataMaybe core name = listToMaybe [dat | dat <- coreDatas core, name `elem` map coreCtorName (coreDataCtors dat)]

-- | Get a 'CoreCtor' from a field name
coreFieldCtorMaybe :: Core -> CoreFieldName -> Maybe CoreCtor
coreFieldCtorMaybe core name = listToMaybe [ctr | dat <- coreDatas core, ctr <- coreDataCtors dat
                                           , name `elem` mapMaybe snd (coreCtorFields ctr)]


coreFieldData :: Core -> CoreFieldName -> CoreData
coreFieldData core name = fromMaybe (error msg) $ coreFieldDataMaybe core name
    where msg = "Yhc.Core.coreFieldData, looking for " ++ name

coreCtorData :: Core -> CoreCtorName -> CoreData
coreCtorData core = fromMaybe (error "Yhc.Core.coreCtorData") . coreCtorDataMaybe core

coreFieldCtor :: Core -> CoreFieldName -> CoreCtor
coreFieldCtor core = fromMaybe (error "Yhc.Core.coreFieldCtor") . coreFieldCtorMaybe core

coreCtor :: Core -> CoreCtorName -> CoreCtor
coreCtor core name = head [ctr | dat <- coreDatas core, ctr <- coreDataCtors dat, coreCtorName ctr == name]

coreData :: Core -> CoreDataName -> CoreData
coreData core name = head [dat | dat <- coreDatas core, coreDataName dat == name]


-- | Take a function that operates on bodies, and apply it to a program
applyBodyCore :: (CoreExpr -> CoreExpr) -> (Core -> Core)
applyBodyCore f = applyFuncCore (applyBodyFunc f)


-- | Take a function that operates on bodies, and apply it to a function
applyBodyFunc :: (CoreExpr -> CoreExpr) -> (CoreFunc -> CoreFunc)
applyBodyFunc f func | isCoreFunc func = func{coreFuncBody = f (coreFuncBody func)}
                     | otherwise = func


-- | Take a function that operates on functions, and apply it to a program
applyFuncCore :: (CoreFunc -> CoreFunc) -> (Core -> Core)
applyFuncCore f core = core{coreFuncs = map f (coreFuncs core)}


applyCtorCore :: (CoreCtor -> CoreCtor) -> (Core -> Core)
applyCtorCore f = applyDataCore (applyCtorData f)

applyDataCore :: (CoreData -> CoreData) -> (Core -> Core)
applyDataCore f core = core{coreDatas = map f (coreDatas core)}

applyCtorData :: (CoreCtor -> CoreCtor) -> (CoreData -> CoreData)
applyCtorData f dat = dat{coreDataCtors = map f (coreDataCtors dat)}


applyBodyCoreM :: Monad m => (CoreExpr -> m CoreExpr) -> Core -> m Core
applyBodyCoreM f = applyFuncCoreM g
    where
        g (CoreFunc a b c) = liftM (CoreFunc a b) $ f c
        g x = return x


applyFuncCoreM :: Monad m => (CoreFunc -> m CoreFunc) -> Core -> m Core
applyFuncCoreM f c = do
    res <- mapM f (coreFuncs c)
    return $ c{coreFuncs = res}



-- | Split up a coreDataType into lexical elements
--   None of the result elements will be space, or blank
--   Some may be "(", ")" or "!"
coreDataTypeSplit :: String -> [String]
coreDataTypeSplit [] = []
coreDataTypeSplit (x:xs)
        | x `elem` special = [x] : coreDataTypeSplit xs
        | isSpace x = coreDataTypeSplit xs
        | otherwise = let (a,b) = break (\x -> isSpace x || x `elem` special) (x:xs)
                      in a : coreDataTypeSplit b
    where
        special = "!()"


-- | can pretty print much nicer, just something that works for now
coreDataTypeJoin :: [String] -> String
coreDataTypeJoin = concat . intersperse " "



fromCoreFuncMap :: Core -> CoreFuncMap -> Core
fromCoreFuncMap core fm = core{coreFuncs = Map.elems fm}

toCoreFuncMap :: Core -> CoreFuncMap
toCoreFuncMap core = Map.fromList [(coreFuncName x, x) | x <- coreFuncs core]

coreFuncMap :: CoreFuncMap -> CoreFuncName -> CoreFunc
coreFuncMap fm name = fromMaybe (error $ "Yhc.Core.coreFuncMap, function not found, " ++ name) $
                      Map.lookup name fm

coreFuncMapMaybe :: CoreFuncMap -> CoreFuncName -> Maybe CoreFunc
coreFuncMapMaybe fm name = Map.lookup name fm