Skip to content

Commit

Permalink
configure doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
tscholak committed Feb 25, 2022
1 parent 22b2a92 commit 861aa4d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Distribution.Extra.Doctest (defaultMainWithDoctests)

main :: IO ()
main = defaultMainWithDoctests "doctests"
22 changes: 22 additions & 0 deletions doctests/Doctests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Main where

import Build_doctests (flags_exe_website, module_sources_exe_website, pkgs_exe_website)
import Data.Foldable (traverse_)
import System.Environment (lookupEnv)
import Test.DocTest (doctest)

main :: IO ()
main = do
libDir <- lookupEnv "NIX_GHC_LIBDIR"

let args =
concat
[ flags_exe_website,
pkgs_exe_website,
maybe [] (\x -> ["-package-db " <> x <> "/package.conf.d"]) libDir,
["-XOverloadedStrings", "-XScopedTypeVariables", "-XDataKinds"],
module_sources_exe_website
]

traverse_ putStrLn args
doctest args
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
};

website = haskellNix.website.components.exes.website;
website-doctests = haskellNix.website.components.tests.doctests;
in rec {
packages = {
inherit website;
inherit website-doctests;
};

apps.website = utils.lib.mkApp {
Expand Down
10 changes: 5 additions & 5 deletions site/posts/Flattening.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ image: tape.gif
Preliminaries
-------------

This is a literate Haskell essay.
This is a [Literate Haskell](https://wiki.haskell.org/Literate_programming) essay:
Every line of program code in this article has been checked by the Haskell compiler.
Every example and property in the Haddock comments has been tested by the doctest tool.

Expand Down Expand Up @@ -99,8 +99,8 @@ We used these techniques to reimplement two simple recursive functions,
These functions are both specific examples of a `fold`.
They consume a value of type `Tree`,
a data type for binary trees with two constructors, `Nil` and `Node`.
`printTree` reduces the tree bit by bit in depth-first left-to-right order
to an effect, that is, printing each leaf value to stdout as it is encountered.
`printTree` reduces the tree node by node in depth-first, left-to-right order
to an effect, that is, printing each leaf value to `stdout` as it is encountered.
And `accumTree` reduces the tree to value, that is, the sum of all leaf values.

Even though we worked very hard to remove all recursion from these functions,
Expand Down Expand Up @@ -268,7 +268,7 @@ Both of these are defined in
[Data.Functor.Foldable](https://hackage.haskell.org/package/recursion-schemes-5.2.2.2/docs/Data-Functor-Foldable.html).
The `cata` function is a generalization of `fold` and takes two arguments:

* A function that performs one step of a recursive computation. For us, that function is `linearizeStep` which is doing the actual work. It has the type `Base a (TTape t) -> TTape t`.
* A function that performs one step of a recursive computation. For us, that function is `linearizeStep` that is doing the actual work. It has the type `Base a (TTape t) -> TTape t`.
* The value that needs to be worked on. That value has the type `a`.

With these, `cata` is recursively chewing up the value `a` and turning it into a `TTape t`.
Expand Down Expand Up @@ -407,7 +407,7 @@ I propose the following encoding:
I hope the examples make it clear enough that in this encoding:

1. `R` (for "right") is the token for `MoreF`.
2. `I 0` (for "integer") is the token for the first argument of `MoreF`, which is always `0 :: Int` in this contrived example.
2. `I 0` (for "integer") is the token for the first argument of `MoreF`. That argument is always `0 :: Int` in this contrived example.
3. `Rec _` is the token for the recursive case. Its argument counts the number of tokens needed to encode it. Effectively, this just measures the length of the previous tape we pass to the `linearizedMore` function.

Note how, in the above examples,
Expand Down
5 changes: 3 additions & 2 deletions site/posts/Unrecurse.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ image: goodbye.gif
Preliminaries
-------------

For this literate Haskell essay, a few language extensions are required.
For this [Literate Haskell](https://wiki.haskell.org/Literate_programming)
essay, a few language extensions are required.
Nothing extraordinarily fancy, just the usually fancy Haskell flavour.

\begin{code}
Expand Down Expand Up @@ -775,7 +776,7 @@ that executes the `Writer` and `State` effects:
\begin{code}
-- | Run the unrolled `accumTree'''''''` program
-- and calculate the sum of the content values of the tree.
-- >>> runAccumTree''''''' exampleTree
-- >>> sumTree''''''' exampleTree
-- 28
sumTree''''''' :: Num a => Tree a -> a
sumTree''''''' tree =
Expand Down
46 changes: 33 additions & 13 deletions website.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,20 @@ maintainer: torsten.scholak@googlemail.com
copyright: 2021 Torsten Scholak
license: BSD-3-Clause
license-file: LICENSE
build-type: Simple
build-type: Custom

source-repository head
type: git
location: https://github.com/tscholak/website

executable website
main-is: Main.hs
other-modules:
Contact
Unrecurse
Flattening
hs-source-dirs:
app
, site
, site/posts
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
custom-setup
setup-depends:
base
, Cabal
, cabal-doctest >= 1 && <1.1

common shared-properties
default-language: Haskell2010
build-depends:
aeson
, attoparsec
Expand Down Expand Up @@ -57,4 +54,27 @@ executable website
, transformers
, unordered-containers
, vector
default-language: Haskell2010
ghc-options: -W -Wall -O2 -threaded -rtsopts -with-rtsopts=-N


executable website
import: shared-properties
main-is: Main.hs
other-modules:
Contact
, Unrecurse
, Flattening
, Paths_website
autogen-modules: Paths_website
hs-source-dirs:
app
, site
, site/posts

test-suite doctests
import: shared-properties
type: exitcode-stdio-1.0
hs-source-dirs: doctests
x-doctest-options: --no-magic
main-is: Doctests.hs
build-depends: doctest

0 comments on commit 861aa4d

Please sign in to comment.