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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use proc_macro::Diagnostic;
use quote::ToTokens;
use rust_ad_core::traits::*;
use rust_ad_core::Arg;
use rust_ad_core::*;
use std::collections::HashMap;
#[cfg(not(debug_assertions))]
use std::collections::HashSet;
use syn::spanned::Spanned;
pub fn update_forward_return(
block: &mut syn::Block,
function_inputs: &[String],
#[cfg(not(debug_assertions))] type_map: HashMap<String, String>,
#[cfg(not(debug_assertions))] non_zero_derivatives: HashSet<String>,
) -> Result<(), PassError> {
match block.stmts.last_mut() {
Some(last_stmt) => {
*last_stmt = match last_stmt {
syn::Stmt::Semi(syn::Expr::Return(expr_return_opt), _) => {
let expr_return = match expr_return_opt.expr.as_ref() {
Some(e) => e,
None => {
Diagnostic::spanned(
expr_return_opt.span().unwrap(),
proc_macro::Level::Error,
"No return expression",
)
.emit();
return Err("update_forward_return: No return expression".into());
}
};
match &**expr_return {
syn::Expr::Tuple(expr_tuple) => {
let return_idents = expr_tuple.to_token_stream().to_string();
eprintln!("return_idents: {}", return_idents);
let return_str = format!("return ({},({}));",
return_idents,
expr_tuple.elems.iter()
.map(|e|
match e {
syn::Expr::Path(ep) => {
let ep_str = ep.to_token_stream().to_string();
format!("({})",function_inputs
.iter()
.map(|input| if ep_str == *input {
der!(input)
} else {
let der = wrt!(ep_str, input);
#[cfg(not(debug_assertions))]
match non_zero_derivatives.contains(&der) {
true => der,
false => format!("0{}", type_map.get(input).unwrap()),
}
#[cfg(debug_assertions)]
der
})
.intersperse(String::from(","))
.collect::<String>()
)
}
_ => panic!("update_forward_return: Unsupported inner tuple type (e.g. `return (x,y)` is supported, `return (x,(a,b))` is not supported)")
})
.intersperse(String::from(","))
.collect::<String>()
);
syn::parse_str(&return_str)
.expect("update_forward_return: tuple parse fail")
}
syn::Expr::Path(expr_path) => {
let return_ident = expr_path.to_token_stream().to_string();
// The if case where `ident == input` is for when you are returning an input.
let return_str = format!(
"return ({},{});",
return_ident,
match function_inputs.len() {
0 => String::new(),
1 => {
let input = &function_inputs[0];
if return_ident == *input {
der!(input)
} else {
let der = wrt!(return_ident, input);
#[cfg(not(debug_assertions))]
match non_zero_derivatives.contains(&der) {
true => der,
false => {
format!("0{}", type_map.get(input).unwrap())
}
}
#[cfg(debug_assertions)]
der
}
}
_ => format!(
"({})",
function_inputs
.iter()
.map(|input| if return_ident == *input {
der!(input)
} else {
let der = wrt!(return_ident, input);
#[cfg(not(debug_assertions))]
match non_zero_derivatives.contains(&der) {
true => der,
false => {
format!("0{}", type_map.get(input).unwrap())
}
}
#[cfg(debug_assertions)]
der
})
.intersperse(String::from(","))
.collect::<String>()
),
}
);
syn::parse_str(&return_str)
.expect("update_forward_return: path parse fail")
}
_ => {
Diagnostic::spanned(
expr_return_opt.span().unwrap(),
proc_macro::Level::Error,
"Unsupported return expression",
)
.emit();
return Err(
"update_forward_return: unsupported return expression".into()
);
}
}
}
_ => {
Diagnostic::spanned(
block.span().unwrap(),
proc_macro::Level::Error,
"Unsupported return statement",
)
.emit();
return Err("update_forward_return: Unsupported return statement".into());
}
};
}
_ => {
Diagnostic::spanned(
block.span().unwrap(),
proc_macro::Level::Error,
"No return statement",
)
.emit();
return Err("update_forward_return: No return statement".into());
}
};
Ok(())
}
/// Intersperses values with respect to the preceding values.
pub fn intersperse_succeeding_stmts<K>(
mut x: Vec<syn::Stmt>,
mut extra: K,
f: fn(&syn::Stmt, &mut K) -> Result<Option<syn::Stmt>, PassError>,
) -> Result<Vec<syn::Stmt>, PassError> {
let len = x.len();
let new_len = len * 2 - 1;
let mut y = Vec::with_capacity(new_len);
while x.len() > 1 {
y.push(x.remove(0));
let after_opt = pass!(
f(y.last().unwrap(), &mut extra),
"intersperse_succeeding_stmts"
);
if let Some(after) = after_opt {
y.push(after);
}
}
y.push(x.remove(0));
Ok(y)
}
// TODO Reduce code duplication between `reverse_derivative` and `forward_derivative`
pub fn forward_derivative(
stmt: &syn::Stmt,
#[cfg(not(debug_assertions))] (type_map, function_inputs, non_zero_derivatives): &mut (
&HashMap<String, String>,
&[String],
&mut HashSet<String>,
),
#[cfg(debug_assertions)] (type_map, function_inputs): &mut (
&HashMap<String, String>,
&[String],
),
) -> Result<Option<syn::Stmt>, PassError> {
if let syn::Stmt::Local(local) = stmt {
let local_ident = local
.pat
.ident()
.expect("forward_derivative: not ident")
.ident
.to_string();
if let Some(init) = &local.init {
// eprintln!("init: {:#?}",init);
if let syn::Expr::Binary(bin_expr) = &*init.1 {
// Creates operation signature struct
let operation_sig = pass!(
operation_signature(bin_expr, type_map),
"forward_derivative"
);
// Looks up operation with the given lhs type and rhs type and BinOp.
let operation_out_signature = match SUPPORTED_OPERATIONS.get(&operation_sig) {
Some(sig) => sig,
None => {
let error = format!("unsupported derivative for {}", operation_sig);
Diagnostic::spanned(
bin_expr.span().unwrap(),
proc_macro::Level::Error,
error,
)
.emit();
return Err(String::from("forward_derivative"));
}
};
// Applies the forward derivative function for the found operation.
let new_stmt = (operation_out_signature.forward_derivative)(
local_ident,
&[
Arg::try_from(&*bin_expr.left).expect("forward_derivative: bin left"),
Arg::try_from(&*bin_expr.right).expect("forward_derivative: bin right"),
],
function_inputs,
#[cfg(not(debug_assertions))]
non_zero_derivatives,
);
return Ok(Some(new_stmt));
} else if let syn::Expr::Call(call_expr) = &*init.1 {
// Create function in signature
let function_in_signature = pass!(
function_signature(call_expr, type_map),
"forward_derivative"
);
// Gets function out signature
let function_out_signature = match SUPPORTED_FUNCTIONS.get(&function_in_signature) {
Some(sig) => sig,
None => {
let error = format!("unsupported derivative for {}", function_in_signature);
Diagnostic::spanned(
call_expr.span().unwrap(),
proc_macro::Level::Error,
error,
)
.emit();
return Err(String::from("forward_derivative"));
}
};
let args = call_expr
.args
.iter()
.map(|a| Arg::try_from(a).expect("forward_derivative: call arg"))
.collect::<Vec<_>>();
// Gets new stmt
let new_stmt = (function_out_signature.forward_derivative)(
local_ident,
args.as_slice(),
function_inputs,
#[cfg(not(debug_assertions))]
non_zero_derivatives,
);
return Ok(Some(new_stmt));
} else if let syn::Expr::MethodCall(method_expr) = &*init.1 {
let method_sig = pass!(
method_signature(method_expr, type_map),
"forward_derivative"
);
let method_out = match SUPPORTED_METHODS.get(&method_sig) {
Some(sig) => sig,
None => {
let error = format!("unsupported derivative for {}", method_sig);
Diagnostic::spanned(
method_expr.span().unwrap(),
proc_macro::Level::Error,
error,
)
.emit();
return Err(String::from("forward_derivative"));
}
};
let args = {
let mut base = Vec::new();
let receiver = Arg::try_from(&*method_expr.receiver)
.expect("forward_derivative: method receiver");
base.push(receiver);
let mut args = method_expr
.args
.iter()
.map(|a| Arg::try_from(a).expect("forward_derivative: method arg"))
.collect::<Vec<_>>();
base.append(&mut args);
base
};
let new_stmt = (method_out.forward_derivative)(
local_ident,
args.as_slice(),
function_inputs,
#[cfg(not(debug_assertions))]
non_zero_derivatives,
);
return Ok(Some(new_stmt));
} else if let syn::Expr::Path(expr_path) = &*init.1 {
// Given `let x = y;`
// This is `x`
let out_ident = local
.pat
.ident()
.expect("forward_derivative: not ident")
.ident
.to_string();
// This `y`
let in_ident = expr_path.path.segments[0].ident.to_string();
// This is type of `y`
let out_type = type_map
.get(&in_ident)
.expect("forward_derivative: return not found type");
let return_type = rust_ad_core::Type::try_from(out_type.as_str())
.expect("forward_derivative: unsupported return type");
let idents = function_inputs
.iter()
.map(|input| wrt!(out_ident, input))
.intersperse(String::from(","))
.collect::<String>();
let derivatives = function_inputs
.iter()
.map(|input| {
cumulative_derivative_wrt_rt(&*init.1, input, function_inputs, &return_type)
})
.intersperse(String::from(","))
.collect::<String>();
let stmt_str = format!("let ({}) = ({});", idents, derivatives);
let new_stmt: syn::Stmt =
syn::parse_str(&stmt_str).expect("forward_derivative: parse fail");
return Ok(Some(new_stmt));
} else if let syn::Expr::Lit(expr_lit) = &*init.1 {
// Given `let x = y;`
// This is `x`
let out_ident = local
.pat
.ident()
.expect("forward_derivative: not ident")
.ident
.to_string();
// This is type of `y`
let out_type = literal_type(expr_lit).expect("forward_derivative: bad lit type");
let return_type = rust_ad_core::Type::try_from(out_type.as_str())
.expect("forward_derivative: unsupported return type");
let idents = function_inputs
.iter()
.map(|input| wrt!(out_ident, input))
.intersperse(String::from(","))
.collect::<String>();
let derivatives = function_inputs
.iter()
.map(|input| {
cumulative_derivative_wrt_rt(&*init.1, input, function_inputs, &return_type)
})
.intersperse(String::from(","))
.collect::<String>();
let stmt_str = format!("let ({}) = ({});", idents, derivatives);
let new_stmt: syn::Stmt =
syn::parse_str(&stmt_str).expect("forward_derivative: parse fail");
return Ok(Some(new_stmt));
}
}
}
Ok(None)
}