Programmatic Manipulation of Type Specifiers
in Common Lisp
Jim Newton
10th European Lisp Symposium
3-4 April 2017
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 1 / 43
Overview
1
Common Lisp Types
Native type specifiers
Type calculus with type specifiers
2
Reduced Ordered Binary Decision Diagrams (ROBDDs)
Representing CL types as ROBDDs
Reductions to accommodate CL subtypes
Type calculus using ROBDDs
Type checking and code generation with BDDs
3
Conclusion
Summary
Questions
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 2 / 43
Common Lisp Types
Table of Contents
1
Common Lisp Types
Native type specifiers
Type calculus with type specifiers
2
Reduced Ordered Binary Decision Diagrams (ROBDDs)
Representing CL types as ROBDDs
Reductions to accommodate CL subtypes
Type calculus using ROBDDs
Type checking and code generation with BDDs
3
Conclusion
Summary
Questions
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 3 / 43
Common Lisp Types
Types are sets. Subtypes are subsets. Intersecting types
are intersecting sets. Disjoint types are disjoint sets.
unsigned-byte
bit
fixnum
rational
float
number
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 4 / 43
Common Lisp Types Native type specifiers
Type specifiers are powerful and intuitive
Type specifiers can be extremely intuitive thanks to homoiconicity.
Simple
integer
Compound type specifiers
(satisfies oddp)
(and (or number string) (not (satisfies MY-FUN)))
Specifiers for the empty type
nil
(and number string)
(and (satisfies evenp) (satisfies oddp))
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 5 / 43
Common Lisp Types Native type specifiers
Type specifiers are powerful and intuitive
Type specifiers can be extremely intuitive thanks to homoiconicity.
Simple
integer
Compound type specifiers
(satisfies oddp)
(and (or number string) (not (satisfies MY-FUN)))
Specifiers for the empty type
nil
(and number string)
(and (satisfies evenp) (satisfies oddp))
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 5 / 43
Common Lisp Types Native type specifiers
Type specifiers are powerful and intuitive
Type specifiers can be extremely intuitive thanks to homoiconicity.
Simple
integer
Compound type specifiers
(satisfies oddp)
(and (or number string) (not (satisfies MY-FUN)))
Specifiers for the empty type
nil
(and number string)
(and (satisfies evenp) (satisfies oddp))
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 5 / 43
Common Lisp Types Native type specifiers
Type specifiers are powerful and intuitive
Type specifiers can be extremely intuitive thanks to homoiconicity.
Simple
integer
Compound type specifiers
(satisfies oddp)
(and (or number string) (not (satisfies MY-FUN)))
Specifiers for the empty type
nil
(and number string)
(and (satisfies evenp) (satisfies oddp))
There are many type specifiers for the same type.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 5 / 43
Common Lisp Types Type calculus with type specifiers
We can ask questions with CL type specifiers.
Type membership? (typep x T1)
Type inclusion? (subtypep T1 T2)
Type equivalence? (and (subtypep T1 T2) (subtypep T2 T1))
Type disjointness? (subtypep ‘(and ,T1 ,T2) nil)
x T
1
Sometimes, subtypep returns don’t know.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43
Common Lisp Types Type calculus with type specifiers
We can ask questions with CL type specifiers.
Type membership? (typep x T1)
Type inclusion? (subtypep T1 T2)
Type equivalence? (and (subtypep T1 T2) (subtypep T2 T1))
Type disjointness? (subtypep ‘(and ,T1 ,T2) nil)
T
1
T
2
Sometimes, subtypep returns don’t know.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43
Common Lisp Types Type calculus with type specifiers
We can ask questions with CL type specifiers.
Type membership? (typep x T1)
Type inclusion? (subtypep T1 T2)
Type equivalence? (and (subtypep T1 T2) (subtypep T2 T1))
Type disjointness? (subtypep ‘(and ,T1 ,T2) nil)
(T
1
T
2
) (T
2
T
1
)
Sometimes, subtypep returns don’t know.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43
Common Lisp Types Type calculus with type specifiers
We can ask questions with CL type specifiers.
Type membership? (typep x T1)
Type inclusion? (subtypep T1 T2)
Type equivalence? (and (subtypep T1 T2) (subtypep T2 T1))
Type disjointness? (subtypep ‘(and ,T1 ,T2) nil)
T
1
T
2
Sometimes, subtypep returns don’t know.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43
Common Lisp Types Type calculus with type specifiers
We can ask questions with CL type specifiers.
Type membership? (typep x T1)
Type inclusion? (subtypep T1 T2)
Type equivalence? (and (subtypep T1 T2) (subtypep T2 T1))
Type disjointness? (subtypep ‘(and ,T1 ,T2) nil)
Sometimes, subtypep returns don’t know.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43
Common Lisp Types Type calculus with type specifiers
Type expressions can be barely human readable.
(setf T1 ’(not (or (and fixnum unsigned-byte)
(and number float)
(and fixnum float))))
(setf T2 ’(or (and fixnum
(not rational)
(or (and number (not float))
(not number)))
(and (not fixnum)
(or (and number (not float))
(not rational)))))
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 7 / 43
Common Lisp Types Type calculus with type specifiers
Type expressions can be barely human readable.
(setf T1 ’(not (or (and fixnum unsigned-byte)
(and number float)
(and fixnum float))))
(setf T2 ’(or (and fixnum
(not rational)
(or (and number (not float))
(not number)))
(and (not fixnum)
(or (and number (not float))
(not rational)))))
The same type may be checked multiple times.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 7 / 43
Common Lisp Types Type calculus with type specifiers
Type expressions can be barely human readable.
(setf T1 ’(not (or (and fixnum unsigned-byte)
(and number float)
(and fixnum float))))
(setf T2 ’(or (and fixnum
(not rational)
(or (and number (not float))
(not number)))
(and (not fixnum)
(or (and number (not float))
(not rational)))))
The same type may be checked multiple times. We can do better.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 7 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs)
Table of Contents
1
Common Lisp Types
Native type specifiers
Type calculus with type specifiers
2
Reduced Ordered Binary Decision Diagrams (ROBDDs)
Representing CL types as ROBDDs
Reductions to accommodate CL subtypes
Type calculus using ROBDDs
Type checking and code generation with BDDs
3
Conclusion
Summary
Questions
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 8 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Type specifier viewed as a Boolean expression of variables
A CL type specifier has a dual in Boolean algebra notation.
Type specifier: (not (or (and A C) (and B C) (and B D)))
Boolean Expression: ¬ ((A C ) (B C ) (B D))
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Type specifier viewed as a Boolean expression of variables
Forget about the CL type system for the moment,
and just concentrate on Boolean algebra with binary variables.
Boolean Expression: ¬ ((A C ) (B C ) (B D))
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Type specifier viewed as a Boolean expression of variables
If we order the variables,
then every Boolean expression has a unique truth table.
Boolean Expression: ¬ ((A C ) (B C ) (B D))
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Type specifier viewed as a Boolean expression of variables
The truth table can be represented as an OBDD, ordered binary decision
diagram. A green arrow a variable being true; a red arrow represents the
variable being false.
Boolean Expression: ¬ ((A C ) (B C ) (B D))
A
B B
C C
D D
NIL NIL NIL T
D D
NIL NIL T T
C C
D D
NIL NIL
D D
T T T TNIL T
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Type specifier viewed as a Boolean expression of variables
Every path from root to leaf corresponds to one row of the truth table.
A
B B
C C
D D
NIL NIL NIL T
D D
NIL NIL T T
C C
D D
NIL NIL
D D
T T T TNIL T
A B C D ¬ ((A C ) (B C ) (B D))
> > >
> >
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Type specifier viewed as a Boolean expression of variables
Every path from root to leaf corresponds to one row of the truth table.
A
B B
C C
D D
NIL NIL NIL T
D D
NIL NIL T T
C C
D D
NIL NIL
D D
T T T TNIL T
A B C D ¬ ((A C ) (B C ) (B D))
> > >
> >
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Type specifier viewed as a Boolean expression of variables
A
B B
C C
D D
NIL NIL NIL T
D D
NIL NIL T T
C C
D D
NIL NIL
D D
T T T TNIL T
4 variables = 2
4+1
1 = 31 nodes
The graph size grows exponentially with number of variables.
We can do better.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Standard Rule 1: the terminal rule
There are 3 standard reduction rules. The terminal rule allows us to
replace leaf nodes with singleton objects, NIL and T. Divides size by 2.
NIL T
A
B B
C C
D DD D
C C
D D DD
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 10 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Standard Rule 2: the deletion rule
The deletion rule allows us to remove nodes which have the same
red (false) and green (true) pointer.
B
C C
B
CC
D DD D DDD D
NIL T
A
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 11 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Reducing to 11 nodes
NIL T
A
B B
C C C C
D D
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 12 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
More reduction
The deletion rule can be applied multiple times.
NIL T
A
B B
C C C C
D D
NIL T
B
C C C
B
D D
A
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 13 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
Standard Rule 3: the merging rule
The merging rule allows us to merge structurally congruent nodes, i.e.,
with same children, and same label.
NIL T
B
C C C
B
D D
A
NIL T
C
D
C C
A
B B
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 14 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
More congruent nodes
The merging rule can be applied multiple times.
NIL T
C
D
C C
A
B B
C
NIL
D
C
T
A
B B
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 15 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
ROBDD: Reduced ordered binary decision diagram
Started with 31 nodes, we can represent the CL type specifier with only 8
nodes.
C
NIL
D
C
T
A
B B
( not ( or ( and A C)
( and B C)
( and B D) ) )
Standard algorithm to serialize to a canonical
disjunctive form.
( or ( and A ( not B) ( not C ) )
( and A B ( not C) ( not D) )
( and ( not A) B ( not C) ( not D) )
( and ( not A) ( not B ) ) )
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 16 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Representing CL types as ROBDDs
ROBDD: Reduced ordered binary decision diagram
C
NIL
D
C
T
A
B B
( not ( or ( and A C)
( and B C)
( and B D) ) )
( or ( and A ( not B) ( not C ) )
( and A B ( not C) ( not D) )
( and ( not A) B ( not C) ( not D) )
( and ( not A) ( not B ) ) )
This serialization is in no sense minimum in form.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 16 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Standard ROBDD reduction rules are insufficient for CL
type system.
(and number (not string)) = number are equivalent types, but the
BDDs are different!
number
NIL
string
T
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 17 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Brief Recap
We would like to use ORBDDs to programmatically represent and
manipulate CL types.
We have used the ORBDD developed for Boolean algebra of binary
variables,
Applying: the (1) terminal rule, (2) deletion rule, and (3) merging
rule.
We unfortunately lack unique ORBDD representations for equivalent
CL types.
We find that it does not quite work for reasoning about CL types.
A solution is needed.
We introduce a 4th reduction rule: the subtype rule. Our
contribution.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Brief Recap
We would like to use ORBDDs to programmatically represent and
manipulate CL types.
We have used the ORBDD developed for Boolean algebra of binary
variables,
Applying: the (1) terminal rule, (2) deletion rule, and (3) merging
rule.
We unfortunately lack unique ORBDD representations for equivalent
CL types.
We find that it does not quite work for reasoning about CL types.
A solution is needed.
We introduce a 4th reduction rule: the subtype rule. Our
contribution.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Brief Recap
We would like to use ORBDDs to programmatically represent and
manipulate CL types.
We have used the ORBDD developed for Boolean algebra of binary
variables,
Applying: the (1) terminal rule, (2) deletion rule, and (3) merging
rule.
We unfortunately lack unique ORBDD representations for equivalent
CL types.
We find that it does not quite work for reasoning about CL types.
A solution is needed.
We introduce a 4th reduction rule: the subtype rule. Our
contribution.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Brief Recap
We would like to use ORBDDs to programmatically represent and
manipulate CL types.
We have used the ORBDD developed for Boolean algebra of binary
variables,
Applying: the (1) terminal rule, (2) deletion rule, and (3) merging
rule.
We unfortunately lack unique ORBDD representations for equivalent
CL types.
We find that it does not quite work for reasoning about CL types.
A solution is needed.
We introduce a 4th reduction rule: the subtype rule. Our
contribution.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Brief Recap
We would like to use ORBDDs to programmatically represent and
manipulate CL types.
We have used the ORBDD developed for Boolean algebra of binary
variables,
Applying: the (1) terminal rule, (2) deletion rule, and (3) merging
rule.
We unfortunately lack unique ORBDD representations for equivalent
CL types.
We find that it does not quite work for reasoning about CL types.
A solution is needed.
We introduce a 4th reduction rule: the subtype rule. Our
contribution.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Brief Recap
We would like to use ORBDDs to programmatically represent and
manipulate CL types.
We have used the ORBDD developed for Boolean algebra of binary
variables,
Applying: the (1) terminal rule, (2) deletion rule, and (3) merging
rule.
We unfortunately lack unique ORBDD representations for equivalent
CL types.
We find that it does not quite work for reasoning about CL types.
A solution is needed.
We introduce a 4th reduction rule: the subtype rule. Our
contribution.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Subtype rule (4), CL type system compatibility
The types number and string are disjoint,
therefore, string number .
Child to search Relation Reduction
P.green P C C C .green
P.green P C C C .red
P.red P C C C.green
P.red P C C C.red
P.red P C C C.red
P.red P C C C.green
P.green P C C C .red
P.green P C C C .green
number
NIL
string
T
number
NILT
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 19 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Reductions to accommodate CL subtypes
Subtype rule (4), CL type system compatibility
The types number and string are disjoint;
therefore, string number .
Child to search Relation Reduction
P.green P C C C .green
P.green P C C C .red
P.red P C C C.green
P.red P C C C.red
P.red P C C C.red
P.red P C C C.green
number.green number string string string .red
P.green P C C C .green
number
NIL
string
T
number
NILT
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 20 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs
Type calculus using ROBDDs
As before, we can ask questions with ROBDDs.
Questions
Are two types the same? Or disjoint? Or is one a subtype of the other?
Functions
bdd-and, bdd-or, bdd-and-not.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 21 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs
Type calculus using ROBDDs
As before, we can ask questions with ROBDDs.
Questions
Are two types the same? Or disjoint? Or is one a subtype of the other?
Functions
bdd-and, bdd-or, bdd-and-not.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 21 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs
Are the two types the same? No, BDDs are different.
( not ( o r ( and A C)
( and B C)
( and B D) ) )
A
B B
CC
TNIL
D
( or ( and A
( no t C)
( or ( and B ( not D) )
( no t B ) ) )
( and ( not A)
( or ( and B ( not C) ( no t D) )
( no t D) ) ) )
A
B
D
C C
NIL T
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 22 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs
Are two types disjoint? No, the intersection is non-nil.
( s e t f T1
( bdd ( and ( not ( and ( not A) D) )
( not ( o r ( and A C)
( and B C)
( and B D) ) ) ) ) )
( s e t f T2
( bdd ( or ( and A
( not C)
( o r ( and B ( not D) )
( not B ) ) )
( and ( not A)
( o r ( and B
( not C)
( not D) )
( not D ) ) ) ) ) )
( bddand T1 T2)
A
C
B
NIL
D
T
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 23 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs
Is one a subtype of the other? Yes. T
1
T
2
.
( bddandnot T2 T1)
A
B B
NIL
C C
D D
T
( bddandnot T1 T2)
NIL
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 24 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type checking and code generation with BDDs
Run-time calls to bdd-type-p
( defun bddtypep ( o b j bdd )
( etypeca se bdd
( bdd fals e
n i l )
( bddtrue
t )
( bddnode
( bddtypep o b j
( i f ( typep o b j ( bdd la bel bdd ) )
( bdd l e f t bdd )
( bdd ri ght bdd ) ) ) ) ) )
Guarantees that each base-type is checked maximum of once.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 25 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type checking and code generation with BDDs
Compile time call to bdd-typep, via compiler-macro
( bddtypep X ( or ( and s e quen c e ( not a r r a y ) )
number
( and ( not s e que n ce ) a r r a y ) ) )
( f u n c a l l ( lambda ( o b j )
( b l o c k n i l
( tagbody
1 ( i f ( typep o b j ’ a r r a y )
( go 2)
( go 3 ) )
2 ( re t ur n ( not ( typep obj ’ s eque n ce ) ) )
3 ( i f ( typep o b j number )
( re t ur n t )
( go 4 ) )
4 ( re t ur n ( typep obj ’ s eque n ce ) ) ) ) )
X)
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 26 / 43
Reduced Ordered Binary Decision Diagrams (ROBDDs) Type checking and code generation with BDDs
Compile time call to bdd-typep, via compiler-macro
( bddtypep X ( or ( and s e quen c e ( not a r r a y ) )
number
( and ( not s e que n ce ) a r r a y ) ) )
( f u n c a l l ( lambda ( o b j )
( b l o c k n i l
( tagbody
1 ( i f ( typep o b j ’ a r r a y )
( go 2)
( go 3 ) )
2 ( re t ur n ( not ( typep obj ’ s eque n ce ) ) )
3 ( i f ( typep o b j number )
( re t ur n t )
( go 4 ) )
4 ( re t ur n ( typep obj ’ s eque n ce ) ) ) ) )
X)
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 26 / 43
Conclusion
Table of Contents
1
Common Lisp Types
Native type specifiers
Type calculus with type specifiers
2
Reduced Ordered Binary Decision Diagrams (ROBDDs)
Representing CL types as ROBDDs
Reductions to accommodate CL subtypes
Type calculus using ROBDDs
Type checking and code generation with BDDs
3
Conclusion
Summary
Questions
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 27 / 43
Conclusion
Donald Knuth’s new toy.
Binary decision diagrams (BDDs) are wonderful, and
the more I play with them the more I love them. For
fifteen months I’ve been like a child with a new toy,
being able now to solve problems that I never
imagined would be tractable... I suspect that many
readers will have the same experience ... there will
always be more to learn about such a fertile subject.
[Donald Knuth, Art of Computer Science, Volume 4]
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 28 / 43
Conclusion Summary
Summary
Native CL type specifiers are
Powerful and intuitive
But may suffer performance issues
Missing capability (subtypep)
ROBDDs offer an interesting alternative
We have extended Standard ROBDD theory to CL types
Shown type calculus operations, equality, intersection, relative
complement, etc
Demonstrated efficient compile time code generation for type checking.
Competitive performance
Lots more work to do.
For more information see the LRDE website:
https://www.lrde.epita.fr/wiki/User:Jnewton
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 29 / 43
Conclusion Questions
Questions/Answers
Questions?
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 30 / 43
Conclusion Questions
ROBDD: Reduced Ordered Binary Design Diagrams
Having as few nodes as possible has advantages in:
Correctness in presence of subtypes,
Memory allocation,
Execution time of graph-traversal related operations, and
Generated code size (as we’ll see later).
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 31 / 43
Conclusion Questions
Possible ROBDD sizes for 4 variables
Of the 2
2
4
= 65, 536 different Boolean functions of 4 variables, various
sizes of reduced BDDs are possible.
Worst case size is 32 nodes. Average size is approximately 20 nodes.
0 5 10 15 20 25 30 35
0
0.5
1
1.5
·10
4
BDD node count
Number of Boolean functions
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 32 / 43
Conclusion Questions
Distributions for 2 to 5 variables
0 10 20 30 40 50 60
0
0.2
0.4
0.6
0.8
BDD Size
Probability
”Size with 5 variables”
”Size with 4 variables”
”Size with 3 variables”
”Size with 2 variables”
”Size with 1 variables”
Distribution of ROBDD size over all possible Boolean functions of N
variables.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 33 / 43
Conclusion Questions
Expected and worst case ROBDD size
1 2 3 4 5
0
20
40
60
Number of variables
BDD size
”Worst case size”
”Average size”
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 34 / 43
Conclusion Questions
FIRST TRY: Expands to the following. O(2
n
) code size.
O(n) execution time.
If the type specifier is known at compile time.
( f u n c a l l ( lambda ( o b j )
( i f ( typep o b j ’ a r r a y )
( i f ( typep o b j ’ seq u ence )
n i l
t )
( i f ( typep o b j number )
t
( i f ( typep o b j ’ seq u ence )
t
n i l ) ) ) )
X)
We can do better.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 35 / 43
Conclusion Questions
BETTER: O(2
n
2
) code size. O(n) execution time.
1
( f u n c a l l ( lambda ( o bj )
( l a b e l s (( #: f 1 ( )
( typep o bj s e qu en ce ) )
(#: f 2 ( )
( o r ( typep o b j number )
(#: f 1 ) ) )
(#: f 3 ( )
( not ( typep o bj s equ en ce ) ) )
(#: f 4 ( )
( i f ( typep o b j a r r a y )
(#: f 3 )
(#: f 2 ) ) ) )
(#: f 4 ) ) )
X)
1
O(2
n
2
) is a non-rigorous estimate.
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 36 / 43
Conclusion Questions
Experimental problem: thoroughly partition a set of types
unsigned-byte
bit
fixnum
rational
float
number
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 37 / 43
Conclusion Questions
Maximal Disjoint Type Decomposition
unsigned-byte
bit
fixnum
rational
float
number
( b i t f l o a t fi xnum number r a t i o n a l un si gn ed b yt e )
>
( b i t
f l o a t
( and fi xnu m un si gn ed b yt e ( not b i t ) )
( and fi xnu m ( not u ns ig ne d by te ) )
( and number ( not f l o a t ) ( not r a t i o n a l ) )
( and r a t i o n a l ( not fi xnu m ) ( not u nsigned b yt e ) )
( and un si gn ed b yt e ( not f ixn um ) ) )
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 38 / 43
Conclusion Questions
Combinations of number and condition
10
0
10
1
10
2
10
3
10
4
10
3
10
2
10
1
10
0
10
1
Size
Time
DECOMPOSE-TYPES
DECOMPOSE-TYPES-GRAPH
BDD-DECOMPOSE-TYPES
DECOMPOSE-TYPES-BDD-GRAPH
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 39 / 43
Conclusion Questions
Subtypes of fixnum: (member ...)
10
1
10
2
10
3
10
2
10
1
10
0
10
1
Size
Time
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 40 / 43
Conclusion Questions
Type specifier summary
Easy and intuitive (thanks to homoiconicity)
Run-time calls to subtypep and typep
Issues of performance and correctness of subtypep and typep
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 41 / 43
Conclusion Questions
Subtypes
( and ( not number )
( not s t r i n g ) )
number
NIL
string
T
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 42 / 43
Conclusion Questions
Caveat of subtypep
Sometimes subtypep returns don’t know. Sometimes for good reasons.
Sometimes not.
CLUSER> ( subtypep ( s a t i s f i e s oddp ) ( s a t i s f i e s evenp ) )
> NIL , NIL
CLUSER> ( subtypep a r i t h m e t i c e r r o r ( not c e l l e r r o r ) )
> NIL , NIL
Jim Newton (10th European Lisp Symposium)Programmatic Manipulation of Type Specifiers 3-4 April 2017 43 / 43