Skip to content

YAML Compiler

Bases: Compiler

Source code in src/typedlogic/compilers/yaml_compiler.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@dataclass
class YAMLCompiler(Compiler):
    default_suffix: ClassVar[str] = "yaml"
    parser_class = YAMLParser

    def compile(self, theory: Theory, syntax: Optional[Union[str, ModelSyntax]] = None, **kwargs) -> str:
        """
        Compile a Theory object into YAML code.

        Example:
        -------
            >>> from typedlogic import *
            >>> theory = Theory()
            >>> x = Variable("x")
            >>> theory.predicate_definitions = [PredicateDefinition("P", {"x": "str"}),
            ...              PredicateDefinition("Q", {"x": "str"})]
            >>> s = Implies(Term("P", x), Term("Q", x))
            >>> theory.add(sentence=s)
            >>> compiler = YAMLCompiler()
            >>> print(compiler.compile(theory))
            type: Theory
            constants: {}
            type_definitions: {}
            predicate_definitions:
            - type: PredicateDefinition
              predicate: P
              arguments:
                x: str
            - type: PredicateDefinition
              predicate: Q
              arguments:
                x: str
            sentence_groups:
            - type: SentenceGroup
              name: Sentences
              sentences:
              - type: Implies
                arguments:
                - type: Term
                  arguments:
                  - P
                  - type: Variable
                    arguments:
                    - x
                - type: Term
                  arguments:
                  - Q
                  - type: Variable
                    arguments:
                    - x
            ground_terms: []

        :param theory:
        :param syntax:
        :param kwargs:
        :return:

        """
        obj = as_object(theory)
        return yaml.dump(obj, sort_keys=False)

    def compile_sentence(self, sentence: Sentence, syntax: Optional[Union[str, ModelSyntax]] = None, **kwargs) -> str:
        obj = as_object(sentence)
        return yaml.dump(obj, sort_keys=False)

compile(theory, syntax=None, **kwargs)

Compile a Theory object into YAML code.

Example:

>>> from typedlogic import *
>>> theory = Theory()
>>> x = Variable("x")
>>> theory.predicate_definitions = [PredicateDefinition("P", {"x": "str"}),
...              PredicateDefinition("Q", {"x": "str"})]
>>> s = Implies(Term("P", x), Term("Q", x))
>>> theory.add(sentence=s)
>>> compiler = YAMLCompiler()
>>> print(compiler.compile(theory))
type: Theory
constants: {}
type_definitions: {}
predicate_definitions:
- type: PredicateDefinition
  predicate: P
  arguments:
    x: str
- type: PredicateDefinition
  predicate: Q
  arguments:
    x: str
sentence_groups:
- type: SentenceGroup
  name: Sentences
  sentences:
  - type: Implies
    arguments:
    - type: Term
      arguments:
      - P
      - type: Variable
        arguments:
        - x
    - type: Term
      arguments:
      - Q
      - type: Variable
        arguments:
        - x
ground_terms: []

Parameters:

Name Type Description Default
theory Theory
required
syntax Optional[Union[str, ModelSyntax]]
None
kwargs
{}

Returns:

Type Description
str
Source code in src/typedlogic/compilers/yaml_compiler.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def compile(self, theory: Theory, syntax: Optional[Union[str, ModelSyntax]] = None, **kwargs) -> str:
    """
    Compile a Theory object into YAML code.

    Example:
    -------
        >>> from typedlogic import *
        >>> theory = Theory()
        >>> x = Variable("x")
        >>> theory.predicate_definitions = [PredicateDefinition("P", {"x": "str"}),
        ...              PredicateDefinition("Q", {"x": "str"})]
        >>> s = Implies(Term("P", x), Term("Q", x))
        >>> theory.add(sentence=s)
        >>> compiler = YAMLCompiler()
        >>> print(compiler.compile(theory))
        type: Theory
        constants: {}
        type_definitions: {}
        predicate_definitions:
        - type: PredicateDefinition
          predicate: P
          arguments:
            x: str
        - type: PredicateDefinition
          predicate: Q
          arguments:
            x: str
        sentence_groups:
        - type: SentenceGroup
          name: Sentences
          sentences:
          - type: Implies
            arguments:
            - type: Term
              arguments:
              - P
              - type: Variable
                arguments:
                - x
            - type: Term
              arguments:
              - Q
              - type: Variable
                arguments:
                - x
        ground_terms: []

    :param theory:
    :param syntax:
    :param kwargs:
    :return:

    """
    obj = as_object(theory)
    return yaml.dump(obj, sort_keys=False)