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
@@ -465,8 +466,6 @@ To solve these two problems we'll introduce some syntactic sugar. First some pre
465
466
466
467
And now the sugar.
467
468
468
-
TODO: Please multiply the values instead of throwing them away. I'm pretty sure (#) should behave as "scale" of a vector space. So that (x#(y#z)) == ((x*y)#z).
469
-
470
469
> (#):: (Numv) =>v->Quantitydv->Quantitydv
471
470
> v # (ValQuantity d bv) =ValQuantity d (v*bv)
472
471
@@ -490,6 +489,8 @@ But let's not stop there. It would be prettier if you could actually write `mete
490
489
491
490
**Exercise** Make it so that one can write the SI-units instead of the base dimensions when one uses the sugar. Then show how to write $4$ seconds.
492
491
492
+
{.float-img-right}
493
+
493
494
<details>
494
495
<summary>**Solution**</summary>
495
496
<div>
@@ -501,7 +502,7 @@ But let's not stop there. It would be prettier if you could actually write `mete
Copy file name to clipboardExpand all lines: Physics/src/Dimensions/Quantity/Test.lhs
+23-25Lines changed: 23 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
Testing of Quantity
3
3
===================
4
4
5
+
\ignore{
6
+
5
7
> {-# LANGUAGE DataKinds #-}
6
8
> {-# LANGUAGE GADTs #-}
7
9
> {-# LANGUAGE KindSignatures #-}
@@ -11,11 +13,23 @@ Testing of Quantity
11
13
> {-# LANGUAGE FlexibleInstances #-}
12
14
> {-# LANGUAGE TypeSynonymInstances #-}
13
15
16
+
}
17
+
14
18
> moduleDimensions.Quantity.Test
15
19
> ( runTests
16
20
> )
17
21
>where
18
22
23
+
24
+
< {-# LANGUAGE DataKinds #-}
25
+
< {-# LANGUAGE GADTs #-}
26
+
< {-# LANGUAGE KindSignatures #-}
27
+
< {-# LANGUAGE TypeFamilies #-}
28
+
< {-# LANGUAGE UndecidableInstances #-}
29
+
< {-# LANGUAGE TypeOperators #-}
30
+
< {-# LANGUAGE FlexibleInstances #-}
31
+
< {-# LANGUAGE TypeSynonymInstances #-}
32
+
19
33
> importPreludehiding (length, div)
20
34
> importTest.QuickCheck
21
35
@@ -36,9 +50,9 @@ First we need an `Arbitrary` instance for `Quantity d val`. For `d` we'll mostly
36
50
A generator for an arbitrary dimension.
37
51
38
52
> genQuantity::QuantitydDouble->Gen (Qd)
39
-
> genQuantity sugar=do
53
+
> genQuantity quantity=do
40
54
> value <- arbitrary
41
-
>return (value #sugar)
55
+
>return (value #quantity)
42
56
43
57
And now we make `Arbitrary` instances of arbitrary selected dimensions in the `Quantity`s.
44
58
@@ -57,7 +71,7 @@ And now we make `Arbitrary` instances of arbitrary selected dimensions in the `Q
57
71
Testing arithmetic properties
58
72
-----------------------------
59
73
60
-
One regular numbers, and hence too on quantites with their dimensions, a bunch of properties should hold. The things we test here are
74
+
On regular numbers, and hence too on quantites with their dimensions, a bunch of properties should hold. The things we test here are
61
75
62
76
- Addition commutative
63
77
- Addition associative
@@ -69,20 +83,14 @@ One regular numbers, and hence too on quantites with their dimensions, a bunch o
69
83
- Subtraction and addition cancel each other out
70
84
- Division and multiplication cancel each other out
71
85
- Pythagoran trigonometric identity
72
-
- The sugar doesn't care about input value
73
86
74
87
Let's start!
75
88
76
89
We could write the type signatures in a general way like
77
90
78
91
< prop_addCom::Qd->Qd->Bool
79
92
80
-
But we won't do that for two reasons.
81
-
82
-
1. QuickCheck needs concrete types in order to work. So we would have to do a bunch of specialization anyway. And even if we begin with a general signature, we can't cover all cases since there are infinitly many dimensions.
83
-
2. TODO: Går inte för vissa fall, t.ex.
84
-
85
-
<Couldn't match type `Mul d (Mul d d)' with `Mul (Mul d d) d'
93
+
But we won't do that since QuickCheck needs concrete types in order to work. So we would have to do a bunch of specialization anyway. And even if we begin with a general signature, we can't cover all cases since there are infinitly many dimensions.
86
94
87
95
Instead we'll pick some arbitrary dimensions that have an `Arbitrary` instance.
88
96
@@ -98,7 +106,6 @@ Instead we'll pick some arbitrary dimensions that have an `Arbitrary` instance.
98
106
> prop_addId::QTime->Bool
99
107
> prop_addId a = zero +# a ~= a
100
108
>where
101
-
> -- `zero` will, thanks to the versatile suger, have > -- the same dimension as `a`.
102
109
> zero =0# a
103
110
104
111
>-- a * b = b * a
@@ -133,33 +140,24 @@ Instead we'll pick some arbitrary dimensions that have an `Arbitrary` instance.
Copy file name to clipboardExpand all lines: Physics/src/Dimensions/Usage.lhs
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2,16 +2,22 @@
2
2
Usage
3
3
=====
4
4
5
-
In this module we'll show how to use value-level dimensions, type-level dimensions and `Quantity` in an "actual" progam.
5
+
\ignore{
6
6
7
7
> {-# LANGUAGE TypeOperators #-}
8
8
9
+
}
10
+
9
11
> moduleDimensions.Usage
10
12
> (
11
13
> )
12
14
>where
13
15
14
-
Let's first import the things we have created.
16
+
In this module we'll show how to use value-level dimensions, type-level dimensions and `Quantity` in an "actual" progam. Let's first use this fancy GHC-extension
17
+
18
+
< {-# LANGUAGE TypeOperators #-}
19
+
20
+
and then import the things we have created.
15
21
16
22
> importDimensions.TypeLevel
17
23
> importDimensions.Quantity
@@ -24,8 +30,6 @@ Values and types
24
30
25
31
Let's create a length, time and mass, in the way hinted in the previous chapter.
26
32
27
-
TODO: Av okända skäl så blir de nedanstående av typ `Integer` om ej en decimal tas med. Man vill nog ha dom som `Double`. Idealt skulle de ha typ `(Num v) => v` men jag har inte fått till det så bra.
0 commit comments