-
Notifications
You must be signed in to change notification settings - Fork 0
/
vscode-haskell-cabal.sh
329 lines (255 loc) · 7.5 KB
/
vscode-haskell-cabal.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env bash
# example:
# ➜ ./vscode-haskell-cabal.sh "your-project" "your name" "[email protected]"
function main {
cat > $project.code-workspace <<EOF
{
"folders": [
{
"path": "$project"
}
],
"settings": {}
}
EOF
mkdir $project;
chmod 777 $project;
cd $project;
### us it to build initial files and dirs, replace it later
cabal init --non-interactive \
--license MIT --is-executable --package-name "$project" \
--author="$author" --email="$email" \
--language=GHC2021 --synopsis "My Project" \
--version=0.1.0.0 --cabal-version=3.0 \
--libandexe --no-comments
rm app/Main.hs
rm src/MyLib.hs
rm CHANGELOG.md
mkdir test
cat > CHANGELOG.md <<EOF
# Revision history for: $project
## 0.1.0.0 -- $(date +"%Y-%m-%d")
* First version. Released on an unsuspecting world.
EOF
cat > README.md <<EOF
# README for: $project
## V. 0.1.0.0 -- $(date +"%Y-%m-%d")
#### Useful Cabal commands
* \`cabal clean\`
* \`cabal build\`
* \`cabal test\`
* \`cabal run\`
* \`cabal repl $project\`
#### During Development
* \`cabal repl\`<br>
_A) tests_
\`\`\`
ghci> :l test/TestSuite
ghci> main
\`\`\`
_B) app_
\`\`\`
ghci> :l app/Main
ghci> main
\`\`\`
_C) src_
\`\`\`
ghci> :l src/Doodles
ghci> life'sMeaning
\`\`\`
EOF
cat > app/Main.hs <<EOF
module
Main
where
import qualified Lib (sayHi)
main :: IO ()
main = do
putStrLn "Hello Haskell!"
Lib.sayHi
EOF
cat > src/Lib.hs <<EOF
module
Lib (Option (..), sayHi)
where
sayHi :: IO ()
sayHi = putStrLn "Hi from Lib!"
data Option m =
None
| Some m
deriving (Eq, Show)
-- class constraint on 'm' indicates that it also must be a Semigroup, e.g. it understands (<>)
instance Semigroup m => Semigroup (Option m)
where
(<>) :: Semigroup m => Option m -> Option m -> Option m
(<>) None m = m
(<>) m None = m
(<>) (Some m) (Some m') = Some ( m <> m')
-- class constraint on 'm' indicates that it also must be a Semigroup, e.g. it understands (<>)
instance Semigroup m => Monoid (Option m)
where
mempty :: Semigroup m => Option m
mempty = None
mappend :: Semigroup m => Option m -> Option m -> Option m
mappend = (<>)
-- fmap, <$>
instance Functor Option
where
fmap :: (a -> b) -> Option a -> Option b
fmap f (Some a) = Some (f a)
fmap _ None = None
instance Applicative Option
where
pure :: a -> Option a
pure = Some
(<*>) :: Option (a -> b) -> Option a -> Option b
Some f <*> g = fmap f g
None <*> _ = None
instance Foldable Option
where
foldMap :: Monoid m => (a -> m) -> Option a -> m
foldMap _ None = mempty
foldMap f (Some a) = f a
instance Traversable Option
where
traverse :: (Applicative f )=> (a -> f b) -> Option a -> f (Option b)
traverse f (Some a) = Some <$> f a
traverse _ None = pure None
-- SAMPLE REPL USAGES
-- ghci> import Data.Monoid
-- ghci> Some ( Sum 3) <> Some ( Sum 3)
-- ghci> Some (Sum {getSum = 6})
-- ghci> foldMap id $ Some "Tom"
-- ghci> foldMap id $ Some (Sum 12)
-- ghci> traverse id $ Some "Tom"
-- ghci> traverse id $ Some (Sum 12)
EOF
cat > src/Doodles.hs <<EOF
module
Doodles
where
main :: IO ()
main = do
what <- life'sMeaning
putStr "It is "
print what
return ()
life'sMeaning :: IO Integer
life'sMeaning =
humbleBeginnings >>=
stormAndStress >>=
enlightenment
where
humbleBeginnings :: IO Integer
humbleBeginnings = readIO "0"
stormAndStress :: Integer -> IO Integer
stormAndStress i = let storm = readIO $ show i :: IO Integer
stress x = readIO ( "2" ++ show x )
in storm >>= stress
enlightenment :: Integer -> IO Integer
enlightenment i = readIO $ show (2 * i + 2)
EOF
cat > test/TestSuite.hs <<EOF
module
Main
where
import Lib
import Test.Hspec
import Test.QuickCheck
import Test.QuickCheck.Function
import Data.Monoid
-- ============= QUICKCHECK PROPERTIES
prop_monoidAssoc :: (Eq m, Monoid m) => m -> m -> m -> Bool
prop_monoidAssoc a b c = (a <> (b <> c)) == ((a <> b) <> c)
prop_monoidLeftIdentity :: (Eq m, Monoid m) => m -> Bool
prop_monoidLeftIdentity a = (mempty <> a) == a
prop_monoidRightIdentity :: (Eq m, Monoid m) => m -> Bool
prop_monoidRightIdentity a = (a <> mempty) == a
main :: IO ()
main = do
unitTests
quickCheckTests
unitTests :: IO ()
unitTests = hspec $ do
describe "unit tests" $ do
it "Some (Sum 1) \`mappend\` Some (Sum 1)" $ do
let n = Some (Sum 1) \`mappend\` Some (Sum 1)
getSum <$> n \`shouldBe\` Some (2::Int)
it "Some (Product 2) \`mappend\` Some (Product 3)" $ do
let n = Some (Product 2) \`mappend\` Some (Product 3)
getProduct <$> n \`shouldBe\` Some (6::Int)
it "Some (Sum 1) \`mappend\` None" $ do
let n = Some (Sum 1) \`mappend\` None
getSum <$> n \`shouldBe\` Some (1::Int)
it "None \`mappend\` Some (Sum 1)" $ do
let n = None \`mappend\` Some (Sum 1)
getSum <$> n \`shouldBe\` Some (1::Int)
it "(Some D \`mappend\` ((Some o) \`mappend\` (Some g))" $ do
let result = Some "D" \`mappend\` (Some "o" \`mappend\` Some "g")
result \`shouldBe\` Some "Dog"
quickCheckTests :: IO ()
quickCheckTests = hspec $ do
describe "quick check tests" $ do
it "associativity rule check of monoids with arbitrary Sum Int input" $ do
quickCheck (prop_monoidAssoc :: Sum Int -> Sum Int -> Sum Int -> Bool)
it "associativity rule check of monoids with arbitrary String input" $ do
quickCheck (prop_monoidAssoc :: String -> String -> String -> Bool)
it "left identity rule check with arbitrary Sum Int input" $ do
quickCheck (prop_monoidLeftIdentity :: Sum Int -> Bool)
it "right identity rule check with arbitrary Sum Int input" $ do
quickCheck (prop_monoidRightIdentity :: Sum Int -> Bool)
EOF
cat > $project.cabal <<EOF
cabal-version: 3.0
name: $project
version: 0.1.0.0
synopsis: Synopsis of $project
license: MIT
license-file: LICENSE
author: $author
maintainer: $email
build-type: Simple
extra-doc-files: CHANGELOG.md
common common-all
ghc-options: -Wall -fwarn-tabs
default-language: GHC2021
build-depends: base ^>=4.18.1.0 && < 5,
Cabal ^>=3.10.2.1
library
import: common-all
exposed-modules: Lib, Doodles
hs-source-dirs: src
build-depends: hspec ^>= 2.11.7,
QuickCheck ^>= 2.14.3,
checkers ^>=0.6.0
executable monad-transformers
import: common-all
main-is: Main.hs
other-modules: Lib, Doodles
hs-source-dirs: app, src
build-depends: hspec ^>= 2.11.7,
QuickCheck ^>= 2.14.3
test-suite test-app
import: common-all
other-modules: Lib
type: exitcode-stdio-1.0
main-is: TestSuite.hs
hs-source-dirs: src, test
build-depends: hspec ^>= 2.11.7,
QuickCheck ^>= 2.14.3
EOF
cabal run
}
project='"your project"' #$1
author='"your name"'
email='"[email protected]"'
# Check if two arguments were provided
if [ "$#" -ne 3 ]; then
echo "project name, author name and email address are required"
echo "Usage: /vscode-haskell-cabal.sh $project $author $email"
exit 1 # Exit with an error code
fi
project=$1
author=$2
email=$3
main