Programmatic Manipulation of Type Specifiers in Common Lisp Jim - - PowerPoint PPT Presentation

programmatic manipulation of type specifiers
SMART_READER_LITE
LIVE PREVIEW

Programmatic Manipulation of Type Specifiers in Common Lisp Jim - - PowerPoint PPT Presentation

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 Common


slide-1
SLIDE 1

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

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

Common Lisp Types Native type specifiers

Type specifiers are powerful and intuitive

Type specifiers can be extremely intuitive thanks to homoiconicity. Simple

integer

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 5 / 43

slide-6
SLIDE 6

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)))

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 5 / 43

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Common Lisp Types Type calculus with type specifiers

We can ask questions with CL type specifiers.

Type membership? (typep x T1)

x ∈ T1

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43

slide-10
SLIDE 10

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)

T1 ⊂ T2

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43

slide-11
SLIDE 11

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))

(T1 ⊂ T2) ∧ (T2 ⊂ T1)

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43

slide-12
SLIDE 12

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)

T1 ∩ T2 ⊂ ∅

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 6 / 43

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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 T NIL T

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 9 / 43

slide-22
SLIDE 22

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 T NIL 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

slide-23
SLIDE 23

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 T NIL 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

slide-24
SLIDE 24

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 T NIL T

4 variables = ⇒ 24+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

slide-25
SLIDE 25

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 D D D C C D D D D

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 10 / 43

slide-26
SLIDE 26

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 C C D D D D D D D D NIL T A

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 11 / 43

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

number NIL T

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 17 / 43

slide-34
SLIDE 34

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,

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43

slide-35
SLIDE 35

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.

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43

slide-36
SLIDE 36

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.

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43

slide-37
SLIDE 37

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.

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43

slide-38
SLIDE 38

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.

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 18 / 43

slide-39
SLIDE 39

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

slide-40
SLIDE 40

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 NIL T

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 19 / 43

slide-41
SLIDE 41

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 NIL T

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 20 / 43

slide-42
SLIDE 42

Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs

Type calculus using ROBDDs

As before, we can ask questions with ROBDDs.

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 21 / 43

slide-43
SLIDE 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

slide-44
SLIDE 44

Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs

Are the two types the same? No, BDDs are different.

( not ( or ( and A C) ( and B C) ( and B D) ) )

A B B C C T NIL D

( or ( and A ( not C) ( or ( and B ( not D)) ( not B) ) ) ( and ( not A) ( or ( and B ( not C) ( not D)) ( not 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

slide-45
SLIDE 45

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 ( or ( and A C) ( and B C) ( and B D) ) ) ) ) ) ( s e t f T2 ( bdd ’( or ( and A ( not C) ( or ( and B ( not D)) ( not B) ) ) ( and ( not A) ( or ( and B ( not C) ( not D)) ( not D) ) ) ) ) )

