-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.rb
More file actions
640 lines (537 loc) · 14.2 KB
/
expr.rb
File metadata and controls
640 lines (537 loc) · 14.2 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
require 'active_support/concern'
require 'active_support/core_ext/module/concerning'
require 'multiset'
require_relative 'tuple'
require_relative 'latex'
module Expr
extend ActiveSupport::Concern
include Latex::Inspectable
UNARY_OPS = %i[
! ~ +@ -@
]
BINARY_OPS = %i[
**
* / %
+ -
>> <<
& ^ |
<= < > >=
<=> == != =~ !~
]
ARITHMETIC_OPS = %i[
+@ -@
**
* / %
+ -
]
LOGICAL_OPS = %i[
~ & ^ |
]
# NARY_OPS = %i{
# [] []=
# }
OPERATORS = {}
UNARY_OPS.each{|op| OPERATORS[op] = 1 }
BINARY_OPS.each{|op| OPERATORS[op] = 2 }
# NARY_OPS.each{|op| OPERATORS[op] = -1 }
HANDLERS = {}
UNARY_OPS.each do |op|
define_method op do
if handlers = HANDLERS[op]
handlers.each do |(pa), handler|
if pa === self
return Expr.wrap_after_op(handler[self])
end
end
end
super
# raise TypeError, "Undefined operation '#{op} #{inspect}'"
end
end
BINARY_OPS.each do |op|
define_method op do |b|
if handlers = HANDLERS[op]
b = Expr[b]
handlers.each do |(pa, pb), handler|
if pa === self && pb === b
return Expr.wrap_after_op(handler[self, b])
end
end
end
super(b)
# raise TypeError, "Undefined operation '#{inspect} #{op} #{b.inspect}'"
end
end
module ObjectExt
def defop(op, *params, &handler)
op = op.to_sym
if UNARY_OPS.include?(op)
params.size == 1 or raise ArgumentError, "Unary operator '#{op}' has one parameter"
elsif BINARY_OPS.include?(op)
params.size == 2 or raise ArgumentError, "Binary operator '#{op}' has two parameters"
else
raise ArgumentError, "Unknown operator '#{op}'"
end
(HANDLERS[op] ||= {})[params.map{|p| Expr.matcher(p)}] = handler
end
end
extend ObjectExt
::Object.__send__(:include, ObjectExt)
attr_const precedence: 0
def inspect_child(expr, *args)
if expr.is_a?(Expr) && expr.precedence < precedence
"(#{expr.inspect(*args)})"
else
expr.inspect(*args)
end
end
def inspect_child_latex(expr, *args)
if expr.is_a?(Expr) && expr.precedence < precedence
"(#{expr.inspect_latex(*args)})"
else
expr.inspect_latex(*args)
end
end
forward :to_s, :inspect
def zero?
false
end
def one?
false
end
def free_variables
Set.empty
end
def coerce(x)
if ex = Expr.wrap_or_nil(x)
return ex, self
elsif x.respond_to?(:to_f) && respond_to?(:to_f)
return x.to_f, to_f
else
raise Expr.coercion_error(x)
end
end
def args
Tuple[]
end
def syntax
if args.empty?
self
else
[self.class, *args.map(&:syntax)]
end
end
def create
self
end
class << self
def coercion_error(x)
TypeError.new("#{x.class} can't be coerced into an expression")
end
def wrap_or_nil(x)
if x.is_a? Expr
x
elsif x.is_a? Literal::Wrappable
Literal.new(x)
end
end
def wrap_or_x(x)
wrap_or_nil(x) or x
end
def wrap_after_op(x)
if x.is_a? Expr
x.simplify
elsif x.is_a? Literal::Wrappable
Literal.new(x)
else
x
end
end
def [](x)
wrap_or_nil(x) or raise coercion_error(x)
end
def matcher(p)
if p.is_a? Module
p
else
Expr[p]
end
end
def _nosimp?
@nosimp
end
def rules(&block)
@nosimp = true
class_eval(&block)
ensure
@nosimp = nil
end
end
def compare_weight(expr)
case precedence <=> expr.precedence
when -1
args.any? do |arg|
arg.compare_weight(expr) >= 0
end ? 1 : -1
when 1
expr.args.all? do |arg|
compare_weight(arg) > 0
end ? 1 : -1
else
a = Multiset.new(args)
b = Multiset.new(expr.args)
if a.proper_superset?(b)
1
elsif a.proper_subset?(b)
-1
else
c = a - b
d = b - a
if c.size == 1 && d.all?{|arg| arg.compare_weight(c.first) < 0}
1
elsif d.size == 1 && c.all?{|arg| arg.compare_weight(d.first) < 0}
-1
else
0
end
end
end
end
def matches?(expr, vars={})
vars if eql?(expr)
end
def rewrite(rules)
rules.fetch(self, self)
end
def simplify
return self if Expr._nosimp?
expr = create(*args.map(&:simplify))
REDUCTIONS.each do |pat, trans|
vars = pat.free_variables.map_to{Expr}
if m = pat.matches?(expr, vars)
expr = Expr[trans[m]].simplify
break
end
end
expr
end
REDUCTIONS = {}
class << self
def rewrite(rels={})
rels.each do |pat, result|
REDUCTIONS[Expr[pat]] = Expr[result].method(:rewrite)
# case a.compare_weight(b)
# when -1
# equate(b, a)
# when 1
# REDUCTIONS[[a, a.free_variables.map_to{Expr}]] = b
# else
# raise ArgumentError, "Expressions have equal weight: #{a} and #{b}"
# end
end
end
def process(pat, &block)
vars = block.map_params do |name|
pat.free_variables.find{|var| var.name == name } or raise ArgumentError, "Pattern has no variable named '#{name}'"
end
REDUCTIONS[pat] = proc do |m|
block[*vars.map{|var| m[var] }]
end
end
# def rewrite(*types, &block)
# expr, *vars = Variable.call_with_vars(&block)
# types.size == vars.size or raise ArgumentError, "Expected #{vars.size} variable constraints"
# REDUCTIONS[[expr, vars.zip(types).mash]] = expr
# end
end
end
class Literal < SimpleDelegator
include Expr
attr_const precedence: 100
WRAPPED_TYPES = [Integer, Float, Rational, Complex]
module Wrappable
WRAPPED_TYPES.each{|t| t.__send__(:include, self) }
end
class << self
def new(x)
if x.is_a? Literal
x
elsif x.is_a? Wrappable
super
else
raise TypeError, "#{x.class} is not a literal value"
end
end
forward :[], :new
end
forward :value, :__getobj__
def is_a?(type)
super(type) || value.is_a?(type)
end
defop(:==, Literal, Literal) {|x, y| x.value == y.value }
module EqlFix
def eql?(x)
if x.is_a?(Literal)
eql?(x.value)
else
super
end
end
WRAPPED_TYPES.each{|t| t.prepend(self) }
end
end
module Operation
extend ActiveSupport::Concern
include Expr
attr :args
def initialize(*args)
@args = args.frozen_copy
end
class_methods do
def create(*args)
ex = allocate
ex.__send__(:initialize, *args)
ex
end
def new(*args)
Expr[create(*args.map{|arg| Expr[arg] })].simplify
end
forward :[], :new
def handleop(op, *params)
Expr.defop(op, *params, &method(:create))
end
end
def create(*args)
if args == self.args
self
else
self.class.create(*args)
end
end
def rewrite(rules)
rules.fetch(self) do
create(*args.map do |arg|
arg.rewrite(rules)
end)
end
end
def name
self.class.name
end
def name_latex
name
end
def eql?(x)
self.class == x.class && args == x.args
end
def hash
[self.class, *args].hash
end
def inspect
if args.empty?
name
else
"#{name}(#{args.map(&:inspect).join(', ')})"
end
end
def inspect_latex
if args.empty?
name_latex
else
"#{name_latex}(#{args.map{|x| x.inspect_latex }.join(', ')})"
end
end
def free_variables
args.map(&:free_variables).reduce(&:union)
end
def matches?(expr, vars={})
if self.class == expr.class && args.size == expr.args.size
args.zip(expr.args) do |x, y|
vars = x.matches?(y, vars) or break
end
vars
end
end
end
module PrefixOp
extend ActiveSupport::Concern
include Operation
def initialize(arg)
super
end
def arg
args[0]
end
def inspect
"#{name} #{inspect_child(arg)}"
end
def inspect_latex
"#{name_latex} #{inspect_child_latex(arg)}"
end
end
module InfixOp
extend ActiveSupport::Concern
include Operation
def initialize(lhs, rhs)
super
end
def lhs
args[0]
end
def rhs
args[1]
end
def inspect
"#{inspect_child(lhs)} #{name} #{inspect_child(rhs)}"
end
def inspect_latex
"#{inspect_child_latex(lhs)} #{name_latex} #{inspect_child_latex(rhs)}"
end
end
class Variable
include Expr
attr_const precedence: 90
class << self
def [](*args)
vars = []
args.each do |arg|
if arg.respond_to? :to_str
arg.to_str.split.each do |name|
vars << new(name)
end
elsif arg.respond_to? :to_hash
arg.each do |name, con|
vars << new(name, con)
end
else
raise ArgumentError, arg
end
end
case vars.size
when 0
nil
when 1
vars[0]
else
vars
end
end
def call_with_vars(&block)
block.call_with_mapped_params do |name|
new(name)
end
end
end
attr :name, :constraint
def initialize(name, constraint=Expr)
@name = name.to_sym
@constraint = constraint
end
def inspect
if constraint == Expr
name.to_s
else
"#{name} ∈ #{constraint.inspect}"
end
end
def hash
[name, constraint].hash
end
def eql?(x)
x.is_a?(Variable) && name.eql?(x.name) && constraint.eql?(x.constraint)
end
def free_variables
Set[self].freeze
end
def matches?(expr, vars={})
if x = vars[self]
# this variable has a constraint, expr must satisfy it
if x.is_a? Expr
x.matches?(expr, vars)
else
vars.merge(self => expr) if x === expr && constraint === expr
end
else
# this is a free variable, so it must match exactly
eql?(expr)
end
end
end
def vars(*names, &block)
if block
names.empty? or raise ArgumentError, "Cannot combine argument and block form of #vars"
Variable.call_with_vars(&block)
else
Variable[*names]
end
end
class Function
include Expr
attr :name, :params, :expr
def initialize(name, params, expr)
@name = (name || :λ).to_sym
@params = params.to_a.freeze
@expr = expr
@params.empty? and raise ArgumentError, "Empty parameter list"
@params.each{|p| p.is_a?(Variable) or raise ArgumentError, "#{p} is not a variable" }
end
class << self
def [](x)
if x.is_a? Function
x
elsif x.is_a? Expr
vars = x.free_variables
if vars.empty?
x
elsif vars.size == 1
new(nil, vars, x)
else
raise ArgumentError, "Multiple free variables in expression: #{vars.map(&:inspect).join(', ')}"
end
end
end
end
def inspect
"#{name}(#{params.map(&:inspect).join(', ')}) = #{expr.inspect}"
end
def inspect_latex
"#{name}(#{params.map(&:inspect_latex).join(', ')}) = #{Latex.render(expr)}"
end
def hash
[name, params, expr].hash
end
def eql?(x)
x.is_a?(Function) && params.eql?(x.params) && expr.eql?(x.expr)
end
def free_variables
expr.free_variables - params
end
def [](*args)
if args.empty?
self
elsif args.size > params.size
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected #{params.size})"
else
args = args.map{|arg| Expr[arg] }
ex = expr.rewrite(params.zip(args).mash).simplify
if args.size < params.size
self.class.new(nil, params[args.size..-1], ex)
else
ex
end
end
end
def to_proc
proc do |*args|
self[*args]
end
end
end
def func(name=nil, &block)
params = block.map_params{|var| Variable.new(var) }
if params.empty?
block[]
else
Function.new(name, params, block[*params])
end
end