forked from DSLsofMath/DSLsofMath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
P4.hs
52 lines (50 loc) · 1.31 KB
/
P4.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import FunExp
-- 4a
d :: Func -> Func
d = undefined
eqChain4a e =
[
eval (derive (Exp e))
, -- = spec.
d (eval (Exp e))
, -- = def. eval
d (exp (eval e))
, -- = def. lifted exp
d (exp (eval e))
, -- = def. lifted exp
d (exp . eval e)
, -- = d chain rule
(d exp . eval e) * d (eval e)
, -- = d for exp, specification
(exp . eval e) * eval (derive e)
, -- = lifted exp
exp (eval e) * eval (derive e)
, -- = eval for Exp
eval (Exp e) * eval (derive e)
, -- = eval for :*:
eval (Exp e :*: derive e)
]
-- 4b
eqChain4b e1 e2 =
[
eval (derive (e1 :*: e2))
, -- = spec.
d (eval (e1 :*: e2))
, -- = def. eval for :*:
d (eval e1 * eval e2)
, -- = derivative of product
eval e1 * d (eval e2) + d (eval e1) * eval e2
, -- = spec. bacwards
eval e1 * eval (derive e2) + eval (derive e1) * eval e2
, -- = eval for :*:, twice
eval (e1 :*: derive e2) + eval (derive e1 :*: e2)
, -- = eval for :+:
eval ((e1 :*: derive e2) :+: (derive e1 :*: e2))
]
-- 4c
derive :: FunExp -> FunExp
derive (Const alpha) = Const 0
derive Id = Const 1
derive (e1 :+: e2) = derive e1 :+: derive e2
derive (e1 :*: e2) = (derive e1 :*: e2) :+: (e1 :*: derive e2)
derive (Exp e) = Exp e :*: derive e