Skip to content

S-Expression Compiler

Bases: Compiler

Source code in src/typedlogic/compilers/sexpr_compiler.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@dataclass
class SExprCompiler(Compiler):
    default_suffix: ClassVar[str] = "sexpr"

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

        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 = SExprCompiler()
            >>> print(compiler.compile(theory))
            (Theory
              (name null)
              (constants
                (dict
                  ()))
              (type_definitions
                (dict
                  ()))
              (predicate_definitions
                ((PredicateDefinition
                    (predicate "P")
                    (arguments
                      (dict
                        ((x "str"))))
                    (description null)
                    (metadata null)
                    (parents null)
                    (python_class null))
                  (PredicateDefinition
                    (predicate "Q")
                    (arguments
                      (dict
                        ((x "str"))))
                    (description null)
                    (metadata null)
                    (parents null)
                    (python_class null))))
              (sentence_groups
                ((SentenceGroup
                    (name "Sentences")
                    (group_type null)
                    (docstring null)
                    (sentences
                      ((Implies
                          (P
                            (Variable "x"))
                          (Q
                            (Variable "x")))))
                    (_annotations null))))
              (ground_terms
                ())
              (_annotations null)
              (source_module_name null))

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

        """
        sexpr = as_sexpr(theory)
        return _render(sexpr)

    def compile_sentence(self, sentence: Sentence, syntax: Optional[Union[str, ModelSyntax]] = None, **kwargs) -> str:
        sexpr = as_sexpr(sentence)
        return _render(sexpr)

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

Compile a Theory object into S-Expressions.

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 = SExprCompiler()
>>> print(compiler.compile(theory))
(Theory
  (name null)
  (constants
    (dict
      ()))
  (type_definitions
    (dict
      ()))
  (predicate_definitions
    ((PredicateDefinition
        (predicate "P")
        (arguments
          (dict
            ((x "str"))))
        (description null)
        (metadata null)
        (parents null)
        (python_class null))
      (PredicateDefinition
        (predicate "Q")
        (arguments
          (dict
            ((x "str"))))
        (description null)
        (metadata null)
        (parents null)
        (python_class null))))
  (sentence_groups
    ((SentenceGroup
        (name "Sentences")
        (group_type null)
        (docstring null)
        (sentences
          ((Implies
              (P
                (Variable "x"))
              (Q
                (Variable "x")))))
        (_annotations null))))
  (ground_terms
    ())
  (_annotations null)
  (source_module_name null))

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/sexpr_compiler.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def compile(self, theory: Theory, syntax: Optional[Union[str, ModelSyntax]] = None, **kwargs) -> str:
    """
    Compile a Theory object into S-Expressions.

    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 = SExprCompiler()
        >>> print(compiler.compile(theory))
        (Theory
          (name null)
          (constants
            (dict
              ()))
          (type_definitions
            (dict
              ()))
          (predicate_definitions
            ((PredicateDefinition
                (predicate "P")
                (arguments
                  (dict
                    ((x "str"))))
                (description null)
                (metadata null)
                (parents null)
                (python_class null))
              (PredicateDefinition
                (predicate "Q")
                (arguments
                  (dict
                    ((x "str"))))
                (description null)
                (metadata null)
                (parents null)
                (python_class null))))
          (sentence_groups
            ((SentenceGroup
                (name "Sentences")
                (group_type null)
                (docstring null)
                (sentences
                  ((Implies
                      (P
                        (Variable "x"))
                      (Q
                        (Variable "x")))))
                (_annotations null))))
          (ground_terms
            ())
          (_annotations null)
          (source_module_name null))

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

    """
    sexpr = as_sexpr(theory)
    return _render(sexpr)