Skip to content

Commit 151082b

Browse files
author
Oskar Lundström
committed
Quantity som Functor
1 parent e25922b commit 151082b

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

Physics/src/Dimensions/Quantity.lhs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Quantities
2323
> , (#)
2424
> , (+#), (-#), (*#), (/#)
2525
> , sinq, cosq, asinq, acosq, atanq, expq, logq
26+
> , qfold
2627
> ) where
2728

2829

@@ -340,26 +341,26 @@ The other functions can be written as similar power series and we'll see on thos
340341

341342
We quickly realize a pattern, so let's generalize a bit.
342343

343-
> qmap :: (a -> b) -> Quantity One a -> Quantity One b
344-
> qmap f (ValQuantity d1 v) = ValQuantity d1 (f v)
344+
> qmap :: (a -> b) -> Quantity dim a -> Quantity dim b
345+
> qmap f (ValQuantity d v) = ValQuantity d (f v)
345346

346-
> qmap' :: (a -> b) -> Quantity dim a -> Quantity dim b
347-
> qmap' f (ValQuantity d v) = ValQuantity d (f v)
347+
> instance Functor (Quantity d) where
348+
> fmap = qmap
348349

349350
> qfold :: (a -> a -> b) -> Quantity dim a -> Quantity dim a -> Quantity dim b
350351
> qfold f (ValQuantity d v1) (ValQuantity _ v2) = ValQuantity d (f v1 v2)
351352

352353
> sinq, cosq, asinq, acosq, atanq, expq, logq :: (Floating v) =>
353354
> Quantity One v -> Quantity One v
354-
> sinq = qmap sin
355-
> cosq = qmap cos
356-
> asinq = qmap asin
357-
> acosq = qmap acos
358-
> atanq = qmap atan
359-
> expq = qmap exp
360-
> logq = qmap log
355+
> sinq = fmap sin
356+
> cosq = fmap cos
357+
> asinq = fmap asin
358+
> acosq = fmap acos
359+
> atanq = fmap atan
360+
> expq = fmap exp
361+
> logq = fmap log
361362

362-
Why not make `Quantity` an instance of `Num`, `Fractional`, `Floating` och `Functor`? The reason is that the functions of those type classes have the following type
363+
Why not make `Quantity` an instance of `Num`, `Fractional` and `Floating`? The reason is that the functions of those type classes have the following type
363364

364365
< (*) :: (Num a) => a -> a -> a
365366

@@ -372,7 +373,7 @@ The input here may actually be of *different* types, and the output has a type d
372373

373374
However, operations with only scalars (type `One`) has types compatible with `Num`.
374375

375-
**Exercise** `Quantity One` has compatible types. Make it an instance of `Num`, `Fractional`, `Floating` and `Functor`.
376+
**Exercise** `Quantity One` has compatible types. Make it an instance of `Num`, `Fractional` and `Floating`.
376377

377378
<details>
378379
<summary>**Solution**</summary>
@@ -382,8 +383,8 @@ However, operations with only scalars (type `One`) has types compatible with `Nu
382383
> (+) = (+#)
383384
> (-) = (-#)
384385
> (*) = (*#)
385-
> abs = qmap abs
386-
> signum = qmap signum
386+
> abs = fmap abs
387+
> signum = fmap signum
387388
> fromInteger n = ValQuantity V.one (fromInteger n)
388389

389390
> instance (Fractional v) => Fractional (Quantity One v) where
@@ -399,14 +400,11 @@ However, operations with only scalars (type `One`) has types compatible with `Nu
399400
> asin = asinq
400401
> acos = acosq
401402
> atan = atanq
402-
> sinh = qmap sinh
403-
> cosh = qmap cosh
404-
> asinh = qmap asinh
405-
> acosh = qmap acosh
406-
> atanh = qmap atanh
407-
408-
> instance Functor (Quantity One) where
409-
> fmap = qmap
403+
> sinh = fmap sinh
404+
> cosh = fmap cosh
405+
> asinh = fmap asinh
406+
> acosh = fmap acosh
407+
> atanh = fmap atanh
410408

411409
</div>
412410
</details>
@@ -463,6 +461,7 @@ To solve these two problems we'll introduce some syntactic sugar. First some pre
463461

464462
And now the sugar.
465463

464+
> infixl 3 #
466465
> (#) :: (Num v) => v -> Quantity d v -> Quantity d v
467466
> v # (ValQuantity d bv) = ValQuantity d (v*bv)
468467

0 commit comments

Comments
 (0)