1{-# LANGUAGE PatternGuards #-}
3module Idris.DataOpts where
5-- Forcing, detagging and collapsing
8import Idris.AbsSyntaxTree
11import Control.Applicative
16class Optimisable term where
17 applyOpts :: term -> Idris term
19instance (Optimisable a, Optimisable b) => Optimisable (a, b) where
20 applyOpts (x, y) = (,) <$> applyOpts x <*> applyOpts y
22instance (Optimisable a, Optimisable b) => Optimisable (vs, a, b) where
23 applyOpts (v, x, y) = (,,) v <$> applyOpts x <*> applyOpts y
25instance Optimisable a => Optimisable [a] where
26 applyOpts = mapM applyOpts
28instance Optimisable a => Optimisable (Either a (a, a)) where
29 applyOpts (Left t) = Left <$> applyOpts t
30 applyOpts (Right t) = Right <$> applyOpts t
32-- Raw is for compile time optimisation (before type checking)
33-- Term is for run time optimisation (after type checking, collapsing allowed)
34-- Compile time: no collapsing
36instance Optimisable Raw where
37 applyOpts t@(RApp f a)
38 | (Var n, args) <- raw_unapply t -- MAGIC HERE
39 = raw_apply (Var n) <$> mapM applyOpts args
40 | otherwise = RApp <$> applyOpts f <*> applyOpts a
42 applyOpts (RBind n b t) = RBind n <$> applyOpts b <*> applyOpts t
43 applyOpts (RForce t) = applyOpts t
44 applyOpts t = return t
46-- Erase types (makes ibc smaller, and we don't need them)
47instance Optimisable (Binder (TT Name)) where
48 applyOpts (Let t v) = Let <$> return Erased <*> applyOpts v
49 applyOpts b = return (b { binderTy = Erased })
51instance Optimisable (Binder Raw) where
52 applyOpts b = do t' <- applyOpts (binderTy b)
53 return (b { binderTy = t' })
55-- Run-time: do everything
57prel = [txt "Nat", txt "Prelude"]
59instance Optimisable (TT Name) where
60 applyOpts (P _ (NS (UN fn) mod) _)
61 | fn == txt "plus" && mod == prel
62 = return (P Ref (sUN "prim__addBigInt") Erased)
63 applyOpts (P _ (NS (UN fn) mod) _)
64 | fn == txt "mult" && mod == prel
65 = return (P Ref (sUN "prim__mulBigInt") Erased)
66 applyOpts (App (P _ (NS (UN fn) mod) _) x)
67 | fn == txt "fromIntegerNat" && mod == prel
69 applyOpts (P _ (NS (UN fn) mod) _)
70 | fn == txt "fromIntegerNat" && mod == prel
71 = return (App (P Ref (sNS (sUN "id") ["Basics","Prelude"]) Erased) Erased)
72 applyOpts (P _ (NS (UN fn) mod) _)
73 | fn == txt "toIntegerNat" && mod == prel
74 = return (App (P Ref (sNS (sUN "id") ["Basics","Prelude"]) Erased) Erased)
75 applyOpts c@(P (DCon t arity uniq) n _)
76 = return $ applyDataOptRT n t arity uniq []
78 | (c@(P (DCon t arity uniq) n _), args) <- unApply t
79 = applyDataOptRT n t arity uniq <$> mapM applyOpts args
80 | otherwise = App <$> applyOpts f <*> applyOpts a
81 applyOpts (Bind n b t) = Bind n <$> applyOpts b <*> applyOpts t
82 applyOpts (Proj t i) = Proj <$> applyOpts t <*> pure i
83 applyOpts t = return t
85-- Need to saturate arguments first to ensure that optimisation happens uniformly
86applyDataOptRT :: Name -> Int -> Int -> Bool -> [Term] -> Term
87applyDataOptRT n tag arity uniq args
88 | length args == arity = doOpts n args
89 | otherwise = let extra = satArgs (arity - length args)
90 tm = doOpts n (args ++ map (\n -> P Bound n Erased) extra)
93 satArgs n = map (\i -> sMN i "sat") [1..n]
96 bind (n:ns) tm = Bind n (Lam Erased) (pToV n (bind ns tm))
99 -- TODO: Would be nice if this was configurable in idris source!
100 -- Issue #1597 https://github.com/idris-lang/Idris-dev/issues/1597
101 doOpts (NS (UN z) [nat, prelude]) []
102 | z == txt "Z" && nat == txt "Nat" && prelude == txt "Prelude"
104 doOpts (NS (UN s) [nat, prelude]) [k]
105 | s == txt "S" && nat == txt "Nat" && prelude == txt "Prelude"
106 = App (App (P Ref (sUN "prim__addBigInt") Erased) k) (Constant (BI 1))
108 doOpts n args = mkApp (P (DCon tag arity uniq) n Erased) args
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