syntactic-1.17: Generic abstract syntax, and utilities for embedded languages

Safe HaskellNone
LanguageHaskell2010

Language.Syntactic.Constructs.Binding.Optimize

Description

Basic optimization

Synopsis

Documentation

type ConstFolder dom = forall a. ASTF dom a -> a -> ASTF dom a Source #

Constant folder

Given an expression and the statically known value of that expression, returns a (possibly) new expression with the same meaning as the original. Typically, the result will be a Literal, if the relevant type constraints are satisfied.

class Optimize sym where Source #

Basic optimization

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (sym sig -> AST dom sig) -> sym sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Bottom-up optimization of an expression. The optimization performed is up to each instance, but the intention is to provide a sensible set of "always-appropriate" optimizations. The default implementation optimizeSymDefault does only constant folding. This constant folding uses the set of free variables to know when it's static evaluation is possible. Thus it is possible to help constant folding of other constructs by pruning away parts of the syntax tree that are known not to be needed. For example, by replacing (using ordinary Haskell as an example)

if True then a else b

with a, we don't need to report the free variables in b. This, in turn, can lead to more constant folding higher up in the expression.

Instances
Optimize Empty Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Empty sig -> AST dom sig) -> Empty sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Tuple Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Tuple sig -> AST dom sig) -> Tuple sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Select Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Select sig -> AST dom sig) -> Select sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Literal Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Literal sig -> AST dom sig) -> Literal sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Identity Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Identity sig -> AST dom sig) -> Identity sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Construct Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Construct sig -> AST dom sig) -> Construct sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Condition Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Condition sig -> AST dom sig) -> Condition sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Let Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Let sig -> AST dom sig) -> Let sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Lambda Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Lambda sig -> AST dom sig) -> Lambda sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize Variable Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> (Variable sig -> AST dom sig) -> Variable sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

(Optimize sub1, Optimize sub2) => Optimize (sub1 :+: sub2) Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom => ConstFolder dom -> ((sub1 :+: sub2) sig -> AST dom sig) -> (sub1 :+: sub2) sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Optimize dom => Optimize (dom :|| p) Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom0 => ConstFolder dom0 -> ((dom :|| p) sig -> AST dom0 sig) -> (dom :|| p) sig -> Args (AST dom0) sig -> Writer (Set VarId) (ASTF dom0 (DenResult sig)) Source #

Optimize dom => Optimize (dom :| p) Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom0 => ConstFolder dom0 -> ((dom :| p) sig -> AST dom0 sig) -> (dom :| p) sig -> Args (AST dom0) sig -> Writer (Set VarId) (ASTF dom0 (DenResult sig)) Source #

Optimize dom => Optimize (SubConstr1 c dom p) Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom0 => ConstFolder dom0 -> (SubConstr1 c dom p sig -> AST dom0 sig) -> SubConstr1 c dom p sig -> Args (AST dom0) sig -> Writer (Set VarId) (ASTF dom0 (DenResult sig)) Source #

Optimize dom => Optimize (SubConstr2 c dom pa pb) Source # 
Instance details

Defined in Language.Syntactic.Constructs.Binding.Optimize

Methods

optimizeSym :: Optimize' dom0 => ConstFolder dom0 -> (SubConstr2 c dom pa pb sig -> AST dom0 sig) -> SubConstr2 c dom pa pb sig -> Args (AST dom0) sig -> Writer (Set VarId) (ASTF dom0 (DenResult sig)) Source #

type Optimize' dom = (Optimize dom, EvalBind dom, AlphaEq dom dom dom [(VarId, VarId)], ConstrainedBy dom Typeable) Source #

optimizeM :: Optimize' dom => ConstFolder dom -> ASTF dom a -> Writer (Set VarId) (ASTF dom a) Source #

optimize :: Optimize' dom => ConstFolder dom -> ASTF dom a -> ASTF dom a Source #

Optimize an expression

optimizeSymDefault :: Optimize' dom => ConstFolder dom -> (sym sig -> AST dom sig) -> sym sig -> Args (AST dom) sig -> Writer (Set VarId) (ASTF dom (DenResult sig)) Source #

Convenient default implementation of optimizeSym (uses evalBind to partially evaluate)