(bdd−and 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

slide-46
SLIDE 46

Reduced Ordered Binary Decision Diagrams (ROBDDs) Type calculus using ROBDDs

Is one a subtype of the other? Yes. T1 ⊂ T2.

( bdd−and−not T2 T1)

A B B NIL C C D D T

( bdd−and−not T1 T2)

NIL

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 24 / 43

slide-47
SLIDE 47

Reduced Ordered Binary Decision Diagrams (ROBDDs) Type checking and code generation with BDDs

Run-time calls to bdd-type-p

( defun bdd−type−p ( obj bdd ) ( etypecase bdd ( bdd−false n i l ) ( bdd−true t ) ( bdd−node ( bdd−type−p obj ( i f ( typep

  • bj

( bdd−label bdd )) ( bdd−left bdd ) ( bdd−right 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

slide-48
SLIDE 48

Reduced Ordered Binary Decision Diagrams (ROBDDs) Type checking and code generation with BDDs

Compile time call to bdd-typep, via compiler-macro

( bdd−typep X ’( or (and sequence ( not array )) number (and ( not sequence ) array ) ) )

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 26 / 43

slide-49
SLIDE 49

Reduced Ordered Binary Decision Diagrams (ROBDDs) Type checking and code generation with BDDs

Compile time call to bdd-typep, via compiler-macro

( bdd−typep X ’( or (and sequence ( not array )) number (and ( not sequence ) array ) ) ) ( f u n c a l l ( lambda ( obj ) ( block n i l ( tagbody 1 ( i f ( typep

  • bj

’ array ) ( go 2) ( go 3)) 2 ( return ( not ( typep

  • bj

’ sequence ) ) ) 3 ( i f ( typep

  • bj

’ number ) ( return t ) ( go 4)) 4 ( return ( typep

  • bj

’ sequence ) ) ) ) ) X)

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 26 / 43

slide-50
SLIDE 50

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

slide-51
SLIDE 51

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

slide-52
SLIDE 52

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

slide-53
SLIDE 53

Conclusion Questions

Questions/Answers

Questions?

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 30 / 43

slide-54
SLIDE 54

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

slide-55
SLIDE 55

Conclusion Questions

Possible ROBDD sizes for 4 variables

Of the 224 = 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.

5 10 15 20 25 30 35 0.5 1 1.5 ·104 BDD node count Number of Boolean functions

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 32 / 43

slide-56
SLIDE 56

Conclusion Questions

Distributions for 2 to 5 variables

10 20 30 40 50 60 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

slide-57
SLIDE 57

Conclusion Questions

Expected and worst case ROBDD size

1 2 3 4 5 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

slide-58
SLIDE 58

Conclusion Questions

FIRST TRY: Expands to the following. O(2n) code size. O(n) execution time.

If the type specifier is known at compile time.

( f u n c a l l ( lambda ( obj ) ( i f ( typep

  • bj

’ array ) ( i f ( typep

  • bj

’ sequence ) n i l t ) ( i f ( typep

  • bj

’ number ) t ( i f ( typep

  • bj

’ sequence ) 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

slide-59
SLIDE 59

Conclusion Questions

BETTER: O(2

n 2) code size. O(n) execution time.1

( f u n c a l l ( lambda ( obj ) ( l a b e l s ((#: f1 () ( typep

  • bj

’ sequence )) (#: f2 () ( or ( typep

  • bj

’ number ) (#: f1 ) ) ) (#: f3 () ( not ( typep

  • bj

’ sequence ) ) ) (#: f4 () ( i f ( typep

  • bj

’ a r r a y ) (#: f3 ) (#: f2 ) ) ) ) (#: f4 ) ) ) X)

1O(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

slide-60
SLIDE 60

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

slide-61
SLIDE 61

Conclusion Questions

Maximal Disjoint Type Decomposition

unsigned-byte bit fixnum rational float number

( b i t f l o a t fixnum number r a t i o n a l unsigned−byte ) −− > ( b i t f l o a t ( and fixnum unsigned−byte ( not b i t )) ( and fixnum ( not unsigned−byte )) ( 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 fixnum ) ( not unsigned−byte )) ( and unsigned−byte ( not fixnum ) ) )

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 38 / 43

slide-62
SLIDE 62

Conclusion Questions

Combinations of number and condition

100 101 102 103 104 10−3 10−2 10−1 100 101 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

slide-63
SLIDE 63

Conclusion Questions

Subtypes of fixnum: (member ...)

101 102 10−3 10−2 10−1 100 101 Size Time

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 40 / 43

slide-64
SLIDE 64

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

slide-65
SLIDE 65

Conclusion Questions

Subtypes

(and ( not number ) ( not string ))

number NIL string T

Jim Newton (10th European Lisp Symposium) Programmatic Manipulation of Type Specifiers 3-4 April 2017 42 / 43

slide-66
SLIDE 66

Conclusion Questions

Caveat of subtypep

Sometimes subtypep returns don’t know. Sometimes for good reasons. Sometimes not.

CL−USER> ( subtypep ’( s a t i s f i e s

  • ddp)

’( s a t i s f i e s evenp )) > NIL , NIL CL−USER> ( 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