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

Safe HaskellNone

Language.Syntactic.Constructs.Binding.Optimize

Description

Basic optimization of expressions

Synopsis

Documentation

type ConstFolder dom = forall a. ASTF dom a -> a -> ASTF dom aSource

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 EvalBind dom => Optimize sub ctx dom whereSource

Basic optimization of a sub-domain

Methods

optimizeSym :: Proxy ctx -> ConstFolder dom -> sub a -> Args (AST dom) a -> Writer (Set VarId) (ASTF dom (DenResult a))Source

Bottom-up optimization of a sub-domain. 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 syntax tree.

Instances

(:<: (Condition ctx') dom, :<: (Lambda ctx) dom, :<: (Variable ctx) dom, AlphaEq dom dom dom [(VarId, VarId)], Optimize dom ctx dom) => Optimize (Condition ctx') ctx dom 
(:<: (Construct ctx') dom, Optimize dom ctx dom) => Optimize (Construct ctx') ctx dom 
(:<: (Identity ctx') dom, Optimize dom ctx dom) => Optimize (Identity ctx') ctx dom 
(:<: (Literal ctx') dom, Optimize dom ctx dom) => Optimize (Literal ctx') ctx dom 
(:<: (Select ctx') dom, Optimize dom ctx dom) => Optimize (Select ctx') ctx dom 
(:<: (Tuple ctx') dom, Optimize dom ctx dom) => Optimize (Tuple ctx') ctx dom 
(:<: (Lambda ctx) dom, Optimize dom ctx dom) => Optimize (Lambda ctx) ctx dom 
(:<: (Variable ctx) dom, Optimize dom ctx dom) => Optimize (Variable ctx) ctx dom 
(Optimize sub1 ctx dom, Optimize sub2 ctx dom) => Optimize (:+: sub1 sub2) ctx dom 
(:<: (Let ctxa ctxb) dom, Optimize dom ctx dom) => Optimize (Let ctxa ctxb) ctx dom 

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

optimize :: Optimize dom ctx dom => Proxy ctx -> ConstFolder dom -> ASTF dom a -> ASTF dom aSource

Optimize an expression

optimizeSymDefault :: (sub :<: dom, WitnessCons sub, Optimize dom ctx dom) => Proxy ctx -> ConstFolder dom -> sub a -> Args (AST dom) a -> Writer (Set VarId) (ASTF dom (DenResult a))Source

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