Skip to content

Commit e0b4233

Browse files
authored
Merge pull request #168 from haskell-jp/strict-gotchas
Strict拡張を使用する際の注意点
2 parents 59538e6 + 40e54c2 commit e0b4233

File tree

9 files changed

+523
-0
lines changed

9 files changed

+523
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cabal-dev
55
*.chi
66
*.chs.h
77
.virtualenv
8+
*.exe
89
.hsenv
910
.cabal-sandbox/
1011
cabal.sandbox.config
@@ -16,3 +17,5 @@ cabal.config
1617

1718
# stack
1819
.stack-work/
20+
21+
*~
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# haskell-jp-blog-example-strict-gotchas
2+
3+
[こちらのIssue](https://github.com/haskell-jp/blog/issues/167)について、せっかく盛り上がってきたので少しでも進めようと、まずはサンプルを書いてみることにしました。
4+
5+
## 注記
6+
7+
- 記事の内容と大きく連動しそうなので、敢えて同じリポジトリーに上げています。
8+
-`*.hs ` ファイルを `runghc --ghc-arg=-XStrict` で実行した場合と `--ghc-arg=-XStrict` なしで実行した場合とで比較することで、挙動の違いを体験できるように作っています。

examples/2020/strict-gotchas/const.hs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dontReferArgs :: a -> b -> a
2+
dontReferArgs = const
3+
4+
referArgs :: a -> b -> a
5+
referArgs x _ = x
6+
7+
main :: IO ()
8+
main = do
9+
print $ dontReferArgs "dontReferArgs" (undefined :: Int)
10+
print $ referArgs "referArgs" (undefined :: Int)
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This file was automatically generated by 'stack init'
2+
#
3+
# Some commonly used options have been documented as comments in this file.
4+
# For advanced use and comprehensive documentation of the format, please see:
5+
# https://docs.haskellstack.org/en/stable/yaml_configuration/
6+
7+
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8+
# A snapshot resolver dictates the compiler version and the set of packages
9+
# to be used for project dependencies. For example:
10+
#
11+
# resolver: lts-3.5
12+
# resolver: nightly-2015-09-21
13+
# resolver: ghc-7.10.2
14+
#
15+
# The location of a snapshot can be provided as a file or url. Stack assumes
16+
# a snapshot provided as a file might change, whereas a url resource does not.
17+
#
18+
# resolver: ./custom-snapshot.yaml
19+
# resolver: https://example.com/snapshots/2018-01-01.yaml
20+
resolver: ghc-8.10.1
21+
22+
# User packages to be built.
23+
# Various formats can be used as shown in the example below.
24+
#
25+
# packages:
26+
# - some-directory
27+
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
28+
# subdirs:
29+
# - auto-update
30+
# - wai
31+
packages: []
32+
# Dependency packages to be pulled from upstream that are not in the resolver.
33+
# These entries can reference officially published versions as well as
34+
# forks / in-progress versions pinned to a git hash. For example:
35+
#
36+
# extra-deps:
37+
# - acme-missiles-0.3
38+
# - git: https://github.com/commercialhaskell/stack.git
39+
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
40+
#
41+
# extra-deps: []
42+
43+
# Override default flag values for local packages and extra-deps
44+
# flags: {}
45+
46+
# Extra package databases containing global packages
47+
# extra-package-dbs: []
48+
49+
# Control whether we use the GHC we find on the path
50+
# system-ghc: true
51+
#
52+
# Require a specific version of stack, using version ranges
53+
# require-stack-version: -any # Default
54+
# require-stack-version: ">=2.1"
55+
#
56+
# Override the architecture used by stack, especially useful on Windows
57+
# arch: i386
58+
# arch: x86_64
59+
#
60+
# Extra directories used by stack for building
61+
# extra-include-dirs: [/path/to/dir]
62+
# extra-lib-dirs: [/path/to/dir]
63+
#
64+
# Allow a newer minor version of GHC than the snapshot specifies
65+
# compiler-check: newer-minor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file was autogenerated by Stack.
2+
# You should not edit this file by hand.
3+
# For more information, please see the documentation at:
4+
# https://docs.haskellstack.org/en/stable/lock_files
5+
6+
packages: []
7+
snapshots: []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-
2+
GHCRTS=-K100k stack exec runghc -- ./stackoverflow-foldr.hs
3+
GHCRTS=-K100k stack exec runghc -- --ghc-arg=-XStrict ./stackoverflow-foldr.hs
4+
-}
5+
6+
import Control.Exception
7+
import Data.List
8+
9+
main :: IO ()
10+
main = do
11+
let size = 5000
12+
13+
evaluate . length $ foldr (:) [] [1 .. size]
14+
putStrLn "DONE: foldr 1"
15+
16+
evaluate . length $ foldr (\x z -> x : z) [] [1 .. size]
17+
putStrLn "DONE: foldr 2"
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
2+
3+
import Foreign.Marshal.Alloc
4+
import Foreign.Ptr
5+
import Foreign.Storable
6+
7+
data Test = Test Int Int deriving Show
8+
9+
instance Storable Test where
10+
sizeOf _ = sizeOf (1 :: Int) * 2
11+
alignment _ = 8
12+
peek = error "This should not be called in this program"
13+
poke = error "This should not be called in this program"
14+
15+
main :: IO ()
16+
main = alloca $ \(_ :: Ptr Test) -> putStrLn "This won't be printed when Strict is enabled"

examples/2020/strict-gotchas/where.hs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Originally created by @pxfnc
2+
-- https://qiita.com/pxfnc/items/a26bda6d11402daba675
3+
4+
-- Compare
5+
-- runghc where.hs
6+
-- And
7+
-- runghc --ghc-arg=-XStrict where.hs
8+
9+
10+
main :: IO ()
11+
main = print $ div10 0
12+
13+
div10 :: Int -> Int
14+
div10 n
15+
| n == 0 = 0
16+
| otherwise = result
17+
where
18+
result = 10 `div` n

0 commit comments

Comments
 (0)