Documentation

ExistentialRules.AtomsAndFacts.Basic

Atoms, Facts, Rules and the like #

In this file, we define the next layers of building blocks above terms. This includes first and foremost Atom and Fact but also Rule, RuleSet, Database and KnowledgeBase to name a few.

The atom-like datastructures are all expressed in terms of a GeneralizedAtom. This will turn out convenient when defining substitutions and homomorphisms next since these can (for the most part) just be defines as generic mapping over GeneralizedAtom.

structure GeneralizedAtom (sig : Signature) (T : Type u) [DecidableEq sig.P] :
Type (max u u_1)

A GeneralizedAtom consists of a predicate symbol and a list of terms of an arbitrary type such that the number of terms matches the predicate's arity.

Instances For
    def instDecidableEqGeneralizedAtom.decEq {sig✝ : Signature} {T✝ : Type u_4} {inst✝ : DecidableEq sig✝.P} [DecidableEq T✝] (x✝ x✝¹ : GeneralizedAtom sig✝ T✝) :
    Decidable (x✝ = x✝¹)
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      Atom #

      @[reducible, inline]
      abbrev Atom (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
      Type (max (max u_3 u_2) u_1)

      An Atom is a GeneralizedAtom with SkolemTerms.

      Equations
      Instances For

        FunctionFreeAtom #

        @[reducible, inline]
        abbrev FunctionFreeAtom (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
        Type (max (max u_3 u_2) u_1)

        A FunctionFreeAtom is a GeneralizedAtom with VarOrConsts.

        Equations
        Instances For

          Using VarOrConst.filterVars, we can obtain all variables from the terms of the FunctionFreeAtom.

          Equations
          Instances For

            Using VarOrConst.filterConsts, we can obtain all constants from the terms of the FunctionFreeAtom.

            Equations
            Instances For
              def FunctionFreeAtom.skolemize {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (ruleId disjunctIndex : Nat) (frontier : List sig.V) (a : FunctionFreeAtom sig) :
              Atom sig

              We can skolemize a FunctionFreeAtom by skolemizing all its VarOrConsts. This yields an Atom.

              Equations
              Instances For

                A variable occurs in variables iff it is a term of the `FunctionFreeAtom.

                A constant occurs in constants iff it is a term of the `FunctionFreeAtom.

                theorem FunctionFreeAtom.length_skolemize {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] {ruleId disjunctIndex : Nat} {frontier : List sig.V} {a : FunctionFreeAtom sig} :
                (skolemize ruleId disjunctIndex frontier a).terms.length = a.terms.length

                The number of terms remains unchanged when Skolemizing.

                theorem FunctionFreeAtom.mem_skolemize_of_mem {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] {ruleId disjunctIndex : Nat} {frontier : List sig.V} {a : FunctionFreeAtom sig} {t : VarOrConst sig} :
                t a.termsVarOrConst.skolemize ruleId disjunctIndex frontier t (skolemize ruleId disjunctIndex frontier a).terms

                If a a VarOrConst occurs in the terms of the FunctionFreeAtom, then the Skolemized VarOrConst occurs in the Skolemized Atom.

                FunctionFreeConjunction #

                @[reducible, inline]
                abbrev FunctionFreeConjunction (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                Type (max (max u_3 u_2) u_1)

                A conjunction of FunctionFreeAtoms $p(x, y) \land q(y)$ can simply be represented as a list of FunctionFreeAtoms.

                Equations
                Instances For

                  The terms of a FunctionFreeConjunction are the terms of all its atoms.

                  Equations
                  Instances For

                    The vars of a FunctionFreeConjunction are the variables of all its atoms.

                    Equations
                    Instances For

                      The consts of a FunctionFreeConjunction are the constants of all its atoms.

                      Equations
                      Instances For

                        The predicates of a FunctionFreeConjunction are the predicates of all its atoms.

                        Equations
                        Instances For

                          Different from the definition, we can also say that a variable is in variables iff there is a FunctionFreeAtom in the conjunction that features the variable as a term.

                          Different from the definition, we can also say that a constant is in constants iff there is a FunctionFreeAtom in the conjunction that features the constant as a term.

                          (Disjunctive) (Existential) Rule #

                          A disjunctive existential rule, or simply Rule, formally is an expression of the form $$∀ \vec{x}, \vec{y}. B(x, y) \to \bigvee_{i = 1}^{k} \exists \vec{z}_i. H_i(y_i, z_i)$$ where $B,H_1,\dots,H_k$ are conjunctions of function free atoms, $y$ is exactly the union of all $y_i$ and $x$, $y$, and all $z_i$ are disjoint lists of variables. $y$ is called frontier. $B$ is called body and the $H_i$ are called heads. We call a rule determinstic if $k = 1$ so if the head is merely a conjunction.

                          To represent this formal definition in Lean, we use a structure with a FunctionFreeConjunction for the body and a list of FunctionFreeConjunctions for the disjunction in the head. For bookkeeping each rule also gets an id. That's it! The frontier variables can simply be defined as the variables occurring both in body and head and the existential variables can be indentified as the variables that occur only in the head, without the need for explicit quantification.

                          structure Rule (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                          Type (max (max u_1 u_2) u_3)

                          The definition of a Rule as discussed above.

                          Instances For
                            instance instDecidableEqRule {sig✝ : Signature} {inst✝ : DecidableEq sig✝.P} {inst✝¹ : DecidableEq sig✝.C} {inst✝² : DecidableEq sig✝.V} :
                            DecidableEq (Rule sig✝)
                            Equations
                            def instDecidableEqRule.decEq {sig✝ : Signature} {inst✝ : DecidableEq sig✝.P} {inst✝¹ : DecidableEq sig✝.C} {inst✝² : DecidableEq sig✝.V} (x✝ x✝¹ : Rule sig✝) :
                            Decidable (x✝ = x✝¹)
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              def Rule.frontier_for_head {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) (i : Fin r.head.length) :
                              List sig.V

                              This function returns the frontier variables that occur in a given head disjunct. This is a sublist of all the frontier variables.

                              Equations
                              Instances For
                                def Rule.frontier {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :
                                List sig.V

                                This returns all the frontier variables of the rule, i.e. the variables that occur in both body and some head.

                                Equations
                                Instances For
                                  def Rule.pure_body_vars {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :
                                  List sig.V

                                  The pure_body_vars are the variables from the body that are not in the frontier.

                                  Equations
                                  Instances For
                                    def Rule.isDatalog {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :

                                    We call a rule isDatalog if it does not contain existential variables, i.e. if all head variables occur in the body.

                                    Equations
                                    Instances For
                                      def Rule.isDeterministic {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :

                                      We call a rule isDeterministic if it has exactly one head disjunct.

                                      Equations
                                      Instances For
                                        def Rule.predicates {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :
                                        List sig.P

                                        The predicate symbols of a rule are just the predicate symbols from the body and all heads.

                                        Equations
                                        Instances For
                                          def Rule.constants {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :
                                          List sig.C

                                          The constants of a rule are just the constants from the body and all heads.

                                          Equations
                                          Instances For
                                            def Rule.head_constants {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :
                                            List sig.C

                                            Sometimes we require only the constants from the heads and therefore we define them here.

                                            Equations
                                            Instances For
                                              def Rule.existential_vars_for_head_disjunct {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) (i : Nat) (lt : i < r.head.length) :
                                              List sig.V

                                              The existential variables for a given head are simply the variables from the head that are not in the frontier.

                                              Equations
                                              Instances For
                                                def Rule.skolem_functions {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (r : Rule sig) :

                                                The Skolem function symbols of a rule are all SkolemFS with the rule id, all possible head indices and the respective existential variables.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For

                                                  A variable is a frontier variable if and only if it is a frontier variable in some head disjunct.

                                                  A variable is in the frontier of a head if it is in the frontier of the rule and occurs as a term in the given head.

                                                  All frontier variables occur in the body.

                                                  The frontier variables in a given head occur in the list of variables for the same head.

                                                  The head constants of the rule are also constants of the whole rule.

                                                  RuleSet and RuleList #

                                                  structure RuleSet (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                                                  Type (max (max u_1 u_2) u_3)

                                                  A RuleSet is a Set (Rule sig) where rules are uniquely identified by their id.

                                                  Instances For
                                                    structure RuleList (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                                                    Type (max (max u_1 u_2) u_3)

                                                    A RuleList is a List (Rule sig) where rules are uniquely identified by their id.

                                                    Instances For

                                                      RuleSet #

                                                      A RuleSet is is deterministic if each rule is.

                                                      Equations
                                                      Instances For
                                                        def RuleSet.predicates {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (rs : RuleSet sig) :
                                                        Set sig.P

                                                        The predicate symbols of a RuleSet are the predicate symbols from all rules.

                                                        Equations
                                                        Instances For
                                                          def RuleSet.constants {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (rs : RuleSet sig) :
                                                          Set sig.C

                                                          The constants of a RuleSet are the constants from all rules.

                                                          Equations
                                                          Instances For
                                                            def RuleSet.head_constants {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (rs : RuleSet sig) :
                                                            Set sig.C

                                                            The head constants of a RuleSet are the head constants from all rules.

                                                            Equations
                                                            Instances For
                                                              def RuleSet.skolem_functions {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (rs : RuleSet sig) :

                                                              The Skolem function symbols of a RuleSet are the Skolem function symbols from all rules.

                                                              Equations
                                                              Instances For

                                                                If the RuleSet is finite, so are the predicates.

                                                                If the RuleSet is finite, so are the constants.

                                                                RuleList #

                                                                def RuleList.get_by_id {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (rl : RuleList sig) (id : Nat) (id_mem : (r : Rule sig), r rl.rules r.id = id) :
                                                                Rule sig

                                                                In a RuleList, we can obtain a rule based on its id.

                                                                Equations
                                                                Instances For
                                                                  theorem RuleList.get_by_id_mem {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (rl : RuleList sig) (id : Nat) (id_mem : (r : Rule sig), r rl.rules r.id = id) :
                                                                  rl.get_by_id id id_mem rl.rules

                                                                  A rule that we get by id is in the RuleList.

                                                                  theorem RuleList.get_by_id_self {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (rl : RuleList sig) (r : Rule sig) (mem : r rl.rules) :
                                                                  rl.get_by_id r.id = r

                                                                  A rule that we get by id is indeed the rule that the id belongs to.

                                                                  Facts and FunctionFreeFacts #

                                                                  @[reducible, inline]
                                                                  abbrev Fact (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                                                                  Type (max (max u_3 u_2) u_1)

                                                                  A Fact is a GeneralizedAtom with GroundTerms.

                                                                  Equations
                                                                  Instances For
                                                                    @[reducible, inline]
                                                                    abbrev FunctionFreeFact (sig : Signature) [DecidableEq sig.P] :
                                                                    Type (max u_3 u_1)

                                                                    A FunctionFreeFact is a GeneralizedAtom with constants.

                                                                    Equations
                                                                    Instances For
                                                                      def Fact.constants {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (f : Fact sig) :
                                                                      List sig.C

                                                                      The constants of a Fact are the constants of all terms.

                                                                      Equations
                                                                      Instances For
                                                                        def Fact.function_symbols {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (f : Fact sig) :

                                                                        The function_symbols of a Fact are the function symbols of all terms.

                                                                        Equations
                                                                        Instances For
                                                                          def Fact.isFunctionFree {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (f : Fact sig) :

                                                                          A Fact is function free, if each term is a constant.

                                                                          Equations
                                                                          Instances For
                                                                            def Fact.toFunctionFreeFact {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (f : Fact sig) (isFunctionFree : f.isFunctionFree) :

                                                                            If a Fact isFunctionFree, then we can convert it to a FunctionFreeFact.

                                                                            Equations
                                                                            Instances For

                                                                              A FunctionFreeFact can always be converted to a Fact.

                                                                              Equations
                                                                              Instances For
                                                                                theorem Fact.toFact_after_toFunctionFreeFact_is_id {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (f : Fact sig) (isFunctionFree : f.isFunctionFree) :
                                                                                (f.toFunctionFreeFact isFunctionFree).toFact = f

                                                                                Converting a Fact to a FunctionFreeFact and back yields the initial Fact.

                                                                                FactSet #

                                                                                @[reducible, inline]
                                                                                abbrev FactSet (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                                                                                Type (max (max u_3 u_2) u_1)

                                                                                A FactSet is plainly a Set of Facts.

                                                                                Equations
                                                                                Instances For
                                                                                  def FactSet.predicates {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (fs : FactSet sig) :
                                                                                  Set sig.P

                                                                                  The predicate symbols of a FactSet are the predicate symbols from all facts.

                                                                                  Equations
                                                                                  Instances For
                                                                                    def FactSet.terms {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (fs : FactSet sig) :

                                                                                    The terms of a FactSet are the terms from all facts.

                                                                                    Equations
                                                                                    Instances For
                                                                                      def FactSet.constants {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (fs : FactSet sig) :
                                                                                      Set sig.C

                                                                                      The constants of a FactSet are the constants from all facts.

                                                                                      Equations
                                                                                      Instances For
                                                                                        def FactSet.function_symbols {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (fs : FactSet sig) :

                                                                                        The function symbols of a FactSet are the function symbols from all facts.

                                                                                        Equations
                                                                                        Instances For

                                                                                          A FactSet is function free if all of its facts are.

                                                                                          Equations
                                                                                          Instances For

                                                                                            When converting a list to a FactSet, the terms remain the same.

                                                                                            theorem FactSet.terms_subset_of_subset {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] {fs1 fs2 : FactSet sig} :
                                                                                            fs1 fs2fs1.terms fs2.terms

                                                                                            The a FactSet is a subset of another, then their terms share this subset relation.

                                                                                            theorem FactSet.terms_union {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] {fs1 fs2 : FactSet sig} :
                                                                                            (fs1 fs2).terms = fs1.terms fs2.terms

                                                                                            The terms of the union of two FactSets are the union of the terms of both sets.

                                                                                            theorem FactSet.terms_finite_of_finite {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (fs : FactSet sig) (finite : Set.finite fs) :

                                                                                            If a FactSet is finite, so are its terms.

                                                                                            When converting a list to a FactSet, the constants remain the same.

                                                                                            theorem FactSet.constants_union {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] {fs1 fs2 : FactSet sig} :
                                                                                            (fs1 fs2).constants = fs1.constants fs2.constants

                                                                                            The constants of the union of two FactSets are the union of the constants of both sets.

                                                                                            If a FactSet is finite, so are its constants.

                                                                                            A FactSet is finite if both its predicates and terms are. This holds since the fact set must be a subset of all facts that can possibly be constructed using the prediactes and terms available. This overapproximation is easily shown to be finite.

                                                                                            For a list of terms in a given FactSet, we can find a list of facts in the fact set such that all the terms from the list occur in the list of facts.

                                                                                            Database #

                                                                                            @[reducible, inline]
                                                                                            abbrev Database (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                                                                                            Type (max 0 u_3 u_1)

                                                                                            A Database is a finite set of FunctionFreeFacts.

                                                                                            Equations
                                                                                            Instances For

                                                                                              Any Database can trivially be converted to a finite and function free FactSet.

                                                                                              Equations
                                                                                              Instances For
                                                                                                def Database.constants {sig : Signature} [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] (db : Database sig) :
                                                                                                { C : Set sig.C // C.finite }

                                                                                                Each Database has a finite set of constants.

                                                                                                Equations
                                                                                                Instances For

                                                                                                  When converting a Database to a FactSet, the constants remain the same.

                                                                                                  KnowledgeBase #

                                                                                                  structure KnowledgeBase (sig : Signature) [DecidableEq sig.P] [DecidableEq sig.C] [DecidableEq sig.V] :
                                                                                                  Type (max (max u_1 u_2) u_3)

                                                                                                  A KnowledgeBase is a pair of a Database and a RuleSet. Note that usually the RuleSet is enforced to be finite but we only restrict this in places where we really need this.

                                                                                                  Instances For

                                                                                                    A KnowledgeBase is determinstic if the underlying RuleSet is.

                                                                                                    Equations
                                                                                                    Instances For