-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForwardAD.hs
181 lines (158 loc) · 6.51 KB
/
ForwardAD.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
-- | Forward-AD Functions
--
-- Given the following term:
--
-- > Γ |- t : τ
--
-- We produce this term:
--
-- > Df₁[Γ] |- Df[t] : (Df₁[τ] * (Df₂[Γ] -o Df₂[τ]))
module ForwardAD (
df, dfOp,
Df1Env, Df2Env,
) where
import Operation (LinearOperation (..), Operation (..))
import SourceLanguage
import TargetLanguage
import Env
import Types (LT, LTU, UnLin, Df1, Df2, LFun, LEither)
type family Df1Env env where
Df1Env '[] = '[]
Df1Env (t ': env) = Df1 t ': Df1Env env
type family Df2Env env where
Df2Env '[] = ()
Df2Env (t ': env) = (Df2Env env, Df2 t)
cvtDf1EnvIdx :: Idx env t -> Idx (Df1Env env) (Df1 t)
cvtDf1EnvIdx Z = Z
cvtDf1EnvIdx (S i) = S (cvtDf1EnvIdx i)
linPrj :: (LTenv lenv, LT (Df2Env env))
=> Idx env t -> LinTTerm env' lenv (Df2Env env) -> LinTTerm env' lenv (Df2 t)
linPrj Z env = LinSnd env
linPrj (S i) env = linPrj i (LinFst env)
dfOp :: LT (UnLin (Df2 b)) => Operation a b -> TTerm env (a -> LFun (Df2 a) (Df2 b))
dfOp (Constant _) = Lambda (LinFun LinZero)
dfOp EAdd = Lambda $ LinFun $ LinPlus (LinFst (LinVar Z)) (LinSnd (LinVar Z))
dfOp EProd = Lambda $ LinFun $ LinPlus (LinLOp LProd (Fst (Var Z)) (LinSnd (LinVar Z)))
(LinLOp LProd (Snd (Var Z)) (LinFst (LinVar Z)))
dfOp EScalAdd = Lambda $ LinFun $ LinPlus (LinFst (LinVar Z)) (LinSnd (LinVar Z))
dfOp EScalSubt = Lambda $ LinFun $ LinPlus (LinFst (LinVar Z)) (LinLOp LScalNeg Unit (LinSnd (LinVar Z)))
dfOp EScalProd = Lambda $ LinFun $ LinPlus (LinLOp LScalProd (Fst (Var Z)) (LinSnd (LinVar Z)))
(LinLOp LScalProd (Snd (Var Z)) (LinFst (LinVar Z)))
dfOp EScalSin = Lambda $ LinFun $ LinLOp LScalProd (Op EScalCos (Var Z)) (LinVar Z)
dfOp EScalCos = Lambda $ LinFun $ LinLOp LScalProd (neg (Op EScalSin (Var Z))) (LinVar Z)
where neg x = Op EScalSubt (Pair (Op (Constant 0.0) Unit) x)
dfOp EScalSign = Lambda (LinFun LinZero)
df :: LTU (Df2Env env) => STerm env t -> TTerm (Df1Env env) (Df1 t, LFun (Df2Env env) (Df2 t))
df = \case
SVar i ->
Pair (Var (cvtDf1EnvIdx i))
(LinFun (linPrj i (LinVar Z)))
SLambda body ->
Let (Lambda $ df body) $
Pair (Lambda $
Let (Var (S Z) `App` Var Z) $
Pair (Fst (Var Z))
(LinFun $ Snd (Var Z) `LinApp` LinPair LinZero (LinVar Z)))
(LinFun $ LinLam $
Snd (Var (S Z) `App` Var Z) `LinApp` LinPair (LinVar Z) LinZero)
SLet rhs body ->
Let (df rhs) $
Let (substTt (wSucc wId) (Fst (Var Z)) (df body)) $
Pair (Fst (Var Z))
(LinFun $
Snd (Var Z) `LinApp` LinPair (LinVar Z)
(Snd (Var (S Z)) `LinApp` LinVar Z))
SApp fun arg ->
Let (df arg) $
Let (sinkTt1 (df fun)) $
Let (Fst (Var Z) `App` Fst (Var (S Z))) $
Pair (Fst (Var Z))
(LinFun $
LinPlus ((Snd (Var (S Z)) `LinApp` LinVar Z) `LinApp'` Fst (Var (S (S Z))))
(Snd (Var Z) `LinApp` (Snd (Var (S (S Z))) `LinApp` LinVar Z)))
SUnit -> Pair Unit (LinFun LinZero)
SPair e1 e2 ->
Let (df e1) $
Let (sinkTt1 (df e2)) $
Pair (Pair (Fst (Var (S Z))) (Fst (Var Z)))
(LinFun $
LinPair (Snd (Var (S Z)) `LinApp` LinVar Z)
(Snd (Var Z ) `LinApp` LinVar Z))
SFst e ->
Let (df e) $
Pair (Fst (Fst (Var Z)))
(LinFun $ LinFst (Snd (Var Z) `LinApp` LinVar Z))
SSnd e ->
Let (df e) $
Pair (Snd (Fst (Var Z)))
(LinFun $ LinSnd (Snd (Var Z) `LinApp` LinVar Z))
SInl e ->
Let (df e) $
Pair (Inl (Fst (Var Z)))
(LinFun $ LinInl (Snd (Var Z) `LinApp` LinVar Z))
SInr e ->
Let (df e) $
Pair (Inr (Fst (Var Z)))
(LinFun $ LinInr (Snd (Var Z) `LinApp` LinVar Z))
SCase e a b ->
Let (df e) $
Case (Fst (Var Z))
(Let (sinkTt (wSink (wSucc wId)) (df a)) $
Pair (Fst (Var Z))
(LinFun $
Snd (Var Z) `LinApp` LinPair (LinVar Z) (linFromInl $ Snd (Var (S (S Z))) `LinApp` LinVar Z)))
(Let (sinkTt (wSink (wSucc wId)) (df b)) $
Pair (Fst (Var Z))
(LinFun $
Snd (Var Z) `LinApp` LinPair (LinVar Z) (linFromInr $ Snd (Var (S (S Z))) `LinApp` LinVar Z)))
where
linFromInl :: (LTU a, LTU b, LTenv lenv) => LinTTerm env lenv (LEither a b) -> LinTTerm env lenv a
linFromInl e' = LinCase e' (LinVar Z) LinError
linFromInr :: (LTU a, LTU b, LTenv lenv) => LinTTerm env lenv (LEither a b) -> LinTTerm env lenv b
linFromInr e' = LinCase e' LinError (LinVar Z)
SOp op arg ->
Let (df arg) $
Pair (Op op (Fst (Var Z)))
(LinFun $
(dfOp op `App` Fst (Var Z))
`LinApp` (Snd (Var Z) `LinApp` LinVar Z))
SMap f e ->
Let (df f) $
Let (sinkTt1 (df e)) $
Pair (Map (Lambda $ Fst (Fst (Var (S (S Z))) `App` Var Z)) (Fst (Var Z)))
(LinFun $
LinPlus (LinZipWith (Lambda $ LinFun $
Snd (Fst (Var (S (S Z))) `App` Var Z)
`LinApp` LinVar Z)
(Fst (Var Z))
(Snd (Var Z) `LinApp` LinVar Z))
(LinMap (Snd (Var (S Z)) `LinApp` LinVar Z)
(Fst (Var Z))))
SMap1 f e ->
Let (Lambda (df f)) $
Let (sinkTt1 (df e)) $
Pair (Map (Lambda $ Fst (Var (S (S Z)) `App` Var Z)) (Fst (Var Z)))
(LinFun $
LinPlus (LinZipWith (Lambda $ LinFun $
Snd (Var (S (S Z)) `App` Var Z)
`LinApp` LinPair LinZero (LinVar Z))
(Fst (Var Z))
(Snd (Var Z) `LinApp` LinVar Z))
(LinMap (LinLam $
Snd (Var (S (S Z)) `App` Var Z)
`LinApp` LinPair (LinVar Z) LinZero)
(Fst (Var Z))))
SReplicate e ->
Let (df e) $
Pair (Replicate (Fst (Var Z)))
(LinFun $ LinReplicate (Snd (Var Z) `LinApp` LinVar Z))
SSum e ->
Let (df e) $
Pair (Sum (Fst (Var Z)))
(LinFun $ LinSum (Snd (Var Z) `LinApp` LinVar Z))