• Tidak ada hasil yang ditemukan

Type Inference In the Context of FORMULA Rules

5.5 Static Analysis on FORMULA Programs

5.5.3 Type Inference In the Context of FORMULA Rules

GroundTermby filtering out all terms that are not non-ground terms. The next step is to check if two non- ground terms can be unified or not since each non-ground term represents a unique type or a set of ground terms. Here we use the same tactic to find unifiable pairs of terms by finding the non-unifiable pairs of terms first. The relationDisjointSetRelholds all pairs of terms that cannot be unified because two ground terms are obviously not the same term or two non-ground terms have different ground terms inside themselves at the same position. Once DisjointSetRelis derived, the relationNonDisjointSetRel can be easily derived by negation in the exact same wayNonGroundTermis derived. BothDisjointSetRel andNonDisjointSetRelis important to the reasoning over FORMULA programs because FORMULA has its own unique type semantics and the order of rule execution or rule stratification changes based on the type semantics of terms.

The first predicateGigaEdge(a,b)is matched first in the rule execution with variable a matched to a collection of terms of typeAnyand with variableb matched to a collection of terms of typeNode. The second predicateGigaEdge(b,c)has to use theto any()function to convert every term ofNodetype from variableb into a term ofAnytype because in DDLog only two terms of the same type can be compared otherwise they are treated as two different terms even though one term has the exact same meaning as the other one.

The type of the variable is determined by the position of its first occurrence because DDLog rule execution strictly starts from left to right to build a dataflow graph. For example, the variableais set toAnytype in the first occurrence inGigaEdge(a,b)so the type of variableais firstly fixed toAnytype but in later predicate, it has to be converted back intoNodetype in predicateGigaEdge(c,a)with additional functionf rom any() wrapping around variablea. The same conversion applies to the variables in the predicates in the head of the rule in order to derive the terms with the correct type.

In order to translate the FORMULA rule into an equivalent DDLog rule, we create a new relation named ConsVarConversionto track the position of every variable in a predicate of a rule and an enumeration type to decide if keywordto any()or f rom any()has to be added for the type conversion in DDLog’s dataflow construction.

Since DDLog always starts from the left side, every variable in the leftmost predicate does not need to be converted and typeConversionis an enumeration type that decides how the term should be converted such as FromAny, ToAny or NoConversion. The following rule finds out the first predicate represented asConstraintInRule[(0,PredCons f alse,pos term, ,rule)]in all existing rules and derives some new facts saying no conversion is needed for the first occurrence of a variable in the first predicate.

C o n s V a r C o n v e r s i o n [ ( 0 , i n d i c e s , v a r i a b l e , p o s t e r m , r u l e , N o C o n v e r s i o n ) ] : − D e s c e n d e n t V a r O f T e r m [ ( i n d i c e s , v a r i a b l e , p o s t e r m ) ] ,

C o n s t r a i n t I n R u l e [ ( 0 , P r e d C o n s {f a l s e , p o s t e r m , }, r u l e ) ] .

Listing 5.16: No Conversion for the Leftmost Predicate Expressed in DDLog Rule

Since the first occurrence of a variable in the rule (anchor variable) may not occur in the first predicate in a rule, we have to find which predicate in the rule has the first occurrence of a certain variable, and that variable never occur in any of the previous predicates on the left side in a FORMULA rulehead : -pred1,pred2,predm(v)...,predn(v)in which the first variablevoccurs inpredm(v). The following DDLog rule accumulates all variables that occur before the current predicate in predicatespred1,pred2, ...,predm−1

and negation is applied to find all matches in which no variablevin the accumulated variable set. The newly derived facts in relationConsVarConversionhave the last argument set toNoConversion because the

first variable that occurs in a rule is an anchor variable that determines the type of that variable in the rule and other variables are converted based on the type of the anchor variable.

C o n s V a r C o n v e r s i o n [ ( i n d e x 2 , i n d i c e s 2 , v a r i a b l e 2 , p o s t e r m 2 , r u l e 2 , N o C o n v e r s i o n ) ] : −

/ / L o c a t e a v a r i a b l e i n a FORMULA r u l e

C o n s t r a i n t I n R u l e [ ( i n d e x , P r e d C o n s {f a l s e , p o s t e r m , }, r u l e ) ] , D e s c e n d e n t V a r O f T e r m [ ( i n d i c e s , v a r i a b l e , p o s t e r m ) ] ,

/ / F i n d a l l p r e d i c a t e s and v a r i a b l e s b e f o r e a c e r t a i n p r e d i c a t e

C o n s t r a i n t I n R u l e [ ( p r e i n d e x , P r e d C o n s {f a l s e , p r e p o s t e r m , }, r u l e ) ] , p r e i n d e x < i n d e x ,

