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
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module Evaluation
|
|
5
|
+
# The STS application checker: an application whose operator carries
|
|
6
|
+
# an sts/mapsto signature checks its evaluated arguments against the
|
|
7
|
+
# declared domains, and violations refuse typed naming the signature.
|
|
8
|
+
#
|
|
9
|
+
# The v1 profile applies only where the type language is decidable
|
|
10
|
+
# over the evaluated values: mapsto arity (a sts/CartProd domain is
|
|
11
|
+
# one argument position; a trailing sts/nary makes it variadic),
|
|
12
|
+
# setname domains (N, Z, Q, R, P), the algebra carriers (sts/nassoc,
|
|
13
|
+
# sts/assoc) whose carriers are numerical in practice, and
|
|
14
|
+
# sts/Object accepting anything. Constructs outside the profile —
|
|
15
|
+
# free type variables, unknown constructs — are advisory and never
|
|
16
|
+
# refuse.
|
|
17
|
+
module SignatureChecker
|
|
18
|
+
SETNAME_PREDICATES = {
|
|
19
|
+
"N" => ->(value) { value.is_a?(Integer) },
|
|
20
|
+
"Z" => ->(value) { value.is_a?(Integer) },
|
|
21
|
+
"Q" => ->(value) { value.is_a?(Rational) || value.is_a?(Integer) },
|
|
22
|
+
"R" => ->(value) { value.is_a?(Numeric) },
|
|
23
|
+
"P" => ->(value) { value.is_a?(Numeric) && value.positive? },
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
NUMERIC_CONSTRUCTS = %w[nassoc assoc].freeze
|
|
27
|
+
|
|
28
|
+
# The algebra structures the sts CD defines as carrier classes; a
|
|
29
|
+
# bare OMV domain naming one of these is numerical in practice.
|
|
30
|
+
CARRIER_NAMES = %w[
|
|
31
|
+
SemiGroup AbelianSemiGroup Monoid AbelianMonoid
|
|
32
|
+
Group AbelianGroup Ring CommutativeRing Field
|
|
33
|
+
OrderedSet OrderedMonoid OrderedGroup OrderedAbelianGroup
|
|
34
|
+
OrderedRing OrderedField
|
|
35
|
+
].freeze
|
|
36
|
+
|
|
37
|
+
module_function
|
|
38
|
+
|
|
39
|
+
# Returns the list of violations; empty when the application checks.
|
|
40
|
+
def violations(cd, name, signature_object, arguments)
|
|
41
|
+
domains = mapsto_domains(signature_object)
|
|
42
|
+
return [] unless domains
|
|
43
|
+
|
|
44
|
+
expanded = expand_domains(domains)
|
|
45
|
+
return [arity_violation(cd, name, expanded, arguments)] if arity_mismatch?(expanded, arguments)
|
|
46
|
+
|
|
47
|
+
arguments.each_with_index.filter_map do |argument, index|
|
|
48
|
+
domain = expanded[index]
|
|
49
|
+
next if domain.nil? || acceptable?(domain, argument)
|
|
50
|
+
|
|
51
|
+
domain_violation(cd, name, domain, index, argument)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def arity_mismatch?(domains, arguments)
|
|
56
|
+
return false if domains.include?(:variadic)
|
|
57
|
+
|
|
58
|
+
arguments.size != domains.size
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def arity_violation(cd, name, domains, arguments)
|
|
62
|
+
"signature violation for #{cd}##{name}: the signature declares " \
|
|
63
|
+
"#{domains.size} argument(s), the application has #{arguments.size}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def domain_violation(cd, name, domain, index, argument)
|
|
67
|
+
"signature violation for #{cd}##{name}: argument #{index + 1} " \
|
|
68
|
+
"(#{argument.inspect}) is not in the domain #{describe(domain)}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def acceptable?(domain, argument)
|
|
72
|
+
case domain
|
|
73
|
+
when V2::Oms, V3::Oms
|
|
74
|
+
if domain.cd.start_with?("setname")
|
|
75
|
+
predicate = SETNAME_PREDICATES[domain.name]
|
|
76
|
+
predicate ? predicate.call(argument) : true
|
|
77
|
+
else
|
|
78
|
+
true
|
|
79
|
+
end
|
|
80
|
+
when V2::Oma, V3::Oma
|
|
81
|
+
algebra_carrier?(domain) ? argument.is_a?(Numeric) : true
|
|
82
|
+
when V2::Omv, V3::Omv
|
|
83
|
+
CARRIER_NAMES.include?(domain.name) ? argument.is_a?(Numeric) : true
|
|
84
|
+
else
|
|
85
|
+
true
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def algebra_carrier?(domain)
|
|
90
|
+
head = children(domain).first
|
|
91
|
+
(head.is_a?(V2::Oms) || head.is_a?(V3::Oms)) &&
|
|
92
|
+
head.cd == "sts" && NUMERIC_CONSTRUCTS.include?(head.name)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def mapsto_domains(signature_object)
|
|
96
|
+
# The stored type is the OMOBJ wrapper around the mapsto application.
|
|
97
|
+
content = children(signature_object).first
|
|
98
|
+
if content.is_a?(V2::Omobj) || content.is_a?(V3::Omobj)
|
|
99
|
+
content = children(content).first
|
|
100
|
+
end
|
|
101
|
+
parts = children(content)
|
|
102
|
+
head = parts.first
|
|
103
|
+
return nil unless head.is_a?(V2::Oms) || head.is_a?(V3::Oms)
|
|
104
|
+
return nil unless head.cd == "sts" && head.name == "mapsto"
|
|
105
|
+
|
|
106
|
+
parts[1..]
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# A sts/CartProd domain is one argument position carrying its
|
|
110
|
+
# subdomains; a sts/nary domain makes the arity variadic. A bare
|
|
111
|
+
# OMV domain's FIRST occurrence is an STS type parameter (lt's
|
|
112
|
+
# leading OrderedSets): it introduces the carrier and occupies no
|
|
113
|
+
# value slot; later occurrences are value positions of that type.
|
|
114
|
+
def expand_domains(domains)
|
|
115
|
+
introduced = []
|
|
116
|
+
domains.filter_map do |domain|
|
|
117
|
+
if oms_head?(domain, "CartProd")
|
|
118
|
+
cartprod_domain(domain)
|
|
119
|
+
elsif oms_head?(domain, "nary")
|
|
120
|
+
:variadic
|
|
121
|
+
elsif domain.is_a?(V2::Omv) || domain.is_a?(V3::Omv)
|
|
122
|
+
if introduced.include?(domain.name)
|
|
123
|
+
domain
|
|
124
|
+
else
|
|
125
|
+
introduced << domain.name
|
|
126
|
+
nil
|
|
127
|
+
end
|
|
128
|
+
else
|
|
129
|
+
introduced << carrier_name(domain) if carrier_name(domain)
|
|
130
|
+
domain
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# The carrier class an algebra constructor introduces (nassoc(X) → X).
|
|
136
|
+
def carrier_name(domain)
|
|
137
|
+
return nil unless algebra_carrier?(domain)
|
|
138
|
+
|
|
139
|
+
carrier = children(domain)[1]
|
|
140
|
+
return nil unless carrier.is_a?(V2::Omv) || carrier.is_a?(V3::Omv)
|
|
141
|
+
|
|
142
|
+
carrier.name
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def cartprod_domain(domain)
|
|
146
|
+
children(domain)[1]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def oms_head?(domain, name)
|
|
150
|
+
head = children(domain).first
|
|
151
|
+
(head.is_a?(V2::Oms) || head.is_a?(V3::Oms)) && head.cd == "sts" &&
|
|
152
|
+
head.name == name
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def describe(domain)
|
|
156
|
+
case domain
|
|
157
|
+
when V2::Oms, V3::Oms
|
|
158
|
+
domain.cd == "sts" ? "sts##{domain.name}" : "#{domain.cd}##{domain.name}"
|
|
159
|
+
when V2::Omv, V3::Omv
|
|
160
|
+
"carrier #{domain.name}"
|
|
161
|
+
when V2::Oma, V3::Oma
|
|
162
|
+
"an STS construct"
|
|
163
|
+
else
|
|
164
|
+
domain.to_s
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def children(object)
|
|
169
|
+
Evaluation::Evaluator.ordered_children(object)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module Evaluation
|
|
5
|
+
# Deterministic Monte-Carlo evaluation over uncertainty-carrying
|
|
6
|
+
# objects (PM 1002 quantities, JCGM 101's propagation): each Uncertain
|
|
7
|
+
# value in the walk draws from its category's documented distribution
|
|
8
|
+
# under a seeded generator, and the final state — the generator name,
|
|
9
|
+
# the seed, the sample count, the samples, and the sample mean and
|
|
10
|
+
# standard deviation — rides the result, so a corpus row reproduces
|
|
11
|
+
# the run byte for byte. Same seed, same samples, always.
|
|
12
|
+
module Stochastic
|
|
13
|
+
GENERATORS = %w[mt19937].freeze
|
|
14
|
+
|
|
15
|
+
Result = Struct.new(:value, :samples, :mean, :standard_deviation,
|
|
16
|
+
:generator, :seed, :count, keyword_init: true)
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
# Evaluates the object with every Uncertain value sampled +count+
|
|
21
|
+
# times under the seeded generator. The nominal walk runs once
|
|
22
|
+
# (no sampling) for the carried value.
|
|
23
|
+
def evaluate(object, samples:, seed:, generator: "mt19937",
|
|
24
|
+
variables: {}, table: BindingTable.default)
|
|
25
|
+
unless GENERATORS.include?(generator)
|
|
26
|
+
raise UnsupportedExpressionError, "unknown generator #{generator.inspect}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
object = Openmath.parse(object, version: 2) if object.is_a?(String)
|
|
30
|
+
nominal = Evaluator.evaluate(object, variables: variables, table: table)
|
|
31
|
+
random = Random.new(seed)
|
|
32
|
+
draws = Array.new(samples) { walk_sampled(object, variables, table, random).first }
|
|
33
|
+
Result.new(
|
|
34
|
+
value: nominal, samples: draws,
|
|
35
|
+
mean: draws.reduce(0.0, :+) / draws.size,
|
|
36
|
+
standard_deviation: standard_deviation(draws),
|
|
37
|
+
generator: generator, seed: seed, count: samples,
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# A sampled walk: the sampler rides the walk context, so every
|
|
42
|
+
# application producing an Uncertain value draws once, in walk
|
|
43
|
+
# order — two uncertain leaves in one object draw independently.
|
|
44
|
+
# Returns [sampled value, leaf draws].
|
|
45
|
+
def walk_sampled(object, variables, table, random)
|
|
46
|
+
draws = []
|
|
47
|
+
context = Evaluator::Context.new(
|
|
48
|
+
variables: variables, table: table,
|
|
49
|
+
index: Evaluator::Context.scope_index(object),
|
|
50
|
+
memo: {}, in_progress: [],
|
|
51
|
+
sampler: lambda do |uncertain|
|
|
52
|
+
sampled = draw(uncertain, random)
|
|
53
|
+
draws << sampled
|
|
54
|
+
sampled
|
|
55
|
+
end,
|
|
56
|
+
)
|
|
57
|
+
[Evaluator.walk(object, context), draws]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The documented per-category distributions (JCGM 101's propagation
|
|
61
|
+
# models): standard — Normal(value, magnitude); expanded —
|
|
62
|
+
# Normal(value, magnitude / coverage_factor); coverage —
|
|
63
|
+
# Uniform(lower, upper) for the symmetric kind, Triangular(value,
|
|
64
|
+
# lower, upper) for the shortest kind.
|
|
65
|
+
def draw(uncertain, random)
|
|
66
|
+
case uncertain.category
|
|
67
|
+
when UNCERTAIN_STANDARD, UNCERTAIN_EXPANDED
|
|
68
|
+
sigma = if uncertain.category == UNCERTAIN_STANDARD
|
|
69
|
+
uncertain.magnitude
|
|
70
|
+
else
|
|
71
|
+
uncertain.magnitude / uncertain.coverage_factor
|
|
72
|
+
end
|
|
73
|
+
normal(uncertain.value, sigma, random)
|
|
74
|
+
else
|
|
75
|
+
case uncertain.interval_kind
|
|
76
|
+
when "symmetric"
|
|
77
|
+
uniform(uncertain.lower, uncertain.upper, random)
|
|
78
|
+
else
|
|
79
|
+
triangular(uncertain.value, uncertain.lower, uncertain.upper, random)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def uniform(low, high, random)
|
|
85
|
+
low + random.rand * (high - low)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def triangular(peak, low, high, random)
|
|
89
|
+
first = random.rand
|
|
90
|
+
second = random.rand
|
|
91
|
+
midpoint = (peak - low) / (high - low).to_f
|
|
92
|
+
first < midpoint ? low + Math.sqrt(first * midpoint) * (high - low) : high - Math.sqrt((1 - first) * (1 - midpoint)) * (high - low)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Box-Muller over two uniforms; the magnitude scales by the
|
|
96
|
+
# nominal's exactness (exact nominals sample into floats — the
|
|
97
|
+
# sampled value is a realization, not the report).
|
|
98
|
+
def normal(mean, sigma, random)
|
|
99
|
+
u1 = random.rand
|
|
100
|
+
u2 = random.rand
|
|
101
|
+
return mean if u1.zero?
|
|
102
|
+
|
|
103
|
+
mean + sigma * Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math::PI * u2)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def standard_deviation(draws)
|
|
107
|
+
mean = draws.reduce(0.0, :+) / draws.size
|
|
108
|
+
variance = draws.sum { |draw| (draw - mean)**2 } / [draws.size - 1, 1].max
|
|
109
|
+
Math.sqrt(variance)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module Evaluation
|
|
5
|
+
# The coverage-interval kinds (PM 1002 clause 6): shortest and
|
|
6
|
+
# probabilistically-symmetric (spelled symmetric in AsciiMath).
|
|
7
|
+
INTERVAL_KINDS = %w[shortest symmetric].freeze
|
|
8
|
+
|
|
9
|
+
# A PM 1002 quantity: the reported value plus the uncertainty of its
|
|
10
|
+
# category. The category is part of the value's identity — standard,
|
|
11
|
+
# expanded and coverage-interval quantities with the same numbers are
|
|
12
|
+
# different values. Exact operands keep the exact classes; the
|
|
13
|
+
# constructor refusals follow PM 1002 clause 6 (coverage factors are
|
|
14
|
+
# positive unsigned, probabilities lie strictly inside (0, 1) by
|
|
15
|
+
# exact comparison, interval kinds are shortest or symmetric).
|
|
16
|
+
UNCERTAIN_STANDARD = :standard
|
|
17
|
+
UNCERTAIN_EXPANDED = :expanded
|
|
18
|
+
UNCERTAIN_COVERAGE = :coverage
|
|
19
|
+
|
|
20
|
+
Uncertain = Struct.new(:value, :category, :magnitude, :coverage_factor,
|
|
21
|
+
:coverage_probability, :lower, :upper,
|
|
22
|
+
:interval_kind, keyword_init: true) do
|
|
23
|
+
def self.standard(value, magnitude)
|
|
24
|
+
unless magnitude.is_a?(Numeric) && magnitude >= 0
|
|
25
|
+
raise OperandTypeError.new("standard uncertainty magnitude", magnitude)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
new(value: value, category: UNCERTAIN_STANDARD, magnitude: magnitude)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.expanded(value, magnitude, coverage_factor, coverage_probability = nil)
|
|
32
|
+
unless coverage_factor.is_a?(Numeric) && coverage_factor.positive?
|
|
33
|
+
raise OperandTypeError.new("coverage factor", coverage_factor)
|
|
34
|
+
end
|
|
35
|
+
require_probability(coverage_probability) if coverage_probability
|
|
36
|
+
|
|
37
|
+
new(value: value, category: UNCERTAIN_EXPANDED, magnitude: magnitude,
|
|
38
|
+
coverage_factor: coverage_factor,
|
|
39
|
+
coverage_probability: coverage_probability)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.coverage(value, lower, upper, coverage_probability = nil,
|
|
43
|
+
interval_kind = "symmetric")
|
|
44
|
+
if lower.is_a?(Numeric) && upper.is_a?(Numeric) && lower > upper
|
|
45
|
+
raise OperandTypeError.new("coverage interval", [lower, upper])
|
|
46
|
+
end
|
|
47
|
+
require_probability(coverage_probability) if coverage_probability
|
|
48
|
+
unless INTERVAL_KINDS.include?(interval_kind)
|
|
49
|
+
raise OperandTypeError.new("interval kind", interval_kind)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
new(value: value, category: UNCERTAIN_COVERAGE, lower: lower,
|
|
53
|
+
upper: upper, coverage_probability: coverage_probability,
|
|
54
|
+
interval_kind: interval_kind)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.require_probability(probability)
|
|
58
|
+
return if probability.is_a?(Numeric) && probability > 0 && probability < 1
|
|
59
|
+
|
|
60
|
+
raise OperandTypeError.new("coverage probability", probability)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/openmath/evaluation.rb
CHANGED
|
@@ -12,18 +12,28 @@ module Openmath
|
|
|
12
12
|
autoload :ArityError, "openmath/evaluation/errors"
|
|
13
13
|
autoload :OperandTypeError, "openmath/evaluation/errors"
|
|
14
14
|
autoload :UnimplementedOperationError, "openmath/evaluation/errors"
|
|
15
|
+
autoload :RecursionError, "openmath/evaluation/errors"
|
|
16
|
+
autoload :DimensionMismatchError, "openmath/evaluation/errors"
|
|
17
|
+
autoload :SignatureViolationError, "openmath/evaluation/errors"
|
|
18
|
+
autoload :SignatureChecker, "openmath/evaluation/signature_checker"
|
|
15
19
|
autoload :BindingTable, "openmath/evaluation/binding_table"
|
|
16
20
|
autoload :Operations, "openmath/evaluation/operations"
|
|
17
21
|
autoload :ErrorValue, "openmath/evaluation/error_value"
|
|
22
|
+
autoload :Closure, "openmath/evaluation/closure"
|
|
23
|
+
autoload :Interval, "openmath/evaluation/interval"
|
|
24
|
+
autoload :Uncertain, "openmath/evaluation/uncertain"
|
|
25
|
+
autoload :Stochastic, "openmath/evaluation/stochastic"
|
|
26
|
+
autoload :Portable, "openmath/evaluation/portable"
|
|
18
27
|
autoload :Evaluator, "openmath/evaluation/evaluator"
|
|
19
28
|
|
|
20
29
|
module_function
|
|
21
30
|
|
|
22
31
|
# Evaluates an OpenMath object (or XML text) under the variable
|
|
23
32
|
# bindings. The binding table is the shipped artifact unless given.
|
|
24
|
-
def evaluate(object, variables: {}, table: BindingTable.default, version: nil)
|
|
33
|
+
def evaluate(object, variables: {}, table: BindingTable.default, version: nil, signatures: nil)
|
|
25
34
|
object = Openmath.parse(object, version: version || 2) if object.is_a?(String)
|
|
26
|
-
Evaluator.evaluate(object, variables: variables, table: table
|
|
35
|
+
Evaluator.evaluate(object, variables: variables, table: table,
|
|
36
|
+
signatures: signatures)
|
|
27
37
|
end
|
|
28
38
|
end
|
|
29
39
|
end
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "nokogiri"
|
|
4
|
+
|
|
5
|
+
module Openmath
|
|
6
|
+
# Foreign-markup capture for OMFOREIGN (the schema's notOM content).
|
|
7
|
+
#
|
|
8
|
+
# lutaml-model's mapping DSL has no representation for arbitrary-markup
|
|
9
|
+
# children, so capture rides a source-side pass: after the typed parse,
|
|
10
|
+
# the document is re-walked with nokogiri in parallel with the model
|
|
11
|
+
# tree, and every OMFOREIGN's foreign children (elements outside the
|
|
12
|
+
# OpenMath namespace) are serialized verbatim into the instance's
|
|
13
|
+
# +notom_value+, with +notom_positions+ recording how many OpenMath
|
|
14
|
+
# children precede each fragment. Serialization writes the fragments
|
|
15
|
+
# back at those positions through Base::Omobj's to_xml override.
|
|
16
|
+
module Foreign
|
|
17
|
+
OPENMATH_NS = "http://www.openmath.org/OpenMath"
|
|
18
|
+
SAVE_OPTIONS = Nokogiri::XML::Node::SaveOptions::AS_XML
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# True when any OMFOREIGN in the object carries captured fragments.
|
|
23
|
+
def contained?(object)
|
|
24
|
+
children(object).any? do |child|
|
|
25
|
+
next true if omforeign?(child) && Array(child.notom_value).any?
|
|
26
|
+
|
|
27
|
+
contained?(child)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Walks the model object alongside +source+ and fills every
|
|
32
|
+
# Omforeign's notom_value/notom_positions from the serialized foreign
|
|
33
|
+
# children in document order. Nested OMFOREIGN objects descend.
|
|
34
|
+
def capture(object, source)
|
|
35
|
+
root = Nokogiri.XML(source).root
|
|
36
|
+
return object unless root
|
|
37
|
+
|
|
38
|
+
capture_node(object, root)
|
|
39
|
+
object
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Inserts the captured fragments back into a serialized document,
|
|
43
|
+
# locating each OMFOREIGN node by the same positional pairing capture
|
|
44
|
+
# used, and re-serializes.
|
|
45
|
+
def inject_tree(object, xml_string)
|
|
46
|
+
doc = Nokogiri.XML(xml_string)
|
|
47
|
+
place(object, doc.root)
|
|
48
|
+
root_serialize(doc)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Appends the fragments of a standalone Omforeign serialization.
|
|
52
|
+
def inject(xml, fragments)
|
|
53
|
+
doc = Nokogiri.XML(xml)
|
|
54
|
+
target = doc.root
|
|
55
|
+
return xml.to_s unless target
|
|
56
|
+
|
|
57
|
+
fragments.each do |fragment|
|
|
58
|
+
node = Nokogiri.XML(fragment).root
|
|
59
|
+
target.add_child(node) if node
|
|
60
|
+
end
|
|
61
|
+
root_serialize(doc)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def capture_node(model, node)
|
|
65
|
+
om_children = 0
|
|
66
|
+
foreign = []
|
|
67
|
+
positions = []
|
|
68
|
+
cursor = child_cursor(model)
|
|
69
|
+
|
|
70
|
+
node.element_children.each do |child|
|
|
71
|
+
model_child = cursor.call(child.name)
|
|
72
|
+
om_children += 1 if model_child || openmath_child?(child)
|
|
73
|
+
if model_child
|
|
74
|
+
capture_node(model_child, child)
|
|
75
|
+
elsif !openmath_child?(child)
|
|
76
|
+
foreign << child
|
|
77
|
+
positions << om_children
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
return unless omforeign?(model)
|
|
82
|
+
|
|
83
|
+
model.notom_value = foreign.map { |n| n.to_xml(save_with: SAVE_OPTIONS) }
|
|
84
|
+
model.notom_positions = positions
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def place(model, node)
|
|
88
|
+
cursor = child_cursor(model)
|
|
89
|
+
|
|
90
|
+
node.element_children.each do |child|
|
|
91
|
+
model_child = cursor.call(child.name)
|
|
92
|
+
next unless model_child
|
|
93
|
+
|
|
94
|
+
place(model_child, child)
|
|
95
|
+
next unless omforeign?(model_child)
|
|
96
|
+
|
|
97
|
+
insert_fragments(child, model_child)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def insert_fragments(node, model)
|
|
102
|
+
fragments = Array(model.notom_value)
|
|
103
|
+
positions = Array(model.notom_positions)
|
|
104
|
+
siblings = node.element_children.select { |c| openmath_child?(c) }
|
|
105
|
+
fragments.each_with_index do |fragment, i|
|
|
106
|
+
fragment_node = Nokogiri.XML(fragment).root
|
|
107
|
+
next unless fragment_node
|
|
108
|
+
|
|
109
|
+
anchor = siblings[positions[i]] if positions[i]
|
|
110
|
+
anchor ? anchor.add_previous_sibling(fragment_node) : node.add_child(fragment_node)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def omforeign?(model)
|
|
115
|
+
model.is_a?(V2::Omforeign) || model.is_a?(V3::Omforeign)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def openmath_child?(node)
|
|
119
|
+
(node.namespace&.href || OPENMATH_NS) == OPENMATH_NS
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Document-order model child of +name+ matched positionally against
|
|
123
|
+
# the walk (the evaluator's per-name cursor contract).
|
|
124
|
+
def child_cursor(model)
|
|
125
|
+
seen = Hash.new(0)
|
|
126
|
+
lambda do |name|
|
|
127
|
+
attribute = Evaluation::Evaluator::CHILD_ATTRIBUTES[name]
|
|
128
|
+
return nil unless attribute
|
|
129
|
+
|
|
130
|
+
values = Array(model.public_send(attribute))
|
|
131
|
+
index = seen[name]
|
|
132
|
+
seen[name] += 1
|
|
133
|
+
values[index]
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def children(object)
|
|
138
|
+
Evaluation::Evaluator::CHILD_ATTRIBUTES.values
|
|
139
|
+
.flat_map { |attribute| Array(object.public_send(attribute)) }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def root_serialize(doc)
|
|
143
|
+
doc.root.to_xml(save_with: SAVE_OPTIONS)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
# The canonical tagged form of a typed model (the testsuite's
|
|
5
|
+
# tree-parity contract): every captured value is a tagged node with
|
|
6
|
+
# exactly one tag, so two values compare equal only when the Ruby
|
|
7
|
+
# values are the same in every way that matters. Models capture
|
|
8
|
+
# through their declared attributes and types, never through inspect;
|
|
9
|
+
# nil fields are left out; numbers keep their text; child order is
|
|
10
|
+
# never sorted. Tag keys are plain strings so the YAML round trip
|
|
11
|
+
# stays within safe_load's portable classes.
|
|
12
|
+
module Tagged
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def node(object)
|
|
16
|
+
case object
|
|
17
|
+
when Lutaml::Model::Serializable then model(object)
|
|
18
|
+
when Hash
|
|
19
|
+
{ "hash" => object.map { |key, value| [node(key), node(value)] } }
|
|
20
|
+
when Array then { "array" => object.map { |item| node(item) } }
|
|
21
|
+
when Symbol then { "sym" => object.to_s }
|
|
22
|
+
when Integer then { "int" => object.to_s }
|
|
23
|
+
when Float then { "float" => object.inspect }
|
|
24
|
+
when true, false then { "bool" => object }
|
|
25
|
+
when nil then { "nil" => true }
|
|
26
|
+
else { "str" => object.to_s }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def model(object)
|
|
31
|
+
fields = object.class.attributes.filter_map do |name, attribute|
|
|
32
|
+
value = object.public_send(name)
|
|
33
|
+
next if value.nil?
|
|
34
|
+
next if attribute.collection? && Array(value).empty?
|
|
35
|
+
|
|
36
|
+
[name.to_s, node(value)]
|
|
37
|
+
end
|
|
38
|
+
{ "node" => object.class.name, "fields" => fields }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/openmath/v2.rb
CHANGED
data/lib/openmath/v3.rb
CHANGED
data/lib/openmath/version.rb
CHANGED
data/lib/openmath.rb
CHANGED
|
@@ -10,6 +10,12 @@ module Openmath
|
|
|
10
10
|
autoload :V3, "openmath/v3"
|
|
11
11
|
autoload :Validation, "openmath/validation"
|
|
12
12
|
autoload :Evaluation, "openmath/evaluation"
|
|
13
|
+
autoload :Foreign, "openmath/foreign"
|
|
14
|
+
autoload :Canonical, "openmath/canonical"
|
|
15
|
+
autoload :Binary, "openmath/binary"
|
|
16
|
+
autoload :ContentMathML, "openmath/content_mathml"
|
|
17
|
+
autoload :ContentMathMLOperators, "openmath/content_mathml_operators"
|
|
18
|
+
autoload :Tagged, "openmath/tagged"
|
|
13
19
|
autoload :CD, "openmath/cd"
|
|
14
20
|
|
|
15
21
|
module Base
|
|
@@ -58,12 +64,44 @@ module Openmath
|
|
|
58
64
|
CD.parse(input)
|
|
59
65
|
end
|
|
60
66
|
|
|
67
|
+
# Parses a CDGroup document into Openmath::CD::GroupDocument.
|
|
68
|
+
def parse_cdgroup(input)
|
|
69
|
+
CD::Group.parse(input)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def validate_cdgroup!(xml)
|
|
73
|
+
CD::Group.validate!(xml)
|
|
74
|
+
end
|
|
75
|
+
|
|
61
76
|
# Evaluates an OpenMath object (or XML text) under the variable bindings.
|
|
62
|
-
|
|
63
|
-
|
|
77
|
+
# When STS signatures are supplied, applications check against their
|
|
78
|
+
# operator's signature and violations refuse typed.
|
|
79
|
+
def evaluate(object, variables: {}, table: nil, version: nil, signatures: nil)
|
|
80
|
+
args = { variables: variables, version: version, signatures: signatures }.compact
|
|
64
81
|
args[:table] = table if table
|
|
65
82
|
Evaluation.evaluate(object, **args)
|
|
66
83
|
end
|
|
67
84
|
|
|
68
|
-
|
|
85
|
+
# Deterministic Monte-Carlo evaluation over uncertainty-carrying
|
|
86
|
+
# objects: every pmquant quantity draws from its category's documented
|
|
87
|
+
# distribution under the seeded generator; the result carries the
|
|
88
|
+
# generator, seed, sample count, samples, mean and standard deviation,
|
|
89
|
+
# so the same seed reproduces the same run.
|
|
90
|
+
def evaluate_stochastic(object, samples:, seed:, generator: "mt19937",
|
|
91
|
+
variables: {}, table: nil, version: nil)
|
|
92
|
+
args = { variables: variables, version: version }.compact
|
|
93
|
+
args[:table] = table if table
|
|
94
|
+
Evaluation::Stochastic.evaluate(object, samples: samples, seed: seed,
|
|
95
|
+
generator: generator, **args)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Canonical equality: two objects are canonically equal when their
|
|
99
|
+
# canonical serializations hash identically (see Openmath::Canonical).
|
|
100
|
+
def canonical_equal?(left, right)
|
|
101
|
+
Canonical.equal?(left, right)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
module_function :parse, :validate!, :valid?, :parse_cd, :canonical_equal?,
|
|
105
|
+
:parse_cdgroup, :validate_cdgroup!, :evaluate,
|
|
106
|
+
:evaluate_stochastic
|
|
69
107
|
end
|
data/schemas/PROVENANCE.md
CHANGED
|
@@ -8,6 +8,9 @@ depends on the network.
|
|
|
8
8
|
|---|---|---|
|
|
9
9
|
| `openmath2.rng` | https://github.com/OpenMath/OMSVN, `www/standard/om20-2013-07-11/openmath2.rng` | The OpenMath 2 RelaxNG schema from the standard's second distribution. |
|
|
10
10
|
| `openmath2.rnc` | https://github.com/OpenMath/OMSVN, `formerCVS/OpenMath2/openmath2.rnc` | The same grammar in compact syntax, kept for reference. |
|
|
11
|
+
| `omcdgroup2.rng` | https://github.com/OpenMath/OMSVN, `formerCVS/OpenMath2/omcdgroup2.rnc`, compiled to RNG with the OMSVN trang.jar | The CDGroup grammar (OpenMathCDG namespace), compiled because nokogiri validates XML-form RelaxNG. |
|
|
12
|
+
| `omcdgroup2.rnc` | https://github.com/OpenMath/OMSVN, `formerCVS/OpenMath2/omcdgroup2.rnc` | The same grammar in compact syntax, kept for reference. |
|
|
13
|
+
| `omcd2.rng` | https://github.com/OpenMath/OMSVN, `formerCVS/OpenMath2/omcd2.rnc` | The CD grammar (OpenMathCD namespace). |
|
|
11
14
|
| `openmath3.rng` | https://github.com/OpenMath/OMSVN, `OpenMath3-effort-2009/rnc/openmath3.rng` | The OpenMath 3 draft schema (2009 effort); includes `om3-common.rng`. |
|
|
12
15
|
| `om3-common.rng` | https://github.com/OpenMath/OMSVN, `OpenMath3-effort-2009/rnc/om3-common.rng` | The OM3 draft's shared grammar. |
|
|
13
16
|
|