openmath 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.adoc +45 -5
- data/data/binding-table-1.yml +76 -0
- data/data/pmquant.ocd +89 -0
- data/data/pmquant.sts +60 -0
- data/data/portable-bindings.json +1 -0
- data/data/replay-vectors.yml +70 -0
- data/lib/openmath/base/omforeign.rb +18 -4
- data/lib/openmath/base/omobj.rb +7 -0
- data/lib/openmath/binary.rb +699 -0
- data/lib/openmath/canonical.rb +127 -0
- data/lib/openmath/cd/cds_namespace.rb +13 -0
- data/lib/openmath/cd/group_collection.rb +160 -0
- data/lib/openmath/cd/group_document.rb +50 -0
- data/lib/openmath/cd/namespace.rb +6 -0
- data/lib/openmath/cd/signature_document.rb +53 -0
- data/lib/openmath/cd/signatures.rb +63 -0
- data/lib/openmath/cd.rb +10 -0
- data/lib/openmath/common_elements.rb +9 -0
- data/lib/openmath/content_mathml.rb +443 -0
- data/lib/openmath/content_mathml_operators.rb +51 -0
- data/lib/openmath/evaluation/closure.rb +11 -0
- data/lib/openmath/evaluation/errors.rb +30 -0
- data/lib/openmath/evaluation/evaluator.rb +192 -32
- data/lib/openmath/evaluation/interval.rb +17 -0
- data/lib/openmath/evaluation/operations.rb +394 -12
- data/lib/openmath/evaluation/portable.rb +114 -0
- data/lib/openmath/evaluation/signature_checker.rb +173 -0
- data/lib/openmath/evaluation/stochastic.rb +113 -0
- data/lib/openmath/evaluation/uncertain.rb +64 -0
- data/lib/openmath/evaluation.rb +12 -2
- data/lib/openmath/foreign.rb +146 -0
- data/lib/openmath/tagged.rb +41 -0
- data/lib/openmath/v2.rb +1 -1
- data/lib/openmath/v3.rb +1 -1
- data/lib/openmath/version.rb +1 -1
- data/lib/openmath.rb +41 -3
- data/schemas/PROVENANCE.md +3 -0
- data/schemas/omcdgroup2.rnc +33 -0
- data/schemas/omcdgroup2.rng +86 -0
- metadata +26 -2
|
@@ -16,8 +16,8 @@ module Openmath
|
|
|
16
16
|
rational: [->(args) { rational(args) }, 2..2],
|
|
17
17
|
plus: [->(args) { numeric_fold(args, :+) }, 1..],
|
|
18
18
|
times: [->(args) { numeric_fold(args, :*) }, 1..],
|
|
19
|
-
minus: [->(args) { numeric_pair(args) { |a, b| a - b } }, 2..2],
|
|
20
|
-
unary_minus: [->(args) {
|
|
19
|
+
minus: [->(args) { numeric_pair(args, :-) { |a, b| a - b } }, 2..2],
|
|
20
|
+
unary_minus: [->(args) { unary_minus(args) }, 1..1],
|
|
21
21
|
divided_by: [->(args) { divided_by(args) }, 2..2],
|
|
22
22
|
power: [->(args) { power(args) }, 2..2],
|
|
23
23
|
abs: [->(args) { args.fetch(0).abs }, 1..1],
|
|
@@ -34,6 +34,43 @@ module Openmath
|
|
|
34
34
|
xor: [->(args) { boolean_fold(args, :^) }, 2..],
|
|
35
35
|
not: [->(args) { !args.fetch(0) }, 1..1],
|
|
36
36
|
implies: [->(args) { !args.fetch(0) || args.fetch(1) }, 2..2],
|
|
37
|
+
# integer1 / rounding1 — exact integer semantics
|
|
38
|
+
factorial: [->(args) { factorial(args) }, 1..1],
|
|
39
|
+
quotient: [->(args) { quotient(args) }, 2..2],
|
|
40
|
+
remainder: [->(args) { remainder(args) }, 2..2],
|
|
41
|
+
floor: [->(args) { rounding(args, :floor) }, 1..1],
|
|
42
|
+
ceiling: [->(args) { rounding(args, :ceil) }, 1..1],
|
|
43
|
+
round: [->(args) { rounding(args, :round) }, 1..1],
|
|
44
|
+
# set1 / list1 — exact collections; suchthat/map ride the lambda layer
|
|
45
|
+
list: [->(args) { args }, 0..],
|
|
46
|
+
set: [->(args) { dedup(args) }, 0..],
|
|
47
|
+
in: [->(args) { member(args) }, 2..2],
|
|
48
|
+
size: [->(args) { collection(args).size }, 1..1],
|
|
49
|
+
map: [->(args) { map_over(args) }, 2..2],
|
|
50
|
+
suchthat: [->(args) { suchthat(args) }, 2..2],
|
|
51
|
+
# interval1 — exact domains (Interval values; Range cannot carry an
|
|
52
|
+
# open lower bound)
|
|
53
|
+
interval: [->(args) { interval(args, closed_low: true, closed_high: true) }, 2..2],
|
|
54
|
+
interval_oo: [->(args) { interval(args, closed_low: false, closed_high: false) }, 2..2],
|
|
55
|
+
interval_oc: [->(args) { interval(args, closed_low: false, closed_high: true) }, 2..2],
|
|
56
|
+
interval_co: [->(args) { interval(args, closed_low: true, closed_high: false) }, 2..2],
|
|
57
|
+
# linalg — exact matrices as rectangular nested arrays over rationals
|
|
58
|
+
matrix: [->(args) { matrix(args) }, 1..],
|
|
59
|
+
matrixrow: [->(args) { require_numeric(args); args }, 0..],
|
|
60
|
+
transpose: [->(args) { matrix_arg(args).transpose }, 1..1],
|
|
61
|
+
determinant: [->(args) { determinant(matrix_arg(args)) }, 1..1],
|
|
62
|
+
vector_product: [->(args) { vector_product(args) }, 2..2],
|
|
63
|
+
# the v1 core's remaining official symbols
|
|
64
|
+
based_integer: [->(args) { based_integer(args) }, 2..2],
|
|
65
|
+
root: [->(args) { root(args) }, 2..2],
|
|
66
|
+
sum: [->(args) { aggregate(args, 0, :+) }, 2..2],
|
|
67
|
+
product: [->(args) { aggregate(args, 1, :*) }, 2..2],
|
|
68
|
+
approx: [->(args) { approx(args) }, 2..2],
|
|
69
|
+
# pmquant — the PM 1002 quantity/uncertainty constructors
|
|
70
|
+
ustd: [->(args) { ustd(args) }, 2..2],
|
|
71
|
+
uexp: [->(args) { uexp(args) }, 3..4],
|
|
72
|
+
ucov: [->(args) { ucov(args) }, 3..5],
|
|
73
|
+
value: [->(args) { quantity_value(args) }, 1..1],
|
|
37
74
|
}.freeze
|
|
38
75
|
|
|
39
76
|
def implementation(operation)
|
|
@@ -58,25 +95,84 @@ module Openmath
|
|
|
58
95
|
end
|
|
59
96
|
|
|
60
97
|
def numeric_fold(args, operator)
|
|
61
|
-
|
|
62
|
-
args.reduce { |
|
|
98
|
+
require_arithmetic(args)
|
|
99
|
+
args.reduce { |left, right| combine(operator, left, right) }
|
|
63
100
|
end
|
|
64
101
|
|
|
65
|
-
def numeric_pair(args, &block)
|
|
66
|
-
|
|
67
|
-
|
|
102
|
+
def numeric_pair(args, operator, &block)
|
|
103
|
+
require_arithmetic(args)
|
|
104
|
+
left, right = args
|
|
105
|
+
value = block.call(reported(left), reported(right))
|
|
106
|
+
propagate(operator, left, right, value)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def unary_minus(args)
|
|
110
|
+
require_arithmetic(args)
|
|
111
|
+
operand = args.fetch(0)
|
|
112
|
+
return -operand unless operand.is_a?(Uncertain)
|
|
113
|
+
|
|
114
|
+
Uncertain.new(**operand.to_h, value: -operand.value)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# The reported value of a quantity; plain numbers are themselves.
|
|
118
|
+
def reported(operand)
|
|
119
|
+
operand.is_a?(Uncertain) ? operand.value : operand
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# The arithmetic domain: numbers and standard-uncertainty
|
|
123
|
+
# quantities; the other categories refuse arithmetically.
|
|
124
|
+
def require_arithmetic(args)
|
|
125
|
+
args.each do |arg|
|
|
126
|
+
next if arg.is_a?(Numeric)
|
|
127
|
+
next if arg.is_a?(Uncertain) && arg.category == UNCERTAIN_STANDARD
|
|
128
|
+
|
|
129
|
+
raise OperandTypeError.new("arithmetic", arg)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Arithmetic over quantities (PM 1004's computations, JCGM 101's
|
|
134
|
+
# propagation): the reported values combine; standard-uncertainty
|
|
135
|
+
# magnitudes propagate — absolute quadrature under plus and minus,
|
|
136
|
+
# relative quadrature under times and divided_by (first-order).
|
|
137
|
+
# Expanded and coverage categories refuse arithmetically: converting
|
|
138
|
+
# between categories is a computation, never an accident. A
|
|
139
|
+
# propagated magnitude is a realization of a combined real, so it
|
|
140
|
+
# carries the limited class; reported values keep their exactness.
|
|
141
|
+
def combine(operator, left, right)
|
|
142
|
+
value = reported(left).public_send(operator, reported(right))
|
|
143
|
+
propagate(operator, left, right, value)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def propagate(operator, left, right, value)
|
|
147
|
+
uncertains = [left, right].grep(Uncertain)
|
|
148
|
+
return value if uncertains.empty?
|
|
149
|
+
|
|
150
|
+
magnitude =
|
|
151
|
+
if %i[* /].include?(operator)
|
|
152
|
+
relative = uncertains.sum(0.0) do |uncertain|
|
|
153
|
+
(uncertain.magnitude / uncertain.value)**2
|
|
154
|
+
end
|
|
155
|
+
value.abs * Math.sqrt(relative)
|
|
156
|
+
else
|
|
157
|
+
Math.sqrt(uncertains.sum(0.0) { |uncertain| uncertain.magnitude**2 })
|
|
158
|
+
end
|
|
159
|
+
Uncertain.new(value: value, category: UNCERTAIN_STANDARD,
|
|
160
|
+
magnitude: magnitude)
|
|
68
161
|
end
|
|
69
162
|
|
|
70
163
|
def divided_by(args)
|
|
71
|
-
|
|
164
|
+
require_arithmetic(args)
|
|
72
165
|
numerator, denominator = args
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
166
|
+
reported_numerator = reported(numerator)
|
|
167
|
+
reported_denominator = reported(denominator)
|
|
168
|
+
if reported_numerator.is_a?(Float) || reported_denominator.is_a?(Float)
|
|
169
|
+
value = reported_numerator / reported_denominator
|
|
170
|
+
elsif reported_denominator.zero?
|
|
76
171
|
raise ZeroDivisionError, "exact division by zero"
|
|
77
172
|
else
|
|
78
|
-
Rational(
|
|
173
|
+
value = Rational(reported_numerator, reported_denominator)
|
|
79
174
|
end
|
|
175
|
+
propagate(:/, numerator, denominator, value)
|
|
80
176
|
end
|
|
81
177
|
|
|
82
178
|
def power(args)
|
|
@@ -90,6 +186,292 @@ module Openmath
|
|
|
90
186
|
end
|
|
91
187
|
end
|
|
92
188
|
|
|
189
|
+
# integer1 / rounding1 — exact integer semantics; a float operand
|
|
190
|
+
# contaminates through Float arithmetic before the final exact step.
|
|
191
|
+
def factorial(args)
|
|
192
|
+
n = args.fetch(0)
|
|
193
|
+
unless n.is_a?(Integer) && n >= 0
|
|
194
|
+
raise OperandTypeError.new("factorial", n)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
(1..n).reduce(1, :*)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def quotient(args)
|
|
201
|
+
numerator, denominator = args
|
|
202
|
+
require_numeric(args)
|
|
203
|
+
(numerator.is_a?(Float) || denominator.is_a?(Float) ? numerator / denominator : Rational(numerator, denominator)).floor
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def remainder(args)
|
|
207
|
+
numerator, denominator = args
|
|
208
|
+
numerator - denominator * quotient(args)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def rounding(args, method)
|
|
212
|
+
require_numeric(args)
|
|
213
|
+
value = args.fetch(0)
|
|
214
|
+
value.is_a?(Integer) ? value : value.public_send(method)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# set1 / list1 — exact collections
|
|
218
|
+
def dedup(args)
|
|
219
|
+
result = []
|
|
220
|
+
args.each { |arg| result << arg unless result.any? { |kept| arg == kept } }
|
|
221
|
+
result
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def member(args)
|
|
225
|
+
value, collection = args
|
|
226
|
+
case collection
|
|
227
|
+
when Array then collection.any? { |kept| value == kept }
|
|
228
|
+
when Range then collection.cover?(value)
|
|
229
|
+
when Interval then collection.member?(value)
|
|
230
|
+
else raise OperandTypeError.new("in", collection)
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def collection(args)
|
|
235
|
+
value = args.fetch(0)
|
|
236
|
+
case value
|
|
237
|
+
when Array, Range then value
|
|
238
|
+
else raise OperandTypeError.new("collection size", value)
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def map_over(args)
|
|
243
|
+
closure, elements = args
|
|
244
|
+
apply_elementwise(closure, collection([elements])) { |value, result| result }
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def suchthat(args)
|
|
248
|
+
collection_arg, closure = args
|
|
249
|
+
elements = collection([collection_arg])
|
|
250
|
+
apply_elementwise(closure, elements) do |value, result|
|
|
251
|
+
require_boolean([result])
|
|
252
|
+
result ? value : nil
|
|
253
|
+
end.compact
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The lambda layer rides here: an elementwise application merges the
|
|
257
|
+
# element over the closure's captured environment and evaluates the
|
|
258
|
+
# body (the evaluator's discipline, one element at a time).
|
|
259
|
+
def apply_elementwise(closure, elements)
|
|
260
|
+
unless closure.is_a?(Closure) && closure.names.size == 1
|
|
261
|
+
raise ArityError.new("lambda", 1..1, closure.is_a?(Closure) ? closure.names.size : 0)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
elements.map do |element|
|
|
265
|
+
environment = closure.environment.merge(closure.names[0] => element)
|
|
266
|
+
result = Evaluator.evaluate(closure.body, variables: environment,
|
|
267
|
+
table: closure.table)
|
|
268
|
+
yield element, result
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# interval1
|
|
273
|
+
def interval(args, closed_low:, closed_high:)
|
|
274
|
+
require_numeric(args)
|
|
275
|
+
Interval.new(low: args.fetch(0), high: args.fetch(1),
|
|
276
|
+
low_closed: closed_low, high_closed: closed_high)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# linalg — exact matrices as rectangular nested arrays (the stdlib
|
|
280
|
+
# matrix gem is not a bundled gem in current Rubies; the value model
|
|
281
|
+
# stays plain data and the arithmetic stays exact).
|
|
282
|
+
def matrix(args)
|
|
283
|
+
args.each do |row|
|
|
284
|
+
unless row.is_a?(Array) && row.size == args.first.size
|
|
285
|
+
raise DimensionMismatchError, "matrix rows must be rectangular"
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
args
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def matrix_arg(args)
|
|
292
|
+
matrix = args.fetch(0)
|
|
293
|
+
unless matrix.is_a?(Array) && matrix.first.is_a?(Array) &&
|
|
294
|
+
matrix.all? { |row| row.is_a?(Array) && row.size == matrix.first.size }
|
|
295
|
+
raise OperandTypeError.new("matrix", matrix)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
matrix
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
# Cofactor expansion over the exact ring: integers and rationals
|
|
302
|
+
# never convert through floats.
|
|
303
|
+
def determinant(rows)
|
|
304
|
+
size = rows.size
|
|
305
|
+
unless size == rows.first.size
|
|
306
|
+
raise DimensionMismatchError, "determinant needs a square matrix"
|
|
307
|
+
end
|
|
308
|
+
return rows[0][0] if size == 1
|
|
309
|
+
|
|
310
|
+
(0...size).sum do |column|
|
|
311
|
+
minor = rows.drop(1).map { |row| row.reject.with_index { |_, i| i == column } }
|
|
312
|
+
term = rows[0][column] * determinant(minor)
|
|
313
|
+
column.even? ? term : -term
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def vector_product(args)
|
|
318
|
+
left, right = args
|
|
319
|
+
require_numeric(left + right)
|
|
320
|
+
unless left.size == 3 && right.size == 3
|
|
321
|
+
raise DimensionMismatchError, "vector_product needs two 3-vectors"
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
[
|
|
325
|
+
left[1] * right[2] - left[2] * right[1],
|
|
326
|
+
left[2] * right[0] - left[0] * right[2],
|
|
327
|
+
left[0] * right[1] - left[1] * right[0],
|
|
328
|
+
]
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# nums1/based_integer: (base, digit string) — digits 0-9a-z, base
|
|
332
|
+
# 2-36 per the CD (10 and 16 ride the encodings).
|
|
333
|
+
def based_integer(args)
|
|
334
|
+
base, digits = args
|
|
335
|
+
unless base.is_a?(Integer) && base.between?(2, 36)
|
|
336
|
+
raise OperandTypeError.new("based_integer base", base)
|
|
337
|
+
end
|
|
338
|
+
unless digits.is_a?(String) && digits.match?(/\A[+-]?[0-9a-z]+\z/i)
|
|
339
|
+
raise OperandTypeError.new("based_integer digits", digits)
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
Integer(digits, base)
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# arith1/root: the inverse of power — exact when the operand is a
|
|
346
|
+
# perfect n-th power over the exact ring, float-contaminated
|
|
347
|
+
# otherwise (power's own negative-exponent discipline mirrored).
|
|
348
|
+
def root(args)
|
|
349
|
+
base, n = args
|
|
350
|
+
require_numeric(args)
|
|
351
|
+
unless n.is_a?(Integer) && n.positive?
|
|
352
|
+
raise OperandTypeError.new("root index", n)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
exact_root(base, n) || base.to_f**Rational(1, n)
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def exact_root(base, n)
|
|
359
|
+
if base.is_a?(Rational)
|
|
360
|
+
numerator = integer_root_floor(base.numerator, n)
|
|
361
|
+
denominator = integer_root_floor(base.denominator, n)
|
|
362
|
+
if numerator**n == base.numerator && denominator**n == base.denominator
|
|
363
|
+
Rational(numerator, denominator)
|
|
364
|
+
end
|
|
365
|
+
elsif base.is_a?(Integer)
|
|
366
|
+
root = integer_root_floor(base, n)
|
|
367
|
+
root if root**n == base
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# The integer floor of the n-th root by bisection under a
|
|
372
|
+
# bit-length bound; integers and rationals never convert through
|
|
373
|
+
# floats.
|
|
374
|
+
def integer_root_floor(value, n)
|
|
375
|
+
negative = value.negative?
|
|
376
|
+
magnitude = value.abs
|
|
377
|
+
return value if n == 1
|
|
378
|
+
return 0 if magnitude.zero?
|
|
379
|
+
|
|
380
|
+
high = 1 << ((magnitude.bit_length + n - 1) / n)
|
|
381
|
+
low = high >> 1
|
|
382
|
+
while low < high
|
|
383
|
+
mid = (low + high + 1) / 2
|
|
384
|
+
mid**n <= magnitude ? low = mid : high = mid - 1
|
|
385
|
+
end
|
|
386
|
+
negative ? -low : low
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
# arith1/sum and arith1/product: (domain, function) per the CD —
|
|
390
|
+
# the function applies elementwise over the enumerated domain and
|
|
391
|
+
# folds under the monoid identity. Infinite domains refuse typed
|
|
392
|
+
# (the CD permits them; enumeration of them does not terminate).
|
|
393
|
+
def aggregate(args, identity, operator)
|
|
394
|
+
domain, closure = args
|
|
395
|
+
values = apply_elementwise(closure, domain_elements(domain)) do |_element, result|
|
|
396
|
+
result
|
|
397
|
+
end
|
|
398
|
+
values.reduce(identity, operator)
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def domain_elements(domain)
|
|
402
|
+
case domain
|
|
403
|
+
when Range then domain.to_a
|
|
404
|
+
when Array
|
|
405
|
+
unless domain.all? { |element| element.is_a?(Numeric) }
|
|
406
|
+
raise OperandTypeError.new("summation domain", domain)
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
domain
|
|
410
|
+
when Interval then interval_elements(domain)
|
|
411
|
+
else raise OperandTypeError.new("summation domain", domain)
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
def interval_elements(interval)
|
|
416
|
+
unless interval.low.is_a?(Integer) && interval.high.is_a?(Integer)
|
|
417
|
+
raise OperandTypeError.new("interval bounds", interval)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
low = interval.low_closed ? interval.low : interval.low + 1
|
|
421
|
+
high = interval.high_closed ? interval.high : interval.high - 1
|
|
422
|
+
low > high ? [] : (low..high).to_a
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# relation1/approx: exact operands compare exactly; a float
|
|
426
|
+
# contaminates the comparison to the limited class under a
|
|
427
|
+
# documented relative tolerance of 10^-6 — loose enough for the
|
|
428
|
+
# CD's own example (approx(355/113, pi) holds), exact equality
|
|
429
|
+
# still exact.
|
|
430
|
+
APPROX_TOLERANCE = 1e-6
|
|
431
|
+
|
|
432
|
+
def approx(args)
|
|
433
|
+
left, right = args
|
|
434
|
+
require_numeric(args)
|
|
435
|
+
if left.is_a?(Float) || right.is_a?(Float)
|
|
436
|
+
fl = left.to_f
|
|
437
|
+
fr = right.to_f
|
|
438
|
+
scale = [fl.abs, fr.abs].max
|
|
439
|
+
return scale.zero? || (fl - fr).abs <= APPROX_TOLERANCE * scale
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
left == right
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# pmquant — the PM 1002 quantity/uncertainty constructors. The
|
|
446
|
+
# category is part of the value's identity; exact operands stay
|
|
447
|
+
# exact inside the constructed quantity.
|
|
448
|
+
def ustd(args)
|
|
449
|
+
value, magnitude = args
|
|
450
|
+
require_numeric(args)
|
|
451
|
+
Uncertain.standard(value, magnitude)
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
def uexp(args)
|
|
455
|
+
value, magnitude, factor, probability = args
|
|
456
|
+
require_numeric(args)
|
|
457
|
+
Uncertain.expanded(value, magnitude, factor, probability)
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
def ucov(args)
|
|
461
|
+
value, lower, upper = args
|
|
462
|
+
require_numeric([value, lower, upper])
|
|
463
|
+
probability = args[3]
|
|
464
|
+
require_numeric([probability]) if probability
|
|
465
|
+
Uncertain.coverage(value, lower, upper, probability,
|
|
466
|
+
args[4] || INTERVAL_KINDS.last)
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
def quantity_value(args)
|
|
470
|
+
quantity = args.fetch(0)
|
|
471
|
+
require_numeric([quantity]) unless quantity.is_a?(Uncertain)
|
|
472
|
+
quantity.is_a?(Uncertain) ? quantity.value : quantity
|
|
473
|
+
end
|
|
474
|
+
|
|
93
475
|
def compare_chain(args, operator)
|
|
94
476
|
require_numeric(args)
|
|
95
477
|
args.each_cons(2).all? { |a, b| a.public_send(operator, b) }
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "digest"
|
|
5
|
+
|
|
6
|
+
module Openmath
|
|
7
|
+
module Evaluation
|
|
8
|
+
# The portable bindings artifact: the shipped binding table plus the
|
|
9
|
+
# replay ledger as language-independent JSON, so Rust and Wasm
|
|
10
|
+
# runtimes can implement parse and evaluate against the same
|
|
11
|
+
# semantics and replay the same vectors (the plurimath#584 rs/wasm
|
|
12
|
+
# replay lanes). The export is deterministic — same table, same
|
|
13
|
+
# vectors, same bytes — and `scripts/export_portable.rb --check`
|
|
14
|
+
# gates that in CI.
|
|
15
|
+
module Portable
|
|
16
|
+
SCHEMA = "openmath-portable-bindings/1"
|
|
17
|
+
DATA_PATH = File.expand_path("../../../data/portable-bindings.json", __dir__)
|
|
18
|
+
VECTORS_PATH = File.expand_path("../../../data/replay-vectors.yml", __dir__)
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# Exports the artifact structure: the bindings with their
|
|
23
|
+
# operations and arities, the table checksum, and the replay
|
|
24
|
+
# vectors verbatim from the ledger.
|
|
25
|
+
def export
|
|
26
|
+
table = BindingTable.default
|
|
27
|
+
{
|
|
28
|
+
"schema" => SCHEMA,
|
|
29
|
+
"generator" => "openmath-ruby #{Openmath::VERSION}",
|
|
30
|
+
"table_checksum" => BindingTable.default_checksum,
|
|
31
|
+
"bindings" => table.bindings.sort_by(&:key).map do |binding|
|
|
32
|
+
{
|
|
33
|
+
"cd" => binding.cd,
|
|
34
|
+
"name" => binding.name,
|
|
35
|
+
"operation" => binding.operation.to_s,
|
|
36
|
+
"arity" => arity_spec(binding.operation),
|
|
37
|
+
}
|
|
38
|
+
end,
|
|
39
|
+
"vectors" => YAML.safe_load_file(VECTORS_PATH, aliases: false)
|
|
40
|
+
.fetch("vectors"),
|
|
41
|
+
}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Deterministic bytes: sorted keys, no whitespace variance.
|
|
45
|
+
def export_json
|
|
46
|
+
JSON.generate(export, sort_keys: true)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Writes the artifact; returns the bytes.
|
|
50
|
+
def write(path = DATA_PATH)
|
|
51
|
+
json = export_json
|
|
52
|
+
File.write(path, json + "\n")
|
|
53
|
+
json
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The determinism gate: the shipped artifact must equal a fresh
|
|
57
|
+
# export byte for byte.
|
|
58
|
+
def check(path = DATA_PATH)
|
|
59
|
+
export_json + "\n" == File.read(path)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Replays every ledger row through the evaluator: request XML in,
|
|
63
|
+
# expected value or refusal out. Returns the list of failures;
|
|
64
|
+
# empty when the artifact replays clean.
|
|
65
|
+
def replay(table: BindingTable.default, signatures: nil)
|
|
66
|
+
failures = []
|
|
67
|
+
export.fetch("vectors").each do |vector|
|
|
68
|
+
failures.concat(replay_vector(vector, table, signatures))
|
|
69
|
+
end
|
|
70
|
+
failures
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def replay_vector(vector, table, signatures)
|
|
74
|
+
object = Openmath.parse(vector.fetch("request"), version: 2)
|
|
75
|
+
result = Evaluator.evaluate(object, table: table,
|
|
76
|
+
variables: vector["variables"] || {},
|
|
77
|
+
signatures: signatures)
|
|
78
|
+
expected = vector["expected"]
|
|
79
|
+
return [] if expected &&
|
|
80
|
+
values_equal?(result, expected.key?("class") ? expected : expected["value"])
|
|
81
|
+
|
|
82
|
+
[replay_failure(vector, result)]
|
|
83
|
+
rescue Openmath::Evaluation::Error, ZeroDivisionError => e
|
|
84
|
+
refusal = vector["refusal"]
|
|
85
|
+
return [] if refusal && e.class.name.end_with?(refusal["error"])
|
|
86
|
+
|
|
87
|
+
[replay_failure(vector, e)]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def values_equal?(result, expected)
|
|
91
|
+
case expected
|
|
92
|
+
when Hash
|
|
93
|
+
result.class.name == expected["class"] &&
|
|
94
|
+
expected["fields"].all? do |field, value|
|
|
95
|
+
result.public_send(field).to_s == value.to_s
|
|
96
|
+
end
|
|
97
|
+
else
|
|
98
|
+
result == expected || result.to_s == expected.to_s
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def replay_failure(vector, result)
|
|
103
|
+
expected = vector.dig("expected", "value") || vector.dig("refusal", "error")
|
|
104
|
+
"replay failure #{vector.fetch("id")}: expected #{expected} — " \
|
|
105
|
+
"got #{result.inspect[0, 120]}"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def arity_spec(operation)
|
|
109
|
+
spec = Operations.arity(operation)
|
|
110
|
+
spec.end.nil? ? [spec.min, nil] : [spec.min, spec.max]
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|