D e s c e n d e n t V a r O f T e r m [ ( p r e i n d i c e s , p r e v a r i a b l e , p r e p o s t e r m ) ] ,

/ / F i n d a l l v a r i a b l e s i n t h e p r e v i o u s p r e d i c a t e s b e f o r e t h e c u r r e n t p r e d i c a t e v a r p r e v a r g r o u p = ( p r e v a r i a b l e ) . g r o u p b y ( ( i n d e x , i n d i c e s , p o s t e r m ,

v a r i a b l e , r u l e ) ) ,

( v a r i n d e x 2 , v a r i n d i c e s 2 , v a r p o s t e r m 2 , v a r v a r i a b l e 2 , v a r r u l e 2 ) = p r e v a r g r o u p . g r o u p k e y ( ) ,

v a r v a r i a b l e s e t = p r e v a r g r o u p . t o h a s h s e t ( ) , n o t h a s h s e t c o n t a i n s ( v a r i a b l e s e t , v a r i a b l e 2 ) .

Listing 5.17: DDLog Rule for Finding Anchor Variables

The places to addto any()conversion are where a variablevof a union type appears inpredn(v)but appears not to be a union type inpredm(v). predm(v)must be prior topredn(v)and this is one of the situations in that we need to addto any()keyword to denote the conversion when translating from FORMULA rules to DDLog rules. The following DDLog rule has two parts the first part enumerates all variables of union type and the second part enumerates all possible predicates that contain the same variable but do not have to be converted or in other words the anchor variable.

C o n s V a r C o n v e r s i o n [ ( i n d e x , i n d i c e s , v a r i a b l e , p o s t e r m , r u l e , ToAny ) ] : − / / F i n d t h e v a r i a b l e t h a t i s o f u n i o n t y p e .

C o n s t r a i n t I n R u l e [ ( i n d e x , P r e d C o n s {f a l s e , p o s t e r m , }, r u l e ) ] , D e s c e n d e n t V a r O f T e r m [ ( i n d i c e s , v a r i a b l e , p o s t e r m ) ] ,

D e s c e n d e n t T e r m T y p e S p e c [ ( i n d i c e s , v a r i a b l e , p o s t e r m , UnionType{ }) ] ,

/ / F i n d a n o t h e r p r e d i c a t e t e r m p r i o r t o t h e c u r r e n t p r e d i c a t e

/ / t h a t h a s t h e same v a r i a b l e i n i t b u t n o t u n i o n t y p e .

D e s c e n d e n t T e r m T y p e S p e c [ ( p r e i n d i c e s , v a r i a b l e , p r e p o s t e r m , n o n u n i o n t y p e ) ] , n o t i s u n i o n t y p e ( n o n u n i o n t y p e ) ,

C o n s V a r C o n v e r s i o n [ ( p r e i n d e x , p r e i n d i c e s , v a r i a b l e , p r e p o s t e r m , r u l e , N o C o n v e r s i o n ) ] ,

p r e i n d e x < i n d e x .

Listing 5.18: DDLog Rule for LocatingToAnyConversion Places

The first occurrence of variablevis of union type in predm(v)but the next one is not a union type in predn(v)and we need to convert from union type to regular type with the keyword f rom any().

C o n s V a r C o n v e r s i o n [ ( i n d e x , i n d i c e s , v a r i a b l e , p o s t e r m , r u l e , FromAny ) ] : − / / The f i r s t o c c u r r e n c e o f v a r i a b l e t h a t i s b o r n t o be u n i o n t y p e . C o n s V a r C o n v e r s i o n [ ( p r e i n d e x , p r e i n d i c e s , v a r i a b l e , p r e p o s t e r m , r u l e , N o C o n v e r s i o n ) ] ,

D e s c e n d e n t T e r m T y p e S p e c [ ( p r e i n d i c e s , v a r i a b l e , p r e p o s t e r m , UnionType{ }) ] , / / I n t h e c u r r e n t p r e d i c a t e , t h e v a r i a b l e i s n o t u n i o n t y p e .

C o n s t r a i n t I n R u l e [ ( i n d e x , P r e d C o n s {f a l s e , p o s t e r m , }, r u l e ) ] , p r e i n d e x < i n d e x ,

D e s c e n d e n t V a r O f T e r m [ ( i n d i c e s , v a r i a b l e , p o s t e r m ) ] ,

D e s c e n d e n t T e r m T y p e S p e c [ ( i n d i c e s , v a r i a b l e , p o s t e r m , n o n u n i o n t y p e ) ] , n o t i s u n i o n t y p e ( n o n u n i o n t y p e ) .

Listing 5.19: DDLog Rule for LocatingToAnyConversion Places