aboutsummaryrefslogtreecommitdiff
path: root/nn.in~
blob: dd89d0344f9966cb4c0b6c7b4960aedb618e0976 (plain)
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
// Agents

Any >< Optimize(out) =>
	out ~ Any;

// Rules

// Optimizer interacts with Const
Const(k) >< Optimize(out) =>
	out ~ Const(k);


// Optimizer interacts with Add
Add(a, b) >< Optimize(out) =>
	a ~ Optimize(a_opt),
	a_opt ~ Add_CheckLeft(out, b);

// CHECK LEFT
Const(int k) >< Add_CheckLeft(out, b)
	| k == 0 => b ~ Optimize(b_opt), out ~ b_opt
	| _ => b ~ Optimize(b_opt), b_opt ~ Add_CheckRight_WithConst(out, k);
a_opt >< Add_CheckLeft(out, b) =>
	b ~ Optimize(b_opt),
	b_opt ~ Add_CheckRight_WithAny(out, (*L)a_opt);

// CHECK RIGHT
Const(int j) >< Add_CheckRight_WithConst(out, int k) =>
	out ~ Const(k + j);
b_opt >< Add_CheckRight_WithConst(out, int k) =>
	out ~ Add(Const(k), (*L)b_opt);
Const(int j) >< Add_CheckRight_WithAny(out, a_opt)
	| j == 0 => out ~ a_opt
	| _ => out ~ Add(a_opt, Const(j));
b_opt >< Add_CheckRight_WithAny(out, a_opt) =>
	out ~ Add(a_opt, (*L)b_opt);


// Optimizer interacts with Mul
Mul(a, b) >< Optimize(out) =>
	a ~ Optimize(a_opt),
	a_opt ~ Mul_CheckLeft(out, b);

// Check LEFT
Const(int k) >< Mul_CheckLeft(out, b)
	| k == 0 => b ~ Eraser, out ~ Const(0)
	| k == 1 => b ~ Optimize(b_opt), out ~ b_opt
	| _ => b ~ Optimize(b_opt), b_opt ~ Mul_CheckRight_WithConst(out, k);
a_opt >< Mul_CheckLeft(out, b) =>
	b ~ Optimize(b_opt),
	b_opt ~ Mul_CheckRight_WithAny(out, (*L)a_opt);

// Check RIGHT
Const(int j) >< Mul_CheckRight_WithConst(out, int k) =>
	out ~ Const(k * j);
b_opt >< Mul_CheckRight_WithConst(out, int k) =>
	out ~ Mul(Const(k), (*L)b_opt);
Const(int j) >< Mul_CheckRight_WithAny(out, a_opt)
	| j == 0 => Eraser ~ a_opt, out ~ Const(0)
	| j == 1 => out ~ a_opt
	| _ => out ~ Mul(a_opt, Const(j));
b_opt >< Mul_CheckRight_WithAny(out, a_opt) =>
	out ~ Mul(a_opt, (*L)b_opt);


// Net
Mul(Const(1), Add(Any, Mul(Const(0), Add(Any, Any)))) ~ Optimize(out);
out;

free out;
Mul(Const(0), Add(Any, Any)) ~ Optimize(out);
out;