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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +45 -5
  3. data/data/binding-table-1.yml +76 -0
  4. data/data/pmquant.ocd +89 -0
  5. data/data/pmquant.sts +60 -0
  6. data/data/portable-bindings.json +1 -0
  7. data/data/replay-vectors.yml +70 -0
  8. data/lib/openmath/base/omforeign.rb +18 -4
  9. data/lib/openmath/base/omobj.rb +7 -0
  10. data/lib/openmath/binary.rb +699 -0
  11. data/lib/openmath/canonical.rb +127 -0
  12. data/lib/openmath/cd/cds_namespace.rb +13 -0
  13. data/lib/openmath/cd/group_collection.rb +160 -0
  14. data/lib/openmath/cd/group_document.rb +50 -0
  15. data/lib/openmath/cd/namespace.rb +6 -0
  16. data/lib/openmath/cd/signature_document.rb +53 -0
  17. data/lib/openmath/cd/signatures.rb +63 -0
  18. data/lib/openmath/cd.rb +10 -0
  19. data/lib/openmath/common_elements.rb +9 -0
  20. data/lib/openmath/content_mathml.rb +443 -0
  21. data/lib/openmath/content_mathml_operators.rb +51 -0
  22. data/lib/openmath/evaluation/closure.rb +11 -0
  23. data/lib/openmath/evaluation/errors.rb +30 -0
  24. data/lib/openmath/evaluation/evaluator.rb +192 -32
  25. data/lib/openmath/evaluation/interval.rb +17 -0
  26. data/lib/openmath/evaluation/operations.rb +394 -12
  27. data/lib/openmath/evaluation/portable.rb +114 -0
  28. data/lib/openmath/evaluation/signature_checker.rb +173 -0
  29. data/lib/openmath/evaluation/stochastic.rb +113 -0
  30. data/lib/openmath/evaluation/uncertain.rb +64 -0
  31. data/lib/openmath/evaluation.rb +12 -2
  32. data/lib/openmath/foreign.rb +146 -0
  33. data/lib/openmath/tagged.rb +41 -0
  34. data/lib/openmath/v2.rb +1 -1
  35. data/lib/openmath/v3.rb +1 -1
  36. data/lib/openmath/version.rb +1 -1
  37. data/lib/openmath.rb +41 -3
  38. data/schemas/PROVENANCE.md +3 -0
  39. data/schemas/omcdgroup2.rnc +33 -0
  40. data/schemas/omcdgroup2.rng +86 -0
  41. metadata +26 -2
