Skip to content

Commit eddc0a2

Browse files
committed
[haskell_edsl] Add examples
1 parent e4812d5 commit eddc0a2

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

haskell_edsl/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,15 @@ The tests can be run with:
4040
```
4141
cabal test
4242
```
43+
44+
## Building the examples and other checker programs
45+
46+
Install the library globally on your system with:
47+
```
48+
cabal install
49+
```
50+
51+
Then a checker program can be build with:
52+
```
53+
ghc --make ProgramName.hs
54+
```

haskell_edsl/examples/ArrangingHat.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- NWERC 2016 - Arranging Hat
2+
-- See http://2016.nwerc.eu
3+
4+
module Main where
5+
6+
import Control.Monad
7+
import Checktestdata
8+
9+
main :: IO ()
10+
main = ctdMain $ do
11+
n <- int 1 40
12+
space
13+
m <- int 1 400
14+
newline
15+
replicateM_ (fromIntegral n) $ do
16+
replicateM_ (fromIntegral m) $ regex "[0-9]"
17+
newline
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- NWERC 2016 - Careful Ascent
2+
-- See http://2016.nwerc.eu
3+
4+
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
5+
module Main where
6+
7+
import Control.Monad
8+
import Checktestdata
9+
10+
main :: IO ()
11+
main = ctdMain $ do
12+
x <- int (-(10^7)) (10^7)
13+
space
14+
y <- int (max 1 (abs x)) (10^8)
15+
newline
16+
17+
n <- int 0 100
18+
newline
19+
20+
shields <- forM [1..n] $ \i -> do
21+
li <- int 0 y
22+
space
23+
ui <- int (li+1) y
24+
space
25+
fi <- floatP 0.1 10.0 0 10
26+
newline
27+
return (i,li,ui,fi)
28+
29+
-- Check for each pair that they don't intersect
30+
assert $ and $
31+
[ i == j || ui <= lj || uj <= li
32+
| (i,li,ui,_) <- shields
33+
, (j,lj,uj,_) <- shields
34+
]

haskell_edsl/examples/FreeWeights.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- NWERC 2016 - Free Weights
2+
-- See http://2016.nwerc.eu
3+
4+
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
5+
module Main where
6+
7+
import Control.Monad
8+
import Checktestdata
9+
import Data.List
10+
11+
main :: IO ()
12+
main = ctdMain $ do
13+
n <- int 1 (10^6)
14+
newline
15+
16+
row1 <- repSep n space $ int 1 (10^9)
17+
newline
18+
row2 <- repSep n space $ int 1 (10^9)
19+
newline
20+
21+
-- Check that each number appears exactly twice.
22+
-- Numbers at odd positions should be equal to
23+
-- the next number in the list, and numbers at
24+
-- even positions should be smaller than the next.
25+
let ws = sort $ row1 ++ row2
26+
let check (i,v,w) | odd i = v == w
27+
| otherwise = v < w
28+
assert $ all check $ zip3 [1..] ws (tail ws)
29+
30+
-- | Replicate the given script n times, separated by
31+
-- something else.
32+
repSep :: Integer -> CTD () -> CTD a -> CTD [a]
33+
repSep n sep a = forM [1..n] $ \i -> when (i > 1) sep >> a

haskell_edsl/examples/Interception.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- NCPC 2016 - Interception
2+
-- See https://ncpc.idi.ntnu.no/ncpc2016/
3+
4+
import Checktestdata
5+
import Control.Monad
6+
7+
main :: IO ()
8+
main = ctdMain $ do
9+
n <- int 4 250000
10+
space
11+
m <- int 1 500000
12+
space
13+
c1 <- int 1 (n-1)
14+
space
15+
c2 <- int 1 (n-1)
16+
newline
17+
18+
assert $ n `mod` 2 == 0
19+
assert $ c1 `mod` 2 == 1
20+
assert $ c2 `mod` 2 == 1
21+
assert $ c1 < c2
22+
23+
ab <- replicateM (fromInteger m) $ do
24+
ai <- int 1 n
25+
space
26+
bi <- int 1 n
27+
assert $ ai /= bi
28+
if (ai `mod` 2 /= bi `mod` 2) then do
29+
space
30+
c <- int 1 n
31+
newline
32+
assert $ c == c1 || c == c2
33+
else newline
34+
return $ if ai > bi then (bi, ai) else (ai, bi)
35+
36+
assert $ unique ab

0 commit comments

Comments
 (0)