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,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+
5
+ module Openmath
6
+ # The canonical serialization profile: the single deterministic form of
7
+ # an OpenMath object, invariant under syntactic variation — attribute
8
+ # order (sorted alphabetically), namespace prefix choice (the default
9
+ # namespace is declared once on the root of the canonical form),
10
+ # inter-element whitespace (none), OMI/OMB insignificant internal
11
+ # whitespace (stripped), and spelling normalization of insignificant
12
+ # differences. OMSTR text and OMFOREIGN fragments are content and stay
13
+ # verbatim. OMF dec/hex spellings are preserved (dec is not lossless to
14
+ # hex); OMR references are preserved unexpanded (resolution is a
15
+ # separate operation).
16
+ module Canonical
17
+ NAMESPACE = "http://www.openmath.org/OpenMath"
18
+
19
+ module_function
20
+
21
+ def xml(object)
22
+ out = +""
23
+ element(object, out, root: true)
24
+ out
25
+ end
26
+
27
+ def sha256(object)
28
+ Digest::SHA256.hexdigest(xml(object))
29
+ end
30
+
31
+ def equal?(left, right)
32
+ sha256(left) == sha256(right)
33
+ end
34
+
35
+ def element(node, out, root: false)
36
+ name = element_name(node)
37
+ out << "<#{name}"
38
+ out << %( xmlns="#{NAMESPACE}") if root
39
+ canonical_attributes(node).each do |key, value|
40
+ out << %( #{key}="#{escape(value, attribute: true)}")
41
+ end
42
+ text = content_text(node)
43
+ children = ordered_children(node)
44
+ fragments = foreign_fragments(node)
45
+ if text.nil? && children.empty? && fragments.empty?
46
+ out << "/>"
47
+ return
48
+ end
49
+
50
+ out << ">"
51
+ out << escape(text) unless text.nil?
52
+ emit_children(node, children, fragments, out)
53
+ out << "</#{name}>"
54
+ end
55
+
56
+ def emit_children(node, children, fragments, out)
57
+ return children.each { |child| element(child, out) } if fragments.empty?
58
+
59
+ emitted = 0
60
+ positions = Array(node.notom_positions)
61
+ children.each_with_index do |child, index|
62
+ while emitted < fragments.size && positions[emitted] == index
63
+ out << fragments[emitted]
64
+ emitted += 1
65
+ end
66
+ element(child, out)
67
+ end
68
+ fragments.drop(emitted).each { |fragment| out << fragment }
69
+ end
70
+
71
+ def canonical_attributes(node)
72
+ attributes = {
73
+ "cdbase" => node.respond_to?(:cdbase) ? node.cdbase : nil,
74
+ "cd" => node.respond_to?(:cd) ? node.cd : nil,
75
+ "encoding" => node.respond_to?(:foreign_encoding) ? node.foreign_encoding : nil,
76
+ "href" => node.respond_to?(:href) ? node.href : nil,
77
+ "id" => node.id,
78
+ "name" => node.respond_to?(:name) ? node.name : nil,
79
+ "version" => node.respond_to?(:version) ? node.version : nil,
80
+ "dec" => omf_attribute(node, :dec),
81
+ "hex" => omf_attribute(node, :hex),
82
+ }
83
+ attributes.filter_map do |key, value|
84
+ next unless value
85
+
86
+ [key, value]
87
+ end
88
+ end
89
+
90
+ def omf_attribute(node, key)
91
+ return unless node.is_a?(V2::Omf) || node.is_a?(V3::Omf)
92
+
93
+ node.public_send(key)
94
+ end
95
+
96
+ def content_text(node)
97
+ case node
98
+ when V2::Omstr, V3::Omstr
99
+ Array(node.value).join
100
+ when V2::Omb, V3::Omb
101
+ Array(node.value).join.gsub(/\s+/, "")
102
+ when V2::Omi, V3::Omi
103
+ Array(node.value).join.gsub(/\s+/, "")
104
+ end
105
+ end
106
+
107
+ def ordered_children(node)
108
+ Evaluation::Evaluator.ordered_children(node)
109
+ end
110
+
111
+ def foreign_fragments(node)
112
+ return [] unless node.is_a?(V2::Omforeign) || node.is_a?(V3::Omforeign)
113
+
114
+ Array(node.notom_value)
115
+ end
116
+
117
+ def element_name(node)
118
+ node.class.mappings_for(:xml).element_name
119
+ end
120
+
121
+ def escape(value, attribute: false)
122
+ text = value.to_s
123
+ text = text.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
124
+ attribute ? text.gsub('"', "&quot;") : text
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+
5
+ module Openmath
6
+ module CD
7
+ # The CD signatures XML namespace (omcdsig2.rnc).
8
+ class CdsNamespace < Lutaml::Xml::Namespace
9
+ uri "http://www.openmath.org/OpenMathCDS"
10
+ prefix_default "cds"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ module Openmath
6
+ module CD
7
+ # A revision-pinned CD collection: loads a CDGroup document (the
8
+ # OpenMathCDG namespace, omcdgroup2.rnc) and then each member CD from
9
+ # a local root or the member's recorded URL, refusing to resolve any
10
+ # symbol whose dictionary's CDVersion contradicts the pin. Provenance
11
+ # records the exact group document and the member revisions.
12
+ class GroupCollection
13
+ # The loaded dictionary's CDVersion contradicts the group's pin.
14
+ class VersionMismatchError < StandardError
15
+ attr_reader :cd, :pinned, :actual
16
+
17
+ def initialize(cd, pinned, actual)
18
+ @cd = cd
19
+ @pinned = pinned
20
+ @actual = actual
21
+ super("CD version mismatch for #{cd}: group pins #{pinned.inspect}, loaded #{actual.inspect}")
22
+ end
23
+ end
24
+
25
+ # The group names a CD no loader could find.
26
+ class MemberNotFoundError < StandardError
27
+ attr_reader :cd, :source
28
+
29
+ def initialize(cd, source)
30
+ @cd = cd
31
+ @source = source
32
+ super("CDGroup member #{cd} not found (source: #{source.inspect})")
33
+ end
34
+ end
35
+
36
+ attr_reader :group, :collection, :pins
37
+
38
+ def initialize(group, collection)
39
+ @group = group
40
+ @collection = collection
41
+ @pins = group.member_value
42
+ .select { |member| member.cd_version }
43
+ .map { |member| [member.cd_name, member.cd_version] }
44
+ .to_h
45
+ end
46
+
47
+ # Resolves a symbol through the pinned collection: a cdbase mismatch
48
+ # refuses as in Collection#resolve, and a pinned dictionary whose
49
+ # loaded CDVersion contradicts the pin refuses naming the CD and
50
+ # both revisions.
51
+ def resolve(cd:, name:, cdbase: nil)
52
+ document = collection[cd]
53
+ if @pins.key?(cd) && document && document.cd_version != @pins[cd]
54
+ raise VersionMismatchError.new(cd, @pins[cd], document.cd_version)
55
+ end
56
+
57
+ collection.resolve(cd: cd, name: name, cdbase: cdbase)
58
+ end
59
+
60
+ def [](name)
61
+ collection[name]
62
+ end
63
+
64
+ class << self
65
+ # Loads a group document (XML text or file path) and its member
66
+ # CDs. Member sources resolve against +root+ (a directory holding
67
+ # <CDName>.ocd files) or the member's CDURL; without either, only
68
+ # members already loaded into +collection+ count.
69
+ def load(group_source, root: nil, collection: Collection.new)
70
+ group = Group.parse(group_source)
71
+ group.member_value.each do |member|
72
+ source = member_source(member, root)
73
+ if source
74
+ collection.load(source)
75
+ elsif collection[member.cd_name].nil?
76
+ raise MemberNotFoundError.new(member.cd_name, member.cd_url || root)
77
+ end
78
+ document = collection[member.cd_name]
79
+ next unless member.cd_version && document &&
80
+ document.cd_version != member.cd_version
81
+
82
+ raise VersionMismatchError.new(member.cd_name, member.cd_version,
83
+ document.cd_version)
84
+ end
85
+ new(group, collection)
86
+ end
87
+
88
+ # Validates a group document against the vendored omcdgroup2.rng.
89
+ # Returns the list of Openmath::Validation::Error objects; empty
90
+ # when valid.
91
+ def validate(xml)
92
+ @schema ||= Nokogiri::XML::RelaxNG(
93
+ File.open(File.expand_path("../../../schemas/omcdgroup2.rng", __dir__)),
94
+ )
95
+ begin
96
+ document = Nokogiri.XML(xml) { |config| config.norecover }
97
+ rescue Nokogiri::XML::SyntaxError
98
+ raise Openmath::Validation::ValidationError,
99
+ [Openmath::Validation::Error.new("malformed XML", "/")]
100
+ end
101
+ raise Openmath::Validation::ValidationError,
102
+ [Openmath::Validation::Error.new("malformed XML", "/")] if document.errors.any?
103
+
104
+ @schema.validate(document).map do |e|
105
+ Openmath::Validation::Error.new(e.message, e.path)
106
+ end
107
+ end
108
+
109
+ def validate!(xml)
110
+ errors = validate(xml)
111
+ raise Openmath::Validation::ValidationError, errors unless errors.empty?
112
+
113
+ true
114
+ end
115
+
116
+ def valid?(xml)
117
+ validate(xml).empty?
118
+ end
119
+
120
+ private
121
+
122
+ def member_source(member, root)
123
+ candidate = root ? File.join(root, "#{member.cd_name}.ocd") : nil
124
+ return candidate if candidate && File.exist?(candidate)
125
+
126
+ member.cd_url if member.cd_url && File.exist?(member.cd_url)
127
+ end
128
+ end
129
+ end
130
+
131
+ # The group-level API: parse, validate, load.
132
+ module Group
133
+ module_function
134
+
135
+ def parse(source)
136
+ if source.match?(/\A</)
137
+ GroupDocument.from_xml(source)
138
+ else
139
+ GroupDocument.from_xml(File.read(source))
140
+ end
141
+ end
142
+
143
+ def validate(xml)
144
+ GroupCollection.validate(xml)
145
+ end
146
+
147
+ def validate!(xml)
148
+ GroupCollection.validate!(xml)
149
+ end
150
+
151
+ def valid?(xml)
152
+ GroupCollection.valid?(xml)
153
+ end
154
+
155
+ def load(source, **options)
156
+ GroupCollection.load(source, **options)
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+
5
+ module Openmath
6
+ module CD
7
+ # A member entry: the CD name with its pinned version and optional
8
+ # location.
9
+ class GroupMember < Lutaml::Model::Serializable
10
+ attribute :comment, :string
11
+ attribute :cd_name, :string
12
+ attribute :cd_version, :string
13
+ attribute :cd_url, :string
14
+
15
+ xml do
16
+ namespace GroupNamespace
17
+ element "CDGroupMember"
18
+ map_element "CDComment", to: :comment
19
+ map_element "CDName", to: :cd_name
20
+ map_element "CDVersion", to: :cd_version
21
+ map_element "CDURL", to: :cd_url
22
+ end
23
+ end
24
+
25
+ # The CDGroup document model (omcdgroup2.rnc, the OpenMathCDG
26
+ # namespace): the group's name, version, revision, URL and
27
+ # description, then the member entries with their pinned CDVersion.
28
+ class GroupDocument < Lutaml::Model::Serializable
29
+ attribute :name, :string
30
+ attribute :group_version, :string
31
+ attribute :group_revision, :string
32
+ attribute :url, :string
33
+ attribute :group_description, :string
34
+ attribute :comment_value, :string, collection: true
35
+ attribute :member_value, GroupMember, collection: true
36
+
37
+ xml do
38
+ namespace GroupNamespace
39
+ element "CDGroup"
40
+ map_element "CDGroupName", to: :name
41
+ map_element "CDGroupVersion", to: :group_version
42
+ map_element "CDGroupRevision", to: :group_revision
43
+ map_element "CDGroupURL", to: :url
44
+ map_element "CDGroupDescription", to: :group_description
45
+ map_element "CDComment", to: :comment_value
46
+ map_element "CDGroupMember", to: :member_value
47
+ end
48
+ end
49
+ end
50
+ end
@@ -9,5 +9,11 @@ module Openmath
9
9
  uri "http://www.openmath.org/OpenMathCD"
10
10
  prefix_default "cd"
11
11
  end
12
+
13
+ # The CDGroup XML namespace (omcdgroup2.rnc).
14
+ class GroupNamespace < Lutaml::Xml::Namespace
15
+ uri "http://www.openmath.org/OpenMathCDG"
16
+ prefix_default "cdg"
17
+ end
12
18
  end
13
19
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+
5
+ module Openmath
6
+ module CD
7
+ # One signature record: the symbol's name and its STS type as an
8
+ # OpenMath object (the omcdsig2 grammar embeds a standard OMOBJ; the
9
+ # object rides the versioned object model through the CD registry).
10
+ class Signature < Lutaml::Model::Serializable
11
+ def self.lutaml_default_register
12
+ Configuration::CONTEXT_ID
13
+ end
14
+
15
+ attribute :name, :string
16
+ attribute :object, :omobj
17
+
18
+ xml do
19
+ namespace CdsNamespace
20
+ element "Signature"
21
+ map_attribute "name", to: :name
22
+ map_element "OMOBJ", to: :object
23
+ end
24
+ end
25
+
26
+ # The CDSignatures document: which CD it signs (the cd attribute),
27
+ # what kind of signatures it carries (the type attribute, e.g. "sts"),
28
+ # and the Signature records.
29
+ class SignatureDocument < Lutaml::Model::Serializable
30
+ def self.lutaml_default_register
31
+ Configuration::CONTEXT_ID
32
+ end
33
+
34
+ attribute :cd, :string
35
+ attribute :signature_type, :string
36
+ attribute :comment_value, :string, collection: true
37
+ attribute :review_date, :string
38
+ attribute :status, :string
39
+ attribute :signature_value, Signature, collection: true
40
+
41
+ xml do
42
+ namespace CdsNamespace
43
+ element "CDSignatures"
44
+ map_attribute "cd", to: :cd
45
+ map_attribute "type", to: :signature_type
46
+ map_element "CDSComment", to: :comment_value
47
+ map_element "CDSReviewDate", to: :review_date
48
+ map_element "CDSStatus", to: :status
49
+ map_element "Signature", to: :signature_value
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openmath
4
+ module CD
5
+ # Loaded signature documents, indexed by (cd, symbol): the STS layer
6
+ # the application checker consults. Signatures load from XML text or
7
+ # .sts file paths.
8
+ class SignatureCollection
9
+ attr_reader :documents
10
+
11
+ def initialize
12
+ @documents = []
13
+ @by_cd = {}
14
+ end
15
+
16
+ # Loads a signature document (XML text or a .sts file path).
17
+ # Returns self for chaining.
18
+ def load(source)
19
+ Configuration.register_models!
20
+ xml = source.match?(/\A</) ? source : File.read(source)
21
+ document = SignatureDocument.from_xml(
22
+ xml, register: Configuration::CONTEXT_ID
23
+ )
24
+ add_document(document)
25
+ self
26
+ end
27
+
28
+ def add_document(document)
29
+ @documents << document
30
+ @by_cd[document.cd] = document
31
+ self
32
+ end
33
+
34
+ # The STS type object for a symbol, or nil when unsigned.
35
+ def lookup(cd, name)
36
+ document = @by_cd[cd]
37
+ return nil unless document
38
+
39
+ document.signature_value.find { |signature| signature.name == name }
40
+ &.object
41
+ end
42
+
43
+ def size
44
+ @documents.size
45
+ end
46
+ end
47
+
48
+ # The signature-level API.
49
+ module Signatures
50
+ module_function
51
+
52
+ def load(*sources)
53
+ collection = SignatureCollection.new
54
+ sources.each { |source| collection.load(source) }
55
+ collection
56
+ end
57
+
58
+ def parse(source)
59
+ SignatureCollection.new.load(source)
60
+ end
61
+ end
62
+ end
63
+ end
data/lib/openmath/cd.rb CHANGED
@@ -8,6 +8,8 @@ module Openmath
8
8
  # resolution. Modeled from schemas/omcd2.rng (the OpenMath 2 CD schema).
9
9
  module CD
10
10
  autoload :Namespace, "openmath/cd/namespace"
11
+ autoload :GroupNamespace, "openmath/cd/namespace"
12
+ autoload :CdsNamespace, "openmath/cd/cds_namespace"
11
13
  autoload :Configuration, "openmath/cd/configuration"
12
14
  autoload :Fmp, "openmath/cd/fmp"
13
15
  autoload :Example, "openmath/cd/example"
@@ -15,6 +17,14 @@ module Openmath
15
17
  autoload :Document, "openmath/cd/document"
16
18
  autoload :Collection, "openmath/cd/collection"
17
19
  autoload :Validation, "openmath/cd/validation"
20
+ autoload :GroupMember, "openmath/cd/group_document"
21
+ autoload :GroupDocument, "openmath/cd/group_document"
22
+ autoload :GroupCollection, "openmath/cd/group_collection"
23
+ autoload :Group, "openmath/cd/group_collection"
24
+ autoload :Signature, "openmath/cd/signature_document"
25
+ autoload :SignatureDocument, "openmath/cd/signature_document"
26
+ autoload :SignatureCollection, "openmath/cd/signatures"
27
+ autoload :Signatures, "openmath/cd/signatures"
18
28
 
19
29
  module_function
20
30
 
@@ -7,5 +7,14 @@ module Openmath
7
7
  # subclasses (e.g. Openmath::V2::CommonElements) declare the child object
8
8
  # collections and their element maps for their version's grammar.
9
9
  class CommonElements < Lutaml::Model::Serializable
10
+ # The canonical serialization profile (Openmath::Canonical): deterministic
11
+ # bytes for the same mathematical object.
12
+ def to_canonical_xml
13
+ Openmath::Canonical.xml(self)
14
+ end
15
+
16
+ def canonical_sha256
17
+ Openmath::Canonical.sha256(self)
18
+ end
10
19
  end
11
20
  end