You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Physics/src/Dimensions/Quantity.lhs
+44-59Lines changed: 44 additions & 59 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
Quantities
3
3
==========
4
4
5
-
We'll now create a data type for quantities and combine dimensions on value-level and type-level. Just as before, a bunch of GHC-extensions are necessary.
5
+
\ignore{
6
6
7
7
> {-# LANGUAGE UndecidableInstances #-}
8
8
> {-# LANGUAGE FlexibleInstances #-}
@@ -11,43 +11,32 @@ We'll now create a data type for quantities and combine dimensions on value-leve
11
11
> {-# LANGUAGE TypeOperators #-}
12
12
> {-# LANGUAGE KindSignatures #-}
13
13
14
-
TODO: The module header is overly long. Please group several (related) operations on each line
14
+
}
15
15
16
16
> moduleDimensions.Quantity
17
-
> ( Quantity
18
-
> , length
19
-
> , mass
20
-
> , time
21
-
> , current
22
-
> , temperature
23
-
> , substance
24
-
> , luminosity
25
-
> , one
26
-
> , velocity
27
-
> , acceleration
28
-
> , force
29
-
> , momentum
30
-
> , (~=)
31
-
> , isZero
32
-
> , (#)
33
-
> , (+#)
34
-
> , (-#)
35
-
> , (*#)
36
-
> , (/#)
37
-
> , sinq
38
-
> , cosq
39
-
> , asinq
40
-
> , acosq
41
-
> , atanq
42
-
> , expq
43
-
> , logq
44
-
> )
45
-
>where
46
-
17
+
> ( Quantity
18
+
> , length, mass, time, current, temperature, substance, luminosity, one
19
+
> , velocity, acceleration, force, momentum
20
+
> , (~=)
21
+
> , isZero
22
+
> , (#)
23
+
> , (+#), (-#), (*#), (/#)
24
+
> , sinq, cosq, asinq, acosq, atanq, expq, logq
25
+
> ) where
26
+
>
47
27
> importqualifiedDimensions.ValueLevelasV
48
28
> importDimensions.TypeLevelasT
49
29
> importPreludeasPhiding (length, div)
50
30
31
+
We'll now create a data type for quantities and combine dimensions on value-level and type-level. Just as before, a bunch of GHC-extensions are necessary.
32
+
33
+
< {-# LANGUAGE UndecidableInstances #-}
34
+
< {-# LANGUAGE FlexibleInstances #-}
35
+
< {-# LANGUAGE GADTs #-}
36
+
< {-# LANGUAGE DataKinds #-}
37
+
< {-# LANGUAGE TypeOperators #-}
38
+
< {-# LANGUAGE KindSignatures #-}
39
+
51
40
First we create the data type for quantities.
52
41
53
42
TODO: A bit confusing to use the same name for both the type constructor and the value constructor.
@@ -102,7 +91,7 @@ TODO: (here and elsewhere) use "print" or "pretty print" or "pretty-print", not
102
91
103
92
Note that the `Quantity` data type has both value-level and type-level dimensions. As previosuly mentioned, value-level in order to print prettily and type-level to only permit legal operations.
104
93
105
-
**Exercise.** Create a data type which has two type-level dimensions, always use `Rational` as the value-holding type (the role of `v`) but which has no value-level dimensions.
94
+
**Exercise** Create a data type which has two type-level dimensions, always use `Rational` as the value-holding type (the role of `v`) but which has no value-level dimensions.
106
95
107
96
TODO: What is the purpose (semantics, intended meaning or use) of Quantity'?
108
97
@@ -142,18 +131,18 @@ Multiplication is
142
131
143
132
The type has the following interpretation: two values of type `Quantity dx v` is input, where `dx` are two types representing (potentially different) dimensions. As output, a value of type `Quantity` is returned. The type in the `Quantity` will be the type that is the product of the two dimensions in.
144
133
145
-
**Exercise.** Implement subtraction and division.
134
+
**Exercise** Implement subtraction and division.
146
135
147
-
{.float-img-right}
136
+
{.float-img-right}
148
137
149
138
<details>
150
139
<summary>**Solution**</summary>
151
140
<div>
152
141
153
142
Hold on cowboy! Not so fast. We'll come back later to subtraction and division, so check your solution then.
154
143
155
-
</div>
156
-
</details>
144
+
</div>
145
+
</details>
157
146
158
147
Now on to some example values and types.
159
148
@@ -198,7 +187,7 @@ The type-level dimensions are used below to enforce, at compile-time, that only
198
187
199
188
If the dimensions had been value-level only, this error would go undetected until run-time.
200
189
201
-
**Exercise.** What is the volume of a cuboid with sides $1.2\ m$, $0.4\ m$ and $1.9\ m$? Create values for the involved quantities. Use `Float` instead of `Double`.
190
+
**Exercise** What is the volume of a cuboid with sides $1.2\ m$, $0.4\ m$ and $1.9\ m$? Create values for the involved quantities. Use `Float` instead of `Double`.
202
191
203
192
<details>
204
193
<summary>**Solution**</summary>
@@ -218,8 +207,8 @@ If the dimensions had been value-level only, this error would go undetected unti
@@ -243,7 +232,7 @@ It's useful to be able to compare quantities. Perhaps one wants to know which of
243
232
> instance (Eqv) =>Eq (Quantitydv) where
244
233
>(==)= quantityEq
245
234
246
-
**Exercise.** Make `Quantity` an instance of `Ord`.
235
+
**Exercise** Make `Quantity` an instance of `Ord`.
247
236
248
237
<details>
249
238
<summary>**Solution**</summary>
@@ -257,8 +246,8 @@ It's useful to be able to compare quantities. Perhaps one wants to know which of
257
246
> instance (Ordv) =>Ord (Quantitydv) where
258
247
>compare= quantityCompare
259
248
260
-
</div>
261
-
</details>
249
+
</div>
250
+
</details>
262
251
263
252
We often use `Double` as the value holding type. Doing exact comparsions isn't always possible to due rounding errors. Therefore, we'll create a `~=` function for testing if two quantities are almost equal.
264
253
@@ -346,7 +335,7 @@ The input here may actually be of *different* types, and the output has a type d
346
335
347
336
However, operations with only scalars (type `One`) has types compatible with `Num`.
348
337
349
-
**Exercise.** `Quantity One` has compatible types. Make it an instance of `Num`, `Fractional`, `Floating` and `Functor`.
338
+
**Exercise** `Quantity One` has compatible types. Make it an instance of `Num`, `Fractional`, `Floating` and `Functor`.
350
339
351
340
<details>
352
341
<summary>**Solution**</summary>
@@ -382,8 +371,8 @@ However, operations with only scalars (type `One`) has types compatible with `Nu
382
371
> instanceFunctor (QuantityOne) where
383
372
>fmap= qmap
384
373
385
-
</div>
386
-
</details>
374
+
</div>
375
+
</details>
387
376
388
377
Syntactic sugar
389
378
---------------
@@ -413,7 +402,7 @@ TODO: Please use "1" instead of "0" in all the dimensions. That signifies an amo
413
402
> time:: (Numv) =>QuantityTimev
414
403
> time =QuantityV.time 0
415
404
416
-
**Exercise.** Do the rest.
405
+
**Exercise** Do the rest.
417
406
418
407
<details>
419
408
<summary>**Solution**</summary>
@@ -434,8 +423,8 @@ TODO: Please use "1" instead of "0" in all the dimensions. That signifies an amo
434
423
> one:: (Numv) =>QuantityOnev
435
424
> one =QuantityV.one 0
436
425
437
-
</div>
438
-
</details>
426
+
</div>
427
+
</details>
439
428
440
429
And now the sugar.
441
430
@@ -488,9 +477,7 @@ TODO: Dessa har alla Double som värdetyp. Hur förhindra det? Explicita typsign
488
477
489
478
Just for convenience sake we'll define a bunch of common composite dimensions. Erm, *you'll* define.
490
479
491
-
---
492
-
493
-
**Exericse.** Define pre-made values for velocity, acceleration, force, momentum and energy.
480
+
**Exericse** Define pre-made values for velocity, acceleration, force, momentum and energy.
494
481
495
482
<details>
496
483
<summary>**Solution**</summary>
@@ -502,10 +489,8 @@ Just for convenience sake we'll define a bunch of common composite dimensions. E
502
489
> momentum = force *# time
503
490
> energy = force *#length
504
491
505
-
</div>
506
-
</details>
507
-
508
-
---
492
+
</div>
493
+
</details>
509
494
510
495
Alternative sugar with support for units
511
496
----------------------------------------
@@ -543,8 +528,8 @@ Notice how the value is multiplied with the "base value" of the unit. This funct
543
528
< ghci> velocity1 +# velocity2
544
529
<67.77 m/s
545
530
546
-
</div>
547
-
</details>
531
+
</div>
532
+
</details>
548
533
549
534
A physics example
550
535
-----------------
@@ -557,7 +542,7 @@ The exercise is "A dog runs, jumps and lands sliding on a carriage. The dog weig
0 commit comments