Safe Haskell | None |
---|---|
Language | Haskell2010 |
This is a helper module that enables the use of let-bound variables in your S-expression.
- discoverLetBindings :: (Monoid str, IsString str, Eq str, Eq a, Show a) => DiscoveryGuide a str -> SExpr a -> SExpr a
- data DiscoveryGuide a str = Guide {
- maxLetBinds :: Int -> Int
- minExprSize :: Int
- allowRecursion :: Bool
- weighting :: SExpr a -> Int -> Int
- letMaker :: IsString str => str -> a
- labelMaker :: (IsString str, Monoid str) => str -> SExpr a -> a
- extractStr :: IsString str => a -> Maybe str
- nativeGuide :: (str -> a) -> (str -> SExpr a -> a) -> DiscoveryGuide a str
- letExpand :: (Eq a, Show a, Eq str, IsString str) => (a -> Maybe str) -> SExpr a -> SExpr a
Documentation
This module allows let bindings to be introduced into the S-Expression syntax.
For example, instead of:
(concat (if (enabled x) (+ (width x) (width y)) (width y)) " meters")
this can be re-written with let bindings:
(let ((wy (width y)) (wboth (+ (width x) wy)) (wide (if (enabled x) wboth wy)) ) (concat wide " meters"))
As S-expressions grow larger, let-binding can help readability for
those expressions. This module provides the discoverLetBindings
function that will convert an S-expression into one containing
let-bound variables, and the inverse function letExpand
which will
expand let-bound variables back into the expression.
id = letExpand . discoverLetBindings guide
The typical use is to add let bindings before serializing to disk, and then expand the bindings after reading from the disk but before passing to other processing; this process allows the application using the S-Expressions to be unaware of the let-binding compression, although it does not obtain corresponding advantages of the re-use of let-bound variables.
The discoverLetBindings
function can be called to automatically
assign let bindings based on a weighting algorithm of discovered
S-expression phrases. The discovery is guided by parameters provided
by the caller in the DiscoveryGuide
; this guide also provides the
functions used to create the variables and the top-level let statement
in the language of the current S-expression.
The weighting
function of the DiscoveryGuide
can be used to assign
weights to various S-expression phrases: the S-expressions with the
highest weights will be let-bound to variables (up to the
maxLetBinds
limit). A weighting value of 0 will cause the
sub-expression to be ignored (never let-bound) and a value of >=
1000000 will *always* insert a let-binding, ignoring all other limits.
discoverLetBindings :: (Monoid str, IsString str, Eq str, Eq a, Show a) => DiscoveryGuide a str -> SExpr a -> SExpr a Source #
Called to convert a plain S-expression into one with let-bound variables. The let bindings are "discovered" with the assistance of the guide.
data DiscoveryGuide a str Source #
This object provides guidance to the discoverLetBindings
function, establishing various parameters for the discovery
process.
Guide | |
|
nativeGuide :: (str -> a) -> (str -> SExpr a -> a) -> DiscoveryGuide a str Source #
Returns a default DiscoveryGuide
.
Expanding
letExpand :: (Eq a, Show a, Eq str, IsString str) => (a -> Maybe str) -> SExpr a -> SExpr a Source #
The letExpand
function is passed an S-expression that (may)
contain let-bound variables and will return an equivalent
S-expression that does not contain any let bindings, where let
bindings have been expanded into the expression.