3Description : Internal representation of Idris' constructs.
6Maintainer : The Idris Community.
8{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, DeriveGeneric, PatternGuards #-}
12import Idris.Core.CaseTree
15import Control.Applicative hiding (Const)
16import Control.Monad.State hiding (lift)
17import Data.Data (Data)
19import Data.Typeable (Typeable)
21import GHC.Generics (Generic)
23data Endianness = Native | BE | LE deriving (Show, Eq)
25data LVar = Loc Int | Glob Name
28-- ASSUMPTION: All variable bindings have unique names here
29-- Constructors commented as lifted are not present in the LIR provided to the different backends.
31 | LApp Bool LExp [LExp] -- True = tail call
32 | LLazyApp Name [LExp] -- True = tail call
33 | LLazyExp LExp -- lifted out before compiling
34 | LForce LExp -- make sure Exp is evaluted
35 | LLet Name LExp LExp -- name just for pretty printing
36 | LLam [Name] LExp -- lambda, lifted out before compiling
37 | LProj LExp Int -- projection
38 | LCon (Maybe LVar) -- Location to reallocate, if available
40 | LCase CaseType LExp [LAlt]
42 | LForeign FDesc -- Function descriptor (usually name as string)
43 FDesc -- Return type descriptor
44 [(FDesc, LExp)] -- first LExp is the FFI type description
57data Export = ExportData FDesc -- Exported data descriptor (usually string)
58 | ExportFun Name -- Idris name
59 FDesc -- Exported function descriptor
60 FDesc -- Return type descriptor
61 [FDesc] -- Argument types
64data ExportIFace = Export Name -- FFI descriptor
65 String -- interface file
69-- Primitive operators. Backends are not *required* to implement all
70-- of these, but should report an error if they are unable
72data PrimFn = LPlus ArithTy | LMinus ArithTy | LTimes ArithTy
73 | LUDiv IntTy | LSDiv ArithTy | LURem IntTy | LSRem ArithTy
74 | LAnd IntTy | LOr IntTy | LXOr IntTy | LCompl IntTy
75 | LSHL IntTy | LLSHR IntTy | LASHR IntTy
76 | LEq ArithTy | LLt IntTy | LLe IntTy | LGt IntTy | LGe IntTy
77 | LSLt ArithTy | LSLe ArithTy | LSGt ArithTy | LSGe ArithTy
78 | LSExt IntTy IntTy | LZExt IntTy IntTy | LTrunc IntTy IntTy
79 | LStrConcat | LStrLt | LStrEq | LStrLen
80 | LIntFloat IntTy | LFloatInt IntTy | LIntStr IntTy | LStrInt IntTy
81 | LFloatStr | LStrFloat | LChInt IntTy | LIntCh IntTy
82 | LBitCast ArithTy ArithTy -- Only for values of equal width
84 | LFExp | LFLog | LFSin | LFCos | LFTan | LFASin | LFACos | LFATan
85 | LFSqrt | LFFloor | LFCeil | LFNegate
87 | LStrHead | LStrTail | LStrCons | LStrIndex | LStrRev | LStrSubstr
88 | LReadStr | LWriteStr
94 | LPar -- evaluate argument anywhere, possibly on another
95 -- core or another machine. 'id' is a valid implementation
100 deriving (Show, Eq, Generic)
102-- Supported target languages for foreign calls
104data FCallType = FStatic | FObject | FConstructor
107data FType = FArith ArithTy
118-- FIXME: Why not use this for all the IRs now?
119data LAlt' e = LConCase Int Name [Name] e
122 deriving (Show, Eq, Functor, Data, Typeable)
124type LAlt = LAlt' LExp
126data LDecl = LFun [LOpt] Name [Name] LExp -- options, name, arg names, def
127 | LConstructor Name Int Int -- constructor name, tag, arity
130type LDefs = Ctxt LDecl
132data LOpt = Inline | NoInline
135addTags :: Int -> [(Name, LDecl)] -> (Int, [(Name, LDecl)])
136addTags i ds = tag i ds []
137 where tag i ((n, LConstructor n' (-1) a) : as) acc
138 = tag (i + 1) as ((n, LConstructor n' i a) : acc)
139 tag i ((n, LConstructor n' t a) : as) acc
140 = tag i as ((n, LConstructor n' t a) : acc)
141 tag i (x : as) acc = tag i as (x : acc)
142 tag i [] acc = (i, reverse acc)
144data LiftState = LS Name Int [(Name, LDecl)]
146lname (NS n x) i = NS (lname n i) x
147lname (UN n) i = MN i n
148lname x i = sMN i (showCG x ++ "_lam")
150liftAll :: [(Name, LDecl)] -> [(Name, LDecl)]
151liftAll xs = concatMap (\ (x, d) -> lambdaLift x d) xs
153lambdaLift :: Name -> LDecl -> [(Name, LDecl)]
154lambdaLift n (LFun opts _ args e)
155 = let (e', (LS _ _ decls)) = runState (lift args e) (LS n 0 []) in
156 (n, LFun opts n args e') : decls
157lambdaLift n x = [(n, x)]
159getNextName :: State LiftState Name
160getNextName = do LS n i ds <- get
161 put (LS n (i + 1) ds)
164addFn :: Name -> LDecl -> State LiftState ()
165addFn fn d = do LS n i ds <- get
166 put (LS n i ((fn, d) : ds))
168lift :: [Name] -> LExp -> State LiftState LExp
169lift env (LV v) = return (LV v) -- Lifting happens before these can exist...
170lift env (LApp tc (LV (Glob n)) args) = do args' <- mapM (lift env) args
171 return (LApp tc (LV (Glob n)) args')
172lift env (LApp tc f args) = do f' <- lift env f
174 addFn fn (LFun [Inline] fn env f')
175 args' <- mapM (lift env) args
176 return (LApp tc (LV (Glob fn)) (map (LV . Glob) env ++ args'))
177lift env (LLazyApp n args) = do args' <- mapM (lift env) args
178 return (LLazyApp n args')
179lift env (LLazyExp (LConst c)) = return (LConst c)
180-- lift env (LLazyExp (LApp tc (LV (Glob f)) args))
181-- = lift env (LLazyApp f args)
182lift env (LLazyExp e) = do e' <- lift env e
183 let usedArgs = nub $ usedIn env e'
185 addFn fn (LFun [NoInline] fn usedArgs e')
186 return (LLazyApp fn (map (LV . Glob) usedArgs))
187lift env (LForce e) = do e' <- lift env e
189lift env (LLet n v e) = do v' <- lift env v
190 e' <- lift (env ++ [n]) e
191 return (LLet n v' e')
192lift env (LLam args e) = do e' <- lift (env ++ args) e
193 let usedArgs = nub $ usedIn env e'
195 addFn fn (LFun [Inline] fn (usedArgs ++ args) e')
196 return (LApp False (LV (Glob fn)) (map (LV . Glob) usedArgs))
197lift env (LProj t i) = do t' <- lift env t
199lift env (LCon loc i n args) = do args' <- mapM (lift env) args
200 return (LCon loc i n args')
201lift env (LCase up e alts) = do alts' <- mapM liftA alts
203 return (LCase up e' alts')
205 liftA (LConCase i n args e) = do e' <- lift (env ++ args) e
206 return (LConCase i n args e')
207 liftA (LConstCase c e) = do e' <- lift env e
208 return (LConstCase c e')
209 liftA (LDefaultCase e) = do e' <- lift env e
210 return (LDefaultCase e')
211lift env (LConst c) = return (LConst c)
212lift env (LForeign t s args) = do args' <- mapM (liftF env) args
213 return (LForeign t s args')
215 liftF env (t, e) = do e' <- lift env e
217lift env (LOp f args) = do args' <- mapM (lift env) args
219lift env (LError str) = return $ LError str
220lift env LNothing = return LNothing
222allocUnique :: LDefs -> (Name, LDecl) -> (Name, LDecl)
223allocUnique defs p@(n, LConstructor _ _ _) = p
224allocUnique defs (n, LFun opts fn args e)
225 = let e' = evalState (findUp e) [] in
226 (n, LFun opts fn args e')
228 -- Keep track of 'updatable' names in the state, i.e. names whose heap
229 -- entry may be reused, along with the arity which was there
230 findUp :: LExp -> State [(Name, Int)] LExp
231 findUp (LApp t (LV (Glob n)) as)
232 | Just (LConstructor _ i ar) <- lookupCtxtExact n defs,
234 = findUp (LCon Nothing i n as)
236 | Just (LConstructor _ i 0) <- lookupCtxtExact n defs
237 = return $ LCon Nothing i n [] -- nullary cons are global, no need to update
238 findUp (LApp t f as) = LApp t <$> findUp f <*> mapM findUp as
239 findUp (LLazyApp n as) = LLazyApp n <$> mapM findUp as
240 findUp (LLazyExp e) = LLazyExp <$> findUp e
241 findUp (LForce e) = LForce <$> findUp e
242 -- use assumption that names are unique!
243 findUp (LLet n val sc) = LLet n <$> findUp val <*> findUp sc
244 findUp (LLam ns sc) = LLam ns <$> findUp sc
245 findUp (LProj e i) = LProj <$> findUp e <*> return i
246 findUp (LCon (Just l) i n es) = LCon (Just l) i n <$> mapM findUp es
247 findUp (LCon Nothing i n es)
249 v <- findVar [] avail (length es)
250 LCon v i n <$> mapM findUp es
251 findUp (LForeign t s es)
252 = LForeign t s <$> mapM (\ (t, e) -> do e' <- findUp e
254 findUp (LOp o es) = LOp o <$> mapM findUp es
255 findUp (LCase Updatable e@(LV (Glob n)) as)
256 = LCase Updatable e <$> mapM (doUpAlt n) as
257 findUp (LCase t e as)
258 = LCase t <$> findUp e <*> mapM findUpAlt as
261 findUpAlt (LConCase i t args rhs) = do avail <- get
264 return $ LConCase i t args rhs'
265 findUpAlt (LConstCase i rhs) = LConstCase i <$> findUp rhs
266 findUpAlt (LDefaultCase rhs) = LDefaultCase <$> findUp rhs
268 doUpAlt n (LConCase i t args rhs)
270 put ((n, length args) : avail)
273 return $ LConCase i t args rhs'
274 doUpAlt n (LConstCase i rhs) = LConstCase i <$> findUp rhs
275 doUpAlt n (LDefaultCase rhs) = LDefaultCase <$> findUp rhs
277 findVar _ [] i = return Nothing
278 findVar acc ((n, l) : ns) i | l == i = do put (reverse acc ++ ns)
279 return (Just (Glob n))
280 findVar acc (n : ns) i = findVar (n : acc) ns i
283-- Return variables in list which are used in the expression
285usedArg env n | n `elem` env = [n]
288usedIn :: [Name] -> LExp -> [Name]
289usedIn env (LV (Glob n)) = usedArg env n
290usedIn env (LApp _ e args) = usedIn env e ++ concatMap (usedIn env) args
291usedIn env (LLazyApp n args) = concatMap (usedIn env) args ++ usedArg env n
292usedIn env (LLazyExp e) = usedIn env e
293usedIn env (LForce e) = usedIn env e
294usedIn env (LLet n v e) = usedIn env v ++ usedIn (env \\ [n]) e
295usedIn env (LLam ns e) = usedIn (env \\ ns) e
296usedIn env (LCon v i n args) = let rest = concatMap (usedIn env) args in
299 Just (Glob n) -> usedArg env n ++ rest
300usedIn env (LProj t i) = usedIn env t
301usedIn env (LCase up e alts) = usedIn env e ++ concatMap (usedInA env) alts
302 where usedInA env (LConCase i n ns e) = usedIn env e
303 usedInA env (LConstCase c e) = usedIn env e
304 usedInA env (LDefaultCase e) = usedIn env e
305usedIn env (LForeign _ _ args) = concatMap (usedIn env) (map snd args)
306usedIn env (LOp f args) = concatMap (usedIn env) args
309lsubst :: Name -> LExp -> LExp -> LExp
310lsubst n new (LV (Glob x)) | n == x = new
311lsubst n new (LApp t e args) = let e' = lsubst n new e
312 args' = map (lsubst n new) args in
314lsubst n new (LLazyApp fn args) = let args' = map (lsubst n new) args in
316lsubst n new (LLazyExp e) = LLazyExp (lsubst n new e)
317lsubst n new (LForce e) = LForce (lsubst n new e)
318lsubst n new (LLet v val sc) = LLet v (lsubst n new val) (lsubst n new sc)
319lsubst n new (LLam ns sc) = LLam ns (lsubst n new sc)
320lsubst n new (LProj e i) = LProj (lsubst n new e) i
321lsubst n new (LCon lv t cn args) = let args' = map (lsubst n new) args in
323lsubst n new (LOp op args) = let args' = map (lsubst n new) args in
325lsubst n new (LForeign fd rd args)
326 = let args' = map (\(d, a) -> (d, lsubst n new a)) args in
328lsubst n new (LCase t e alts) = let e' = lsubst n new e
329 alts' = map (fmap (lsubst n new)) alts in
333instance Show LExp where
334 show e = show' [] "" e where
335 show' env ind (LV (Loc i)) = env!!i
336 show' env ind (LV (Glob n)) = show n
338 show' env ind (LLazyApp e args)
339 = show e ++ "|(" ++ showSep ", " (map (show' env ind) args) ++")"
341 show' env ind (LApp _ e args)
342 = show' env ind e ++ "(" ++ showSep ", " (map (show' env ind) args) ++")"
344 show' env ind (LLazyExp e) = "lazy{ " ++ show' env ind e ++ " }"
345 show' env ind (LForce e) = "force{ " ++ show' env ind e ++ " }"
346 show' env ind (LLet n v e)
347 = "let " ++ show n ++ " = " ++ show' env ind v
348 ++ " in " ++ show' (env ++ [show n]) ind e
350 show' env ind (LLam args e)
351 = "\\ " ++ showSep "," (map show args)
352 ++ " => " ++ show' (env ++ (map show args)) ind e
354 show' env ind (LProj t i) = show t ++ "!" ++ show i
356 show' env ind (LCon loc i n args)
357 = atloc loc ++ show n ++ "(" ++ showSep ", " (map (show' env ind) args) ++ ")"
358 where atloc Nothing = ""
359 atloc (Just l) = "@" ++ show (LV l) ++ ":"
361 show' env ind (LCase up e alts)
362 = "case" ++ update ++ show' env ind e ++ " of \n" ++ fmt alts
369 = "\t" ++ ind ++ "| " ++ showAlt env (ind ++ " ") alt
371 = "\t" ++ ind ++ "| " ++ showAlt env (ind ++ ". ") alt
374 show' env ind (LConst c) = show c
376 show' env ind (LForeign ty n args) = concat
379 , showSep ", " (map (\(ty,x) -> show' env ind x ++ " : " ++ show ty) args)
385 show' env ind (LOp f args)
386 = show f ++ "(" ++ showSep ", " (map (show' env ind) args) ++ ")"
388 show' env ind (LError str) = "error " ++ show str
389 show' env ind LNothing = "____"
391 showAlt env ind (LConCase _ n args e)
392 = show n ++ "(" ++ showSep ", " (map show args) ++ ") => "
395 showAlt env ind (LConstCase c e) = show c ++ " => " ++ show' env ind e
396 showAlt env ind (LDefaultCase e) = "_ => " ++ show' env ind e
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list