The Plan Compile, interpret and execute CLP to a QRA: Semantics (set of true instances): QRA Σ . Σ -allegories. Translation, logical meta-aspects and variables formalized at the relational level. Program to theory between ground terms. Program to allegory. Sharing and memory is captured. Execution: Two notions of rewriting. Executable semantics. Many extensions and optimizations possible declaratively : partial evaluation, abstract interpretation, different search strategies. . . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 6 / 35
The Plan Compile, interpret and execute CLP to a QRA: Semantics (set of true instances): QRA Σ . Σ -allegories. Translation, logical meta-aspects and variables formalized at the relational level. Program to theory between ground terms. Program to allegory. Sharing and memory is captured. Execution: Two notions of rewriting. Executable semantics. Many extensions and optimizations possible declaratively : partial evaluation, abstract interpretation, different search strategies. . . Logic Without Variables + Logic Programming = Formula Relation Proof = Equational Derivation EJGA (CRI-Mines) Allegories and CLP 09/22/2013 6 / 35
The Plan Compile, interpret and execute CLP to a QRA: Semantics (set of true instances): QRA Σ . Σ -allegories. Translation, logical meta-aspects and variables formalized at the relational level. Program to theory between ground terms. Program to allegory. Sharing and memory is captured. Execution: Two notions of rewriting. Executable semantics. Many extensions and optimizations possible declaratively : partial evaluation, abstract interpretation, different search strategies. . . Logic Without Variables + Logic Programming = = Program Formula Relation Computation = Proof Search = Equational Derivation EJGA (CRI-Mines) Allegories and CLP 09/22/2013 6 / 35
The Plan Compile, interpret and execute CLP to a QRA: Semantics (set of true instances): QRA Σ . Σ -allegories. Translation, logical meta-aspects and variables formalized at the relational level. Program to theory between ground terms. Program to allegory. Sharing and memory is captured. Execution: Two notions of rewriting. Executable semantics. Many extensions and optimizations possible declaratively : partial evaluation, abstract interpretation, different search strategies. . . Logic Without Variables + Logic Programming = Program Relation Computation = Equational Derivation EJGA (CRI-Mines) Allegories and CLP 09/22/2013 6 / 35
The Relational Theory Assume a signature Σ ≡ {C , F , CP , P} and Constr. Dom. D . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 7 / 35
The Relational Theory Assume a signature Σ ≡ {C , F , CP , P} and Constr. Dom. D . Generate the relational language: R C = { ( a , a ) | a ∈ C Σ } R F = { R f | f ∈ F Σ , } R CP = { r | r ∈ CP Σ } R P = { p | p ∈ P Σ } :: = R C | R F | R CP | R P | id | di | 1 | 0 | hd | tl R atom R atom | R Σ ◦ | R Σ ∪ R Σ | R Σ ∩ R Σ | R Σ R Σ R Σ :: = EJGA (CRI-Mines) Allegories and CLP 09/22/2013 7 / 35
The Relational Theory Assume a signature Σ ≡ {C , F , CP , P} and Constr. Dom. D . Generate the relational language: R C = { ( a , a ) | a ∈ C Σ } R F = { R f | f ∈ F Σ , } R CP = { r | r ∈ CP Σ } R P = { p | p ∈ P Σ } :: = R C | R F | R CP | R P | id | di | 1 | 0 | hd | tl R atom R atom | R Σ ◦ | R Σ ∪ R Σ | R Σ ∩ R Σ | R Σ R Σ R Σ :: = Interpretation � · � : R Σ → P ( D † × D † ) , where D † = ∪ n D n . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 7 / 35
The Relational Theory Assume a signature Σ ≡ {C , F , CP , P} and Constr. Dom. D . Generate the relational language: R C = { ( a , a ) | a ∈ C Σ } R F = { R f | f ∈ F Σ , } R CP = { r | r ∈ CP Σ } R P = { p | p ∈ P Σ } :: = R C | R F | R CP | R P | id | di | 1 | 0 | hd | tl R atom R atom | R Σ ◦ | R Σ ∪ R Σ | R Σ ∩ R Σ | R Σ R Σ R Σ :: = Interpretation � · � : R Σ → P ( D † × D † ) , where D † = ∪ n D n . Example u ′ ∈ D † } u ′ ) | m ≤ n ; m , n ∈ R , � � ≤ � = { ( � m , n � � u , � m , n � � u , � EJGA (CRI-Mines) Allegories and CLP 09/22/2013 7 / 35
The Relational Theory Assume a signature Σ ≡ {C , F , CP , P} and Constr. Dom. D . Generate the relational language: R C = { ( a , a ) | a ∈ C Σ } R F = { R f | f ∈ F Σ , } R CP = { r | r ∈ CP Σ } R P = { p | p ∈ P Σ } :: = R C | R F | R CP | R P | id | di | 1 | 0 | hd | tl R atom R atom | R Σ ◦ | R Σ ∪ R Σ | R Σ ∩ R Σ | R Σ R Σ R Σ :: = Interpretation � · � : R Σ → P ( D † × D † ) , where D † = ∪ n D n . Example u ′ ∈ D † } u ′ ) | m ≤ n ; m , n ∈ R , � � ≤ � = { ( � m , n � � u , � m , n � � u , � u ′ ∈ D † } u ′ ) | m + n = o ; m , n , o ∈ N , � � add � = { ( � m , n , o � � u , � m , n , o � � u , � EJGA (CRI-Mines) Allegories and CLP 09/22/2013 7 / 35
Translation Overview: Helpers Projections and Permutations P i is the relation projecting the i -th component of a vector; given a permutation π , W π is the associated relation. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 8 / 35
Translation Overview: Helpers Projections and Permutations P i is the relation projecting the i -th component of a vector; given a permutation π , W π is the associated relation. Partial Identity and Existential Quantification The quasi-identity relation Q i is such that ( � u , � v ) ∈ � Q i � if the all but i -th component of � u and � v agree. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 8 / 35
Translation Overview: Helpers Projections and Permutations P i is the relation projecting the i -th component of a vector; given a permutation π , W π is the associated relation. Partial Identity and Existential Quantification The quasi-identity relation Q i is such that ( � u , � v ) ∈ � Q i � if the all but i -th component of � u and � v agree. Wrapping a relation R in Q i RQ i has the logical effect of existentially quantifying i ! Q i ; R · · · S ; Q i � �� � i − private for R ··· S EJGA (CRI-Mines) Allegories and CLP 09/22/2013 8 / 35
Translation Overview: Helpers Projections and Permutations P i is the relation projecting the i -th component of a vector; given a permutation π , W π is the associated relation. Partial Identity and Existential Quantification The quasi-identity relation Q i is such that ( � u , � v ) ∈ � Q i � if the all but i -th component of � u and � v agree. Wrapping a relation R in Q i RQ i has the logical effect of existentially quantifying i ! Q i ; R · · · S ; Q i � �� � i − private for R ··· S We’ll also use a variation of Q i , I n that “hides” all the elements greater than n . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 8 / 35
Translation Overview [1/3]: Terms Key Idea A term t [ � x ] ∈ T Σ ( X ) is translated to a relation between all its ground instances and instantiations for � x : EJGA (CRI-Mines) Allegories and CLP 09/22/2013 9 / 35
Translation Overview [1/3]: Terms Key Idea A term t [ � x ] ∈ T Σ ( X ) is translated to a relation between all its ground instances and instantiations for � x : x ]) � D † ⇐ ⇒ b = t D [ � ( b , � a � u ) ∈ � K ( t [ � a / � x ] EJGA (CRI-Mines) Allegories and CLP 09/22/2013 9 / 35
Translation Overview [1/3]: Terms Key Idea A term t [ � x ] ∈ T Σ ( X ) is translated to a relation between all its ground instances and instantiations for � x : x ]) � D † ⇐ ⇒ b = t D [ � ( b , � a � u ) ∈ � K ( t [ � a / � x ] Formally: ( c , c ) 1 if t ≡ c P ◦ K ( t ) : T Σ ( X ) → R Σ = if t ≡ x i i � i ≤ n f r i K ( t i ) if t ≡ f ( t 1 , . . . , t n ) EJGA (CRI-Mines) Allegories and CLP 09/22/2013 9 / 35
Translation Overview [1/3]: Terms Key Idea A term t [ � x ] ∈ T Σ ( X ) is translated to a relation between all its ground instances and instantiations for � x : x ]) � D † ⇐ ⇒ b = t D [ � ( b , � a � u ) ∈ � K ( t [ � a / � x ] Formally: ( c , c ) 1 if t ≡ c P ◦ K ( t ) : T Σ ( X ) → R Σ = if t ≡ x i i � i ≤ n f r i K ( t i ) if t ≡ f ( t 1 , . . . , t n ) Example K ( f ( x 1 , g ( x 2 , a , h ( x 1 )))) = f 2 1 ; P ◦ 1 ∩ f 2 1 ; ( g 3 1 ; P ◦ 2 ∩ g 2 1 ; ( a , a ) ; 1 ∩ g 3 3 ; h ; P ◦ 1 ) EJGA (CRI-Mines) Allegories and CLP 09/22/2013 9 / 35
Translation Overview [2/3]: Constraints Key Idea A constraint ϕ [ � x ] ∈ L D is translated to the set of all its ground solutions, encoded as a coreflexive relation: EJGA (CRI-Mines) Allegories and CLP 09/22/2013 10 / 35
Translation Overview [2/3]: Constraints Key Idea A constraint ϕ [ � x ] ∈ L D is translated to the set of all its ground solutions, encoded as a coreflexive relation: x ]) � D † ⇐ u ′ ) ∈ � ˙ ( � a � u , � a � K ( ϕ [ � ⇒ D | = ϕ [ � a / � x ] EJGA (CRI-Mines) Allegories and CLP 09/22/2013 10 / 35
Translation Overview [2/3]: Constraints Key Idea A constraint ϕ [ � x ] ∈ L D is translated to the set of all its ground solutions, encoded as a coreflexive relation: x ]) � D † ⇐ u ′ ) ∈ � ˙ ( � a � u , � a � K ( ϕ [ � ⇒ D | = ϕ [ � a / � x ] Formally: � t ) ◦ ; p ; K ( � � K ( t ) if ϕ ≡ p ( t ) ˙ K ( ϕ ) ∩ ˙ ˙ K ( t ) : L D → R Σ = K ( θ ) if ϕ ≡ ϕ ∧ θ Q i ; ˙ K ( ϕ ) ; Q i if ϕ ≡ ∃ x i . ϕ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 10 / 35
Translation Overview [2/3]: Constraints Key Idea A constraint ϕ [ � x ] ∈ L D is translated to the set of all its ground solutions, encoded as a coreflexive relation: x ]) � D † ⇐ u ′ ) ∈ � ˙ ( � a � u , � a � K ( ϕ [ � ⇒ D | = ϕ [ � a / � x ] Formally: � t ) ◦ ; p ; K ( � � K ( t ) if ϕ ≡ p ( t ) ˙ K ( ϕ ) ∩ ˙ ˙ K ( t ) : L D → R Σ = K ( θ ) if ϕ ≡ ϕ ∧ θ Q i ; ˙ K ( ϕ ) ; Q i if ϕ ≡ ∃ x i . ϕ Example K ( ∃ x 1 x 2 . s ( x 1 ) ≤ x 2 ) = Q 1 Q 2 ; ( P ◦ ˙ 1 ; s ◦ ; P 1 ∩ P ◦ 2 ; P 2 ) ; ≤ ; ( P 1 ; s ; P ◦ 1 ∩ P 2 ; P ◦ 2 ) ; Q 1 Q 2 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 10 / 35
Translation Overview [3/3]: Predicates Key Idea Defined predicates p are translated to equations p ⊜ R . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 11 / 35
Translation Overview [3/3]: Predicates Key Idea Defined predicates p are translated to equations p ⊜ R . Theorem (Adequacy) u ′ ) ∈ � p � D † ⇐ a ) ∈ T ω ( � a � u , � a � ⇒ p ( � P EJGA (CRI-Mines) Allegories and CLP 09/22/2013 11 / 35
Translation Overview [3/3]: Predicates Key Idea Defined predicates p are translated to equations p ⊜ R . Theorem (Adequacy) u ′ ) ∈ � p � D † ⇐ a ) ∈ T ω ( � a � u , � a � ⇒ p ( � P The Procedure 1 Purify clause’ heads, canonical renaming, Clark completion. 2 The Relational Step! EJGA (CRI-Mines) Allegories and CLP 09/22/2013 11 / 35
Translation Overview [3/3]: Predicates Key Idea Defined predicates p are translated to equations p ⊜ R . Theorem (Adequacy) u ′ ) ∈ � p � D † ⇐ a ) ∈ T ω ( � a � u , � a � ⇒ p ( � P The Procedure 1 Purify clause’ heads, canonical renaming, Clark completion. 2 The Relational Step! add ( x 1 , x 2 , x 3 ) ← x 1 = o , x 2 = x 3 . add ( x 1 , x 2 , x 3 ) ← x 1 = s ( x 4 ) , x 3 = s ( x 5 ) , add ( x 4 , x 2 , x 5 ) . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 11 / 35
Translation Overview [3/3]: Predicates Key Idea Defined predicates p are translated to equations p ⊜ R . Theorem (Adequacy) u ′ ) ∈ � p � D † ⇐ a ) ∈ T ω ( � a � u , � a � ⇒ p ( � P The Procedure 1 Purify clause’ heads, canonical renaming, Clark completion. 2 The Relational Step! add ( x 1 , x 2 , x 3 ) ← x 1 = o , x 2 = x 3 . add ( x 1 , x 2 , x 3 ) ← x 1 = s ( x 4 ) , x 3 = s ( x 5 ) , add ( x 4 , x 2 , x 5 ) . ˙ = K ( x 1 = o ∧ x 2 = x 3 ) add I 3 ; ˙ K ( x 1 = s ( x 4 ) ∧ x 3 = s ( x 5 )) ; W ; add ; W ◦ ; I 3 ∪ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 11 / 35
Program Execution Relations and Computation r ∧ ( s ∨ t ) ↔ r ∧ s ∨ r ∧ t R ∩ ( S ∪ T ) = ( R ∩ S ) ∪ ( R ∩ T ) EJGA (CRI-Mines) Allegories and CLP 09/22/2013 12 / 35
Program Execution Relations and Computation r ∧ ( s ∨ t ) ↔ r ∧ s ∨ r ∧ t R ∩ ( S ∪ T ) = ( R ∩ S ) ∪ ( R ∩ T ) R ∩ ( S ∪ T ) �→ ( R ∩ S ) ∪ ( R ∩ T ) Computation rule EJGA (CRI-Mines) Allegories and CLP 09/22/2013 12 / 35
Program Execution Relations and Computation r ∧ ( s ∨ t ) ↔ r ∧ s ∨ r ∧ t R ∩ ( S ∪ T ) = ( R ∩ S ) ∪ ( R ∩ T ) R ∩ ( S ∪ T ) �→ ( R ∩ S ) ∪ ( R ∩ T ) Computation rule Cut rule S ∪ T ⊇ S S ∪ T �→ S EJGA (CRI-Mines) Allegories and CLP 09/22/2013 12 / 35
Program Execution Relations and Computation r ∧ ( s ∨ t ) ↔ r ∧ s ∨ r ∧ t R ∩ ( S ∪ T ) = ( R ∩ S ) ∪ ( R ∩ T ) R ∩ ( S ∪ T ) �→ ( R ∩ S ) ∪ ( R ∩ T ) Computation rule Cut rule S ∪ T ⊇ S S ∪ T �→ S Queries For executing a query, we just intersect and rewrite, for instance for add ( o , o , o ) : ˙ K ( x 1 = x 2 = x 3 = o ) ∩ add EJGA (CRI-Mines) Allegories and CLP 09/22/2013 12 / 35
Rewriting and the Modular Law Some Sample Rules P P P 0 ∪ R �− → R 0 ∩ R �− → 0 ( R ∪ S ) ∩ T �− → ( R ∩ T ) ∪ ( S ∩ T ) Meta Rules Calls to the constraint solver are modeled by meta-rewriting rules: K ( ψ 1 ) ∩ ˙ ˙ K ( ψ 2 ) → ˙ K ( ψ 1 ∧ ψ 2 ) Procedure Call: The Modular Law ˙ → I m ( I m ( ˙ K ( ψ )) ∩ R ) ∩ ˙ P K ( ψ ) ∩ I m ( R ) �− K ( ψ ) We hide variables in ψ that may be in conflict with variables in R , but we need to “unhide” them later. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 13 / 35
Example Execution Query: add (o,s(o),X) I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ add → 1 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 14 / 35
Example Execution Query: add (o,s(o),X) I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ add → 1 I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ ( ˙ K ( o , x 2 , x 2 ) ∪ 2 ( I 3 [ ˙ K ( s ( x 4 ) , x 2 , s ( x 5 ) , x 4 , x 5 ) ∩ W add W ◦ ] I 3 )) → EJGA (CRI-Mines) Allegories and CLP 09/22/2013 14 / 35
Example Execution Query: add (o,s(o),X) I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ add → 1 I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ ( ˙ K ( o , x 2 , x 2 ) ∪ 2 ( I 3 [ ˙ K ( s ( x 4 ) , x 2 , s ( x 5 ) , x 4 , x 5 ) ∩ W add W ◦ ] I 3 )) → I 3 [ ˙ K ( o , s ( o ) , x 3 ) ∩ ˙ 3 K ( o , x 2 , x 2 )] I 3 ∪ I 3 ( ˙ K ( o , s ( o ) , x 3 ) ∩ ˙ K ( s ( x 4 ) , x 2 , s ( x 5 ) , x 4 , x 5 ) ∩ W add W ◦ ) I 3 → EJGA (CRI-Mines) Allegories and CLP 09/22/2013 14 / 35
Example Execution Query: add (o,s(o),X) I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ add → 1 I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ ( ˙ K ( o , x 2 , x 2 ) ∪ 2 ( I 3 [ ˙ K ( s ( x 4 ) , x 2 , s ( x 5 ) , x 4 , x 5 ) ∩ W add W ◦ ] I 3 )) → I 3 [ ˙ K ( o , s ( o ) , x 3 ) ∩ ˙ 3 K ( o , x 2 , x 2 )] I 3 ∪ I 3 ( ˙ K ( o , s ( o ) , x 3 ) ∩ ˙ K ( s ( x 4 ) , x 2 , s ( x 5 ) , x 4 , x 5 ) ∩ W add W ◦ ) I 3 → K ( o , s ( o ) , s ( o )) ∪ I 3 [ 0 ∩ W add W ◦ ] I 3 ˙ 4 → EJGA (CRI-Mines) Allegories and CLP 09/22/2013 14 / 35
Example Execution Query: add (o,s(o),X) I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ add → 1 I 3 ˙ K ( o , s ( o ) , x 3 ) I 3 ∩ ( ˙ K ( o , x 2 , x 2 ) ∪ 2 ( I 3 [ ˙ K ( s ( x 4 ) , x 2 , s ( x 5 ) , x 4 , x 5 ) ∩ W add W ◦ ] I 3 )) → I 3 [ ˙ K ( o , s ( o ) , x 3 ) ∩ ˙ 3 K ( o , x 2 , x 2 )] I 3 ∪ I 3 ( ˙ K ( o , s ( o ) , x 3 ) ∩ ˙ K ( s ( x 4 ) , x 2 , s ( x 5 ) , x 4 , x 5 ) ∩ W add W ◦ ) I 3 → K ( o , s ( o ) , s ( o )) ∪ I 3 [ 0 ∩ W add W ◦ ] I 3 ˙ 4 → ˙ K ( o , s ( o ) , s ( o )) 5 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 14 / 35
Why move to Categories? An old thought (2004) “We need types to run fast and allocate memory for the relations.” Just a implementor’s intuition. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 15 / 35
Why move to Categories? An old thought (2004) “We need types to run fast and allocate memory for the relations.” Just a implementor’s intuition. Problems of the Pure Relational Approach Terms and substitution are complex. Duplicity of relational terms everywhere. 6 months of research just for unification. Difficult to implement. A † = T Σ ∪ T ∗ Σ ) ∗ ∪ . . . is a hell of a Σ ∪ ( T ∗ data type. Renaming apart is difficult to model and understand. Crucial information is missing the number of variables currently in use . Combinatorial approach: bad for performance. Efficiency is difficult due to duplicity. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 15 / 35
Allegories versus RA What is the domain for the relations? Signature? How are variables represented? EJGA (CRI-Mines) Allegories and CLP 09/22/2013 16 / 35
Allegories versus RA What is the domain for the relations? Σ ) ∗ ∪ . . . . QRA A single domain T Σ ∪ T ∗ Σ ∪ ( T ∗ Allegory Types represent fixed-length sequences of terms. Signature? How are variables represented? EJGA (CRI-Mines) Allegories and CLP 09/22/2013 16 / 35
Allegories versus RA What is the domain for the relations? Σ ) ∗ ∪ . . . . QRA A single domain T Σ ∪ T ∗ Σ ∪ ( T ∗ Allegory Types represent fixed-length sequences of terms. Signature? QRA New relations for every constant and term former. Allegory Freely adjoined arrows. How are variables represented? EJGA (CRI-Mines) Allegories and CLP 09/22/2013 16 / 35
Allegories versus RA What is the domain for the relations? Σ ) ∗ ∪ . . . . QRA A single domain T Σ ∪ T ∗ Σ ∪ ( T ∗ Allegory Types represent fixed-length sequences of terms. Signature? QRA New relations for every constant and term former. Allegory Freely adjoined arrows. How are variables represented? QRA Untyped projections. Allegory We use categorical projections. In essence, we replace typed projections π N i : N → 1 for untyped quasiprojections P i : A † ↔ A † . A small change with big implications. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 16 / 35
Categories of Syntax (Lawvere Categories) Defining the Category For a signature Σ , we define a Lawvere Category C : Objects are the natural numbers. Terminal object 0, the rest of the objects are powers of 1, 1 x 1 x 1 = 2 x 1 = 1 x 2 = 3. For every constant a ∈ T Σ , we freely adjoin an arrow a : 0 → 1. For every function symbol f ∈ T Σ with arity α ( f ) = N , we freely adjoin an arrow f : N → 1. Example For instance, for Σ = ( { o } , { s / 1 , + / 2 } ) , C has all the terminal and product arrows plus o : 0 → 1, s : 1 → 1 and + : 2 → 1. Initial Model The initial model is a functor C → Set which preserves finite products and pullbacks. It maps the object 1 to T Σ . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 17 / 35
Regular Categories, Tabular Relations Regular Category Category with products, pullbacks and certain exactness conditions. Categories of Relations f : C → A and g : C → B is a monic pair iff � f , g � : C �→ A × B is monic, informally, a subset of A × B , thus, ( f , g ) represent a relation from A to B : C g f ✛ ✲ A B EJGA (CRI-Mines) Allegories and CLP 09/22/2013 18 / 35
Allegories Allegories and Distributive Allegories An (distributive) allegory is a category with added structure, such that if f , g : A → B are arrows, ( f ∪ g ) f ∩ g : A → B and f ◦ : B → A are arrows and obey the appropriate relational laws. Typed version of relational algebras. Tabular Allegories An allegory is tabular if for each morphism R there is a pair of maps f , g , such that R = f ◦ ; g . We say that ( f , g ) tabulate R . Regular categories are categories of maps for tabular allegories, thus they generate them. Diagrammatically: C C ✲ ◦ g g f f ✲ ✛ ✲ R R A ✛ B A ✛ B ✲ ✲ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 19 / 35
Regular Lawvere Categories Pullbacks in Syntax Categories In C , arrows are freely added. No way of equalizing constants a , b : 0 → 1. C is not a regular category, it lacks pullbacks. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 20 / 35
Regular Lawvere Categories Pullbacks in Syntax Categories In C , arrows are freely added. No way of equalizing constants a , b : 0 → 1. C is not a regular category, it lacks pullbacks. Regular Completion of C Adjoin an initial object ⊥ Freely adjoin the corresponding initial arrows ? A : ⊥ → A for every object A . Apply the quotient ? A ; f = ? B for any arrow f : A → B . Now every arrow can be made equal to another, ⊥ ; a = ⊥ ; b . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 20 / 35
Regular Lawvere Categories: Examples Renaming Apart In the case of a pullback, every term feeds from a different set of variables, so unification module renaming apart is guaranteed. id � π 1 , π 2 � 2 ✛ 2 2 ✛ 3 � π 1 , π 3 � π 1 id π 1 ❄ ❄ ❄ ❄ 1 2 1 2 ✛ ✛ π 1 π 1 So each clause will be translated to feed from the same set of variables. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 21 / 35
Σ -Allegories Σ -Allegories Σ -Allegories are distributive allegories generated from a Regular Lawvere Category for the signature Σ and thus partially tabular. They are the target of our translation . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 22 / 35
Term Translation x = � � For a sequence of terms � t [ � y ] , K ( t [ � y ]) is the coreflexive relation: | � y | ) K � � t ( y ( � t K � y ) ✛ ✲ � t [ � x | ✛ K ( y ]) ✲ | � | � x | Registers If we look at a completed clause: x ′ ) ← � x = � p ( � t [ � y ] , p 1 ( w 1 ( � x )) , . . . , p n ( w n ( � x )) . it is clear that � x = x 1 , . . . , x n plays the role of parameter registers. Names are eliminated by using � t 1 , . . . , t n � . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 23 / 35
Encoding terms and registers Correspondence of concepts: Arrows (tabulations, f , g ) Arrays of terms Projections ( π i ) Pointers Domain of tabulations Free (heap) variables ( Yi ) Target of tabulations Registers ( Xi ) Composition of tabulations ( f ; g ) Substitution Intersection Term Unification Example (Term Storage in Registers) X 1 = f ( Y 1 , Y 3 ) 3 f 1 = � π 1 , π 3 � ; f � f 1 , f 2 � � f 1 , f 2 � X 2 = g ( Y 2 , a ) f 2 = � π 2 , ! 2 ; a � ; g � f 1 , f 2 � ◦ ; � f 1 , f 2 � ✛ ✲ 2 ◦ ◦ 2 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 24 / 35
The Relational Step Step 1: Local Storage We previously translated the clause: x ′ ) ← � x = � p ( � t [ � y ] , p 1 ( w 1 ( � x )) , . . . , p n ( w n ( � x )) . to t ) ∩ W 1 ; p 1 ; W ◦ 1 ∩ · · · ∩ W n ; p n ; W ◦ � p = K ( n x ′ | may not be the same. but now the number of arguments of | � x | , | � EJGA (CRI-Mines) Allegories and CLP 09/22/2013 25 / 35
The Relational Step Step 1: Local Storage We previously translated the clause: x ′ ) ← � x = � p ( � t [ � y ] , p 1 ( w 1 ( � x )) , . . . , p n ( w n ( � x )) . to t ) ∩ W 1 ; p 1 ; W ◦ 1 ∩ · · · ∩ W n ; p n ; W ◦ � p = K ( n x ′ | may not be the same. but now the number of arguments of | � x | , | � Step 2: Environments We introduce environment creation (and its reciprocal, destruction) relations I MN = � π 1 , . . . , π m � ◦ : M → N . Now the clause is translated to: � t ) ∩ W 1 ; p 1 ; W ◦ 1 ∩ · · · ∩ W n ; p n ; W ◦ n ) ; I ◦ I MN ; ( K ( MN M are parameters, N − M is the number of local variables. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 25 / 35
Environment Management Environments: I MN We introduce an environment creation 3 (and its reciprocal, destruction) relation � π 2 I MN = � π 1 , . . . , π m � ◦ : M → N . i d , 3 π 1 � 1 ) ◦ : 1 → 2 is the canonical “new” ✛ ✲ ( π 2 I 23 2 ✛ 3 ✲ variable creation relation. Example (Compile time optimization) 2 � π � g Let two registers in 1 ; f ; 1 , π π R = � π 1 ; f , π 1 ; g � : 1 → 2. , f 1 ; g Compute R ◦ ; R ; I 23 . ; 1 π , π � 2 � ✲ ✛ R ; R ◦ ; I 23 2 ✛ 3 ✲ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 26 / 35
Intersection and Procedure Call Coreflexive Arrows If R , S are coreflexive then R ∩ S = R ; S . This is highly convenient, and we may eliminate ∩ and simplify our machine. Procedure Call The previous translation is semantically correct translation, but W i ; I NK ; p i ; I ◦ NK ; W ◦ i : N → N is not in general a coreflexive relation, so we cannot apply ∩ elimination. We fix this using a correflexive version: ( id M − α ( p i ) × p i ) N → N . Then, if A i = N − α ( p i ) the final translation is: � t ) ; W 1 ; ( id A 1 × p 1 ) ; W ◦ 1 ; . . . ; W n ; ( id A n × p n ) ; W ◦ n ) ; I ◦ p = I MN ; ( K ( MN EJGA (CRI-Mines) Allegories and CLP 09/22/2013 27 / 35
Example: Partial Evaluation Translation of add add ( x 1 , x 2 , x 3 ) ← x 1 = o , x 2 = y 1 , x 3 = y 1 . add ( x 1 , x 2 , x 3 ) ← x 1 = s ( y 1 ) , x 2 = y 2 , x 3 = s ( y 3 ) , x 4 = y 1 , x 5 = y 3 , add ( x 4 , x 2 , x 5 ) . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 28 / 35
Example: Partial Evaluation Translation of add add ( x 1 , x 2 , x 3 ) ← x 1 = o , x 2 = y 1 , x 3 = y 1 . add ( x 1 , x 2 , x 3 ) ← x 1 = s ( y 1 ) , x 2 = y 2 , x 3 = s ( y 3 ) , x 4 = y 1 , x 5 = y 3 , add ( x 4 , x 2 , x 5 ) . � o , π 1 , π 1 � ◦ ; � o , π 1 , π 1 � add = I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ∪ 35 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 28 / 35
Example: Partial Evaluation Translation of add add ( x 1 , x 2 , x 3 ) ← x 1 = o , x 2 = y 1 , x 3 = y 1 . add ( x 1 , x 2 , x 3 ) ← x 1 = s ( y 1 ) , x 2 = y 2 , x 3 = s ( y 3 ) , x 4 = y 1 , x 5 = y 3 , add ( x 4 , x 2 , x 5 ) . � o , π 1 , π 1 � ◦ ; � o , π 1 , π 1 � add = I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ∪ 35 Unfold Execute add without a query! � o , π 1 , π 1 � ◦ ; � o , π 1 , π 1 � = add � os , π 1 , π 1 s � ◦ ; � os , π 1 , π 1 s � ∪ I 35 ; � π 1 ss , π 2 , π 3 ss , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ∪ 35 etc. . . EJGA (CRI-Mines) Allegories and CLP 09/22/2013 28 / 35
Composition of tabular relations: the core ⇒ M N g h i f ✲ ✲ ✛ ✛ A B C EJGA (CRI-Mines) Allegories and CLP 09/22/2013 29 / 35
Composition of tabular relations: the core P ⇒ ✛ ✲ M N g h i f ✲ ✲ ✛ ✛ A B C EJGA (CRI-Mines) Allegories and CLP 09/22/2013 29 / 35
Composition of tabular relations: the core D � u v P ⇒ ✲ ✛ ✛ ✲ M N g h i f ✲ ✛ ✲ ✛ A B C EJGA (CRI-Mines) Allegories and CLP 09/22/2013 29 / 35
Composition of tabular relations: the core D � u v P ⇒ ✲ ✛ ✛ ✲ M N D v f g h i f u ; ; i ✲ ✛ ✲ ✲ ✛ ✛ A B C A C EJGA (CRI-Mines) Allegories and CLP 09/22/2013 29 / 35
Composition of tabular relations: the core D � u v P ⇒ ✲ ✛ ✛ ✲ M N D v f g h i f u ; ; i ✲ ✛ ✲ ✲ ✛ ✛ A B C A C Composition captures unification, parameter passing, renaming apart, variable allocation and (a form of) garbage collection. EJGA (CRI-Mines) Allegories and CLP 09/22/2013 29 / 35
The Pullback Algorithm Definition (Arrow Normalization) We write → ! R for the associated normalizing relation based on → R : h ; � f , g � → R � h ; f , h ; g � � f , g � ; π 1 → R f � f , g � ; π 2 → R g → R f : M → N f ; ! N ! M Definition (Starting Diagram) For a pullback problem, build the pre-starting diagram P : π 1 ; f ✲ N × N ′ ✲ M π 2 ; g EJGA (CRI-Mines) Allegories and CLP 09/22/2013 30 / 35
The Pullback Algorithm The starting diagram is: f ′ N + N ′ id = � π 1 , . . . , π N + N ′ � ✲ ✲ N + N ′ ✲ M g ′ f ′ = � f 1 , . . . , f M � , g ′ = � g 1 , . . . , g M � , S = { f 1 ≈ g 1 , . . . , f M ≈ g M } . Initial state ( S | � π 1 , . . . , π N + N ′ � ) . Proceed iteratively: ≈ ⇒ ! M ; a ! M ; b Fail ! M ; a ≈ h ; f ⇒ Fail g ′ ; f ′ ≈ ⇒ g ; f Fail ( S ′ | S ( j , π i , h )) ≈ ⇒ π i π j ( S ′ | S ( i , g ; f , h )) ≈ ⇒ π i g ; f ( S ′ | S ( i , ! M ; a , h )) ! M ; a ≈ ⇒ π i ( S ′ | h ) ≈ ⇒ ! M ; a ! M ; a n } ∪ S ′ | h ) g ′ ; f ( { g 1 ≈ g ′ 1 } ∪ · · · ∪ { g n ≈ g ′ g ; f ≈ ⇒ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 31 / 35
Specification of the machine Diagram Rewriting Basic diagrams: ( f | g ) , R 1 ∪ · · · ∪ R n and ( f | � g , [ R ] � ) . ( h , h ′ ) ( f | g ) ; ( f ′ | g ′ ) ( h ; f | h ′ ; g ′ ) = = ⇒ ( f | � g K , g N � ) ; ( id K × p N ) = ⇒ ( f | � g K , [ g N ; p 1 ] � ) ∪ . . . ∪ ( f | � g K , [ g N ; p n � ]) ( f | � g , [( g ′ | g ′ )] � ) = ⇒ ( f | � g , g � ) ( h ; f | � h ; g , [ E ′ ] � ) ⇒ E ′ ( f | � g , [ E ] � ) = ⇒ iff E = R ′ ∪ S ⇒ R ′ R ∪ S = ⇒ iff R = 0 ∪ S = ⇒ S EJGA (CRI-Mines) Allegories and CLP 09/22/2013 32 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ ( � π 1 s , π 2 , π 3 � ; � o , π 1 , π 1 � ) ∪ . . . ⇒ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ ( � π 1 s , π 2 , π 3 � ; � o , π 1 , π 1 � ) ∪ . . . ⇒ 0 ∪ � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ ( � π 1 s , π 2 , π 3 � ; � o , π 1 , π 1 � ) ∪ . . . ⇒ 0 ∪ � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ ( � π 1 s , π 2 , π 3 � ; � o , π 1 , π 1 � ) ∪ . . . ⇒ 0 ∪ � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 � | � π 1 s , π 2 , π 3 , π 4 , π 5 � ) ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ ( � π 1 s , π 2 , π 3 � ; � o , π 1 , π 1 � ) ∪ . . . ⇒ 0 ∪ � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 � | � π 1 s , π 2 , π 3 , π 4 , π 5 � ) ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 s � | � π 1 s , π 2 , π 3 s , π 1 , π 3 � ) ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ ( � π 1 s , π 2 , π 3 � ; � o , π 1 , π 1 � ) ∪ . . . ⇒ 0 ∪ � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 � | � π 1 s , π 2 , π 3 , π 4 , π 5 � ) ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 s � | � π 1 s , π 2 , π 3 s , π 1 , π 3 � ) ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 s � | � π 1 s , π 3 s , π 1 , π 2 , π 3 � ) ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
The Machine: An Example A query add ( s ( X ) , Y , Z ) is translated to � π 1 s , π 2 , π 3 � ; add : � π 1 s , π 2 , π 3 � ; add ⇒ ( � π 1 s , π 2 , π 3 � ; � o , π 1 , π 1 � ) ∪ . . . ⇒ 0 ∪ � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 � π 1 s , π 2 , π 3 � ; I 35 ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 � | � π 1 s , π 2 , π 3 , π 4 , π 5 � ) ; � π 1 s , π 2 , π 3 s , π 1 , π 3 � ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 s � | � π 1 s , π 2 , π 3 s , π 1 , π 3 � ) ; W ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 s � | � π 1 s , π 3 s , π 1 , π 2 , π 3 � ) ; ( id 2 × add ) ; W ◦ ; I ◦ ⇒ 35 ( � π 1 s , π 2 , π 3 s � | � π 1 s , π 3 s , [ � π 1 , π 2 , π 3 � ; � o , π 1 , π 1 � ] � ; W ◦ ; I ◦ 35 ∪ . . . ⇒ EJGA (CRI-Mines) Allegories and CLP 09/22/2013 33 / 35
Recommend
More recommend