https://github.com/ocharles/what-it-do
Automatically trace all (showable) binds in do expressions
https://github.com/ocharles/what-it-do
ghc haskell source-plugin
Last synced: 8 months ago
JSON representation
Automatically trace all (showable) binds in do expressions
- Host: GitHub
- URL: https://github.com/ocharles/what-it-do
- Owner: ocharles
- License: mit
- Created: 2018-06-11T11:29:37.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-11T11:33:52.000Z (about 8 years ago)
- Last Synced: 2025-04-30T07:38:28.922Z (about 1 year ago)
- Topics: ghc, haskell, source-plugin
- Language: Haskell
- Size: 5.86 KB
- Stars: 84
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hey GHC, `what-it-do`?
`what-it-do` is a GHC source plugin that rewrites `do` expressions to `trace`
all binds. For example, given the following code:
```haskell
main =
do
a <- return ( 42 :: Int )
b <- return ( a * 2 )
f <- return ( \b -> return ( b - 1 ) )
c <- f ( a * b )
print c
```
We can trace all binds by using `debugDo` from `what-it-do`, and enabling the plugin:
```haskell
{-# OPTIONS -fplugin=WhatItDo #-}
import WhatItDo (traceDo)
main =
traceDo (do
a <- return 42
b <- return ( a * 2 )
f <- return ( \b -> return ( b - 1 ) )
c <- f ( a * b )
print c)
```
Now, when this code runs, we see:
```
(Test.hs:5:5-29) a = 42
(Test.hs:6:5-25) b = 84
(Test.hs:8:5-20) c = 3527
3527
```
Magic!
## Wha... how is this possible?
Under the hood, `what-it-do` has rewritten the original `do` expression to:
```haskell
main =
do
a <- return ( 42 :: Int ) >>= \x -> trace ( show x ) ( return x )
b <- return ( a * 2 ) >>= \x -> trace ( show x ) ( return x )
f <- return ( \b -> return ( b - 1 ) )
c <- f ( a * b ) >>= \x -> trace ( show x ) ( return x )
print c
```
Notice that because the plugin has access to the type-checker, we only trace
things that can be shown. The binding to `f` doesn't get traced, because we
can't show what `f` is.