-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This also makes a number of other improvements: - Support multiple (mutually recursive) bindings in `let` - Fix pattern matching on heap-allocated objects (we were losing sharing) - Support heap inlining - Support for selectors (`fst`, `snd`) - Support the selector thunk optimization - Add `--disable-ansi` command line - Improve trace summarization (it previously generated confusing output; see details comments in the code) - Add some new primitive functions (`min`, `max`)
- Loading branch information
Showing
28 changed files
with
993 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- See "Using Circular Programs for Higher-Order Syntax" | ||
-- by Emil Axelsson and Koen Claessen (ICFP 2013) | ||
-- <https://emilaxelsson.github.io/documents/axelsson2013using.pdf> | ||
-- | ||
-- See Unfolder episode 17 for more details. | ||
-- | ||
-- Suggested execution: | ||
-- | ||
-- > cabal run visualize-cbn -- \ | ||
-- > --show-trace \ | ||
-- > --hide-prelude \ | ||
-- > --gc \ | ||
-- > --selector-thunk-opt \ | ||
-- > --inline-heap \ | ||
-- > --hide-inlining \ | ||
-- > --hide-gc \ | ||
-- > --hide-selector-thunk-opt \ | ||
-- > --javascript foo.js \ | ||
-- > -i examples/circular_hos.hs | ||
maxBV = (\exp -> | ||
case exp of { | ||
Var x -> 0 | ||
; App f e -> max (@maxBV f) (@maxBV e) | ||
; Lam n f -> n | ||
} | ||
) | ||
|
||
lam = (\f -> | ||
let { | ||
body = f (Var n) | ||
; n = succ (@maxBV body) | ||
} | ||
in seq n (Lam n body) | ||
) | ||
|
||
main = @lam (\x -> App (App (@lam (\y -> y)) (@lam (\z -> z))) x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
f = (\x -> @g x) | ||
g = (\x -> @h x) | ||
h = (\x -> succ x) | ||
|
||
main = @f 1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- Simple example of two mutually recursive functions | ||
-- f x will return 0 if x is even and 1 if x is odd. | ||
main = | ||
let { | ||
f = (\x -> if eq x 0 then 0 else g (sub x 1)) | ||
; g = (\x -> if eq x 0 then 1 else f (sub x 1)) | ||
} in f 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
-- The classic repMin circular program due to Richard Bird. | ||
-- See Unfolder episode 17 for more details. | ||
-- | ||
-- Suggested execution: | ||
-- | ||
-- > cabal run visualize-cbn -- \ | ||
-- > --show-trace \ | ||
-- > --hide-prelude \ | ||
-- > --gc \ | ||
-- > --selector-thunk-opt \ | ||
-- > --inline-heap \ | ||
-- > --hide-inlining \ | ||
-- > --hide-gc \ | ||
-- > --hide-selector-thunk-opt \ | ||
-- > --javascript foo.js \ | ||
-- > -i examples/repmin.hs | ||
worker = (\m -> \t -> | ||
case t of { | ||
Leaf x -> Pair x (Leaf m) | ||
; Branch l r -> | ||
let { | ||
resultLeft = @worker m l | ||
; resultRight = @worker m r | ||
; mb = min (fst resultLeft) (fst resultRight) | ||
} | ||
in seq mb (Pair mb (Branch (snd resultLeft) (snd resultRight))) | ||
} | ||
) | ||
|
||
repMin = (\t -> | ||
let result = @worker (fst result) t | ||
in snd result | ||
) | ||
|
||
main = @repMin (Branch (Branch (Leaf 1) (Leaf 2)) (Branch (Leaf 3) (Leaf 4))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- Demonstration of the need for the selector thunk optimization | ||
-- This is the example from "Fixing some space leaks with a garbage collector". | ||
|
||
break = (\xs -> | ||
case xs of { | ||
Nil -> Pair Nil Nil | ||
; Cons x xs' -> | ||
if eq x 0 | ||
then Pair Nil xs' | ||
else let b = @break xs' | ||
in Pair (Cons x (fst b)) (snd b) | ||
} | ||
) | ||
|
||
-- strict version of concat (makes the example more clear) | ||
concat = (\xs -> \ys -> | ||
case xs of { | ||
Nil -> ys | ||
; Cons x xs' -> let r = @concat xs' ys in seq r (Cons x r) | ||
} | ||
) | ||
|
||
surprise = (\xs -> | ||
let b = @break xs | ||
in @concat (fst b) (@concat (Cons 4 (Cons 5 (Cons 6 Nil))) (snd b)) | ||
) | ||
|
||
main = @surprise (Cons 1 (Cons 2 (Cons 3 (Cons 0 (Cons 7 (Cons 8 (Cons 9 Nil))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.