@@ -59,6 +59,8 @@ elabClauses :: ElabInfo -> FC -> FnOpts -> Name -> [PClause] -> Idris ()
5959elabClauses info fc opts n_in cs = let n = liftname info n_in in
6060 do ctxt <- getContext
6161 ist <- getIState
62+ optimise <- getOptimise
63+ let petrans = PETransform `elem` optimise
6264 inacc <- map fst <$> fgetState (opt_inaccessible . ist_optimisation n)
6365
6466 -- Check n actually exists, with no definition yet
@@ -74,9 +76,11 @@ elabClauses info fc opts n_in cs = let n = liftname info n_in in
7476 let atys = map snd (getArgTys fty)
7577 cs_elab <- mapM (elabClause info opts)
7678 (zip [0..] cs)
79+ -- pats_raw is the version we'll work with at compile time:
80+ -- no simplification or PE
7781 let (pats_in, cs_full) = unzip cs_elab
78−
79− logLvl 3 $ "Elaborated patterns:\n" ++ show pats_in
82+ let pats_raw = map (simple_lhs (tt_ctxt ist)) pats_in
83+ logLvl 3 $ "Elaborated patterns:\n" ++ show pats_raw
8084
8185 solveDeferred n
8286
@@ -94,22 +98,28 @@ elabClauses info fc opts n_in cs = let n = liftname info n_in in
9498 -- If the definition is specialisable, this reduces the
9599 -- RHS
96100 pe_tm <- doPartialEval ist tpats
97− let pats = map (simple_lhs (tt_ctxt ist)) pe_tm
101+ let pats_pe = if petrans
102+ then map (simple_lhs (tt_ctxt ist)) pe_tm
103+ else pats_raw
98104
99105 let tcase = opt_typecase (idris_options ist)
100106
101107 -- Look for 'static' names and generate new specialised
102108 -- definitions for them, as well as generating rewrite rules
103109 -- for partially evaluated definitions
104− newrules <- mapM (\ e -> case e of
105− Left _ -> return []
106− Right (l, r) -> elabPE info fc n r) pats
110+ newrules <- if petrans
111+ then mapM (\ e -> case e of
112+ Left _ -> return []
113+ Right (l, r) -> elabPE info fc n r) pats_pe
114+ else return []
107115
108116 -- Redo transforms with the newly generated transformations, so
109117 -- that the specialised application we've just made gets
110118 -- used in place of the general one
111119 ist <- getIState
112− let pats_transformed = transformPats ist pats
120+ let pats_transformed = if petrans
121+ then transformPats ist pats_pe
122+ else pats_pe
113123
114124 -- Summary of what's about to happen: Definitions go:
115125 --
@@ -118,11 +128,13 @@ elabClauses info fc opts n_in cs = let n = liftname info n_in in
118128 -- addCaseDef builds case trees from <pdef> and <pdef'>
119129
120130 -- pdef is the compile-time pattern definition.
121− -- This will get further optimised for run-time, and, separately,
122− -- further inlined to help with totality checking.
123− let pdef = map debind pats_transformed
131+ -- This will get further inlined to help with totality checking.
132+ let pdef = map debind pats_raw
133+ -- pdef_pe is the one which will get further optimised
134+ -- for run-time, and, partially evaluated
135+ let pdef_pe = map debind pats_transformed
124136
125− logLvl 5 $ "Initial typechecked patterns:\n" ++ show pats
137+ logLvl 5 $ "Initial typechecked patterns:\n" ++ show pats_raw
126138 logLvl 5 $ "Initial typechecked pattern def:\n" ++ show pdef
127139
128140 -- NOTE: Need to store original definition so that proofs which
@@ -137,13 +149,13 @@ elabClauses info fc opts n_in cs = let n = liftname info n_in in
137149
138150 case specNames opts of
139151 Just _ ->
140− do logLvl 2 $ "Partially evaluated:\n" ++ show pats
152+ do logLvl 2 $ "Partially evaluated:\n" ++ show pats_pe
141153 logLvl 2 $ "Transformed:\n" ++ show pats_transformed
142154 _ -> return ()
143155
144156 erInfo <- getErasureInfo <$> getIState
145157 tree@(CaseDef scargs sc _) <- tclift $
146− simpleCase tcase False reflect CompileTime fc inacc atys pdef erInfo
158+ simpleCase tcase False reflect CompileTime fc inacc atys pdef erInfo
147159 cov <- coverage
148160 pmissing <-
149161 if cov && not (hasDefault cs)
@@ -164,8 +176,9 @@ elabClauses info fc opts n_in cs = let n = liftname info n_in in
164176 else return []
165177 let pcover = null pmissing
166178
167− -- pdef' is the version that gets compiled for run-time
168− pdef_in' <- applyOpts pdef
179+ -- pdef' is the version that gets compiled for run-time,
180+ -- so we start from the partially evaluated version
181+ pdef_in' <- applyOpts pdef_pe
169182 let pdef' = map (simple_rt (tt_ctxt ist)) pdef_in'
170183
171184 logLvl 5 $ "After data structure transformations:\n" ++ show pdef'
@@ -197,7 +210,7 @@ elabClauses info fc opts n_in cs = let n = liftname info n_in in
197210 ctxt <- getContext
198211 ist <- getIState
199212 let opt = idris_optimisation ist
200− putIState (ist { idris_patdefs = addDef n (force pdef, force pmissing)
213+ putIState (ist { idris_patdefs = addDef n (force pdef_pe, force pmissing)
201214 (idris_patdefs ist) })
202215 let caseInfo = CaseInfo (inlinable opts) (dictionary opts)
203216 case lookupTy n ctxt of
@@ -207,8 +220,11 @@ elabClauses info fc opts n_in cs = let n = liftname info n_in in
207220 (AssertTotal `elem` opts)
208221 atys
209222 inacc
210− pats
211− pdef pdef pdef_inl pdef' ty)
223+ pats_pe
224+ pdef
225+ pdef -- compile time
226+ pdef_inl -- inlined
227+ pdef' ty)
212228 addIBC (IBCDef n)
213229 setTotality n tot
214230 when (not reflect) $ do totcheck (fc, n)
@@ -320,7 +336,8 @@ elabPE info fc caller r =
320336 let cgns' = filter (/= n) cgns
321337 let opts = [Specialise (map (\x -> (x, Nothing)) cgns' ++
322338 (n, Just 65536) :
323− mapMaybe specName (snd specapp))]
339+ mapMaybe (specName (pe_simple specdecl))
340+ (snd specapp))]
324341 logLvl 3 $ "Specialising application: " ++ show specapp
325342 ++ " in " ++ show caller ++
326343 " with " ++ show opts
@@ -347,14 +364,14 @@ elabPE info fc caller r =
347364 -- if it doesn't work, just don't specialise. Could happen for lots
348365 -- of valid reasons (e.g. local variables in scope which can't be
349366 -- lifted out).
350− (\e -> do logLvl 4 $ "Couldn't specialise: " ++ (pshow ist e)
367+ (\e -> do logLvl 3 $ "Couldn't specialise: " ++ (pshow ist e)
351368 return [])
352369
353− specName (ImplicitS, tm)
354− | (P Ref n _, _) <- unApply tm = Just (n, Just 1)
355− specName (ExplicitS, tm)
356− | (P Ref n _, _) <- unApply tm = Just (n, Just 1)
357− specName _ = Nothing
370+ specName simpl (ImplicitS, tm)
371+ | (P Ref n _, _) <- unApply tm = Just (n, Just (if simpl then 1 else 0))
372+ specName simpl (ExplicitS, tm)
373+ | (P Ref n _, _) <- unApply tm = Just (n, Just (if simpl then 1 else 0))
374+ specName simpl _ = Nothing
358375
359376 concreteArg ist (ImplicitS, tm) = concreteTm ist tm
360377 concreteArg ist (ExplicitS, tm) = concreteTm ist tm