Strategies for typecase optimization ELS’18, April 16–17 2018, Marbella, Spain
[
22
, Chapter 8] discusses this in a use case of a symbolic mathemat-
ics simplication program. Both the Ableson and Norvig studies
explicitly target a lisp-literate audience.
5 CONCLUSION AND FUTURE WORK
As illustrated in Example 9, the exhaustive search approach used
in the
auto-permute-typecase
(Section 2.6) can often do a good
job removing redundant type checks occurring in a
typecase
in-
vocation. Unfortunately, as shown in Example 7, sometimes such
optimization is algebraically impossible because the particular type
interdependencies. In addition, an exhaustive search becomes un-
reasonable when the number of clauses is large. In particular there
are
N
! ways to order
N
clauses. This means there are 7!
=
5040
orderings of 7 clauses and 10!
=
3
,
628
,
800 orderings of 10 clauses.
On the other hand, the
bdd-typecase
macro, using the ROBDD
approach (Section 3.2), is always able to remove duplicate checks,
guaranteeing that no type check is performed twice. Nevertheless,
it may fail to eliminate some unnecessary checks which need not
be performed at all.
It is known that the size and shape of a reduced BDD depends
on the ordering chosen for the variables [
9
]. Furthermore, it is
known that nding the best ordering is NP-hard, and in this article
we do not address questions of choosing or improving variable
orderings. It would be feasible, at least in some cases, to apply the
exhaustive search approach with ROBDDs. I.e., we could visit all
orders of the type checks to nd which gives the smallest ROBDD.
In situations where the number of dierent type tests is large, the
development described in Section 3.1 might very well be improved
employing some known techniques for improving BDD size though
variable ordering choices[
6
]. In particular, we might attempt to use
the order specied in the
typecase
as input to the sorting function,
attempting in at least the simple cases to respect the user given
order as much as possible.
In Section 2.4, we presented an approach to approximating the
cost a set of type tests and commented that the heuristics are sim-
plistic. We leave it as a matter for future research as to how to
construct good heuristics, which take into account how compute
intensive certain type speciers are to manipulate.
We believe this research may be useful for two target audiences:
application programmers and compiler developers. Even though
the currently observed use frequency of
typecase
seems low in the
majority of currently supported applications, programmers may
nd the macros explained in this article (
auto-permute-typecase
and
bdd-typecase
) to be useful in rare optimization cases, but more
often for their ability to detect certain dubious code paths. There
are, however, limitations to the portable implementation, namely
the lack of a portable expander for user dened types, and an ability
to distinguish between machine generated and human generated
code. These shortcomings may not be signicant limitations to
the compiler implementer, in which case the compiler may be able
to better optimize user types, implement better heuristics regard-
ing costs of certain type checks, and emit useful warnings about
unreachable code.
REFERENCES
[1]
Harold Abelson and Gerald J. Sussman. Structure and Interpretation of Computer
Programs. MIT Press, Cambridge, MA, USA, 2nd edition, 1996. ISBN 0262011530.
[2]
S. B. Akers. Binary decision diagrams. IEEE Trans. Comput., 27(6):509–516, June
1978. ISSN 0018-9340. doi: 10.1109/TC.1978.1675141. URL http://dx.doi.org/10.
1109/TC.1978.1675141.
[3]
Henrik Reif Andersen. An introduction to binary decision diagrams. Technical
report, Course Notes on the WWW, 1999.
[4]
Ansi. American National Standard: Programming Language – Common Lisp.
ANSI X3.226:1994 (R1999), 1994.
[5]
Lennart Augustsson. Compiling pattern matching. In Proc. Of a Conference on
Functional Programming Languages and Computer Architecture, pages 368–381,
New York, NY, USA, 1985. Springer-Verlag New York, Inc. ISBN 3-387-15975-4.
URL http://dl.acm.org/citation.cfm?id=5280.5303.
[6]
Adnan Aziz, Serdar Taşiran, and Robert K. Brayton. Bdd variable ordering
for interacting nite state machines. In Proceedings of the 31st Annual Design
Automation Conference, DAC ’94, pages 283–288, New York, NY, USA, 1994. ACM.
ISBN 0-89791-653-0. doi: 10.1145/196244.196379. URL http://doi.acm.org/10.1145/
196244.196379.
[7]
Henry G. Baker. A Decision Procedure for Common Lisp’s SUBTYPEP Predicate.
Lisp and Symbolic Computation, 5(3):157–190, 1992. URL http://dblp.uni-trier.de/
db/journals/lisp/lisp5.html#Baker92a.
[8]
T.J. Barnes. SKILL: a CAD system extension language. In Design Automation
Conference, 1990. Proceedings., 27th ACM/IEEE, pages 266–271, Jun 1990. doi:
10.1109/DAC.1990.114865.
[9]
Randal E. Bryant. Graph-Based Algorithms for Boolean Function Manipulation.
IEEE Transactions on Computers, 35:677–691, August 1986.
[10]
Randal E. Bryant. Symbolic boolean manipulation with ordered binary-decision
diagrams. ACM Comput. Surv., 24(3):293–318, September 1992. ISSN 0360-0300.
doi: 10.1145/136035.136043. URL http://doi.acm.org/10.1145/136035.136043.
[11]
J. R. Burch, E. M. Clarke, K. L. McMillan, D. L. Dill, and L. J. Hwang. Symbolic
model checking: 1020 states and beyond. Inf. Comput., 98(2):142–170, June 1992.
ISSN 0890-5401. doi: 10.1016/0890-5401(92)90017-A. URL http://dx.doi.org/10.
1016/0890-5401(92)90017-A.
[12]
G. Castagna and V. Lanvin. Gradual typing with union and intersection types.
Proc. ACM Program. Lang., (1, ICFP ’17, Article 41), sep 2017.
[13]
Giuseppe Castagna. Covariance and contravariance: a fresh look at an old issue.
Technical report, CNRS, 2016.
[14]
Maximilien Colange. Symmetry Reduction and Symbolic Data Structures for Model
Checking of Distributed Systems. Thèse de doctorat, Laboratoire de l’Informatique
de Paris VI, Université Pierre-et-Marie-Curie, France, December 2013. URL
http://www.lsv.ens-cachan.fr/Publis/PAPERS/PDF/colange-phd13.pdf.
[15]
Olivier Coudert, Christian Berthet, and Jean Christophe Madre. Verication of
synchronous sequential machines based on symbolic execution. In Proceedings
of the International Workshop on Automatic Verication Methods for Finite State
Systems, pages 365–373, London, UK, UK, 1990. Springer-Verlag. ISBN 3-540-
52148-8. URL http://dl.acm.org/citation.cfm?id=646691.703286.
[16]
Haruo Hosoya, Jérôme Vouillon, and Benjamin C. Pierce. Regular expression
types for XML. ACM Trans. Program. Lang. Syst., 27(1):46–90, January 2005.
ISSN 0164-0925. doi: 10.1145/1053468.1053470. URL http://doi.acm.org/10.1145/
1053468.1053470.
[17]
Gregor J. Kiczales, Jim des Rivières, and Daniel G. Bobrow. The Art of the
Metaobject Protocol. MIT Press, Cambridge, MA, 1991.
[18]
Donald E. Knuth. The Art of Computer Programming, Volume 4, Fascicle 1: Bitwise
Tricks & Te chniques; Binary Decision Diagrams. Addison-Wesley Professional,
12th edition, 2009. ISBN 0321580508, 9780321580504.
[19]
Luc Maranget. Compiling pattern matching to good decision trees. In Proceedings
of the 2008 ACM SIGPLAN Workshop on ML, ML ’08, pages 35–46, New York, NY,
USA, 2008. ACM. ISBN 978-1-60558-062-3. doi: 10.1145/1411304.1411311. URL
http://doi.acm.org/10.1145/1411304.1411311.
[20]
Jim Newton, Akim Demaille, and Didier Verna. Type-Checking of Heterogeneous
Sequences in Common Lisp. In European Lisp Symposium, Kraków, Poland, May
2016.
[21]
Jim Newton, Didier Verna, and Maximilien Colange. Programmatic manipulation
of Common Lisp type speciers. In European Lisp Symposium, Brussels, Belgium,
April 2017.
[22]
Peter Norvig. Paradigms of Articial Intelligence Programming: Case Studies in
Common Lisp. Morgan Kaufmann, 1992.
[23]
Benjamin C. Pierce. Types and Programming Languages. The MIT Press, 1st
edition, 2002. ISBN 0262162091, 9780262162098.