How does a DFA work as a type predicate? number 2 number ( a 1 1.0 b "a" symbol symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number number 2 1 1.0 b "a" (a symbol symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number 2 number 1.0 b "a" symbol (a 1 symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number number 2 b "a" (a 1 1.0 symbol symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number number 2 "a" symbol (a 1 1.0 b symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number 2 number (a 1 1.0 b "a" symbol symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number 2 number (a 1 1.0 b "a" symbol symbol 0 1 "the" symbol "an" c 2 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number number 2 (a 1 1.0 b "a" symbol symbol 0 1 c 2 "an" "the" symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number number 2 (a 1 1.0 b "a" symbol symbol 0 1 2 "an" "the" c symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number 2 number (a 1 1.0 b "a" symbol symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
How does a DFA work as a type predicate? number number 2 (a 1 1.0 b "a" symbol "an" "the" c 2 symbol 0 1 222 d 2/3) symbol 22 string 3 string 15 / 61
How does a DFA work as a type predicate? number 2 number (a 1 1.0 b "a" symbol symbol "an" "the" c 2 0 1 symbol d 2/3) 22 222 string 3 string 15 / 61
How does a DFA work as a type predicate? number number 2 (a 1 1.0 b "a" symbol symbol "an" "the" c 2 0 1 symbol 2/3 ) 22 222 d string 3 string 15 / 61
How does a DFA work as a type predicate? Yes, it’s a match! number number 2 (a 1 1.0 b "a" symbol symbol 0 1 "an" "the" c 2 symbol 22 222 d 2/3) string 3 string 15 / 61
Code generated from ( symbol · ( number + ∨ string + )) + number ( tagbody 0 number 2 ( u n l e s s seq ( r e t u r n n i l )) ( typecase ( pop seq ) ( symbol ( go 1)) symbol symbol 0 1 ( t ( r e t u r n n i l ) ) ) symbol 1 ( u n l e s s seq ( r e t u r n n i l )) string 3 ( typecase ( pop seq ) ( number ( go 2)) ( s t r i n g ( go 3)) string ( t ( r e t u r n n i l ) ) ) 3 2 ( u n l e s s seq ( r e t u r n t )) ( u n l e s s seq ( r e t u r n t )) ( typecase ( pop seq ) ( typecase ( pop seq ) ( s t r i n g ( go 3)) ( number ( go 2)) ( symbol ( go 1)) ( symbol ( go 1)) ( t ( r e t u r n n i l ) ) ) ) ) ( t ( r e t u r n n i l ) ) ) 16 / 61
Lambda-lists characterized by RTEs A lambda-list in Common Lisp has a fixed part (defun foo (a b) ...) 17 / 61
Lambda-lists characterized by RTEs A lambda-list in Common Lisp has a fixed part, an optional part (defun foo (a b &optional c) ...) 17 / 61
Lambda-lists characterized by RTEs A lambda-list in Common Lisp has a fixed part, an optional part, and a repeating part. (defun foo (a b &optional c &key x y) ...) 17 / 61
Lambda-lists characterized by RTEs A lambda-list in Common Lisp has a fixed part, an optional part, and a repeating part part. Any of the variables may be restricted by type declarations. (defun foo (a b &optional c &key x y) (declare (type integer a x) (type string b c y)) ...) 17 / 61
Lambda-lists characterized by RTEs A lambda-list in Common Lisp has a fixed part, an optional part, and a repeating part part. Any of the variables may be restricted by type declarations. (defun foo (a b &optional c &key x y) (declare (type integer a x) (type string b c y)) ...) The set of valid argument lists for a function may be characterized by an RTE. 17 / 61
destructuring-bind is a different syntax for calling an anonymous function. ( destructuring − bind ( a (b c ) &key ( x t ) ( y ”” ) z ) DATA ( d e c l a r e ( type fixnum a b c z ) ( type symbol x ) ( type s t r i n g y )) . . . body . . . ) 18 / 61
destructuring-bind is a different syntax for calling an anonymous function. ( destructuring − bind ( a (b c ) &key ( x t ) ( y ”” ) z ) DATA ( d e c l a r e ( type fixnum a b c z ) ( type symbol x ) ( type s t r i n g y )) . . . body . . . ) For example: DATA = (2 (3 4) :y "a" :x ’b) ; YES 18 / 61
destructuring-bind is a different syntax for calling an anonymous function. ( destructuring − bind ( a (b c ) &key ( x t ) ( y ”” ) z ) DATA ( d e c l a r e ( type fixnum a b c z ) ( type symbol x ) ( type s t r i n g y )) . . . body . . . ) For example: DATA = (2 (3 4) :y "a" :x ’b) ; YES DATA = (2 (3 4) :y "a" :x ’b :x 42 :y "hello" :y nil) ; YES 18 / 61
destructuring-bind is a different syntax for calling an anonymous function. ( destructuring − bind ( a (b c ) &key ( x t ) ( y ”” ) z ) DATA ( d e c l a r e ( type fixnum a b c z ) ( type symbol x ) ( type s t r i n g y )) . . . body . . . ) For example: DATA = (2 (3 4) :y "a" :x ’b) ; YES DATA = (2 (3 4) :y "a" :x ’b :x 42 :y "hello" :y nil) ; YES DATA = (2 (3 4) :y "a" :x 42 :x ’b) ; NO An invalid argument list will signal an error at run-time. 18 / 61
QUESTION: Can we select an appropriate lambda-list matching DATA , avoiding a run-time error? We propose destructuring-case . ( d e s t r u c t u r i n g − c a s e DATA ; ; Case − 1 (( a b &o p t i o n a l ( c ”” )) ( d e c l a r e ( type i n t e g e r a ) ( type s t r i n g b c )) . . . body . . . ) ; ; Case − 2 (( a ( b c ) &key ( x t ) ( y ”” ) z ) ( d e c l a r e ( type fixnum a b c ) ( type symbol x ) ( type s t r i n g y ) ( type l i s t z )) . . . body . . . ) ) 19 / 61
QUESTION: Can we select an appropriate lambda-list matching DATA , avoiding a run-time error? We propose destructuring-case . ( d e s t r u c t u r i n g − c a s e DATA ; ; Case − 1 (( a b &o p t i o n a l ( c ”” )) ( d e c l a r e ( type i n t e g e r a ) ( type s t r i n g b c )) ◮ integer · string · string ? . . . body . . . ) ; ; Case − 2 (( a ( b c ) &key ( x t ) ( y ”” ) z ) ( d e c l a r e ( type fixnum a b c ) ( type symbol x ) ( type s t r i n g y ) ( type l i s t z )) . . . body . . . ) ) 19 / 61
QUESTION: Can we select an appropriate lambda-list matching DATA , avoiding a run-time error? We propose destructuring-case . ( d e s t r u c t u r i n g − c a s e DATA ; ; Case − 1 ◮ integer · string · string ? (( a b &o p t i o n a l ( c ”” )) ( d e c l a r e ( type i n t e g e r a ) ( type s t r i n g b c )) integer string 0 1 2 string . . . body . . . ) 3 ; ; Case − 2 (( a ( b c ) &key ( x t ) ( y ”” ) z ) ( d e c l a r e ( type fixnum a b c ) ( type symbol x ) ( type s t r i n g y ) ( type l i s t z )) . . . body . . . ) ) 19 / 61
QUESTION: Can we select an appropriate lambda-list matching DATA , avoiding a run-time error? We propose destructuring-case . ( d e s t r u c t u r i n g − c a s e DATA ; ; Case − 1 ◮ integer · string · string ? (( a b &o p t i o n a l ( c ”” )) ( d e c l a r e ( type i n t e g e r a ) ( type s t r i n g b c )) integer string 0 1 2 string . . . body . . . ) 3 ; ; Case − 2 ◮ What is the rational (( a ( b c ) &key ( x t ) ( y ”” ) z ) ( d e c l a r e ( type fixnum a b c ) type expression? ( type symbol x ) ( type s t r i n g y ) ( type l i s t z )) . . . body . . . ) ) 19 / 61
QUESTION: Can we select an appropriate lambda-list matching DATA , avoiding a run-time error? We propose destructuring-case . ( d e s t r u c t u r i n g − c a s e DATA ; ; Case − 1 ◮ integer · string · string ? (( a b &o p t i o n a l ( c ”” )) ( d e c l a r e ( type i n t e g e r a ) ( type s t r i n g b c )) integer string 0 1 2 string . . . body . . . ) 3 ; ; Case − 2 ◮ What is the rational (( a ( b c ) &key ( x t ) ( y ”” ) z ) ( d e c l a r e ( type fixnum a b c ) type expression? ( type symbol x ) ◮ What is the DFA? ( type s t r i n g y ) ( type l i s t z )) . . . body . . . ) ) 19 / 61
RTE auto-generated from destructuring lambda-list ( : cat ( : cat fixnum ( : and l i s t ( r t e ( : cat fixnum fixnum ) ) ) ) ( : and ( : ∗ ( : cat ( : or ( e q l : x ) ( e q l : y ) ( e q l : z )) t )) ( : cat ( : ∗ ( : cat ( not ( e q l : x )) t )) ( : ? ( : cat ( e q l : x ) symbol ( : ∗ t ) ) ) ) ( : cat ( : ∗ ( : cat ( not ( e q l : y )) t )) ( : ? ( : cat ( e q l : y ) s t r i n g ( : ∗ t ) ) ) ) ( : cat ( : ∗ ( : cat ( not ( e q l : z )) t )) ( : ? ( : cat ( e q l : z ) l i s t ( : ∗ t ) ) ) ) ) ) 20 / 61
DFA corresponding to auto-generated RTE 5 T7 T3 6 T4 9 T8 T5 4 T5 T6 T9 8 T10 7 T7 12 T10 3 T6 27 T3 T3 T7 11 T7 26 T11 T4 T8 T8 T4 15 10 24 25 T4 13 T12 T1 T2 0 1 2 T5 T5 T7 14 16 28 T10 T10 T6 T6 19 T7 21 22 T13 T3 T8 T3 20 T4 23 17 T7 T5 18 T 1 = fixnum T 2 = (and list (rte (:cat fixnum fixnum))) T 3 = (eql :x) T 4 = (eql :y) T 5 = (eql :z) T 6 = symbol T 7 = t T 8 = string T 9 = (member :x :y) T 10 = list T 11 = (member :x :y :z) T 12 = (member :x :z) T 13 = (member :y :z) 21 / 61
DFA corresponding to auto-generated RTE 5 T7 T3 6 T4 9 T8 T5 4 T5 T6 T9 8 T10 7 T7 12 T10 3 T6 27 T3 T3 T7 11 T7 26 T8 T11 T8 T4 T4 15 10 24 25 13 T4 T12 T1 T2 0 1 2 T5 T5 T7 14 16 28 T10 T10 T6 T6 19 T7 21 22 T13 T3 T8 T3 T4 20 23 17 T7 T5 18 Multiple transitions from states give rise to serialization problem. 21 / 61
Rational Type Expressions (RTEs) with overlapping types ( number · integer ) ∨ ( integer · number ) We have non-deterministic (NFA). integer ⊂ number integer 1 integer number 2 number 0 integer number 3 22 / 61
Rational Type Expressions (RTEs) with overlapping types ( number · integer ) ∨ ( integer · number ) We want deterministic (DFA). (and number (not integer)) 1 integer 2 number 0 integer 3 22 / 61
Maximal Disjoint Type Decomposition 23 / 61
MDTD: decompose a set of types into disjoint types ◮ Given A i as possibly overlapping regions, A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 24 / 61
MDTD: decompose a set of types into disjoint types ◮ Given A i as possibly overlapping regions, ◮ Calculate X i as disjoint regions. A 1 A 3 A 2 X 13 X 2 X 3 X 11 X 1 X 12 X 10 A 4 A 6 X 6 X 4 X 9 A 5 A 7 X 7 X 5 A 8 X 8 24 / 61
MDTD problem: Baseline algorithm Are there any disjoint sets? A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm Yes, A 7 intersects no other set. A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm So collect it into D : D = { A 7 } A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm Select any intersecting pair of sets. A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm E.g. , A 2 . Does A 2 intersect anything? A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm Yes. A 2 intersects A 4 . A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm So calculate the standard partition of A 2 and A 4 A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm The standard partition is { A 2 ∩ A 4 , ... } A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm The standard partition is { A 2 ∩ A 4 , A 4 ∩ A 2 , ... } A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm The standard partition is { A 2 ∩ A 4 , A 4 ∩ A 2 , A 2 ∩ A 4 } A 1 A 3 A 2 A 4 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm So remove { A 2 , A 4 } and add { A 2 ∩ A 4 , A 4 ∩ A 2 , A 2 ∩ A 4 } . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm Now, restart. Anything disjoint from everything else? No. A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm So select any intersecting pair. A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm E.g. , A 4 ∩ A 2 . Does it intersect anything? A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm Yes, it intersects A 5 . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm So calculate the standard partition of A 5 and A 4 ∩ A 2 . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm The standard partition is { A 5 , ... } . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm The standard partition is { ..., A 4 ∩ A 2 ∩ A 5 } . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm The standard partition is { A 5 , A 4 ∩ A 2 ∩ A 5 } . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm So remove { A 5 , A 4 ∩ A 2 } and add { A 5 , A 4 ∩ A 2 ∩ A 5 } . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 ∩ A 5 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm So remove { A 5 , A 4 ∩ A 2 } and add { A 5 , A 4 ∩ A 2 ∩ A 5 } . A 5 is in both sets. We can optimize, because A 5 ⊂ A 4 ∩ A 2 . A 1 A 3 A 2 ∩ A 4 A 2 ∩ A 4 A 4 ∩ A 2 ∩ A 5 A 6 A 5 A 7 A 8 25 / 61
MDTD problem: Baseline algorithm Continue the procedure until collecting all the pairwise disjoint sets. X 13 X 2 X 3 X 11 X 1 X 12 X 10 X 6 X 4 X 9 X 7 X 5 X 8 25 / 61
MDTD problem: Baseline algorithm Calculating all the colored regions as subsets of original overlapping sets. X 13 X 2 X 3 X 11 X 1 X 12 X 10 X 6 X 4 X 9 X 7 X 5 X 8 26 / 61
X5 =? =? =? =? X3 X4 X6 X11 List with set semantics ◮ Insertion into list with set semantics has linear complexity. ◮ Type equivalence check is X i ⊂ X j ∧ X j ⊂ X i ? ◮ And prevents us from using a hash table to implement sets. ◮ This equivalence function is SLOW! 27 / 61
MDTD result: type specifiers are explosive in size X2 : ( and ( and ( and A2 ( not ( and ( and A4 ( not ( and ( and A1 ( not A2 ) ) A3 ) ) ) ( and A3 ( not ( and A1 ( not A2 ) ) ) ) ) ) ) ( not ( and ( and A3 ( not ( and A1 ( not A2 ) ) ) ) ( not ( and A4 ( not ( and ( and A1 ( not A2 ) ) A3 ) ) ) ) ) ) ) ( not ( and ( and A4 ( not ( and ( and A1 ( not A2 ) ) A3 ) ) ) ( not ( and A3 ( not ( and A1 ( not A2 ) ) ) ) ) ) ) ) X3 : ( and ( and ( and A1 ( not A2 )) A3) ( not A4 ) ) X10 : ( and ( and ( and A2 ( not ( and ( and A4 ( not ( and ( and A1 ( not A2 ) ) A3 ) ) ) ( and A3 ( not ( and A1 ( not A2 ) ) ) ) ) ) ) ( not ( and ( and A3 ( not ( and A1 ( not A2 ) ) ) ) ( not ( and A4 ( not ( and ( and A1 ( not A2 ) ) A3 ) ) ) ) ) ) ) ( and ( and A4 ( not ( and ( and A1 ( not A2 ) ) A3 ) ) ) ( not ( and A3 ( not ( and A1 ( not A2 ) ) ) ) ) ) ) 28 / 61
Problems with baseline algorithm ◮ Explosive size of type specifiers ◮ O ( n 2 ) search on each iteration ◮ Set semantics for lists of types: ◮ To maintain uniqueness, O ( n ) list-insertion. ◮ Equivalent types may appear in many different forms. ... No canonical form ◮ Slow set-equivalence algorithm. ◮ Many redundant checks ◮ subtypep may return don’t-know 29 / 61
Strategies to Improving MDTD algorithm We can do better. ◮ Optimize current algorithm (caching etc). ◮ Change the algorithm. ◮ Change the data structure representing the sets (CL types). 30 / 61
Graph-based MDTD 1 A 1 A 3 8 6 A 2 4 A 4 A 6 A 5 3 5 A 7 A 8 7 2 Topology graph representing type hierarchy and intersections. We find MDTD by controlled breaking and re-wiring of this graph. 31 / 61
Step 0 Node Boolean Standard expression partition → A 1 ∩ A 6 1 A 1 1 2 A 2 3 A 3 6 8 4 A 4 5 A 5 4 6 A 6 collect into D A 6 8 A 8 3 5 X 7 A 7 2 32 / 61
Step 1 Node Boolean Standard expression partition → A 1 ∩ A 6 ∩ A 5 1 A 1 1 2 A 2 3 A 3 8 4 → A 4 ∩ A 5 A 4 5 A 5 A 5 collect into D 4 8 → A 8 ∩ A 5 A 8 X 6 A 6 5 3 X 7 A 7 2 33 / 61
Recommend
More recommend