Skip to content

Commit eef0c7e

Browse files
author
Oskar Lundström
committed
2 parents f97955a + 4af2aa8 commit eef0c7e

File tree

7 files changed

+53
-23
lines changed

7 files changed

+53
-23
lines changed

Physics/src/Calculus/DifferentialCalc.lhs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,17 @@ Again, trivial definition in Haskell
368368
> :* (g :* derive f :+ f :* (Log :. f) :* derive g)
369369
> derive Id = Const 1
370370
> derive (Const _) = Const 0
371-
> derive (f :. g) = derive g :* (derive f :. g)
371+
372+
For a function composition $f \circ g$ we have to handle the case
373+
where $f$ is a constant function, as we may get division by zero if we
374+
naively apply the chain rule. We'll make use of the `simplify` function,
375+
which we'll define later, to reduce compositions of constant functions to
376+
just constant functions.
377+
378+
> derive (f :. g) =
379+
> case simplify f of
380+
> Const a -> Const 0
381+
> sf -> (derive sf :. g) :* derive g
372382
> derive (Delta h f) = Delta h (derive f)
373383
> derive (D f) = derive (derive f)
374384

@@ -441,7 +451,7 @@ For addition and subtraction, it's $0$
441451
> -> simplify (Const (a + b) :* f')
442452
> (f', g') -> f' :+ g'
443453
> simplify (f :- g) = case (simplify f, simplify g) of
444-
> (f', Const 0 :- g') -> f' :+ g'
454+
> (f', Const 0 :- g') -> simplify (f' :+ g')
445455
> (f', Const 0) -> f'
446456
> (Const a, Const b) -> if a > b
447457
> then Const (a - b)
@@ -498,9 +508,10 @@ $$b^{-n} = \frac{1}{b^n}$$
498508
Intuitively, the identity function is the identity element for function composition
499509

500510
> simplify (f :. g) = case (simplify f, simplify g) of
501-
> (Id, g') -> g'
502-
> (f', Id) -> f'
503-
> (f', g') -> f' :. g'
511+
> (Const a, _) -> Const a
512+
> (Id, g') -> g'
513+
> (f', Id) -> f'
514+
> (f', g') -> f' :. g'
504515

505516
> simplify (Delta h f) = Delta h (simplify f)
506517
> simplify (D (I f')) = simplify f'

Physics/src/Calculus/FunExpr.lhs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ A structure with class
144144
----------------------------------------------------------------------
145145

146146
Now that we've defined the basic structure of our language, we can
147-
instantiate some useful classes. There are two in particular we care
148-
for: `Show` and `Arbitrary`.
147+
instantiate some useful classes. There are three in particular we care
148+
for: `Show`, `Num`, and `Arbitrary`.
149149

150150
Try modifying `FunExpr` to derive `Show`, so that our expressions can be printed.
151151

@@ -197,17 +197,33 @@ expressions in a much more human friendly way!
197197

198198
Still a bit noisy with all the parentheses, but much better!
199199

200-
Another class we can instance for our `FunExpr` is
201-
`Arbitrary`. This class is associated with the testing library
202-
*QuickCheck*, and describes how to generate arbitrary values of a type
203-
for use when testing logical properties with `quickCheck`. For
204-
example, a property function could be formulated that states that the
205-
`:*` constructor of `FunExpr` is associative.
200+
Another class we can instantiate for our `FunExpr` is `Num`. This
201+
class allows us to make use of the native Haskell functions and
202+
operators for numeric operations, instead of writing our own
203+
constructors. This sometimes improves the readability of the code.
204+
205+
> instance Num FunExpr where
206+
> negate e = Const 0 :- e
207+
> (+) = (:+)
208+
> (*) = (:*)
209+
> fromInteger = Const . fromInteger
210+
> abs = undefined
211+
> signum = undefined
212+
213+
Third but not least, we'll instantiate `Arbitrary`. This class is
214+
associated with the testing library *QuickCheck*, and describes how to
215+
generate arbitrary values of a type for use when testing logical
216+
properties with `quickCheck`. For example, a property function could
217+
be formulated that states that the `:*` constructor of `FunExpr` is
218+
associative.
206219

207220
The implementation itself is not very interesting. We generate a
208221
function expression that tends to contain mostly elementary functions,
209222
arithmetic operations, and a generous dose of constants; with a light
210-
sprinkle of differences, derivatives, and integrals.
223+
sprinkle of differences. We won't include derivatives as all
224+
elementary functions have elementary derivatives anyways, and
225+
integrals may cause cause approximation errors if we have to
226+
numerically compute them at evaluation.
211227

212228
> instance Arbitrary FunExpr where
213229
> arbitrary =
@@ -223,10 +239,9 @@ generated expressions in complexity.
223239
> , (10, return Id)
224240
> , (20, fmap Const arbitrary)
225241
> , (10, genBinaryApp (:.))
226-
> , (5 , genBinaryApp Delta)
227-
> , (5 , fmap D arbitrary)
228-
> , (5 , fmap I arbitrary) ]
242+
> , (5 , genBinaryApp Delta) ]
229243
> where genElementary = elements [Exp, Log, Sin, Cos, Asin, Acos]
230244
> genBinaryApp op = fmap (\(f, g) -> f `op` g) arbitrary
231245
> genBinaryOperation = elements [(:+), (:-), (:*), (:/), (:^)]
232246
> >>= genBinaryApp
247+

Physics/src/Calculus/IntegralCalc.lhs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,5 +502,5 @@ figure out the integral further.
502502

503503
> eval (I f) = let _F = simplify (integrate f)
504504
> in if _F == (I f)
505-
> then integrateApprox (eval f) 0.01 0
505+
> then integrateApprox (eval f) 0.001 0
506506
> else eval _F

Physics/src/Introduction/About.lhs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ The book is aimed at you who have some knowledge of
3939
sure you could pick it up as we go. We believe in you!
4040

4141
If you wonder about the weird code blocks at the start of many
42-
chapters, they are there to declare the chapters as Haskell
42+
chapters,
43+
44+
> module Introduction.About where
45+
46+
they are there to declare the chapters as Haskell
4347
modules. All chapters are written in *Literate Haskell*, and can be
4448
used with GHC/GHCi directly as source code. Therefore, you may choose
4549
to read each chapter as documented source code, rather than text with

Physics/src/Introduction/GettingStarted.lhs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
2-
3-
1+
> module Introduction.GettingStarted where
42

53
What you need to dive in
64
======================================================================

Physics/src/Introduction/WhatIsADsl.lhs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
> module Introduction.WhatIsADsl where
2+
13
So what's a DSL?
24
======================================================================
35

Rapport/include/Introduktion.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ \section{Bakgrund}
3737
område. Detta område kan vara databashantering, algebraiska uttryck eller till
3838
och med fysik. Språket kan antingen vara implementerat inuti ett annat
3939
programmeringsspråk eller implementerat helt fristående. I kursen och projektet
40-
är de implementerat i Haskell.
40+
är de implementerade i Haskell.
4141

4242
Idéen bakom projektet är att använda domänspecifika språk för att ur ett alternativt perspektiv presentera fysik. Likt det sätt DSLsofMath
4343
presenterar kopplingar mellan matematik och programmering ska projektet på motsvarande sätt visa på kopplingar mellan programmering och fysik och därmed

0 commit comments

Comments
 (0)