@@ -0,0 +1,443 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ module Openmath
6
+ # The Content MathML v4 encoding (the plurimath#35 re-parenting:
7
+ # Content MathML rides the openmath gem's content models, so the
8
+ # semantic layer is shared with the XML and binary encodings).
9
+ #
10
+ # The reader accepts the strict core — csymbol, apply, ci, cn, cs,
11
+ # lambda/bvar — and the non-strict operator elements of the MathML 4
12
+ # content operator table (plus, times, eq, ...), which transform
13
+ # exactly the way the standard's Strict Content MathML Transformation
14
+ # Algorithm does: element to csymbol (ContentMathMLOperators::TABLE),
15
+ # arguments to an apply. The writer always emits the strict form.
16
+ # Everything the semantic core cannot carry — presentation elements,
17
+ # qualifier elements, cn types with no OpenMath form, OMB and
18
+ # OMFOREIGN with no Content MathML form — refuses with a typed error.
19
+ module ContentMathML
20
+ # Malformed Content MathML: unknown elements, missing attributes,
21
+ # qualifiers and presentation markup outside the semantic core.
22
+ class Error < StandardError; end
23
+
24
+ NAMESPACE = "http://www.w3.org/1998/Math/MathML"
25
+
26
+ module_function
27
+
28
+ # Parses Content MathML into the versioned object model: the parsed
29
+ # object slots into an OMOBJ root, as the XML and binary readers do.
30
+ def parse(input, version: 2)
31
+ doc = Nokogiri.XML(input) { |config| config.norecover }
32
+ root = doc.root
33
+ raise Error, "no root element" unless root
34
+
35
+ hierarchy = version == 3 ? :V3 : :V2
36
+ Openmath.const_get(hierarchy).register_models!
37
+ omobj = Openmath.const_get(hierarchy).const_get(:Omobj).new
38
+ child = Reader.new(version: version).element(root)
39
+ name = child.class.mappings_for(:xml).element_name
40
+ attribute = Evaluation::Evaluator::CHILD_ATTRIBUTES[name]
41
+ raise Error, "child #{name} is not allowed in OMOBJ" if attribute.nil?
42
+
43
+ omobj.public_send(:"#{attribute}=", [child])
44
+ omobj.element_order ||= []
45
+ omobj.element_order << Lutaml::Xml::Element.new("Element", name, node_type: :element)
46
+ omobj
47
+ end
48
+
49
+ # Serializes the model (or XML text) to the strict Content MathML form.
50
+ def generate(object_or_xml, version: 2)
51
+ object = object_or_xml.is_a?(String) ? Openmath.parse(object_or_xml, version: version) : object_or_xml
52
+ Writer.new(object).document
53
+ end
54
+
55
+ def content_element(name, children = nil, attributes = nil)
56
+ attributes_arg = attributes && !attributes.empty? ? attributes : nil
57
+ if children.nil?
58
+ node = Nokogiri::XML::Node.new(name, Nokogiri::XML::Document.new)
59
+ attributes_arg&.each { |key, value| node[key] = value }
60
+ node
61
+ else
62
+ node = Nokogiri::XML::Node.new(name, children.first.document)
63
+ attributes_arg&.each { |key, value| node[key] = value }
64
+ children.each { |child| node.add_child(child) }
65
+ node
66
+ end
67
+ end
68
+
69
+ # The reader: one Content MathML element at a time.
70
+ class Reader
71
+ def initialize(version:)
72
+ @version = version
73
+ end
74
+
75
+ def hierarchy
76
+ @hierarchy ||= Openmath.const_get(@version == 3 ? :V3 : :V2)
77
+ end
78
+
79
+ def element(node)
80
+ name = node.name
81
+ case name
82
+ when "OMOBJ", "math" then single(node)
83
+ when "csymbol" then csymbol(node)
84
+ when "apply" then apply(node)
85
+ when "lambda" then lambda_node(node)
86
+ when "ci" then variable(node)
87
+ when "cn" then number(node)
88
+ when "cs" then string(node)
89
+ when ContentMathMLOperators::TABLE then operator(name, node)
90
+ else
91
+ raise Error, "unsupported Content MathML element #{name.inspect} " \
92
+ "(outside the strict semantic core)"
93
+ end
94
+ end
95
+
96
+ def single(node)
97
+ children = node.element_children
98
+ raise Error, "math without exactly one child" if children.empty?
99
+
100
+ # Non-strict container form: the operator element and its
101
+ # arguments sit side by side under the wrapper and form one
102
+ # application (the standard's transform of <plus/>x y</math>).
103
+ if children.size > 1
104
+ first = children.first.name
105
+ if ContentMathMLOperators::TABLE.key?(first)
106
+ application = model(:Oma)
107
+ slot(application, operator(first, children.first))
108
+ children.drop(1).each { |child| slot(application, element(child)) }
109
+ return application
110
+ elsif children.size > 1 && %w[apply csymbol].include?(first)
111
+ application = element(children.first)
112
+ raise Error, "math with #{children.size} children" unless application.is_a?(hierarchy.const_get(:Oma))
113
+
114
+ children.drop(1).each { |child| slot(application, element(child)) }
115
+ return application
116
+ end
117
+
118
+ raise Error, "math with #{children.size} children"
119
+ end
120
+
121
+ element(children.first)
122
+ end
123
+
124
+ def csymbol(node)
125
+ cd = node["cd"]
126
+ name = node["name"]
127
+ unless cd && name
128
+ raise Error, "csymbol without cd and name attributes"
129
+ end
130
+
131
+ model(:Oms).tap do |symbol|
132
+ symbol.cd = cd
133
+ symbol.name = name
134
+ symbol.cdbase = node["cdbase"] if node["cdbase"]
135
+ end
136
+ end
137
+
138
+ def apply(node)
139
+ children = node.element_children
140
+ if children.empty?
141
+ raise Error, "apply without children"
142
+ end
143
+
144
+ model(:Oma).tap do |application|
145
+ # Non-strict form: the operator element leads the apply and its
146
+ # arguments follow as siblings.
147
+ head = children.first
148
+ if ContentMathMLOperators::TABLE.key?(head.name)
149
+ slot(application, operator(head.name, head))
150
+ children.drop(1).each { |child| slot(application, element(child)) }
151
+ else
152
+ children.each { |child| slot(application, element(child)) }
153
+ end
154
+ end
155
+ end
156
+
157
+ # MathML 4 deprecates the non-strict bind form: lambda carries no
158
+ # head symbol, so the binding maps to OMBIND over fns1/lambda —
159
+ # the same construction the evaluator's lambda layer binds.
160
+ def lambda_node(node)
161
+ bvar = node.element_children.select { |child| child.name == "bvar" }
162
+ rest = node.element_children.reject { |child| child.name == "bvar" }
163
+ raise Error, "lambda without a bvar" if bvar.empty?
164
+ raise Error, "lambda without a body" if rest.empty?
165
+ raise Error, "lambda with more than one body" if rest.size > 1
166
+
167
+ binder = model(:Ombind)
168
+ slot(binder, lambda_symbol)
169
+ vars_container = model(:Ombvar)
170
+ bvar.each do |child|
171
+ variable = child.element_children.first
172
+ raise Error, "bvar without a variable" unless variable
173
+
174
+ slot(vars_container, element(variable))
175
+ end
176
+ slot(binder, vars_container)
177
+ slot(binder, element(rest.first))
178
+ binder
179
+ end
180
+
181
+ def lambda_symbol
182
+ model(:Oms).tap do |symbol|
183
+ symbol.cd = "fns1"
184
+ symbol.name = "lambda"
185
+ end
186
+ end
187
+
188
+ def variable(node)
189
+ text = node.text.strip
190
+ raise Error, "ci without a name" if text.empty?
191
+
192
+ model(:Omv).tap { |variable| variable.name = text }
193
+ end
194
+
195
+ def string(node)
196
+ model(:Omstr).tap { |string| string.value = [node.text] }
197
+ end
198
+
199
+ # cn: integer (base 10, or base N through nums1/based_integer),
200
+ # rational (type="rational" with a sep child), e-notation
201
+ # (type="e-notation" with a sep child: mantissa × 10^exponent).
202
+ def number(node)
203
+ type = node["type"] || "integer"
204
+ children = node.element_children
205
+ case type
206
+ when "integer" then integer(node, children)
207
+ when "rational" then rational(node, children)
208
+ when "e-notation" then e_notation(node, children)
209
+ when "real", "double", "hexdouble"
210
+ raise Error, "cn type #{type.inspect} has no exact OpenMath form " \
211
+ "(encode the value with an OMF in the XML encoding)"
212
+ else
213
+ raise Error, "unsupported cn type #{type.inspect}"
214
+ end
215
+ end
216
+
217
+ def integer(node, children)
218
+ unless children.empty?
219
+ raise Error, "cn integer with child elements"
220
+ end
221
+
222
+ text = node.text.strip
223
+ base = node["base"] ? Integer(node["base"], 10) : 10
224
+ if base == 10
225
+ model(:Omi).tap { |integer| integer.value = [text] }
226
+ else
227
+ model(:Oma).tap do |application|
228
+ slot(application, symbol("nums1", "based_integer"))
229
+ slot(application, integer_value(base.to_s))
230
+ slot(application, string_value(text))
231
+ end
232
+ end
233
+ end
234
+
235
+ def rational(node, children)
236
+ unless children.size == 1 && children.first.name == "sep"
237
+ raise Error, 'cn type="rational" needs exactly one sep child'
238
+ end
239
+
240
+ numerator, denominator = node.children.select(&:text?).map { |part| part.text.strip }.reject(&:empty?)
241
+ raise Error, "cn rational without two digits" unless numerator && denominator
242
+
243
+ model(:Oma).tap do |application|
244
+ slot(application, symbol("nums1", "rational"))
245
+ slot(application, integer_value(numerator))
246
+ slot(application, integer_value(denominator))
247
+ end
248
+ end
249
+
250
+ def e_notation(node, children)
251
+ unless children.size == 1 && children.first.name == "sep"
252
+ raise Error, 'cn type="e-notation" needs exactly one sep child'
253
+ end
254
+
255
+ mantissa, exponent = node.children.select(&:text?).map { |part| part.text.strip }.reject(&:empty?)
256
+ unless mantissa && exponent
257
+ raise Error, "cn e-notation without mantissa and exponent"
258
+ end
259
+
260
+ model(:Oma).tap do |application|
261
+ slot(application, symbol("arith1", "times"))
262
+ slot(application, real_literal(mantissa))
263
+ slot(application, power_of_ten(exponent))
264
+ end
265
+ end
266
+
267
+ def real_literal(text)
268
+ model(:Omf).tap { |float| float.dec = text }
269
+ end
270
+
271
+ def power_of_ten(exponent)
272
+ model(:Oma).tap do |application|
273
+ slot(application, symbol("arith1", "power"))
274
+ slot(application, integer_value("10"))
275
+ slot(application, integer_value(exponent))
276
+ end
277
+ end
278
+
279
+ def integer_value(text)
280
+ model(:Omi).tap { |integer| integer.value = [text] }
281
+ end
282
+
283
+ def string_value(text)
284
+ model(:Omstr).tap { |string| string.value = [text] }
285
+ end
286
+
287
+ def symbol(cd, name)
288
+ model(:Oms).tap do |oms|
289
+ oms.cd = cd
290
+ oms.name = name
291
+ end
292
+ end
293
+
294
+ def operator(name, node)
295
+ cd, symbol_name = ContentMathMLOperators::TABLE.fetch(name)
296
+ children = node.element_children
297
+ if children.empty?
298
+ # A nullary operator stands alone (a constant symbol).
299
+ symbol(cd, symbol_name)
300
+ else
301
+ model(:Oma).tap do |application|
302
+ slot(application, symbol(cd, symbol_name))
303
+ children.each { |child| slot(application, element(child)) }
304
+ end
305
+ end
306
+ end
307
+
308
+ def slot(container, child)
309
+ name = child.class.mappings_for(:xml).element_name
310
+ attribute = Evaluation::Evaluator::CHILD_ATTRIBUTES[name]
311
+ if attribute.nil? || !container.respond_to?("#{attribute}=")
312
+ raise Error, "child #{name} is not allowed in " \
313
+ "#{container.class.mappings_for(:xml).element_name}"
314
+ end
315
+
316
+ collection = container.public_send(attribute) || []
317
+ container.public_send(:"#{attribute}=", collection + [child])
318
+ order = container.element_order ||= []
319
+ order << Lutaml::Xml::Element.new("Element", name, node_type: :element)
320
+ end
321
+
322
+ def model(name)
323
+ hierarchy.register_models!
324
+ hierarchy.const_get(name).new
325
+ end
326
+ end
327
+
328
+ # The writer: the strict form only — csymbol, apply, ci, cn, cs,
329
+ # lambda/bvar. Constructs with no Content MathML form refuse typed.
330
+ class Writer
331
+ def initialize(root)
332
+ @root = root
333
+ end
334
+
335
+ def document
336
+ wrapper = Nokogiri::XML::Document.new
337
+ math = Nokogiri::XML::Node.new("math", wrapper)
338
+ math["xmlns"] = NAMESPACE
339
+ math.add_child(element(@root))
340
+ wrapper.root = math
341
+ math.to_xml(indent: 2)
342
+ end
343
+
344
+ def element(node)
345
+ name = node.class.mappings_for(:xml).element_name
346
+ case name
347
+ when "OMOBJ" then content_of(node)
348
+ when "OMS" then csymbol(node)
349
+ when "OMA" then apply(node)
350
+ when "OMV" then variable(node)
351
+ when "OMI" then integer(node)
352
+ when "OMF" then float(node)
353
+ when "OMSTR" then string(node)
354
+ when "OMBIND" then binding(node)
355
+ else
356
+ raise Error, "#{name} has no Content MathML form (the semantic " \
357
+ "core encodes OMS, OMA, OMV, OMI, OMF, OMSTR and " \
358
+ "OMBIND; encode others in the XML encoding)"
359
+ end
360
+ end
361
+
362
+ # An OMOBJ's single child is what gets encoded (the frame is the
363
+ # math wrapper, as the binary framing is).
364
+ def content_of(node)
365
+ child = Evaluation::Evaluator.ordered_children(node).first
366
+ raise Error, "empty OMOBJ" unless child
367
+
368
+ element(child)
369
+ end
370
+
371
+ def csymbol(node)
372
+ attributes = { "cd" => node.cd.to_s, "name" => node.name.to_s }
373
+ attributes["cdbase"] = node.cdbase if node.cdbase
374
+ ContentMathML.content_element("csymbol", nil, attributes)
375
+ end
376
+
377
+ def apply(node)
378
+ children = Evaluation::Evaluator.ordered_children(node)
379
+ raise Error, "application without children" if children.empty?
380
+
381
+ ContentMathML.content_element("apply", children.map { |child| element(child) })
382
+ end
383
+
384
+ def variable(node)
385
+ element_node = ContentMathML.content_element("ci", nil, nil)
386
+ element_node.content = node.name.to_s
387
+ element_node
388
+ end
389
+
390
+ def integer(node)
391
+ text = node.value.join.gsub(/\s+/, "")
392
+ element_node = ContentMathML.content_element("cn", nil, nil)
393
+ element_node.content = text
394
+ element_node
395
+ end
396
+
397
+ def float(node)
398
+ element_node = ContentMathML.content_element("cn", nil,
399
+ { "type" => "e-notation" })
400
+ raise Error, "hex OMF has no exact Content MathML form" if node.hex
401
+ mantissa = ContentMathML.content_element("mn", nil, nil)
402
+ mantissa.content = node.dec.to_s
403
+ exponent = ContentMathML.content_element("mn", nil, nil)
404
+ exponent.content = "0"
405
+ element_node.add_child(mantissa)
406
+ element_node.add_child(sep(element_node.document))
407
+ element_node.add_child(exponent)
408
+ element_node
409
+ end
410
+
411
+ def string(node)
412
+ element_node = ContentMathML.content_element("cs", nil, nil)
413
+ element_node.content = node.value.join
414
+ element_node
415
+ end
416
+
417
+ def binding(node)
418
+ head = node.oms_value&.first
419
+ unless head && head.cd == "fns1" && head.name == "lambda"
420
+ raise Error, "binding whose head is not fns1#lambda has no " \
421
+ "Content MathML form"
422
+ end
423
+
424
+ vars = node.ombvar_value&.first
425
+ raise Error, "binding without OMBVAR" unless vars
426
+
427
+ lambda_node = ContentMathML.content_element("lambda", nil, nil)
428
+ Evaluation::Evaluator.ordered_children(vars).each do |var|
429
+ lambda_node.add_child(ContentMathML.content_element("bvar", [element(var)]))
430
+ end
431
+ body = Evaluation::Evaluator.ordered_children(node).drop(2).first
432
+ raise Error, "binding without a body" unless body
433
+
434
+ lambda_node.add_child(element(body))
435
+ lambda_node
436
+ end
437
+
438
+ def sep(document)
439
+ Nokogiri::XML::Node.new("sep", document)
440
+ end
441
+ end
442
+ end
443
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ # The Content MathML operator table (MathML 4, the content operator
5
+ # classes): the non-strict operator elements that transform to
6
+ # csymbol + apply, with the OpenMath content dictionary each maps to.
7
+ # This is the element set the reader accepts beside the strict core;
8
+ # the writer always emits the strict form.
9
+ module ContentMathMLOperators
10
+ # element name => [cd, name]
11
+ TABLE = {
12
+ "plus" => %w[arith1 plus],
13
+ "times" => %w[arith1 times],
14
+ "minus" => %w[arith1 minus],
15
+ "unary_minus" => %w[arith1 unary_minus],
16
+ "divide" => %w[arith1 divided_by],
17
+ "power" => %w[arith1 power],
18
+ "root" => %w[arith1 root],
19
+ "abs" => %w[arith1 abs],
20
+ "max" => %w[arith1 max],
21
+ "min" => %w[arith1 min],
22
+ "sum" => %w[arith1 sum],
23
+ "product" => %w[arith1 product],
24
+ "rem" => %w[integer1 remainder],
25
+ "factorial" => %w[integer1 factorial],
26
+ "floor" => %w[rounding1 floor],
27
+ "ceiling" => %w[rounding1 ceiling],
28
+ "eq" => %w[relation1 eq],
29
+ "neq" => %w[relation1 neq],
30
+ "gt" => %w[relation1 gt],
31
+ "lt" => %w[relation1 lt],
32
+ "geq" => %w[relation1 geq],
33
+ "leq" => %w[relation1 leq],
34
+ "approx" => %w[relation1 approx],
35
+ "and" => %w[logic1 and],
36
+ "or" => %w[logic1 or],
37
+ "xor" => %w[logic1 xor],
38
+ "not" => %w[logic1 not],
39
+ "implies" => %w[logic1 implies],
40
+ "identity" => %w[fns1 identity],
41
+ "compose" => %w[fns1 left_compose],
42
+ "inverse" => %w[fns1 inverse],
43
+ "transpose" => %w[linalg1 transpose],
44
+ "determinant" => %w[linalg3 determinant],
45
+ "int" => %w[calculus1 int],
46
+ "diff" => %w[calculus1 diff],
47
+ "partialdiff" => %w[calculus1 partialdiff],
48
+ "limit" => %w[calculus1 limit],
49
+ }.freeze
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module Evaluation
5
+ # The value of fns1/lambda: the bound variable names, the body, the
6
+ # lexical environment captured at construction, and the table the
7
+ # body evaluates under. Application merges the arguments over the
8
+ # captured environment — lexically, never at the call site.
9
+ Closure = Struct.new(:names, :body, :environment, :table, keyword_init: true)
10
+ end
11
+ end
@@ -61,5 +61,35 @@ module Openmath
61
61
  super("binding table names unimplemented operation #{operation}")
62
62
  end
63
63
  end
64
+
65
+ # An OMR reference resolves to a target currently under evaluation.
66
+ class RecursionError < Error
67
+ attr_reader :name
68
+
69
+ def initialize(name)
70
+ @name = name
71
+ super("cyclic OMR reference to #{name.inspect}")
72
+ end
73
+ end
74
+
75
+ # Shapes contradict (the PM error extension name).
76
+ class DimensionMismatchError < Error
77
+ attr_reader :context
78
+
79
+ def initialize(context)
80
+ @context = context
81
+ super("DimensionMismatch: #{context}")
82
+ end
83
+ end
84
+
85
+ # An application contradicts the operator's STS signature.
86
+ class SignatureViolationError < Error
87
+ attr_reader :detail
88
+
89
+ def initialize(detail)
90
+ @detail = detail
91
+ super(detail)
92
+ end
93
+ end
64
94
  end
65
95
  end