openmath 0.1.0 → 0.1.1
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 +26 -5
- data/lib/openmath/cd/collection.rb +92 -0
- data/lib/openmath/cd/configuration.rb +65 -0
- data/lib/openmath/cd/definition.rb +42 -0
- data/lib/openmath/cd/document.rb +50 -0
- data/lib/openmath/cd/example.rb +22 -0
- data/lib/openmath/cd/fmp.rb +21 -0
- data/lib/openmath/cd/namespace.rb +13 -0
- data/lib/openmath/cd/validation.rb +10 -0
- data/lib/openmath/cd.rb +27 -0
- data/lib/openmath/evaluation/binding_table.rb +58 -0
- data/lib/openmath/evaluation/error_value.rb +27 -0
- data/lib/openmath/evaluation/errors.rb +65 -0
- data/lib/openmath/evaluation/evaluator.rb +141 -0
- data/lib/openmath/evaluation/operations.rb +126 -0
- data/lib/openmath/evaluation.rb +29 -0
- data/lib/openmath/v2.rb +1 -1
- data/lib/openmath/v3/common_elements.rb +48 -0
- data/lib/openmath/v3/configuration.rb +35 -0
- data/lib/openmath/v3/oma.rb +9 -0
- data/lib/openmath/v3/omatp.rb +9 -0
- data/lib/openmath/v3/omattr.rb +9 -0
- data/lib/openmath/v3/omb.rb +9 -0
- data/lib/openmath/v3/ombind.rb +9 -0
- data/lib/openmath/v3/ombvar.rb +9 -0
- data/lib/openmath/v3/ome.rb +9 -0
- data/lib/openmath/v3/omf.rb +9 -0
- data/lib/openmath/v3/omforeign.rb +9 -0
- data/lib/openmath/v3/omi.rb +9 -0
- data/lib/openmath/v3/omobj.rb +9 -0
- data/lib/openmath/v3/omr.rb +9 -0
- data/lib/openmath/v3/oms.rb +9 -0
- data/lib/openmath/v3/omstr.rb +9 -0
- data/lib/openmath/v3/omv.rb +9 -0
- data/lib/openmath/v3.rb +55 -0
- data/lib/openmath/validation.rb +62 -0
- data/lib/openmath/version.rb +1 -1
- data/lib/openmath.rb +28 -1
- data/schemas/PROVENANCE.md +17 -0
- data/schemas/om3-common.rng +257 -0
- data/schemas/omcd2.rng +180 -0
- data/schemas/openmath2.rnc +88 -0
- data/schemas/openmath2.rng +248 -0
- data/schemas/openmath3.rng +19 -0
- metadata +60 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: afafbcbc28919518be2680eb0550abd7b5ebbe76b1cfc9ab9d87c81781d50469
|
|
4
|
+
data.tar.gz: a04e6ff1fa464d389c295baa6b35c250f348f6fb070fdc72ad96478f447e1a46
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0ee4631a002a6841e53c66f8bb454c3fb7e41d66e44d4c27e36a5d22587bcd483f505c44cedba37cc0150f1ff9bd1bb3c572499e68da8f1621bede85f579c321
|
|
7
|
+
data.tar.gz: 81033d334dd8536385eb41d52e8540ce98bb25ba6a60e67d92b1ff118d3ae58876cdaf2b916076959fa634df5d09f8d47864588a20c287e6f1722443d5e2c01b
|
data/README.adoc
CHANGED
|
@@ -5,7 +5,7 @@ Ruby model: parse the XML encoding, serialize it back, and evaluate through
|
|
|
5
5
|
content dictionaries. Part of the {plurimath}[https://github.com/plurimath]
|
|
6
6
|
ecosystem; consumed by the plurimath gem.
|
|
7
7
|
|
|
8
|
-
== Status
|
|
8
|
+
== Status
|
|
9
9
|
|
|
10
10
|
The complete OpenMath 2 object grammar — OMOBJ, OMS, OMV, OMI, OMB, OMSTR,
|
|
11
11
|
OMF, OMA, OMBIND, OMBVAR, OME, OMATTR, OMATP, OMFOREIGN, OMR — modeled with
|
|
@@ -14,12 +14,22 @@ authoritative schema (`schemas/openmath2.rnc`, vendored from
|
|
|
14
14
|
OpenMath/OMSVN). Namespaced documents parse in both the unprefixed and the
|
|
15
15
|
`om:`-prefixed form; round trips are byte-stable.
|
|
16
16
|
|
|
17
|
+
* **Validation ships**: `Openmath.valid?` / `validate!` check any document
|
|
18
|
+
against its version's vendored RelaxNG schema (OpenMath 2 and the OM3
|
|
19
|
+
draft) through nokogiri; violations refuse with typed errors.
|
|
20
|
+
* **OpenMath 3**: the versioned hierarchy (`Openmath::V3`) parses and
|
|
21
|
+
round-trips the same object grammar under `Openmath.parse(xml, version: 3)`.
|
|
22
|
+
* **Content dictionaries**: the CD document model, the collection loader,
|
|
23
|
+
symbol resolution with cdbase matching, and CD validation against
|
|
24
|
+
`omcd2.rng` — CD content is data, never code.
|
|
25
|
+
* **Evaluation**: the CD binding table (`data/binding-table-1.yml`) binds
|
|
26
|
+
the working core (nums1, arith1, relation1, logic1) to operations;
|
|
27
|
+
`Openmath.evaluate(xml, variables:)` walks the tree under the exactness
|
|
28
|
+
discipline — integers and rationals stay exact, floats contaminate to the
|
|
29
|
+
limited class, unbound symbols refuse naming the symbol.
|
|
30
|
+
|
|
17
31
|
Known limitations (tracked in the issues):
|
|
18
32
|
|
|
19
|
-
* OpenMath 3 is scaffolded, not modeled — its versioned hierarchy lands
|
|
20
|
-
with the draft revision it implements (plurimath#582).
|
|
21
|
-
* Root-element and schema validation are roadmap work; the reader is
|
|
22
|
-
lenient about unknown roots.
|
|
23
33
|
* Non-OpenMath markup inside OMFOREIGN is not yet represented.
|
|
24
34
|
|
|
25
35
|
== Usage
|
|
@@ -38,6 +48,17 @@ object.oma_value.first.omi_value.map { |i| i.value.first.strip }
|
|
|
38
48
|
# => ["1", "1"]
|
|
39
49
|
|
|
40
50
|
puts object.to_xml
|
|
51
|
+
|
|
52
|
+
# Always-available schema validation
|
|
53
|
+
Openmath.valid?(xml, version: 2) # => true
|
|
54
|
+
|
|
55
|
+
# Semantic math that evaluates
|
|
56
|
+
Openmath.evaluate(<<~XML)
|
|
57
|
+
<OMOBJ xmlns="http://www.openmath.org/OpenMath">
|
|
58
|
+
<OMA><OMS cd="nums1" name="rational"/><OMI>1</OMI><OMI>4</OMI></OMA>
|
|
59
|
+
</OMOBJ>
|
|
60
|
+
XML
|
|
61
|
+
# => rational 1/4 (an exact Rational — never a float)
|
|
41
62
|
----
|
|
42
63
|
|
|
43
64
|
== Roadmap
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "nokogiri"
|
|
4
|
+
|
|
5
|
+
module Openmath
|
|
6
|
+
module CD
|
|
7
|
+
# A loaded set of content dictionaries, indexed by CDName for symbol
|
|
8
|
+
# resolution. Load a single document, a file, or a directory of .ocd
|
|
9
|
+
# files.
|
|
10
|
+
class Collection
|
|
11
|
+
attr_reader :documents
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@documents = []
|
|
15
|
+
@by_name = {}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Loads a CD document (XML text), a .ocd file path, or a directory of
|
|
19
|
+
# .ocd files. Returns self for chaining.
|
|
20
|
+
def load(source)
|
|
21
|
+
Configuration.register_models!
|
|
22
|
+
if source.match?(/\A</)
|
|
23
|
+
add_xml(source)
|
|
24
|
+
elsif File.directory?(source)
|
|
25
|
+
Dir[File.join(source, "*.ocd")].sort.each do |path|
|
|
26
|
+
add_xml(File.read(path))
|
|
27
|
+
end
|
|
28
|
+
elsif File.exist?(source)
|
|
29
|
+
add_xml(File.read(source))
|
|
30
|
+
else
|
|
31
|
+
add_xml(source)
|
|
32
|
+
end
|
|
33
|
+
self
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def add_xml(xml)
|
|
37
|
+
add_document(Document.from_xml(xml, register: Configuration::CONTEXT_ID))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def add_document(document)
|
|
41
|
+
@documents << document
|
|
42
|
+
@by_name[document.cd_name] = document
|
|
43
|
+
self
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The dictionary for a CD name, or nil.
|
|
47
|
+
def [](name)
|
|
48
|
+
@by_name[name]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def size
|
|
52
|
+
@documents.size
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Resolves a symbol reference to its CDDefinition: the CD by name, the
|
|
56
|
+
# definition by symbol name. When a cdbase is given, the loaded
|
|
57
|
+
# dictionary's own cdbase must match (OpenMath 2, §cdbase).
|
|
58
|
+
# Returns nil when the symbol is not in the collection — callers
|
|
59
|
+
# refuse unbound symbols; this class never guesses.
|
|
60
|
+
def resolve(cd:, name:, cdbase: nil)
|
|
61
|
+
document = @by_name[cd]
|
|
62
|
+
return nil unless document
|
|
63
|
+
return nil if cdbase && document.cd_base && document.cd_base != cdbase
|
|
64
|
+
|
|
65
|
+
document.definition_for(name)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Validates a CD document against the vendored omcd2.rng. Returns the
|
|
69
|
+
# list of Openmath::Validation::Error objects; empty when valid.
|
|
70
|
+
def self.validate(xml)
|
|
71
|
+
@schema ||= Nokogiri::XML::RelaxNG(
|
|
72
|
+
File.open(File.expand_path("../../../schemas/omcd2.rng", __dir__)),
|
|
73
|
+
)
|
|
74
|
+
document = Nokogiri::XML(xml) { |config| config.norecover }
|
|
75
|
+
raise Openmath::Validation::ValidationError,
|
|
76
|
+
[Openmath::Validation::Error.new("malformed XML", "/")] if document.errors.any?
|
|
77
|
+
|
|
78
|
+
@schema.validate(document).map do |e|
|
|
79
|
+
Openmath::Validation::Error.new(e.message, e.path)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Validates and raises ValidationError on any violation.
|
|
84
|
+
def self.validate!(xml)
|
|
85
|
+
errors = validate(xml)
|
|
86
|
+
raise Openmath::Validation::ValidationError, errors unless errors.empty?
|
|
87
|
+
|
|
88
|
+
true
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module CD
|
|
5
|
+
# Registers the CD model family (and the OpenMath object models its
|
|
6
|
+
# FMP/Example elements embed) under this family's LutaML context.
|
|
7
|
+
module Configuration
|
|
8
|
+
CONTEXT_ID = :openmath_cd
|
|
9
|
+
|
|
10
|
+
extend self
|
|
11
|
+
|
|
12
|
+
def context
|
|
13
|
+
Lutaml::Model::GlobalContext.context(CONTEXT_ID) || create_context!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def register_model(klass, id:)
|
|
17
|
+
create_context! unless Lutaml::Model::GlobalContext.context(CONTEXT_ID)
|
|
18
|
+
Lutaml::Model::GlobalContext.context(CONTEXT_ID)
|
|
19
|
+
.registry.register(id.to_sym, klass)
|
|
20
|
+
klass
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def create_context!
|
|
24
|
+
Lutaml::Model::GlobalContext.register_context(
|
|
25
|
+
Lutaml::Model::TypeContext.new(
|
|
26
|
+
id: CONTEXT_ID,
|
|
27
|
+
registry: Lutaml::Model::TypeRegistry.new,
|
|
28
|
+
),
|
|
29
|
+
)
|
|
30
|
+
Lutaml::Model::GlobalContext.context(CONTEXT_ID)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Registers the CD family and the object models the CD documents
|
|
34
|
+
# embed, once per process.
|
|
35
|
+
# The CD documents embed OpenMath objects, so the whole object
|
|
36
|
+
# family must resolve in this context too.
|
|
37
|
+
OBJECT_IDS = {
|
|
38
|
+
oms: :Oms, omv: :Omv, omi: :Omi, omb: :Omb, omstr: :Omstr, omf: :Omf,
|
|
39
|
+
oma: :Oma, ombind: :Ombind, ombvar: :Ombvar, ome: :Ome,
|
|
40
|
+
omattr: :Omattr, omatp: :Omatp, omforeign: :Omforeign, omr: :Omr,
|
|
41
|
+
omobj: :Omobj,
|
|
42
|
+
}.freeze
|
|
43
|
+
|
|
44
|
+
def register_models!
|
|
45
|
+
return if @registered
|
|
46
|
+
|
|
47
|
+
register_model(Fmp, id: :fmp)
|
|
48
|
+
register_model(Example, id: :example)
|
|
49
|
+
register_model(Definition, id: :definition)
|
|
50
|
+
register_model(Document, id: :cd)
|
|
51
|
+
OBJECT_IDS.each do |id, const|
|
|
52
|
+
register_model(Openmath::V2.const_get(const), id: id)
|
|
53
|
+
end
|
|
54
|
+
# The interpretive reader resolves symbol-typed attributes against
|
|
55
|
+
# the default context (the mml populate_context! pattern): the CD
|
|
56
|
+
# family and its embedded object family must resolve there too.
|
|
57
|
+
default_registry = Lutaml::Model::GlobalContext.context.registry
|
|
58
|
+
{ fmp: Fmp, example: Example, definition: Definition, cd: Document }
|
|
59
|
+
.merge(OBJECT_IDS.to_h { |id, const| [id, Openmath::V2.const_get(const)] })
|
|
60
|
+
.each { |id, klass| default_registry.register(id, klass) unless default_registry.registered?(id) }
|
|
61
|
+
@registered = true
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module CD
|
|
5
|
+
# A symbol definition: <CDDefinition> with Name, optional Role,
|
|
6
|
+
# Description, and any number of CMPs (commented properties, text),
|
|
7
|
+
# FMPs (formal properties, OpenMath objects) and Examples.
|
|
8
|
+
class Definition < Openmath::CommonElements
|
|
9
|
+
def self.lutaml_default_register
|
|
10
|
+
Configuration::CONTEXT_ID
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
attribute :name, :string
|
|
14
|
+
attribute :role, :string
|
|
15
|
+
attribute :description, :string, collection: true
|
|
16
|
+
attribute :cmp_value, :string, collection: true
|
|
17
|
+
attribute :fmp_value, Fmp, collection: true
|
|
18
|
+
attribute :example_value, Example, collection: true
|
|
19
|
+
|
|
20
|
+
xml do
|
|
21
|
+
namespace Openmath::CD::Namespace
|
|
22
|
+
element "CDDefinition"
|
|
23
|
+
map_element "Name", to: :name
|
|
24
|
+
map_element "Role", to: :role
|
|
25
|
+
map_element "Description", to: :description
|
|
26
|
+
map_element "CMP", to: :cmp_value
|
|
27
|
+
map_element "FMP", to: :fmp_value
|
|
28
|
+
map_element "Example", to: :example_value
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The formal properties' OpenMath objects, in document order.
|
|
32
|
+
def fmp_objects
|
|
33
|
+
fmp_value.flat_map(&:omobj_value)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# The worked objects, in document order.
|
|
37
|
+
def example_objects
|
|
38
|
+
example_value.flat_map(&:omobj_value)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module CD
|
|
5
|
+
# The Content Dictionary document: <CD> with its header (CDName,
|
|
6
|
+
# CDBase, CDURL, CDReviewDate, CDStatus, CDDate, CDVersion, CDRevision,
|
|
7
|
+
# CDComments, Description) and its symbol definitions.
|
|
8
|
+
class Document < Openmath::CommonElements
|
|
9
|
+
def self.lutaml_default_register
|
|
10
|
+
Configuration::CONTEXT_ID
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
attribute :cd_name, :string
|
|
14
|
+
attribute :cd_base, :string
|
|
15
|
+
attribute :cd_url, :string
|
|
16
|
+
attribute :cd_review_date, :string
|
|
17
|
+
attribute :cd_status, :string
|
|
18
|
+
attribute :cd_date, :string
|
|
19
|
+
attribute :cd_version, :string
|
|
20
|
+
attribute :cd_revision, :string
|
|
21
|
+
attribute :description, :string, collection: true
|
|
22
|
+
attribute :definition_value, Definition, collection: true
|
|
23
|
+
|
|
24
|
+
xml do
|
|
25
|
+
namespace Openmath::CD::Namespace
|
|
26
|
+
element "CD"
|
|
27
|
+
map_element "CDName", to: :cd_name
|
|
28
|
+
map_element "CDBase", to: :cd_base
|
|
29
|
+
map_element "CDURL", to: :cd_url
|
|
30
|
+
map_element "CDReviewDate", to: :cd_review_date
|
|
31
|
+
map_element "CDStatus", to: :cd_status
|
|
32
|
+
map_element "CDDate", to: :cd_date
|
|
33
|
+
map_element "CDVersion", to: :cd_version
|
|
34
|
+
map_element "CDRevision", to: :cd_revision
|
|
35
|
+
map_element "Description", to: :description
|
|
36
|
+
map_element "CDDefinition", to: :definition_value
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The symbol names, in document order.
|
|
40
|
+
def symbol_names
|
|
41
|
+
definition_value.map(&:name)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The definition of one symbol, or nil.
|
|
45
|
+
def definition_for(name)
|
|
46
|
+
definition_value.find { |d| d.name == name }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module CD
|
|
5
|
+
# A CD example: <Example><OMOBJ ...>...</OMOBJ></Example>. The worked
|
|
6
|
+
# object is an OpenMath object; plain-text examples are held in the
|
|
7
|
+
# same value as text.
|
|
8
|
+
class Example < Openmath::CommonElements
|
|
9
|
+
def self.lutaml_default_register
|
|
10
|
+
Configuration::CONTEXT_ID
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
attribute :omobj_value, :omobj, collection: true
|
|
14
|
+
|
|
15
|
+
xml do
|
|
16
|
+
namespace Openmath::CD::Namespace
|
|
17
|
+
element "Example"
|
|
18
|
+
map_element "OMOBJ", to: :omobj_value
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module CD
|
|
5
|
+
# A formal mathematical property: <FMP><OMOBJ ...>...</OMOBJ></FMP>.
|
|
6
|
+
# The property statement is an OpenMath object.
|
|
7
|
+
class Fmp < Openmath::CommonElements
|
|
8
|
+
def self.lutaml_default_register
|
|
9
|
+
Configuration::CONTEXT_ID
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
attribute :omobj_value, :omobj, collection: true
|
|
13
|
+
|
|
14
|
+
xml do
|
|
15
|
+
namespace Openmath::CD::Namespace
|
|
16
|
+
element "FMP"
|
|
17
|
+
map_element "OMOBJ", to: :omobj_value
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
|
|
5
|
+
module Openmath
|
|
6
|
+
module CD
|
|
7
|
+
# The Content Dictionary XML namespace.
|
|
8
|
+
class Namespace < Lutaml::Xml::Namespace
|
|
9
|
+
uri "http://www.openmath.org/OpenMathCD"
|
|
10
|
+
prefix_default "cd"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/lib/openmath/cd.rb
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
|
|
5
|
+
module Openmath
|
|
6
|
+
# Content dictionaries: the CD XML documents (namespace
|
|
7
|
+
# http://www.openmath.org/OpenMathCD), their loading as data, and symbol
|
|
8
|
+
# resolution. Modeled from schemas/omcd2.rng (the OpenMath 2 CD schema).
|
|
9
|
+
module CD
|
|
10
|
+
autoload :Namespace, "openmath/cd/namespace"
|
|
11
|
+
autoload :Configuration, "openmath/cd/configuration"
|
|
12
|
+
autoload :Fmp, "openmath/cd/fmp"
|
|
13
|
+
autoload :Example, "openmath/cd/example"
|
|
14
|
+
autoload :Definition, "openmath/cd/definition"
|
|
15
|
+
autoload :Document, "openmath/cd/document"
|
|
16
|
+
autoload :Collection, "openmath/cd/collection"
|
|
17
|
+
autoload :Validation, "openmath/cd/validation"
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# Parses a CD document into Openmath::CD::Document.
|
|
22
|
+
def parse(input)
|
|
23
|
+
Configuration.register_models!
|
|
24
|
+
Document.from_xml(input, register: Configuration::CONTEXT_ID)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
module Openmath
|
|
6
|
+
module Evaluation
|
|
7
|
+
# The CD binding table: the checksummed data artifact
|
|
8
|
+
# (data/binding-table-1.yml) declaring which content-dictionary symbols
|
|
9
|
+
# evaluate and which operation each binds to. The evaluator dispatches
|
|
10
|
+
# on nothing this table does not bind.
|
|
11
|
+
class BindingTable
|
|
12
|
+
SCHEMA = "openmath-binding-table/1"
|
|
13
|
+
DATA_PATH = File.expand_path("../../../data/binding-table-1.yml", __dir__)
|
|
14
|
+
|
|
15
|
+
Binding = Struct.new(:cd, :name, :operation) do
|
|
16
|
+
def key
|
|
17
|
+
"#{cd}/#{name}"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
attr_reader :version, :bindings
|
|
22
|
+
|
|
23
|
+
def initialize(bindings, version:)
|
|
24
|
+
@version = version
|
|
25
|
+
@bindings = bindings
|
|
26
|
+
@by_key = bindings.to_h { |b| [b.key, b] }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Loads the shipped table.
|
|
30
|
+
def self.default
|
|
31
|
+
load_file(DATA_PATH)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Loads a table file, checking the schema declaration.
|
|
35
|
+
def self.load_file(path)
|
|
36
|
+
doc = YAML.safe_load_file(path, aliases: false)
|
|
37
|
+
raise Error, "#{path}: schema is not #{SCHEMA}" unless doc["schema"] == SCHEMA
|
|
38
|
+
|
|
39
|
+
bindings = doc.fetch("bindings").map do |entry|
|
|
40
|
+
Binding.new(entry.fetch("cd"), entry.fetch("name"), entry.fetch("operation").to_sym)
|
|
41
|
+
end
|
|
42
|
+
new(bindings, version: doc.fetch("version"))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The binding for a symbol, or nil when unbound.
|
|
46
|
+
def lookup(cd, name)
|
|
47
|
+
@by_key["#{cd}/#{name}"]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The sha256 of the shipped table — the artifact's identity, pinned by
|
|
51
|
+
# the specs.
|
|
52
|
+
def self.default_checksum
|
|
53
|
+
require "digest"
|
|
54
|
+
Digest::SHA256.hexdigest(File.read(DATA_PATH))
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module Evaluation
|
|
5
|
+
# The value of evaluating an OME error constructor: an error object that
|
|
6
|
+
# carries its cd, name and evaluated content — the honest answer to
|
|
7
|
+
# evaluating an expression whose meaning is an error.
|
|
8
|
+
class ErrorValue
|
|
9
|
+
attr_reader :cd, :name, :arguments
|
|
10
|
+
|
|
11
|
+
def initialize(cd, name, arguments)
|
|
12
|
+
@cd = cd
|
|
13
|
+
@name = name
|
|
14
|
+
@arguments = arguments
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def ==(other)
|
|
18
|
+
other.is_a?(ErrorValue) && other.cd == cd &&
|
|
19
|
+
other.name == name && other.arguments == arguments
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_s
|
|
23
|
+
"#{cd}##{name}(#{arguments.join(', ')})"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Openmath
|
|
4
|
+
module Evaluation
|
|
5
|
+
# The typed refusals. Every one names what it refused; none of them
|
|
6
|
+
# guesses (the honest-error discipline of the PM series).
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
|
|
9
|
+
# The symbol is not in the binding table.
|
|
10
|
+
class UnboundSymbolError < Error
|
|
11
|
+
attr_reader :cd, :name
|
|
12
|
+
|
|
13
|
+
def initialize(cd, name)
|
|
14
|
+
@cd = cd
|
|
15
|
+
@name = name
|
|
16
|
+
super("unbound symbol #{cd}##{name}: not in the binding table")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The construct is outside the evaluated surface (bindings, containers,
|
|
21
|
+
# attributed operators...).
|
|
22
|
+
class UnsupportedExpressionError < Error
|
|
23
|
+
attr_reader :element
|
|
24
|
+
|
|
25
|
+
def initialize(element)
|
|
26
|
+
@element = element
|
|
27
|
+
super("unsupported expression: #{element}")
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The application's argument count contradicts the operation's signature.
|
|
32
|
+
class ArityError < Error
|
|
33
|
+
attr_reader :operation, :expected, :actual
|
|
34
|
+
|
|
35
|
+
def initialize(operation, expected, actual)
|
|
36
|
+
@operation = operation
|
|
37
|
+
@expected = expected
|
|
38
|
+
@actual = actual
|
|
39
|
+
super("wrong arity for #{operation}: expected #{expected}, got #{actual}")
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# An operand is outside the operation's domain (e.g. a string in plus).
|
|
44
|
+
class OperandTypeError < Error
|
|
45
|
+
attr_reader :operation, :operand
|
|
46
|
+
|
|
47
|
+
def initialize(operation, operand)
|
|
48
|
+
@operation = operation
|
|
49
|
+
@operand = operand
|
|
50
|
+
super("wrong operand type for #{operation}: #{operand.class}")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The operation is bound in the table but not implemented — a build
|
|
55
|
+
# error, not a caller error.
|
|
56
|
+
class UnimplementedOperationError < Error
|
|
57
|
+
attr_reader :operation
|
|
58
|
+
|
|
59
|
+
def initialize(operation)
|
|
60
|
+
@operation = operation
|
|
61
|
+
super("binding table names unimplemented operation #{operation}")
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|