Skip to content

Commit

Permalink
optimization: begin lambda lifting chapter
Browse files Browse the repository at this point in the history
See #63
  • Loading branch information
doyougnu committed Feb 2, 2023
1 parent a0242f0 commit b7b3036
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
24 changes: 24 additions & 0 deletions bib/book.bib
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,27 @@ @article{pointerTaggingLaziness
pages = {277–288},
numpages = {12}
}

@InProceedings{lambdaLifting,
author="Johnsson, Thomas",
editor="Jouannaud, Jean-Pierre",
title="Lambda lifting: Transforming programs to recursive equations",
booktitle="Functional Programming Languages and Computer Architecture",
year="1985",
publisher="Springer Berlin Heidelberg",
address="Berlin, Heidelberg",
pages="190--203",
abstract="Lambda lifting is a technique for transforming a functional program with local function definitions, possibly with free variables in the function definitions, into a program consisting only of global function (combinator) definitions which will be used as rewrite rules. Different ways of doing lambda lifting are presented, as well as reasons for rejecting or selecting the method used in our Lazy ML compiler. A functional program implementing the chosen algorithm is given.",
isbn="978-3-540-39677-2"
}

@misc{selectiveLambdaLifting,
doi = {10.48550/ARXIV.1910.11717},
url = {https://arxiv.org/abs/1910.11717},
author = {Graf, Sebastian and Jones, Simon Peyton},
keywords = {Programming Languages (cs.PL), FOS: Computer and information sciences, FOS: Computer and information sciences},
title = {Selective Lambda Lifting},
publisher = {arXiv},
year = {2019},
copyright = {arXiv.org perpetual, non-exclusive license}
}
50 changes: 46 additions & 4 deletions src/Optimizations/GHC_opt/lambda_lifting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,55 @@
`Lambda Lifting`
================

Lambda Lifting is a classic optimization technique...
Lambda Lifting :cite:p:`lambdaLifting` is a classic rewriting technique that
rewrites functions to avoid excess allocations. It optimizes function
definitions by moving local functions to the global scope of the program;
thereby closure allocations, and by adding parameters to the function definition
to capture free variables.

A Working Example
-----------------

Testing Exec
Consider the following program [#]_:

.. exec::
:context: true
:process: haskell

module Main where

f :: Int -> Int -> Int
f a 0 = a
f a n = f (g (n `mod` 2)) (n - 1)
where
g 0 = a
g n = 1 + g (n - 1)

main :: IO ()
main = print $ f 10 100


How Lambda Lifting Works in GHC
-------------------------------


Observing the Effect of Lambda Lifting
--------------------------------------

When to Manually Apply Lambda Lifting
-------------------------------------




Testing Exec

.. exec::
:context: false
:process: haskell

module Main where

main :: IO ()
main = do
let x = fmap (+10) [1..10]
Expand Down Expand Up @@ -42,9 +80,13 @@ and also we can load a package

and we can also run from cabal target!!

.. exec:: code/lethargy/bench/Weigh/Main.hs
.. exec:: code/lethargy/bench/TooManyClosures.hs
:context: true
:process: haskell
:project_dir: code/lethargy/
:with: cabal
:args: bench lethargy:weigh
:args: bench lethargy:tooManyClosures


.. [#] This program comes from Sebastian Graf and Simon Peyton Jones
:cite:p:`selectiveLambdaLifting`; thank you for your labor!:

0 comments on commit b7b3036

Please sign in to comment.