Skip to content

Commit

Permalink
first theorem implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddoret committed Dec 14, 2024
1 parent eb142f5 commit 0415f47
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 30 deletions.
39 changes: 20 additions & 19 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified src/punctilious/__pycache__/_representation.cpython-312.pyc
Binary file not shown.
21 changes: 20 additions & 1 deletion src/punctilious/_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ def ensure_tags_assignment(o) -> TagsAssignment:
raise TypeError(f'TagsAssignment validation failure. Type: {type(o)}. Object: {o}.')


def ensure_tags_preferences(o) -> TagsPreferences:
"""Ensure that `o` is of type TagsPreferences, converting it if necessary, or raise an error if it fails."""
if isinstance(o, TagsPreferences):
return o
elif isinstance(o, dict):
o_typed = TagsPreferences()
for i in o.items():
tag = ensure_tag(i[0])
value = i[1] # TODO: define a TagPreferenceValue class and apply validation
o_typed[tag] = value
return o_typed
elif o is None:
return TagsPreferences()
else:
raise TypeError(f'TagsPreferences validation failure. Type: {type(o)}. Object: {o}.')


def ensure_renderer(o) -> Renderer:
"""Ensure that `o` is of type Renderer, converting it if necessary, or raise an error if it fails."""
if isinstance(o, Renderer):
Expand Down Expand Up @@ -126,8 +143,10 @@ def renderers(self):
"""A tuple of renderers configured for this representation."""
return self._renderers

def rep(self, config: TagsPreferences = None, variables: dict[str, str] = None):
def rep(self, variables: dict[str, str] = None, config: TagsPreferences = None):
config = ensure_tags_preferences(config)
if variables is None:
# TODO: Use a RepresentationVariable class and apply proper validation
variables = {}
renderer: Renderer = self.optimize_renderer(config=config)
return renderer.rep(config=config, variables=variables)
Expand Down
18 changes: 13 additions & 5 deletions src/punctilious/data/representations/formula_notations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ representations:
renderers:
- implementation: string_template
string_template: '{{ connector }}'
tags: { }
tags: { parenthesization: not_required }
- slug: function_formula
renderers:
- implementation: string_template
string_template: '{{ connector }}({% for a in argument %}{{ a }}{% if not loop.last %}, {% endif %}{% endfor %})'
tags: { technical_language: unicode_basic }
tags: { technical_language: unicode_basic, parenthesization: not_required }
- implementation: string_template
string_template: '{{ connector }}\left({% for a in argument %}{{ a }}{% if not loop.last %}, {% endif %}{% endfor %}\right)'
tags: { technical_language: latex_math, parenthesization: not_required }
- slug: infix_formula
renderers:
- implementation: string_template
string_template: '{{ argument[0] }} {{ connector }} {{ argument[1] }}'
tags: { technical_language: unicode_basic }
tags: { technical_language: unicode_basic, parenthesization: required_if_ambiguous }
- slug: prefix_formula
renderers:
- implementation: string_template
string_template: '{{ connector }} {{ argument[0] }}'
tags: { technical_language: unicode_basic }
string_template: '{{ connector }}{{ argument[0] }}'
tags: { technical_language: unicode_basic, parenthesization: required_if_ambiguous }
- slug: postfix_formula
renderers:
- implementation: string_template
string_template: '{{ argument[0] }}{{ connector }}'
tags: { technical_language: unicode_basic, parenthesization: required_if_ambiguous }



10 changes: 5 additions & 5 deletions src/punctilious/data/representations/tao_analysis_1_2006.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
schema: http://punctilious.org/data/schemas/punctilious_package_schema_1.yaml
uuid: 4bb42d13-1e72-4192-bf40-efeeee858705
slug: tao_analysis_1_2006_representations
imports:
- slug: formula_notations_1
scheme: python_package
path: data.representations
resource: formula_notations.yaml
representations:
- slug: zero
renderers:
- implementation: string_constant
string_constant: '0'
tags: { technical_language: unicode_basic, connector_representation: symbol }
- slug: successor
renderers:
- implementation: string_constant
string_constant: '++'
tags: { technical_language: unicode_basic, connector_representation: symbol }
bibliography:
- slug: tao_2006
citation: Tao, 2006
Expand Down
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/test_formula_notations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ def test_infix_notation(self):
variables={'connector': 'f', 'argument': ('x', 'y',)}) == 'x f y'

def test_formula_notation(self):
prefs = pu.TagsPreferences()

tag = pu.Tag('technical_language', 'unicode_basic')
prefs[tag] = 100

assert pu.formula_notations.function_formula.rep(
variables={'connector': 'f', 'argument': ()}) == 'f()'
assert pu.formula_notations.function_formula.rep(
Expand All @@ -17,6 +22,13 @@ def test_formula_notation(self):
assert pu.formula_notations.function_formula.rep(
variables={'connector': 'f', 'argument': ('x', 'y', 'z',)}) == 'f(x, y, z)'

tag = pu.Tag('technical_language', 'latex_math')
prefs[tag] = 200

assert pu.formula_notations.function_formula.rep(
variables={'connector': 'f', 'argument': ('x',)},
config=prefs) == 'f\\left(x\\right)'

def test_prefix_notation(self):
assert pu.formula_notations.prefix_formula.rep(
variables={'connector': 'f', 'argument': ('x',)}) == 'f x'
Expand Down

0 comments on commit 0415f47

Please sign in to comment.