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
@@ -6,17 +6,61 @@ module Openmath
6
6
  module Evaluation
7
7
  # The tree walk: literals evaluate to themselves, applications resolve
8
8
  # their operator symbol through the binding table and dispatch to the
9
- # named operation, error constructors evaluate to error values, and
10
- # everything outside the evaluated surface refuses with a typed error.
9
+ # named operation, error constructors evaluate to error values, OMR
10
+ # references resolve within their document scope, and everything
11
+ # outside the evaluated surface refuses with a typed error.
11
12
  module Evaluator
12
13
  module_function
13
14
 
15
+ # The per-walk state: the variable environment and binding table in
16
+ # effect, the optional STS signatures the walk checks against, the
17
+ # optional Monte-Carlo sampler (every Uncertain the walk produces
18
+ # passes through it), the id index of the document scope, and the
19
+ # reference bookkeeping (memoized results, in-progress targets).
20
+ Context = Struct.new(:variables, :table, :signatures, :sampler, :index,
21
+ :memo, :in_progress, keyword_init: true) do
22
+ def self.build(variables, table, root, signatures: nil, sampler: nil)
23
+ new(variables: variables, table: table, signatures: signatures,
24
+ sampler: sampler, index: scope_index(root), memo: {},
25
+ in_progress: [])
26
+ end
27
+
28
+ def with(variables:, table:)
29
+ Context.new(variables: variables, table: table,
30
+ signatures: signatures, sampler: sampler, index: index,
31
+ memo: memo, in_progress: in_progress)
32
+ end
33
+
34
+ def self.scope_index(root)
35
+ ids = {}
36
+ visit = ->(node) do
37
+ id = node.respond_to?(:id) ? node.id : nil
38
+ ids[id] = node if id
39
+ children_of(node).each { |child| visit.call(child) }
40
+ end
41
+ visit.call(root)
42
+ ids
43
+ end
44
+
45
+ def self.children_of(node)
46
+ return [] unless node.respond_to?(:element_order) && node.element_order
47
+
48
+ Evaluator.ordered_children(node)
49
+ end
50
+ end
51
+
14
52
  # Evaluates an OpenMath object under the variable bindings. Variables
15
53
  # bind by name (PM 1004's binding domain: numbers, booleans, strings).
16
- def evaluate(object, variables: {}, table: BindingTable.default)
54
+ # When STS signatures are supplied, applications check against their
55
+ # operator's signature and violations refuse typed.
56
+ def evaluate(object, variables: {}, table: BindingTable.default, signatures: nil)
57
+ walk(object, Context.build(variables, table, object, signatures: signatures))
58
+ end
59
+
60
+ def walk(object, context)
17
61
  case object
18
62
  when Openmath::V2::Omobj, Openmath::V3::Omobj
19
- single(object, variables, table)
63
+ single(object, context)
20
64
  when Openmath::V2::Omi, Openmath::V3::Omi
21
65
  integer_literal(object)
22
66
  when Openmath::V2::Omf, Openmath::V3::Omf
@@ -26,20 +70,27 @@ module Openmath
26
70
  when Openmath::V2::Omb, Openmath::V3::Omb
27
71
  require_base64(object)
28
72
  when Openmath::V2::Omv, Openmath::V3::Omv
29
- lookup_variable(object, variables)
73
+ lookup_variable(object, context.variables)
30
74
  when Openmath::V2::Oma, Openmath::V3::Oma
31
- apply(object, variables, table)
75
+ apply(object, context)
76
+ when Openmath::V2::Ombind, Openmath::V3::Ombind
77
+ bind(object, context)
78
+ when Openmath::V2::Omattr, Openmath::V3::Omattr
79
+ walk(attributed_object(object), context)
32
80
  when Openmath::V2::Ome, Openmath::V3::Ome
33
- error_value(object, variables, table)
81
+ error_value(object, context)
82
+ when Openmath::V2::Omr, Openmath::V3::Omr
83
+ resolve_reference(object, context)
34
84
  else
35
- raise UnsupportedExpressionError, element_name(object)
85
+ raise UnsupportedExpressionError, element_name(object)
36
86
  end
37
87
  end
38
88
 
39
89
  # The children of a container in document order: element_order names
40
90
  # each element as parsed; the per-type collections hold the values in
41
91
  # the same relative order, so a cursor per element name reconstructs
