1{-# LANGUAGE PatternGuards #-}
3module Idris.DataOpts(applyOpts) 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 t = return t
45-- Erase types (makes ibc smaller, and we don't need them)
46instance Optimisable (Binder (TT Name)) where
47 applyOpts (Let t v) = Let <$> return Erased <*> applyOpts v
48 applyOpts b = return (b { binderTy = Erased })
50instance Optimisable (Binder Raw) where
51 applyOpts b = do t' <- applyOpts (binderTy b)
52 return (b { binderTy = t' })
54-- Run-time: do everything
56prel = [txt "Nat", txt "Prelude"]
58instance Optimisable (TT Name) where
59 applyOpts (P _ (NS (UN fn) mod) _)
60 | fn == txt "plus" && mod == prel
61 = return (P Ref (sUN "prim__addBigInt") Erased)
62 applyOpts (P _ (NS (UN fn) mod) _)
63 | fn == txt "mult" && mod == prel
64 = return (P Ref (sUN "prim__mulBigInt") Erased)
65 applyOpts (P _ (NS (UN fn) mod) _)
66 | fn == txt "divNat" && mod == prel
67 = return (P Ref (sUN "prim__sdivBigInt") Erased)
68 applyOpts (P _ (NS (UN fn) mod) _)
69 | fn == txt "modNat" && mod == prel
70 = return (P Ref (sUN "prim__sremBigInt") Erased)
71 applyOpts (App _ (P _ (NS (UN fn) mod) _) x)
72 | fn == txt "fromIntegerNat" && mod == prel
74 applyOpts (P _ (NS (UN fn) mod) _)
75 | fn == txt "fromIntegerNat" && mod == prel
76 = return (App Complete (P Ref (sNS (sUN "id") ["Basics","Prelude"]) Erased) Erased)
77 applyOpts (P _ (NS (UN fn) mod) _)
78 | fn == txt "toIntegerNat" && mod == prel
79 = return (App Complete (P Ref (sNS (sUN "id") ["Basics","Prelude"]) Erased) Erased)
80 applyOpts c@(P (DCon t arity uniq) n _)
81 = return $ applyDataOptRT n t arity uniq []
82 applyOpts t@(App s f a)
83 | (c@(P (DCon t arity uniq) n _), args) <- unApply t
84 = applyDataOptRT n t arity uniq <$> mapM applyOpts args
85 | otherwise = App s <$> applyOpts f <*> applyOpts a
86 applyOpts (Bind n b t) = Bind n <$> applyOpts b <*> applyOpts t
87 applyOpts (Proj t i) = Proj <$> applyOpts t <*> pure i
88 applyOpts t = return t
90-- Need to saturate arguments first to ensure that optimisation happens uniformly
91applyDataOptRT :: Name -> Int -> Int -> Bool -> [Term] -> Term
92applyDataOptRT n tag arity uniq args
93 | length args == arity = doOpts n args
94 | otherwise = let extra = satArgs (arity - length args)
95 tm = doOpts n (args ++ map (\n -> P Bound n Erased) extra)
98 satArgs n = map (\i -> sMN i "sat") [1..n]
101 bind (n:ns) tm = Bind n (Lam Erased) (pToV n (bind ns tm))
104 -- TODO: Would be nice if this was configurable in idris source!
105 -- Issue #1597 https://github.com/idris-lang/Idris-dev/issues/1597
106 doOpts (NS (UN z) [nat, prelude]) []
107 | z == txt "Z" && nat == txt "Nat" && prelude == txt "Prelude"
109 doOpts (NS (UN s) [nat, prelude]) [k]
110 | s == txt "S" && nat == txt "Nat" && prelude == txt "Prelude"
111 = App Complete (App Complete (P Ref (sUN "prim__addBigInt") Erased) k) (Constant (BI 1))
113 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