{"id":20230729,"url":"https://github.com/cdepillabout/heterocephalus-example","last_synced_at":"2026-02-16T23:12:02.711Z","repository":{"id":66327073,"uuid":"79066774","full_name":"cdepillabout/heterocephalus-example","owner":"cdepillabout","description":"simple explanation on how to use -ddump-splices with stack","archived":false,"fork":false,"pushed_at":"2017-01-16T20:26:24.000Z","size":18,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T15:47:27.639Z","etag":null,"topics":["ddump-splices","example","haskell","template-haskell","tutorial"],"latest_commit_sha":null,"homepage":"https://functor.tokyo/blog/2017-01-16-looking-at-generated-template-haskell","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cdepillabout.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-15T23:36:06.000Z","updated_at":"2019-12-05T06:35:11.000Z","dependencies_parsed_at":"2023-03-10T23:49:43.562Z","dependency_job_id":null,"html_url":"https://github.com/cdepillabout/heterocephalus-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdepillabout%2Fheterocephalus-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdepillabout%2Fheterocephalus-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdepillabout%2Fheterocephalus-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdepillabout%2Fheterocephalus-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdepillabout","download_url":"https://codeload.github.com/cdepillabout/heterocephalus-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248265832,"owners_count":21075050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ddump-splices","example","haskell","template-haskell","tutorial"],"created_at":"2024-11-14T07:43:35.735Z","updated_at":"2025-10-12T07:45:30.316Z","avatar_url":"https://github.com/cdepillabout.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Using -ddump-splices\n\n[![Build Status](https://travis-ci.org/cdepillabout/heterocephalus-example.svg?branch=master)](https://travis-ci.org/cdepillabout/heterocephalus-example)\n\nWhen debugging Haskell code, it can be useful to look at the code generated\nfrom Template Haskell and Quasiquote expressions.  This repository is a\ntutorial explaining an easy way to view the generated Haskell code.\n\n## The Intro\n\nThis repository contains a single Haskell file, [Example.hs](app/Example.hs).  It\nis a small example of using\n[heterocephalus](https://hackage.haskell.org/package/heterocephalus), a\ntype-safe template engine.\n\nThe Haskell code defining the template looks like this:\n\n```haskell\ntemplate :: Html\ntemplate =\n  let a = \"hello\"\n      b = 3 :: Int\n      cs = [\"foo\", \"bar\", \"baz\"]\n  in [compileText|\nvariable interpolation:\n  #{a}\n\nif control statement:\n%{ if (b == 3) }\n  b is 3\n%{ else }\n  b is some other number\n%{ endif }\n\nforall control statement:\n%{ forall c \u003c- cs }\n  #{c}\n%{ endforall }\n   |]\n```\n\nEverything within the `compileText` quasiquote is expanded at compile-time into\nHaskell code.  This tutorial explains how to see exactly what code is\ngenerated.\n\n## The Setup\n\nFirst, clone this repository and `cd` into it.\n\n```sh\n$ git clone https://github.com/cdepillabout/heterocephalus-example.git\n$ cd heterocephalus-example/\n```\n\nThis tutorial assumes you are using `stack`.  You need to [install\n`stack`](https://docs.haskellstack.org/en/stable/README/#how-to-install) if you\nhaven't already done so.\n\nIf GHC is not installed, it can be installed with `stack setup`.\n\n```sh\n$ stack setup\n```\n\nNow that GHC is installed, the example program can be built with `stack build`.\n\n```sh\n$ stack build\n```\n\nThe example program is built and placed somewhere under the `.stack-work/`\ndirectory.  `stack exec`  can be used to execute it:\n\n```sh\n$ stack exec -- heterocephalus-example\n\nvariable interpolation:\n  hello\n\nif control statement:\n  b is 3\n\nforall control statement:\n  foo\n  bar\n  baz\n```\n\n## `-ddump-splices`\n\nThe example program needs to be compiled with the GHC flag `-ddump-splices` in\norder to get GHC/Stack to dump the generated Haskell code to a file.\n\nIf the example program has already been built (as above), then `stack clean`\nneeds to be run to make sure the executable gets rebuilt.  After that, `stack\nbuild` can be run again, but this time with the `-ddump-splices` flag.\n\n\n```sh\n$ stack clean\n$ stack build --ghc-options=\"-ddump-splices\"\n```\n\n__NOTE__: _Not running `stack clean` first can cause `stack build` to appear to\nsucceed, but no splice file to be generated.  Make sure `stack clean` is run\nbefore `stack build` whenever `-ddump-splices` is being used._\n\nGHC/Stack will generate a splice file somewhere under `.stack-work/`.  The\nlocation of the splice file will change depending on the architecture and Cabal\nversion. `find` can be used to figure out where the splice file is.\n\n```sh\n$ find .stack-work/ -name '*.dump-splices'\n.stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/heterocephalus-example/heterocephalus-example-tmp/app/Example.dump-splices\n```\n\nThis `.dump-splices` file will show what Haskell code is generated from each\nTemplate Haskell and quasiquote expression.\n\nFor example, a quasiquote like this:\n\n```haskell\n[compileText|foo #{a}|]\n```\n\nwould produce Haskell code that looks like this (slightly simplified):\n\n```haskell\ndo\n  preEscapedText \"foo \"\n  preEscapedToMarkup a\n```\n\n## Generated Code for the Example Template\n\nLets go back to the example program.  Once again, the template is defined like\nthis:\n\n```haskell\ntemplate :: Html\ntemplate =\n  let a = \"hello\"\n      b = 3 :: Int\n      cs = [\"foo\", \"bar\", \"baz\"]\n  in [compileText|\nvariable interpolation:\n  #{a}\n\nif control statement:\n%{ if (b == 3) }\n  b is 3\n%{ else }\n  b is some other number\n%{ endif }\n\nforall control statement:\n%{ forall c \u003c- cs }\n  #{c}\n%{ endforall }\n   |]\n```\n\nIf this is compiled with the `-ddump-splices` flag, the output\n`Example.dump-splices` file will look like this (after being cleaned up a\nlittle to make it more readable):\n\n```haskell\napp/Example.hs:(15,19)-(30,5): Splicing expression\n    template-haskell-2.11.0.0:Language.Haskell.TH.Quote.quoteExp\n      compileText\n      \"variable interpolation:\n         #{a}\n\n       if control statement:\n       %{ if (b == 3) }\n         b is 3\n       %{ else }\n         b is some other number\n       %{ endif }\n\n       forall control statement:\n       %{ forall c \u003c- cs }\n         #{c}\n       %{ endforall }\n          \"\n  ======\u003e\n    do\n      preEscapedText \"variable interpolation:\\n\"\n      preEscapedToMarkup a\n      preEscapedText \"\\n\\nif control statement:\\n\"\n      condH\n        [(b == 3, preEscapedText \"  b is 3\")]\n        (Just (preEscapedText \"  b is some other number\"))\n      preEscapedText \"\\n\\nforall control statement:\\n\"\n      forM_ cs $ \\c -\u003e do\n        preEscapedText \"  \"\n        preEscapedToMarkup c\n        preEscapedText \"\\n\"\n```\n\nAbove the `=======\u003e` line, you can see the quasiquote as it exists in the\nHaskell file.  Below the line, you can see the Haskell code that GHC has\ngenerated.\n\n## Conclusion\n\nSometimes it is difficult to read the splice files, but it there is often no\nother way to easily debug Template Haskell and quasiquotes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdepillabout%2Fheterocephalus-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdepillabout%2Fheterocephalus-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdepillabout%2Fheterocephalus-example/lists"}