definition:
|
optimizeExp :: (QName->Bool, [(QName,Bool)]) -> Expr -> (Bool,Int,Int,Expr)
optimizeExp _ (Var i) = (False,0,0,Var i)
optimizeExp _ (Lit l) = (False,0,0,Lit l)
optimizeExp funinfo@(depinfo,lininfo) (Comb ct f es)
| f==("Prelude","=:<=") && length es == 2
= let e1 = head es
e2 = head (tail es)
(cyc1,nsu1,lnsu1,opte1) = optimizeExp funinfo e1
(cyc2,nsu2,lnsu2,opte2) = optimizeExp funinfo e2
cyclicFP = cyc1 || cyc2 || any depinfo (funcsInExpr e1)
in if linearExpr e1 && onlyLinearFunctions lininfo e1
then (cyclicFP,nsu1+nsu2+1,lnsu1+lnsu2+1,
Comb ct ("Prelude","=:<<=") [opte1,opte2])
else (cyclicFP,nsu1+nsu2+1,lnsu1+lnsu2,
Comb ct ("Prelude","=:<=") [opte1,opte2])
| otherwise
= let (cycs,nsus,lnsus,optes) = unzip4 (map (optimizeExp funinfo) es)
in (or cycs, sum nsus, sum lnsus, Comb ct f optes)
optimizeExp funinfo (Free vs e) =
let (cyc,nsu,lnsu,opte) = optimizeExp funinfo e
in (cyc,nsu,lnsu,Free vs opte)
optimizeExp funinfo (Let bs exp) =
let (cyc,nsu,lnsu,optexp) = optimizeExp funinfo exp
(bvs,bes) = unzip bs
(cycs,nsus,lnsus,optbes) = unzip4 (map (optimizeExp funinfo) bes)
in (or (cyc:cycs), nsu + sum nsus, lnsu + sum lnsus,
Let (zip bvs optbes) optexp)
optimizeExp funinfo (Or e1 e2) =
let (cyc1,nsu1,lnsu1,opte1) = optimizeExp funinfo e1
(cyc2,nsu2,lnsu2,opte2) = optimizeExp funinfo e2
in (cyc1||cyc2, nsu1+nsu2, lnsu1+lnsu2, Or opte1 opte2)
optimizeExp funinfo (Case ct exp bs) =
let (cyc,nsu,lnsu,optexp) = optimizeExp funinfo exp
(cycs,nsus,lnsus,optbs) = unzip4 (map optimizeBranch bs)
in (or (cyc:cycs), nsu + sum nsus, lnsu + sum lnsus, Case ct optexp optbs)
where
optimizeBranch (Branch patt be) =
let (ncyc,nsub,lnsub,optbe) = optimizeExp funinfo be
in (ncyc,nsub,lnsub,Branch patt optbe)
|
iotype:
|
{(_,{Var}) |-> {(,,,)} || (_,{Lit}) |-> {(,,,)} || ({(,)},{Comb}) |-> {(,,,)} || (_,{Free}) |-> {(,,,)} || (_,{Let}) |-> {(,,,)} || (_,{Or}) |-> {(,,,)} || (_,{Case}) |-> {(,,,)}}
|