42
- # the sequence.
92
+ # the sequence. (lutaml-model's order tracking covers every content
93
+ # model as of 0.8.175 — see openmath#16.)
43
94
  CHILD_ATTRIBUTES = {
44
95
  "OMS" => :oms_value, "OMV" => :omv_value, "OMI" => :omi_value,
45
96
  "OMB" => :omb_value, "OMSTR" => :omstr_value, "OMF" => :omf_value,
@@ -51,7 +102,7 @@ raise UnsupportedExpressionError, element_name(object)
51
102
 
52
103
  def ordered_children(container)
53
104
  cursors = Hash.new(0)
54
- ordered = container.element_order.filter_map do |element|
105
+ container.element_order.filter_map do |element|
55
106
  attribute = CHILD_ATTRIBUTES[element.name]
56
107
  next unless attribute
57
108
 
@@ -60,26 +111,18 @@ raise UnsupportedExpressionError, element_name(object)
60
111
  cursors[element.name] += 1
61
112
  values[index]
62
113
  end
63
- # Mixed-content elements can drop out of the order tracking; append
64
- # any values the order walk did not consume, in collection order.
65
- CHILD_ATTRIBUTES.each do |name, attribute|
66
- values = container.public_send(attribute)
67
- consumed = cursors[name]
68
- ordered.concat(values.drop(consumed)) if values && values.size > consumed
69
- end
70
- ordered
71
114
  end
72
115
 
73
- def element_name(object)
74
- object.is_a?(Openmath::V2::Ombind) || object.is_a?(Openmath::V3::Ombind) ? "OMBIND" : object.class.name
75
- end
116
+ def element_name(object)
117
+ object.is_a?(Openmath::V2::Ombind) || object.is_a?(Openmath::V3::Ombind) ? "OMBIND" : object.class.name
118
+ end
76
119
 
77
- def single(omobj, variables, table)
120
+ def single(omobj, context)
78
121
  elements = ordered_children(omobj)
79
122
  raise UnsupportedExpressionError, "empty OMOBJ" if elements.empty?
80
123
  raise UnsupportedExpressionError, "OMOBJ with multiple children" if elements.size > 1
81
124
 
82
- evaluate(elements.first, variables: variables, table: table)
125
+ walk(elements.first, context)
83
126
  end
84
127
 
85
128
  def integer_literal(node)
@@ -89,7 +132,7 @@ def single(omobj, variables, table)
89
132
 
90
133
  def float_literal(node)
91
134
  return Float(node.dec) if node.dec
92
- return [node.hex].pack("H*").unpack1("G") if node.hex
135
+ return [node.hex].pack("H*").unpack1("G") if node.hex
93
136
 
94
137
  raise OperandTypeError.new("OMF", "neither dec nor hex")
95
138
  end
@@ -108,31 +151,148 @@ return [node.hex].pack("H*").unpack1("G") if node.hex
108
151
  raise UnboundSymbolError.new("(variable)", name)
109
152
  end
110
153
 
111
- def apply(oma, variables, table)
154
+ def apply(oma, context)
112
155
  children = ordered_children(oma)
113
156
  operator = children.first
114
- unless operator.is_a?(Openmath::V2::Oms) || operator.is_a?(Openmath::V3::Oms)
115
- raise UnsupportedExpressionError, "application without an OMS operator"
157
+ if operator.is_a?(Openmath::V2::Oms) || operator.is_a?(Openmath::V3::Oms)
158
+ apply_symbol(operator, children[1..], context)
159
+ else
160
+ closure = walk(operator, context)
161
+ unless closure.is_a?(Closure)
162
+ raise UnsupportedExpressionError,
163
+ "application of a #{closure.class} value"
164
+ end
165
+
166
+ apply_closure(closure, children[1..], context)
116
167
  end
168
+ end
117
169
 
118
- binding = table.lookup(operator.cd, operator.name)
170
+ def apply_symbol(operator, argument_nodes, context)
171
+ binding = context.table.lookup(operator.cd, operator.name)
119
172
  raise UnboundSymbolError.new(operator.cd, operator.name) unless binding
120
173
 
174
+ arguments = argument_nodes.map { |child| walk(child, context) }
175
+ check_signature(operator, arguments, context)
121
176
  implementation, arity_spec = Operations.implementation(binding.operation)
122
- arguments = children[1..].map { |child| evaluate(child, variables: variables, table: table) }
123
177
  unless arity_spec.cover?(arguments.size)
124
178
  raise ArityError.new(binding.operation, arity_spec, arguments.size)
125
179
  end
126
180
 
127
- implementation.call(arguments)
181
+ result = implementation.call(arguments)
182
+ return result unless context.sampler && result.is_a?(Uncertain)
183
+
184
+ context.sampler.call(result)
185
+ end
186
+
187
+ # Advisory by default: without supplied signatures nothing checks;
188
+ # with them, a signed operator's application refuses typed on any
189
+ # profile violation.
190
+ def check_signature(operator, arguments, context)
191
+ return unless context.signatures
192
+
193
+ signature = context.signatures.lookup(operator.cd, operator.name)
194
+ return unless signature
195
+
196
+ violations = SignatureChecker.violations(operator.cd, operator.name,
197
+ signature, arguments)
198
+ violations.each { |detail| raise SignatureViolationError, detail }
199
+ end
200
+
201
+ def apply_closure(closure, argument_nodes, context)
202
+ arguments = argument_nodes.map { |child| walk(child, context) }
203
+ unless closure.names.size == arguments.size
204
+ raise ArityError.new("lambda", closure.names.size..closure.names.size,
205
+ arguments.size)
206
+ end
207
+
208
+ environment = closure.environment.merge(closure.names.zip(arguments).to_h)
209
+ walk(closure.body, context.with(variables: environment, table: closure.table))
210
+ end
211
+
212
+ # fns1/lambda is the only binder in the evaluated surface; every
213
+ # other head refuses through the unbound-symbol discipline.
214
+ def bind(ombind, context)
215
+ head = ombind.oms_value.first
216
+ unless head && head.cd == "fns1" && head.name == "lambda"
217
+ raise UnboundSymbolError.new(head&.cd || "(binder)", head&.name || "(missing)")
218
+ end
219
+
220
+ ombvar = ombind.ombvar_value.first
221
+ raise UnsupportedExpressionError, "lambda without OMBVAR" unless ombvar
222
+
223
+ names = bound_variable_names(ombvar)
224
+ body = ordered_children(ombind).drop(2)
225
+ unless body.size == 1
226
+ raise UnsupportedExpressionError,
227
+ "lambda with #{body.size} bodies"
228
+ end
229
+
230
+ Closure.new(names: names, body: body.first,
231
+ environment: context.variables.dup, table: context.table)
232
+ end
233
+
234
+ # omvar = OMV | OMATTR(OMATP, OMV); attributed variables bind by
235
+ # their wrapped name, attributes carried on the pair node itself.
236
+ def bound_variable_names(ombvar)
237
+ ordered_children(ombvar).map { |child| variable_name(child) }
238
+ end
239
+
240
+ def variable_name(node)
241
+ variable = node
242
+ if node.is_a?(Openmath::V2::Omattr) || node.is_a?(Openmath::V3::Omattr)
243
+ variable = attributed_object(node)
244
+ end
245
+ unless variable.is_a?(Openmath::V2::Omv) || variable.is_a?(Openmath::V3::Omv)
246
+ raise UnsupportedExpressionError, "binding variable #{variable.class}"
247
+ end
248
+
249
+ variable.name
250
+ end
251
+
252
+ # OMATTR evaluates its attributed object; the attribution pairs ride
253
+ # along as data and never alter the evaluation.
254
+ def attributed_object(omattr)
255
+ attributed = ordered_children(omattr).last
256
+ if attributed.nil? || attributed.is_a?(Openmath::V2::Omatp) ||
257
+ attributed.is_a?(Openmath::V3::Omatp)
258
+ raise UnsupportedExpressionError, "attribution without an object"
259
+ end
260
+
261
+ attributed
262
+ end
263
+
264
+ # OMR stands for the object it references (the id within this
265
+ # document's scope). A shared subtree evaluates once per walk (the
266
+ # memo is keyed by the environment in effect); a reference to a
267
+ # target currently under evaluation is a cycle and refuses typed.
268
+ def resolve_reference(omr, context)
269
+ href = omr.href.to_s
270
+ name = href.sub(/\A#/, "")
271
+ unless href.start_with?("#") && !name.empty?
272
+ raise UnsupportedExpressionError, "OMR href #{href.inspect} is not a fragment reference"
273
+ end
274
+
275
+ target = context.index[name]
276
+ raise UnboundSymbolError.new("(reference)", name) unless target
277
+ raise RecursionError.new(name) if context.in_progress.include?(name)
278
+
279
+ memo_key = [name, context.variables.object_id]
280
+ return context.memo[memo_key] if context.memo.key?(memo_key)
281
+
282
+ context.in_progress << name
283
+ begin
284
+ context.memo[memo_key] = walk(target, context)
285
+ ensure
286
+ context.in_progress.delete(name)
287
+ end
128
288
  end
129
289
 
130
- def error_value(ome, variables, table)
290
+ def error_value(ome, context)
131
291
  symbol = ome.oms_value.first
132
292
  raise UnsupportedExpressionError, "error without a leading OMS" unless symbol
133
293
 
134
294
  content = ordered_children(ome).drop(1).map do |child|
135
- evaluate(child, variables: variables, table: table)
295
+ walk(child, context)
136
296
  end
137
297
  ErrorValue.new(symbol.cd, symbol.name, content)
138
298
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module Evaluation
5
+ # An interval1 value: the construction's exact bounds and open/closed
6
+ # flags. Ruby's Range cannot represent an open lower bound, so the
7
+ # interval constructions evaluate to this and membership asks it.
8
+ Interval = Struct.new(:low, :high, :low_closed, :high_closed,
9
+ keyword_init: true) do
10
+ def member?(value)
11
+ above_low = low_closed ? value >= low : value > low
12
+ below_high = high_closed ? value <= high : value < high
13
+ above_low && below_high
14
+ end
15
+ end
16
+ end
17
+ end