-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharith.rb
More file actions
148 lines (123 loc) · 3.96 KB
/
arith.rb
File metadata and controls
148 lines (123 loc) · 3.96 KB
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
require_relative 'ext'
require_relative 'expr'
module Arithmetic
class Add
include Operation
attr_const precedence: 10
# defop(:+, 0, Expr) {|_, x| x }
# defop(:+, Expr, 0) {|x, _| x }
#
# defop(:+, Literal, Literal) {|x, y| x.value + y.value }
# donk((a + b) + c, a + (b + c))
#
# donk([Expr + Literal] + Literal) do |(a, b), c|
# a + (b + c)
# end
# defop(:+, Add, Add) {|x, y| [*x.args, *y.args].reduce(&:+) }
# defop(:+, Add, Expr) {|x, y| Add[*x.args, y] }
# defop(:+, Expr, Add) {|x, y| Add[x, *y.args] }
handleop(:+, Expr, Expr)
defop(:-, Expr, Expr) {|x, y| x + -y }
Expr.rules do
x, y, z = vars('x y z')
a, b = vars(a: Literal, b: Literal)
rewrite(
x + 0 => x,
0 + x => x,
(x + y) + z => x + (y + z),
)
process(a + b) {|a, b| a.value + b.value }
process(a + (b + x)) {|a, b, x| (a.value + b.value) + x }
end
# class << self
# def create(*args)
# terms = []
# args.flat_map do |arg|
# if arg.is_a? Add
# arg.args
# else
# [arg]
# end
# end.each do |x|
# if x.zero?
# next
# elsif x.is_a?(Literal) && terms[-1].is_a?(Literal)
# terms[-1] = Expr[terms[-1].value + x.value]
# else
# terms << x
# end
# end
# case terms.size
# when 0
# 0
# when 1
# terms[0]
# else
# super(*terms)
# end
# end
# end
# def create(*args)
# args.reduce(&:+)
# end
def inspect
h, *t = args
s = h.inspect
t.each do |x|
if x.is_a?(Literal) && x.negative?
s << " - #{-x}"
elsif x.is_a?(Mul) && x.args[0] == -1
s << " - #{inspect_child(x, start: 1)}"
else
s << " + #{inspect_child(x)}"
end
end
s
end
end
class Mul
include Operation
attr_const precedence: 20
# defop(:*, 0, Expr) { 0 }
# defop(:*, Expr, 0) { 0 }
# defop(:*, 1, Expr) {|_, x| x }
# defop(:*, Expr, 1) {|x, _| x }
#
# defop(:*, Literal, Literal) {|x, y| x.value * y.value }
#
# defop(:*, Mul, Expr) {|x, y| Mul[*x.args, y] }
# defop(:*, Expr, Mul) {|x, y| Mul[x, *y.args] }
handleop :*, Expr, Expr
defop(:+@, Expr) {|x| x }
defop(:-@, Expr) {|x| -1 * x }
Expr.rules do
x, y, z = vars('x y z')
a, b = vars(a: Literal, b: Literal)
rewrite(
# 0/1 identities
0 * x => 0,
x * 0 => 0,
x * 1 => x,
1 * x => x,
# normalize tree
(x * y) * z => x * (y * z),
# distribute over addition
x * (y + z) => (x * y) + (x * z),
(x + y) * z => (x * z) + (y * z),
)
# collapse literals
process(a * b) {|a, b| a.value * b.value }
process(a * (b * x)) {|a, b, x| (a.value * b.value) * x }
end
# def create(*args)
# args.reduce(&:*)
# end
def inspect(start: 0)
if args[start] == -1
"-#{inspect(start: start+1)}"
else
args[start..-1].map{|x| inspect_child(x) }.join(' × ')
end
end
end
end