Skip to content

Commit f033607

Browse files
author
Oskar Lundström
committed
Bild och övningsfixar i Quantity
1 parent 39f3c22 commit f033607

File tree

2 files changed

+45
-60
lines changed

2 files changed

+45
-60
lines changed

Physics/src/Dimensions/Quantity.lhs

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Quantities
33
==========
44

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{
66

77
> {-# LANGUAGE UndecidableInstances #-}
88
> {-# LANGUAGE FlexibleInstances #-}
@@ -11,43 +11,32 @@ We'll now create a data type for quantities and combine dimensions on value-leve
1111
> {-# LANGUAGE TypeOperators #-}
1212
> {-# LANGUAGE KindSignatures #-}
1313

14-
TODO: The module header is overly long. Please group several (related) operations on each line
14+
}
1515

1616
> module Dimensions.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+
>
4727
> import qualified Dimensions.ValueLevel as V
4828
> import Dimensions.TypeLevel as T
4929
> import Prelude as P hiding (length, div)
5030

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+
5140
First we create the data type for quantities.
5241

5342
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
10291
10392
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.
10493
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.
10695
10796
TODO: What is the purpose (semantics, intended meaning or use) of Quantity'?
10897
@@ -142,18 +131,18 @@ Multiplication is
142131
143132
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.
144133
145-
**Exercise.** Implement subtraction and division.
134+
**Exercise** Implement subtraction and division.
146135
147-
![Are you a cowboy?](Cowboy.png){.float-img-right}
136+
![](Cowboy.png "Are you a cowboy?"){.float-img-right}
148137
149138
<details>
150139
<summary>**Solution**</summary>
151140
<div>
152141
153142
Hold on cowboy! Not so fast. We'll come back later to subtraction and division, so check your solution then.
154143
155-
</div>
156-
</details>
144+
</div>
145+
</details>
157146
158147
Now on to some example values and types.
159148
@@ -198,7 +187,7 @@ The type-level dimensions are used below to enforce, at compile-time, that only
198187
199188
If the dimensions had been value-level only, this error would go undetected until run-time.
200189
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`.
202191
203192
<details>
204193
<summary>**Solution**</summary>
@@ -218,8 +207,8 @@ If the dimensions had been value-level only, this error would go undetected unti
218207
> volume :: Quantity Volume Float
219208
> volume = quantityMul side1 (quantityMul side2 side3)
220209
221-
</div>
222-
</details>
210+
</div>
211+
</details>
223212
224213
Pretty-printer
225214
--------------
@@ -243,7 +232,7 @@ It's useful to be able to compare quantities. Perhaps one wants to know which of
243232
> instance (Eq v) => Eq (Quantity d v) where
244233
> (==) = quantityEq
245234
246-
**Exercise.** Make `Quantity` an instance of `Ord`.
235+
**Exercise** Make `Quantity` an instance of `Ord`.
247236
248237
<details>
249238
<summary>**Solution**</summary>
@@ -257,8 +246,8 @@ It's useful to be able to compare quantities. Perhaps one wants to know which of
257246
> instance (Ord v) => Ord (Quantity d v) where
258247
> compare = quantityCompare
259248
260-
</div>
261-
</details>
249+
</div>
250+
</details>
262251
263252
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.
264253
@@ -346,7 +335,7 @@ The input here may actually be of *different* types, and the output has a type d
346335
347336
However, operations with only scalars (type `One`) has types compatible with `Num`.
348337
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`.
350339
351340
<details>
352341
<summary>**Solution**</summary>
@@ -382,8 +371,8 @@ However, operations with only scalars (type `One`) has types compatible with `Nu
382371
> instance Functor (Quantity One) where
383372
> fmap = qmap
384373
385-
</div>
386-
</details>
374+
</div>
375+
</details>
387376
388377
Syntactic sugar
389378
---------------
@@ -413,7 +402,7 @@ TODO: Please use "1" instead of "0" in all the dimensions. That signifies an amo
413402
> time :: (Num v) => Quantity Time v
414403
> time = Quantity V.time 0
415404
416-
**Exercise.** Do the rest.
405+
**Exercise** Do the rest.
417406
418407
<details>
419408
<summary>**Solution**</summary>
@@ -434,8 +423,8 @@ TODO: Please use "1" instead of "0" in all the dimensions. That signifies an amo
434423
> one :: (Num v) => Quantity One v
435424
> one = Quantity V.one 0
436425
437-
</div>
438-
</details>
426+
</div>
427+
</details>
439428
440429
And now the sugar.
441430
@@ -488,9 +477,7 @@ TODO: Dessa har alla Double som värdetyp. Hur förhindra det? Explicita typsign
488477
489478
Just for convenience sake we'll define a bunch of common composite dimensions. Erm, *you'll* define.
490479
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.
494481
495482
<details>
496483
<summary>**Solution**</summary>
@@ -502,10 +489,8 @@ Just for convenience sake we'll define a bunch of common composite dimensions. E
502489
> momentum = force *# time
503490
> energy = force *# length
504491
505-
</div>
506-
</details>
507-
508-
---
492+
</div>
493+
</details>
509494
510495
Alternative sugar with support for units
511496
----------------------------------------
@@ -543,8 +528,8 @@ Notice how the value is multiplied with the "base value" of the unit. This funct
543528
< ghci> velocity1 +# velocity2
544529
< 67.77 m/s
545530
546-
</div>
547-
</details>
531+
</div>
532+
</details>
548533
549534
A physics example
550535
-----------------
@@ -557,7 +542,7 @@ The exercise is "A dog runs, jumps and lands sliding on a carriage. The dog weig
557542
558543
This is illustrated in the painting below.
559544
560-
<img src="Exercise.png" alt="" style="width: 600px;"/>
545+
![](Exercise.png "Dog on a cart"){.img-center}
561546
562547
a) Calculate the (shared) final velocity of the dog and the carriage.
563548

Physics/src/Dimensions/ValueLevel.lhs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ We use `showDim` to make `Dim` an instance of `Show`
162162
> instance Show Dim where
163163
> show = showDim
164164

165-
Now dimensions are printed quite prettily in GHCi
165+
Now dimensions are printed in a quite pretty way in GHCi
166166

167167
< ghci> momentum
168168
< kg*m/s

0 commit comments

Comments
 (0)