1
1
mirror of https://github.com/sdiehl/wiwinwlh.git synced 2024-09-11 12:05:25 +03:00

Merge branch 'ci-fixes'

This commit is contained in:
sdiehl 2020-02-27 05:15:01 +00:00
commit dec92ff130
143 changed files with 1318 additions and 1274 deletions

1
.gitignore vendored
View File

@ -16,4 +16,5 @@ tutorial.tex
.stack-work
dist-newstyle
resources/*.eps
errata.*
.brokdb

View File

@ -16,6 +16,7 @@ script:
- stack $ARGS --no-terminal --install-ghc ghc includes.hs -- -o includes
- stack $ARGS --no-terminal --install-ghc exec make html
# Run the example test suite
- sudo apt-get install libblas-dev liblapack-dev
- cd src && bash ./ci
cache:
directories:

View File

@ -72,6 +72,10 @@ webpdf: $(INPUT) includes $(COVER) $(BACK)
./includes < $< \
| $(PANDOC) -c -s -f $(IFORMAT) --template $(LTEMPLATE) --include-in-header $(UNICODE_MAP) --pdf-engine=xelatex $(FLAGS) $(LFLAGS) $(PFLAGS) -o $(WPDF)
errata:
$(PANDOC) -i errata.md -o errata.tex
$(PANDOC) -i errata.md -o errata.html
links:
brok tutorial.md

View File

@ -32,16 +32,17 @@ Git: d429c2e21b9636cffa27a8d4f063644b8bcecf1a
Contributing
------------
If you want to submit a fix for a typo or fix for code then just submit a pull
request, and I'm happy to recompile the resulting document. Edit the following
Markdown file which generates all other targets.
If you want to submit a fix for a typo or fix edit the following Markdown file
which generates all other targets.
[tutorial.md](./tutorial.md)
Then submit a Pull Request on Github describing your change.
Source code is directly included from the `src` directory into the resulting
document by the preprocessor. To fix a change or include a new sample edit the
example source code directly. In addition add the new file to the `ci` script
inside of the folder so it is run in the continuous integration script.
document by the preprocessor. To include a new sample edit the example source
code directly. When adding a new Haskell file, add it to the `ci` script inside
of the folder so it is run in the continuous integration script.
Source Code
-----------

2
errata.md Normal file
View File

@ -0,0 +1,2 @@
Errata
======

View File

@ -8,6 +8,10 @@
<li><strong><a href="http://dev.stephendiehl.com/hask/tutorial.mobi">Kindle Version</a></strong></li>
</ul>
<p>Pull requests are always accepted for changes and additional content. This is a living document. The only way this document will stay up to date is through the kindness of readers like you and community patches and <a href="https://github.com/sdiehl/wiwinwlh">pull requests</a> on Github.</p>
<p>If youd like a physical copy of the text you can either print it for yourself (see Printable PDF) or purchase one online:</p>
<ul class="incremental">
<li><a href="https://www.blurb.co.uk/b/9958091-what-i-wish-i-knew-when-learning-haskell"><strong>Blurb Publisher</strong></a></li>
</ul>
<h2 id="author">Author</h2>
<p>This text is authored by Stephen Diehl.</p>
<ul class="incremental">
@ -18,6 +22,6 @@
<p>Special thanks for Erik Aker for copyediting assitance.</p>
<h2 id="license">License</h2>
<p>Copyright © 2009-2020 Stephen Diehl</p>
<p>This code included in the text is dedicated to the public domain. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.</p>
<p>This code included in the text is dedicated to the public domain. You can copy, modify, distribute and perform the code, even for commercial purposes, all without asking permission.</p>
<p>You may distribute this text in its full form freely, but may not reauthor or sublicense this work. Any reproductions of major portions of the text must include attribution.</p>
<p>The software is provided “as is”, without warranty of any kind, express or implied, including But not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, Arising from, out of or in connection with the software or the use or other dealings in the software.</p>

View File

@ -14,6 +14,11 @@ living document. The only way this document will stay up to date is through the
kindness of readers like you and community patches and [pull
requests](https://github.com/sdiehl/wiwinwlh) on Github.
If you'd like a physical copy of the text you can either print it for yourself
(see Printable PDF) or purchase one online:
* [**Blurb Publisher**](https://www.blurb.co.uk/b/9958091-what-i-wish-i-knew-when-learning-haskell)
Author
------

View File

@ -1,10 +0,0 @@
Basics
======
```bash
$ stack exec ghci bottoms.hs
$ stack exec ghci defer.hs
$ stack exec ghci fail.hs
$ stack exec ghci stacktrace.hs
$ stack exec ghci trace.hs
```

View File

@ -1,14 +0,0 @@
Monads
======
```bash
$ stack exec ghci list.hs
$ stack exec ghci maybe.hs
$ stack exec ghci maybe_impl.hs
$ stack exec ghci reader.hs
$ stack exec ghci reader_impl.hs
$ stack exec ghci state.hs
$ stack exec ghci state_impl.hs
$ stack exec ghci writer.hs
$ stack exec ghci writer_impl.hs
```

View File

@ -1,15 +0,0 @@
Monads
======
```bash
$ stach exec list.hs
$ stach exec list_impl.hs
$ stach exec maybe.hs
$ stach exec maybe_impl.hs
$ stach exec reader.hs
$ stach exec reader_impl.hs
$ stach exec state.hs
$ stach exec state_impl.hs
$ stach exec writer.hs
$ stach exec writer_impl.hs
```

View File

@ -1,13 +0,0 @@
Language Extensions
===================
```bash
$ stack exec ghci lambdacase.hs
$ stack exec ghci monomorphism.hs
$ stack exec ghci patterns.hs
$ stack exec ghci safe.hs
$ stack exec ghci views.hs
$ stack exec ghci wildcards.hs
$ stack exec ghci monomorphism.hs
$ stack exec ghci wildcards_update.hs
```

View File

@ -1,7 +0,0 @@
Laziness
======
```bash
$ stach exec ghci lazy_patterns.hs
$ stach exec ghci nodiverge.hs
```

View File

@ -1,7 +0,0 @@
Prelude
=======
```bash
$ stack exec ghci split.hs
$ stack exec ghci foldable_traversable.hs
```

View File

@ -1,5 +1,6 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive foldable_traversable.hs
stack exec ghc -- -e ":q" --interactive split.hs

View File

@ -1,15 +1,15 @@
name: example
version: 0.1
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2020 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
cabal-version: >=1.10
name: example
version: 0.1
copyright: 2020 Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
author: Stephen Diehl
category: Documentation
build-type: Simple
library
default-language: Haskell2010
build-depends:
base >= 4.6 && <4.14,
transformers >= 0.5 && <0.6,
split >= 0.2 && <0.3
library
default-language: Haskell2010
build-depends:
base >=4.6 && <4.14
, split >=0.2.3.3 && <0.3
, transformers >=0.5 && <0.6

View File

@ -1,2 +1,3 @@
resolver: lts-14.7
extra-deps: []
resolver: lts-14.20
extra-deps:
- split-0.2.3.3

View File

@ -1,11 +0,0 @@
Text/ ByteString
===
```bash
$ stack exec ghci builder.hs
$ stack exec ghci bytestring.hs
$ stack exec ghci convert.hs
$ stack exec ghci overloadedlist.hs
$ stack exec ghci printf.hs
$ stack exec ghci text.hs
```

View File

@ -1,6 +1,7 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive builder.hs
stack exec ghc -- -e ":q" --interactive bytestring.hs
#stack exec ghc -- -e ":q" --interactive overloadedlist.hs

View File

@ -1,7 +0,0 @@
Applicative Functors
====================
```bash
$ stack exec ghci applicative.hs
$ stack exec ghci variadic.hs
```

5
src/08-applicatives/ci Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -e
stack exec ghc -- -e ":q" --interactive applicative.hs
stack exec ghc -- -e ":q" --interactive variadic.hs

View File

@ -1,12 +0,0 @@
Language Extensions
===================
```bash
$ stack exec ghci either.hs
$ stack exec ghci either_impl.hs
$ stack exec ghci eithert.hs
$ stack exec ghci errors.hs
$ stack exec ghci exceptions.hs
$ stack exec ghci ioexception.hs
$ stack exec ghci spoon.hs
```

11
src/09-errors/ci Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive either.hs
#stack exec ghc -- -e ":q" --interactive either_impl.hs
stack exec ghc -- -e ":q" --interactive errors.hs
stack exec ghc -- -e ":q" --interactive exceptions.hs
stack exec ghc -- -e ":q" --interactive exceptt.hs
stack exec ghc -- -e ":q" --interactive ioexception.hs
stack exec ghc -- -e ":q" --interactive spoon.hs

View File

@ -1,22 +0,0 @@
import Control.Error
import Control.Monad.Trans
data Failure
= NonPositive Int
| ReadError String
deriving Show
main :: IO ()
main = do
putStrLn "Enter a positive number."
s <- getLine
e <- runEitherT $ do
n <- tryRead (ReadError s) s
if n > 0
then return $ n + 1
else throwT $ NonPositive n
case e of
Left n -> putStrLn $ "Failed with: " ++ show n
Right s -> putStrLn $ "Succeeded with: " ++ show s

View File

@ -1,15 +1,12 @@
import Control.Monad.Error
import Control.Monad.Except
import Control.Monad.Identity
data Exception
= Failure String
| GenericFailure
deriving Show
deriving (Show)
instance Error Exception where
noMsg = GenericFailure
type ErrMonad a = ErrorT Exception Identity a
type ErrMonad a = ExceptT Exception Identity a
example :: Int -> Int -> ErrMonad Int
example x y = do
@ -18,7 +15,7 @@ example x y = do
x -> return $ x `div` y
runFail :: ErrMonad a -> Either Exception a
runFail = runIdentity . runErrorT
runFail = runIdentity . runExceptT
example1 :: Either Exception Int
example1 = runFail $ example 2 3

View File

@ -11,8 +11,11 @@ tested-with: GHC == 7.6.3
library
build-depends:
base >= 4.6 && <4.14,
spoon >= 0.3 && <0.4,
mtl >= 2.2 && <2.3,
base >= 4.6 && <4.10
errors >= 2.3 && <2.4,
either >= 4.4 && <5.1,
transformers >= 0.5 && <0.6,
mtl >= 2.2 && <2.3
default-language: Haskell2010

View File

@ -1,32 +1,7 @@
# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-4.2
# Local packages, usually specified by relative directory name
resolver: lts-14.20
packages:
- '.'
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps: []
# Override default flag values for local packages and extra-deps
flags: {}
# Extra package databases containing global packages
extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: >= 0.1.4.0
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]

View File

@ -1,18 +0,0 @@
Advanced Monads
===============
```bash
$ stack exec ghci cont.hs
$ stack exec ghci cont_impl.hs
$ stack exec ghci free_dsl.hs
$ stack exec ghci free_impl.hs
$ stack exec ghci function.hs
$ stack exec ghci indexed.hs
$ stack exec ghci logict.hs
$ stack exec ghci mmorph.hs
$ stack exec ghci monadfix.hs
$ stack exec ghci monadplus.hs
$ stack exec ghci partiality.hs
$ stack exec ghci rws.hs
$ stack exec ghci st.hs
```

18
src/10-advanced-monads/ci Executable file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive base.hs
stack exec ghc -- -e ":q" --interactive cont.hs
#stack exec ghc -- -e ":q" --interactive cont_impl.hs
stack exec ghc -- -e ":q" --interactive free_dsl.hs
stack exec ghc -- -e ":q" --interactive free_impl.hs
stack exec ghc -- -e ":q" --interactive function.hs
#stack exec ghc -- -e ":q" --interactive indexed.hs
stack exec ghc -- -e ":q" --interactive logict.hs
stack exec ghc -- -e ":q" --interactive mmorph.hs
stack exec ghc -- -e ":q" --interactive monadfix.hs
stack exec ghc -- -e ":q" --interactive monadplus.hs
stack exec ghc -- -e ":q" --interactive partiality.hs
stack exec ghc -- -e ":q" --interactive rws.hs
stack exec ghc -- -e ":q" --interactive st.hs

View File

@ -11,12 +11,13 @@ tested-with: GHC == 7.6.3
library
build-depends:
base >= 4.6 && <4.14,
safe >= 0.3 && <0.4,
mmorph >= 1.0 && <1.1,
mmorph >= 1.0 && <1.5,
mtl >= 2.2 && <2.3,
transformers-base >= 0.4 && <0.5,
monad-control >= 1.0 && <1.1,
lifted-base >= 0.2 && <0.3,
base >= 4.6 && <4.10
free >= 5.1 && <5.2
default-language: Haskell2010

View File

@ -1,13 +1,13 @@
{-# LANGUAGE DeriveFunctor #-}
import System.Exit
import Control.Monad.Free
import System.Exit
data Interaction x
= Puts String x
| Gets (Char -> x)
| Exit
deriving Functor
deriving (Functor)
type IOFree a = Free Interaction a
@ -32,8 +32,8 @@ interp :: IOFree a -> IO a
interp (Pure r) = return r
interp (Free x) = case x of
Puts s t -> putStrLn s >> interp t
Gets f -> getChar >>= interp . f
Exit -> exitSuccess
Gets f -> getChar >>= interp . f
Exit -> exitSuccess
echo :: IOFree ()
echo = do

View File

@ -1,19 +1,34 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
import Control.Applicative
data Free f a
= Pure a
| Free (f (Free f a))
instance Functor f => Monad (Free f) where
return a = Pure a
instance Functor f => Functor (Free f) where
fmap f (Pure a) = Pure (f a)
fmap f x = go x
where
go (Free fa) = Free (go <$> fa)
instance Applicative f => Applicative (Free f) where
pure = Pure
Pure a <*> Pure b = Pure $ a b
Pure a <*> Free mb = Free $ fmap a <$> mb
Free ma <*> Pure b = Free $ fmap ($ b) <$> ma
Free ma <*> Free mb = Free $ fmap (<*>) ma <*> mb
instance Applicative f => Monad (Free f) where
return = Pure
Pure a >>= f = f a
Free f >>= g = Free (fmap (>>= g) f)
class Monad m => MonadFree f m where
class Monad m => MonadFree f m where
wrap :: f (m a) -> m a
instance Applicative f => MonadFree f (Free f) where
wrap = Free
liftF :: (Functor f, MonadFree f m) => f a -> m a
liftF = wrap . fmap return

View File

@ -1,27 +1,22 @@
import Data.STRef
import Control.Monad
import Control.Monad.ST
import Control.Monad.State.Strict
import Data.STRef
example1 :: Int
example1 = runST $ do
x <- newSTRef 0
forM_ [1..1000] $ \j -> do
forM_ [1 .. 1000] $ \j -> do
writeSTRef x j
readSTRef x
example2 :: Int
example2 = runST $ do
count <- newSTRef 0
replicateM_ (10^6) $ modifySTRef' count (+1)
replicateM_ (10 ^ 6) $ modifySTRef' count (+ 1)
readSTRef count
example3 :: Int
example3 = flip evalState 0 $ do
replicateM_ (10^6) $ modify' (+1)
replicateM_ (10 ^ 6) $ modify' (+ 1)
get
modify' :: MonadState a m => (a -> a) -> m ()
modify' f = get >>= (\x -> put $! f x)

View File

@ -1,32 +1,6 @@
# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-4.2
# Local packages, usually specified by relative directory name
resolver: lts-14.20
packages:
- '.'
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps: []
# Override default flag values for local packages and extra-deps
flags: {}
# Extra package databases containing global packages
extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: >= 0.1.4.0
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]

View File

@ -1,9 +0,0 @@
Quantification
==============
```bash
$ stack exec ghci universal.hs
$ stack exec ghci existential.hs
$ stack exec ghci impredicative.hs
$ stack exec ghci rankn.hs
```

10
src/11-quantification/ci Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive existential.hs
stack exec ghc -- -e ":q" --interactive impredicative.hs
#stack exec ghc -- -e ":q" --interactive rankn.hs
#stack exec ghc -- -e ":q" --interactive scopedtvars.hs
#stack exec ghc -- -e ":q" --interactive skolem_capture.hs
stack exec ghc -- -e ":q" --interactive universal.hs

View File

@ -11,6 +11,6 @@ tested-with: GHC == 7.6.3
library
build-depends:
base >= 4.6 && <4.10
base >= 4.6 && <4.14
default-language: Haskell2010

View File

@ -1,32 +1,6 @@
# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-4.2
# Local packages, usually specified by relative directory name
resolver: lts-14.20
packages:
- '.'
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps: []
# Override default flag values for local packages and extra-deps
flags: {}
# Extra package databases containing global packages
extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: >= 0.1.4.0
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]

View File

@ -1,3 +0,0 @@
for f in *.hs; do
echo ":quit" | stack exec ghci $f
done

View File

@ -1,11 +0,0 @@
Generalized Algebraic Datatypes
===============================
```bash
$ stack exec ghci equal.hs
$ stack exec ghci gadt.hs
$ stack exec ghci kindsignatures.hs
$ stack exec ghci phantom.hs
$ stack exec ghci phantom_example.hs
$ stack exec ghci propositional_equal.hs
```

9
src/12-gadts/ci Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -e
stack exec ghc -- -e ":q" --interactive equal.hs
stack exec ghc -- -e ":q" --interactive gadt.hs
stack exec ghc -- -e ":q" --interactive kindsignatures.hs
#stack exec ghc -- -e ":q" --interactive phantom.hs
stack exec ghc -- -e ":q" --interactive phantom_example.hs
#stack exec ghc -- -e ":q" --interactive propositional_equal.hs

View File

@ -0,0 +1,16 @@
name: example
version: 0.1
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2016 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
tested-with: GHC == 7.6.3
library
build-depends:
base >= 4.6 && <4.14
default-language: Haskell2010

View File

@ -1,7 +1,7 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeOperators #-}
-- a ≡ b
@ -24,7 +24,6 @@ trans Refl Refl = Refl
cast :: a :~: b -> a -> b
cast Refl = id
a :: forall n. n :~: n
a = Refl

6
src/12-gadts/stack.yaml Normal file
View File

@ -0,0 +1,6 @@
resolver: lts-14.20
packages:
- '.'
extra-deps: []
flags: {}
extra-package-dbs: []

View File

@ -1,10 +0,0 @@
Lambda Calculus
===============
```bash
$ stack exec bash church_encoding.hs
$ stack exec bash church_list.hs
$ stack exec bash debruijn.hs
$ stack exec bash hoas.hs
$ stack exec bash phoas.hs
```

View File

@ -100,10 +100,10 @@ ex3 :: Integer
ex3 = snd (pair 1 2)
-- 2
ex4 :: Integer
ex4 = head (tail (cons 1 (cons 2 nil)))
--ex4 :: Integer
--ex4 = head (tail (cons 1 (cons 2 nil)))
-- 2
ex5 :: Bool
ex5 = unbool (true `xor` false)
--ex5 :: Bool
--ex5 = unbool (true `xor` false)
-- True

8
src/13-lambda-calculus/ci Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e
stack exec ghc -- -e ":q" --interactive church_encoding.hs
stack exec ghc -- -e ":q" --interactive church_list.hs
stack exec ghc -- -e ":q" --interactive debruijn.hs
stack exec ghc -- -e ":q" --interactive hoas.hs
stack exec ghc -- -e ":q" --interactive phoas.hs

View File

@ -1,6 +1,7 @@
import Control.Monad
import Text.PrettyPrint
import qualified Data.Map as Map
import Text.PrettyPrint
import Prelude hiding ((<>))
-- de Bruijn indices
data DExp
@ -13,7 +14,7 @@ subst :: DExp -> Integer -> DExp -> DExp
subst e n (Var n')
| n == n' = e
| otherwise = (Var n')
subst e n (Lam e') = Lam $ subst e (n+1) e'
subst e n (Lam e') = Lam $ subst e (n + 1) e'
subst e n (App e1 e2) = App (subst e n e1) (subst e n e2)
nf :: DExp -> DExp
@ -21,21 +22,20 @@ nf e@(Var _) = e
nf (Lam e) = Lam (nf e)
nf (App f a) =
case whnf f of
Lam b -> nf (subst a 0 b)
f' -> App (nf f') (nf a)
Lam b -> nf (subst a 0 b)
f' -> App (nf f') (nf a)
whnf :: DExp -> DExp
whnf e@(Var _) = e
whnf e@(Lam _) = e
whnf (App f a) =
case whnf f of
Lam b -> whnf (subst a 0 b)
f' -> App f' a
Lam b -> whnf (subst a 0 b)
f' -> App f' a
-- Pretty printer
parensIf :: Bool -> Doc -> Doc
parensIf :: Bool -> Doc -> Doc
parensIf True = parens
parensIf False = id
@ -43,12 +43,12 @@ class Pretty p where
ppr :: Int -> p -> Doc
instance Pretty DExp where
ppr _ (Var v) = integer (v+1)
ppr p (Lam f) = parensIf (p>0) $ text "λ " <> ppr p f
ppr p (App f x) = ppr' f <+> ppr' x
where
ppr' (Var v) = integer (v+1)
ppr' expr = parens $ ppr p expr
ppr _ (Var v) = integer (v + 1)
ppr p (Lam f) = parensIf (p > 0) $ text "λ " <> ppr p f
ppr p (App f x) = ppr' f <+> ppr' x
where
ppr' (Var v) = integer (v + 1)
ppr' expr = parens $ ppr p expr
ppexpr :: DExp -> String
ppexpr = render . ppr 0
@ -63,32 +63,39 @@ data NExp
type Ctx = Map.Map String Integer
letters :: [String]
letters = [1..] >>= flip replicateM ['a'..'z']
letters = [1 ..] >>= flip replicateM ['a' .. 'z']
shift :: Ctx -> NExp -> DExp
shift c (EVar v) = Var (c Map.! v)
shift c (EApp a b) = App (shift c a) (shift c b)
shift c (ELam v body) = Lam (shift c' body)
where c' = Map.insert v 0 (Map.map (+1) c)
where
c' = Map.insert v 0 (Map.map (+ 1) c)
toDeBruijn :: NExp -> DExp
toDeBruijn = shift Map.empty
fromDeBruijn :: DExp -> NExp
fromDeBruijn = from 0
where from n (Var i) = EVar (letters !! (n - (fromIntegral i) - 1))
from n (Lam b) = ELam (letters !! n) (from (succ n) b)
from n (App f a) = EApp (from n f) (from n a)
where
from n (Var i) = EVar (letters !! (n - (fromIntegral i) - 1))
from n (Lam b) = ELam (letters !! n) (from (succ n) b)
from n (App f a) = EApp (from n f) (from n a)
i = ELam "a" (EVar "a")
k = ELam "a" (ELam "b" (EVar "a"))
s = ELam "a" (ELam "b" (ELam "c" (EApp (EApp (EVar "a") (EVar "c")) (EApp (EVar "b") (EVar "c")))))
ex1 = ppexpr $ toDeBruijn i
-- λ 1
ex2 = ppexpr $ toDeBruijn k
-- λ λ 2
ex3 = ppexpr $ toDeBruijn s
-- λ λ λ (3 1) (2 1)
ex4 = fromDeBruijn $ toDeBruijn s
-- ELam "a" (ELam "b" (ELam "c" (EApp (EApp (EVar "a") (EVar "c")) (EApp (EVar "b") (EVar "c")))))

View File

@ -0,0 +1,16 @@
name: example
version: 0.1
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2016 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
tested-with: GHC == 7.6.3
library
build-depends:
base >= 4.6 && <4.14
default-language: Haskell2010

View File

@ -0,0 +1,6 @@
resolver: lts-14.20
packages:
- '.'
extra-deps: []
flags: {}
extra-package-dbs: []

View File

@ -1,11 +0,0 @@
Interpreters
============
```bash
$ stack exec ghci catamorphism.hs
$ stack exec ghci fext.hs
$ stack exec ghci final.hs
$ stack exec ghci initial.hs
$ stack exec ghci initial_interpreter.hs
$ stack exec ghci recursion_schemes.hs
```

11
src/14-interpreters/ci Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive catamorphism.hs
stack exec ghc -- -e ":q" --interactive factorial.hs
stack exec ghc -- -e ":q" --interactive fext.hs
stack exec ghc -- -e ":q" --interactive final.hs
stack exec ghc -- -e ":q" --interactive initial.hs
stack exec ghc -- -e ":q" --interactive initial_interpreter.hs
stack exec ghc -- -e ":q" --interactive recursion_schemes.hs

View File

@ -1,13 +0,0 @@
Testing
=======
```bash
$ stack exec ghci arbitrary.hs
$ stack exec ghci criterion.hs
$ stack exec ghci qcheck.hs
$ stack exec ghci quickspec.hs
$ stack exec ghci smallcheck.hs
$ stack exec ghci smallcheck_series.hs
$ stack exec ghci smallcheck_tree.hs
$ stack exec ghci tasty.hs
```

13
src/15-testing/ci Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive arbitrary.hs
stack exec ghc -- -e ":q" --interactive criterion.hs
stack exec ghc -- -e ":q" --interactive qcheck.hs
#stack exec ghc -- -e ":q" --interactive quickspec.hs
stack exec ghc -- -e ":q" --interactive silently.hs
stack exec ghc -- -e ":q" --interactive smallcheck.hs
stack exec ghc -- -e ":q" --interactive smallcheck_series.hs
stack exec ghc -- -e ":q" --interactive smallcheck_tree.hs
stack exec ghc -- -e ":q" --interactive tasty.hs

View File

@ -1,29 +1,30 @@
import Criterion.Main
import Criterion.Config
-- Naive recursion for fibonacci numbers.
fib1 :: Int -> Int
fib1 0 = 0
fib1 1 = 1
fib1 n = fib1 (n-1) + fib1 (n-2)
fib1 n = fib1 (n -1) + fib1 (n -2)
-- Use the De Moivre closed form for fibonacci numbers.
fib2 :: Int -> Int
fib2 x = truncate $ ( 1 / sqrt 5 ) * ( phi ^ x - psi ^ x )
fib2 x = truncate $ (1 / sqrt 5) * (phi ^ x - psi ^ x)
where
phi = ( 1 + sqrt 5 ) / 2
psi = ( 1 - sqrt 5 ) / 2
phi = (1 + sqrt 5) / 2
psi = (1 - sqrt 5) / 2
suite :: [Benchmark]
suite = [
bgroup "naive" [
bench "fib 10" $ whnf fib1 5
, bench "fib 20" $ whnf fib1 10
],
bgroup "de moivre" [
bench "fib 10" $ whnf fib2 5
, bench "fib 20" $ whnf fib2 10
]
suite =
[ bgroup
"naive"
[ bench "fib 10" $ whnf fib1 5,
bench "fib 20" $ whnf fib1 10
],
bgroup
"de moivre"
[ bench "fib 10" $ whnf fib2 5,
bench "fib 20" $ whnf fib2 10
]
]
main :: IO ()

View File

@ -1,19 +1,23 @@
name: example
version: 0.1
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2016 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
tested-with: GHC == 7.6.3
name: example
version: 0.1
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2016 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
tested-with: GHC ==7.6.3
library
library
build-depends:
base >=4.6 && <4.14
, criterion >=1.2 && <1.6
, quickspec >=2.1 && <2.2
, silently >=1.2 && <1.3
, smallcheck >=1.1 && <1.2
, tasty >=1.2 && <1.3
, tasty-hunit >=0.9 && <0.11
, tasty-quickcheck >=0.10 && <0.11
, tasty-smallcheck >=0.8 && <0.9
build-depends:
tasty >= 0.11 && <0.12,
tasty-hunit >= 0.9 && <0.10,
silently >= 1.2 && <1.3,
base >= 4.6 && <4.10
default-language: Haskell2010
default-language: Haskell2010

View File

@ -1,48 +1,42 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
import Data.List
import Data.Typeable
import Test.QuickSpec hiding (lists, bools, arith)
import Test.QuickCheck
import QuickSpec hiding (arith, bools, lists)
import Test.QuickCheck.Arbitrary
type Var k a = (Typeable a, Arbitrary a, CoArbitrary a, k a)
listCons :: forall a. Var Ord a => a -> Sig
listCons a = background
[
"[]" `fun0` ([] :: [a]),
":" `fun2` ((:) :: a -> [a] -> [a])
]
listCons a =
background
[ "[]" `fun0` ([] :: [a]),
":" `fun2` ((:) :: a -> [a] -> [a])
]
lists :: forall a. Var Ord a => a -> [Sig]
lists a =
[
-- Names to print arbitrary variables
[ -- Names to print arbitrary variables
funs',
funvars',
vars',
-- Ambient definitions
listCons a,
-- Expressions to deduce properties of
"sort" `fun1` (sort :: [a] -> [a]),
"map" `fun2` (map :: (a -> a) -> [a] -> [a]),
"id" `fun1` (id :: [a] -> [a]),
"reverse" `fun1` (reverse :: [a] -> [a]),
"minimum" `fun1` (minimum :: [a] -> a),
"length" `fun1` (length :: [a] -> Int),
"++" `fun2` ((++) :: [a] -> [a] -> [a])
"sort" `fun1` (sort :: [a] -> [a]),
"map" `fun2` (map :: (a -> a) -> [a] -> [a]),
"id" `fun1` (id :: [a] -> [a]),
"reverse" `fun1` (reverse :: [a] -> [a]),
"minimum" `fun1` (minimum :: [a] -> a),
"length" `fun1` (length :: [a] -> Int),
"++" `fun2` ((++) :: [a] -> [a] -> [a])
]
where
funs' = funs (undefined :: a)
funs' = funs (undefined :: a)
funvars' = vars ["f", "g", "h"] (undefined :: a -> a)
vars' = ["xs", "ys", "zs"] `vars` (undefined :: [a])
vars' = ["xs", "ys", "zs"] `vars` (undefined :: [a])
tvar :: A
tvar = undefined

View File

@ -1,32 +1,8 @@
# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-4.2
# Local packages, usually specified by relative directory name
resolver: lts-14.20
packages:
- '.'
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps: []
# Override default flag values for local packages and extra-deps
extra-deps:
- quickspec-2.1.2
- twee-lib-2.1.5
flags: {}
# Extra package databases containing global packages
extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: >= 0.1.4.0
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]

View File

@ -1,18 +0,0 @@
Type Families
=============
```bash
$ stack exec ghci constraintkinds.hs
$ stack exec ghci datafamily.hs
$ stack exec ghci dict.hs
$ stack exec ghci family_nat_operators.hs
$ stack exec ghci family_type.hs
$ stack exec ghci fundeps.hs
$ stack exec ghci mono.hs
$ stack exec ghci mparam.hs
$ stack exec ghci mparam_fun.hs
$ stack exec ghci noempty.hs
$ stack exec ghci proof.hs
$ stack exec ghci role_infer.hs
$ stack exec ghci roles.hs
```

16
src/16-type-families/ci Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive constraintkinds.hs
stack exec ghc -- -e ":q" --interactive datafamily.hs
stack exec ghc -- -e ":q" --interactive dict.hs
stack exec ghc -- -e ":q" --interactive family_nat_operators.hs
stack exec ghc -- -e ":q" --interactive family_type.hs
stack exec ghc -- -e ":q" --interactive fundeps.hs
stack exec ghc -- -e ":q" --interactive mparam.hs
stack exec ghc -- -e ":q" --interactive mparam_fun.hs
stack exec ghc -- -e ":q" --interactive noempty.hs
stack exec ghc -- -e ":q" --interactive proof.hs
stack exec ghc -- -e ":q" --interactive role_infer.hs
#stack exec ghc -- -e ":q" --interactive roles.hs

View File

@ -1,12 +1,15 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
import GHC.Exts (Constraint)
import Data.Hashable
import Data.HashSet
import Data.Hashable
import GHC.Exts (Constraint)
type family Con a :: Constraint
type instance Con [a] = (Ord a, Eq a)
type instance Con (HashSet a) = (Hashable a)
class Sized a where

View File

@ -0,0 +1,16 @@
cabal-version: >=1.10
name: example
version: 0.1
copyright: 2020 Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
author: Stephen Diehl
category: Documentation
build-type: Simple
library
default-language: Haskell2010
build-depends:
base >=4.6 && <4.14
, hashable >=1.2.0.0 && <1.4
, unordered-containers >=0.2.10.0 && <0.3
, vector >=0.11.0.0 && <0.13

View File

@ -1,63 +0,0 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Text
import Data.Char
import Data.Monoid
import Data.MonoTraversable
import Control.Applicative
bs :: Text
bs = "Hello Haskell."
shift :: Text
shift = omap (chr . (+1) . ord) bs
-- "Ifmmp!Ibtlfmm/"
backwards :: [Char]
backwards = ofoldl' (flip (:)) "" bs
-- ".lleksaH olleH"
data MyMonoType = MNil | MCons Int MyMonoType deriving Show
type instance Element MyMonoType = Int
instance MonoFunctor MyMonoType where
omap f MNil = MNil
omap f (MCons x xs) = f x `MCons` omap f xs
instance MonoFoldable MyMonoType where
ofoldMap f = ofoldr (mappend . f) mempty
ofoldr = mfoldr
ofoldl' = mfoldl'
ofoldr1Ex f = ofoldr1Ex f . mtoList
ofoldl1Ex' f = ofoldl1Ex' f . mtoList
instance MonoTraversable MyMonoType where
omapM f xs = mapM f (mtoList xs) >>= return . mfromList
otraverse f = ofoldr acons (pure MNil)
where acons x ys = MCons <$> f x <*> ys
mtoList :: MyMonoType -> [Int]
mtoList (MNil) = []
mtoList (MCons x xs) = x : (mtoList xs)
mfromList :: [Int] -> MyMonoType
mfromList [] = MNil
mfromList (x:xs) = MCons x (mfromList xs)
mfoldr :: (Int -> a -> a) -> a -> MyMonoType -> a
mfoldr f z MNil = z
mfoldr f z (MCons x xs) = f x (mfoldr f z xs)
mfoldl' :: (a -> Int -> a) -> a -> MyMonoType -> a
mfoldl' f z MNil = z
mfoldl' f z (MCons x xs) = let z' = z `f` x
in seq z' $ mfoldl' f z' xs
ex1 :: Int
ex1 = mfoldl' (+) 0 (mfromList [1..25])
ex2 :: MyMonoType
ex2 = omap (+1) (mfromList [1..25])

View File

@ -1,11 +1,13 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
newtype Age = MkAge { unAge :: Int }
newtype Age = MkAge {unAge :: Int}
type family Inspect x
type instance Inspect Age = Int
type instance Inspect Int = Bool
class Boom a where

View File

@ -0,0 +1,6 @@
resolver: lts-14.20
packages:
- '.'
extra-deps: []
flags: {}
extra-package-dbs: []

View File

@ -1,25 +0,0 @@
Promotion
=========
```bash
$ stack exec ghci closed_typefamily.hs
$ stack exec ghci constraint_list.hs
$ stack exec ghci countargs.hs
$ stack exec ghci datakinds.hs
$ stack exec ghci errors.hs
$ stack exec ghci errors_dsl.hs
$ stack exec ghci hasfield.hs
$ stack exec ghci hlist.hs
$ stack exec ghci kindpoly.hs
$ stack exec ghci nonempty.hs
$ stack exec ghci reverse.hs
$ stack exec ghci reverse_nat.hs
$ stack exec ghci singleton.hs
$ stack exec ghci singleton_class.hs
$ stack exec ghci type_equality.hs
$ stack exec ghci typefamily.hs
$ stack exec ghci typelevel_fields.hs
$ stack exec ghci typemap.hs
$ stack exec ghci typenat.hs
$ stack exec ghci typenat_cmp.hs
```

25
src/17-promotion/ci Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive closed_typefamily.hs
stack exec ghc -- -e ":q" --interactive constraint_list.hs
stack exec ghc -- -e ":q" --interactive countargs.hs
stack exec ghc -- -e ":q" --interactive datakinds.hs
#stack exec ghc -- -e ":q" --interactive errors.hs
#stack exec ghc -- -e ":q" --interactive errors_dsl.hs
stack exec ghc -- -e ":q" --interactive hasfield.hs
stack exec ghc -- -e ":q" --interactive hlist.hs
stack exec ghc -- -e ":q" --interactive kindpoly.hs
#stack exec ghc -- -e ":q" --interactive nonempty.hs
stack exec ghc -- -e ":q" --interactive reverse.hs
stack exec ghc -- -e ":q" --interactive reverse_nat.hs
stack exec ghc -- -e ":q" --interactive singleton.hs
stack exec ghc -- -e ":q" --interactive singleton_class.hs
stack exec ghc -- -e ":q" --interactive type_equality.hs
stack exec ghc -- -e ":q" --interactive typefamily.hs
stack exec ghc -- -e ":q" --interactive typelevel_fields.hs
stack exec ghc -- -e ":q" --interactive typelevel_strings.hs
stack exec ghc -- -e ":q" --interactive typemap.hs
stack exec ghc -- -e ":q" --interactive typenat.hs
stack exec ghc -- -e ":q" --interactive typenat_cmp.hs

View File

@ -4,14 +4,16 @@
import GHC.TypeLits
instance
-- Error Message
TypeError (Text "Equality is not defined for functions"
:$$:
(ShowType a :<>: Text " -> " :<>: ShowType b))
instance-- Error Message
TypeError
( Text "Equality is not defined for functions"
:$$: (ShowType a :<>: Text " -> " :<>: ShowType b)
) =>
-- Instance head
=> Eq (a -> b) where (==) = undefined
Eq (a -> b)
where
(==) = undefined
-- Fail when we try to equate two functions
example = id == id

View File

@ -0,0 +1,13 @@
name: example
version: 0.1
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2020 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
library
default-language: Haskell2010
build-depends:
base >= 4.6 && <4.14

View File

@ -1,13 +1,13 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
data Size = Empty | NonEmpty
data List a b where
Nil :: List Empty a
Nil :: List Empty a
Cons :: a -> List b a -> List NonEmpty a
head' :: List NonEmpty a -> a

View File

@ -0,0 +1,6 @@
resolver: lts-14.20
packages:
- '.'
extra-deps: []
flags: {}
extra-package-dbs: []

View File

@ -1,15 +0,0 @@
Generics
========
```bash
$ stack exec ghci biplate.hs
$ stack exec ghci cereal.hs
$ stack exec ghci data.hs
$ stack exec ghci derive_aeson.hs
$ stack exec ghci dynamic.hs
$ stack exec ghci generic_impl.hs
$ stack exec ghci generics.hs
$ stack exec ghci hashable.hs
$ stack exec ghci typeable.hs
$ stack exec ghci uniplate.hs
```

14
src/18-generics/ci Executable file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive biplate.hs
stack exec ghc -- -e ":q" --interactive cereal.hs
stack exec ghc -- -e ":q" --interactive data.hs
stack exec ghc -- -e ":q" --interactive derive_aeson.hs
stack exec ghc -- -e ":q" --interactive dynamic.hs
stack exec ghc -- -e ":q" --interactive generic_impl.hs
stack exec ghc -- -e ":q" --interactive generics.hs
stack exec ghc -- -e ":q" --interactive hashable.hs
stack exec ghc -- -e ":q" --interactive typeable.hs
stack exec ghc -- -e ":q" --interactive uniplate.hs

View File

@ -1,17 +1,16 @@
name: example
version: 0.1
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2016 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
tested-with: GHC == 7.6.3
cabal-version: >=1.10
name: example
version: 0.1
copyright: 2020 Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
author: Stephen Diehl
category: Documentation
build-type: Simple
library
build-depends:
syb >= 0.6 && <0.7,
base >= 4.6 && <4.10
default-language: Haskell2010
library
default-language: Haskell2010
build-depends:
aeson
, base >=4.6 && <4.14
, cereal >=0.5.8.1 && <0.6
, uniplate >=1.6.12 && <1.7

View File

@ -1,5 +1,6 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
import GHC.Generics
@ -8,7 +9,12 @@ data Animal
| Cat
instance Generic Animal where
type Rep Animal = D1 T_Animal ((C1 C_Dog U1) :+: (C1 C_Cat U1))
type
Rep Animal =
D1 ( 'MetaData "Animal" "Main" "main" 'False )
( C1 ( 'MetaCons "Dog" 'PrefixI 'False)
U1 :+: C1 ( 'MetaCons "Cat" 'PrefixI 'False) U1
)
from Dog = M1 (L1 (M1 U1))
from Cat = M1 (R1 (M1 U1))
@ -16,13 +22,14 @@ instance Generic Animal where
to (M1 (L1 (M1 U1))) = Dog
to (M1 (R1 (M1 U1))) = Cat
data T_Animal
data C_Dog
data C_Cat
data T_Animal -- Animal type
data C_Dog -- Dog Constructor
data C_Cat -- Cat Constructor
instance Datatype T_Animal where
datatypeName _ = "Animal"
moduleName _ = "Main"
packageName _ = "main"
instance Constructor C_Dog where
conName _ = "Dog"

View File

@ -1,32 +1,6 @@
# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md
# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
resolver: lts-4.2
# Local packages, usually specified by relative directory name
resolver: lts-14.20
packages:
- '.'
# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
extra-deps: []
# Override default flag values for local packages and extra-deps
flags: {}
# Extra package databases containing global packages
extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: >= 0.1.4.0
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]

View File

@ -1,29 +0,0 @@
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Data
import Data.Typeable
import Data.Generics.Schemes
import Data.Generics.Aliases (mkT, extT)
data MyTuple a = MyTuple a Float
deriving (Data, Typeable, Show)
exampleT :: Data a => MyTuple a -> MyTuple a
exampleT = everywhere (mkT go1 `extT` go2)
where
go1 :: Int -> Int
go1 x = succ x
go2 :: Float -> Float
go2 x = succ x
findFloat :: Data x => x -> Maybe Float
findFloat = gfindtype
main :: IO ()
main = do
let term = MyTuple (MyTuple (1 :: Int) 2.0) 3.0
print (exampleT term)
print (gsize term)
print (findFloat term)
print (listify ((>0) :: (Int -> Bool)) term)

View File

@ -1,9 +0,0 @@
Numbers
=======
```bash
$ stack exec ghci creal.hs
$ stack exec ghci puzzle.hs
$ stack exec ghci scientific.hs
$ stack exec ghci stats.hs
```

10
src/19-numbers/ci Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive arithmoi.hs
stack exec ghc -- -e ":q" --interactive creal.hs
stack exec ghc -- -e ":q" --interactive polynomial.hs
stack exec ghc -- -e ":q" --interactive puzzle.hs
stack exec ghc -- -e ":q" --interactive scientific.hs
stack exec ghc -- -e ":q" --interactive stats.hs

View File

@ -1,21 +1,19 @@
name: example
version: 0.1
version: 0.1.0.0
author: Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
copyright: 2016 Stephen Diehl
category: Documentation
build-type: Simple
cabal-version: >=1.10
tested-with: GHC == 7.6.3
library
default-language: Haskell2010
build-depends:
base >= 4.10 && <4.14,
poly >= 0.3 && <0.4,
scientific >= 0.3 && <0.4,
sbv >= 8.5 && <8.6,
poly >= 0.3 && <0.4,
scientific >= 0.3 && <0.4,
sbv >= 8.5 && <8.6,
statistics >= 0.15 && <0.16,
arithmoi >= 0.9 && <0.11
default-language: Haskell2010
arithmoi >= 0.9 && <0.11,
numbers -any

View File

@ -1,17 +1,18 @@
import Data.Scientific
c, h, g, a, k :: Scientific
c = scientific 299792458 (0) -- Speed of light
c , h, g, a, k :: Scientific
c = scientific 299792458 (0) -- Speed of light
h = scientific 662606957 (-42) -- Planck's constant
g = scientific 667384 (-16) -- Gravitational constant
g = scientific 667384 (-16) -- Gravitational constant
a = scientific 729735257 (-11) -- Fine structure constant
k = scientific 268545200 (-9) -- Khinchin Constant
k = scientific 268545200 (-9) -- Khinchin-Levy Constant
tau :: Scientific
tau = fromFloatDigits (2*pi)
tau = fromFloatDigits (2 * pi)
maxDouble64 :: Double
maxDouble64 = read "1.7976931348623159e308"
-- Infinity
maxScientific :: Scientific

View File

@ -1,17 +0,0 @@
Data Structures
===============
```bash
$ stack exec ghci dlist.hs
$ stack exec ghci graph.hs
$ stack exec ghci hashtables.hs
$ stack exec ghci hblas.hs
$ stack exec ghci map.hs
$ stack exec ghci matrix_index.hs
$ stack exec ghci sequence.hs
$ stack exec ghci set.hs
$ stack exec ghci tree.hs
$ stack exec ghci unordered.hs
$ stack exec ghci vector.hs
$ stack exec ghci vector_mutable.hs
```

15
src/20-data-structures/ci Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive dlist.hs
stack exec ghc -- -e ":q" --interactive graph.hs
stack exec ghc -- -e ":q" --interactive hashtables.hs
#stack exec ghc -- -e ":q" --interactive hblas.hs
stack exec ghc -- -e ":q" --interactive map.hs
stack exec ghc -- -e ":q" --interactive sequence.hs
stack exec ghc -- -e ":q" --interactive set.hs
stack exec ghc -- -e ":q" --interactive tree.hs
stack exec ghc -- -e ":q" --interactive unordered.hs
stack exec ghc -- -e ":q" --interactive vector.hs
stack exec ghc -- -e ":q" --interactive vector_mutable.hs

View File

@ -0,0 +1,19 @@
cabal-version: >=1.10
name: example
version: 0.1.0.0
copyright: 2016 Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
author: Stephen Diehl
category: Documentation
build-type: Simple
library
default-language: Haskell2010
build-depends:
base >=4.10 && <4.14
, containers >=0.6.0.1 && <0.7
, dlist >=0.8.0.7 && <0.9
, hashtables >= 1.2 && <1.3
, hblas >= 0.4 && <0.5
, hmatrix >= 0.20 && <0.21
, unordered-containers >=0.2.10.0 && <0.3

View File

@ -4,15 +4,13 @@ import Numerical.HBLAS.MatrixTypes
-- Generate the constant mutable square matrix of the given type and dimensions.
constMatrix :: Storable a => Int -> a -> IO (IODenseMatrix Row a)
constMatrix n k = generateMutableDenseMatrix SRow (n,n) (const k)
constMatrix n k = generateMutableDenseMatrix SRow (n, n) (const k)
example_dgemm :: IO ()
example_dgemm = do
left <- constMatrix 2 (2 :: Double)
right <- constMatrix 2 (3 :: Double)
out <- constMatrix 2 (0 :: Double)
dgemm NoTranspose NoTranspose 1.0 1.0 left right out
resulting <- mutableVectorToList $ _bufferDenMutMat out
print resulting
left <- constMatrix 2 (2 :: Double)
right <- constMatrix 2 (3 :: Double)
out <- constMatrix 2 (0 :: Double)
dgemm NoTranspose NoTranspose 1.0 1.0 left right out
resulting <- mutableVectorToList $ _bufferDenMutMat out
print resulting

View File

@ -1,52 +0,0 @@
import qualified Data.Vector as V
data Order = RowMajor | ColMajor
rowMajor :: [Int] -> [Int]
rowMajor = scanr (*) 1 . tail
colMajor :: [Int] -> [Int]
colMajor = init . scanl (*) 1
data Matrix a = Matrix
{ _dims :: [Int]
, _elts :: V.Vector a
, _order :: Order
}
fromList :: [Int] -> Order -> [a] -> Matrix a
fromList sh order elts =
if product sh == length elts
then Matrix sh (V.fromList elts) order
else error "dimensions don't match"
indexTo :: [Int] -> Matrix a -> a
indexTo ix mat = boundsCheck offset
where
boundsCheck n =
if 0 <= n && n < V.length (_elts mat)
then V.unsafeIndex (_elts mat) offset
else error "out of bounds"
ordering = case _order mat of
RowMajor -> rowMajor
ColMajor -> colMajor
offset = sum $ zipWith (*) ix (ordering (_dims mat))
matrix :: Order -> Matrix Int
matrix order = fromList [4,4] order [1..16]
ex1 :: [Int]
ex1 = rowMajor [1,2,3,4]
-- [24,12,4,1]
ex2 :: [Int]
ex2 = colMajor [1,2,3,4]
-- [1,1,2,6]
ex3 :: Int
ex3 = indexTo [1,3] (matrix RowMajor)
-- 8
ex4 :: Int
ex4 = indexTo [1,3] (matrix ColMajor)
-- 14

View File

@ -0,0 +1,3 @@
resolver: lts-14.20
extra-deps:
- hblas-0.4.0.1

View File

@ -1,10 +0,0 @@
Foreign Function Interface
==========================
XXX: include gcc invocation
```
$ stack exec ghci ffi.hs
$ stack exec ghci pointer_use.hs
$ stack exec ghci simple_ffi.hs
```

6
src/21-ffi/ci Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive ffi.hs
stack exec ghc -- -e ":q" --interactive simple_ffi.hs

13
src/21-ffi/example.cabal Normal file
View File

@ -0,0 +1,13 @@
cabal-version: >=1.10
name: example
version: 0.1.0.0
copyright: 2020 Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
author: Stephen Diehl
category: Documentation
build-type: Simple
library
default-language: Haskell2010
build-depends:
base >=4.10 && <4.14

2
src/21-ffi/stack.yaml Normal file
View File

@ -0,0 +1,2 @@
resolver: lts-14.20
extra-deps: []

View File

@ -1,12 +0,0 @@
Concurrency
===========
```bash
$ stack exec ghci async.hs
$ stack exec ghci par.hs
$ stack exec ghci spark.hs
$ stack exec ghci sparks.hs
$ stack exec ghci stm.hs
$ stack exec ghci strategies.hs
$ stack exec ghci strategies_param.hs
```

11
src/22-concurrency/ci Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive async.hs
stack exec ghc -- -e ":q" --interactive par.hs
stack exec ghc -- -e ":q" --interactive spark.hs
stack exec ghc -- -e ":q" --interactive sparks.hs
stack exec ghc -- -e ":q" --interactive stm.hs
stack exec ghc -- -e ":q" --interactive strategies.hs
stack exec ghc -- -e ":q" --interactive strategies_param.hs

View File

@ -0,0 +1,15 @@
cabal-version: >=1.10
name: example
version: 0.1.0.0
copyright: 2020 Stephen Diehl
maintainer: stephen.m.diehl@gmail.com
author: Stephen Diehl
category: Documentation
build-type: Simple
library
default-language: Haskell2010
build-depends:
async >=2.2.2 && <2.3
, base >=4.10 && <4.14
, monad-par >=0.3 && <0.4

View File

@ -1,7 +1,9 @@
{-# LANGUAGE NoMonadFailDesugaring #-}
import Control.Monad
import Control.Monad.Par
f, g :: Int -> Int
f , g :: Int -> Int
f x = x + 10
g x = x * 10
@ -15,7 +17,7 @@ g x = x * 10
example1 :: Int -> (Int, Int)
example1 x = runPar $ do
[a,b,c,d,e] <- replicateM 5 new
[a, b, c, d, e] <- replicateM 5 new
fork (put a (f x))
fork (put b (g x))
a' <- get a
@ -30,13 +32,13 @@ example1 x = runPar $ do
example2 :: [Int]
example2 = runPar $ do
xs <- parMap (+1) [1..25]
xs <- parMap (+ 1) [1 .. 25]
return xs
-- foldr (+) 0 (map (^2) [1..xs])
example3 :: Int -> Int
example3 n = runPar $ do
let range = (InclusiveRange 1 n)
let mapper x = return (x^2)
let reducer x y = return (x+y)
let mapper x = return (x ^ 2)
let reducer x y = return (x + y)
parMapReduceRangeThresh 10 range mapper reducer 0

View File

@ -0,0 +1,2 @@
resolver: lts-14.20
extra-deps: []

View File

@ -1,6 +0,0 @@
Graphics
========
```bash
$ stack exec ghci diagrams.hs
```

View File

@ -1,15 +0,0 @@
Parsing
=======
```bash
$ stack exec ghci attoparsec.hs
$ stack exec ghci attoparsec_lang.hs
$ stack exec ghci generics.hs
$ stack exec ghci lexer_text.hs
$ stack exec ghci optparse_applicative.hs
$ stack exec ghci parsec_applicative.hs
$ stack exec ghci parsec_operators.hs
$ stack exec ghci parser.hs
$ stack exec ghci simple_parser.hs
$ stack exec ghci configurator.hs
```

View File

@ -1,6 +1,7 @@
#!/usr/bin/env bash
set -e
stack build
stack exec ghc -- -e ":q" --interactive attoparsec.hs
stack exec ghc -- -e ":q" --interactive attoparsec_lang.hs
stack exec ghc -- -e ":q" --interactive configurator.hs

View File

@ -1,3 +0,0 @@
for f in *.hs; do
echo ":quit" | stack exec ghci $f
done

View File

@ -1,11 +0,0 @@
Streaming
=========
```bash
$ stack exec ghci conduit.hs
$ stack exec ghci lazyio.hs
$ stack exec ghci pipes.hs
$ stack exec ghci pipes_file.hs
$ stack exec ghci pipes_io.hs
$ stack exec ghci pipes_safe.hs
```

Some files were not shown because too many files have changed in this diff Show More