Open GADTs and declaration-site variance

Paolo G. Giarrusso
2013 Proceedings of the 4th Workshop on Scala - SCALA '13  
Generalized algebraic data types (GADTs) allow embedding extensible typed ASTs and transformations on them. Such transformations on typed ASTs are useful for code optimization in deeply embedded DSLs, for instance when using Lightweight Modular Staging (LMS). However, in Scala it is hard to make transformations for typed ASTs type-safe. Therefore, AST transformations in LMS are often not fully typechecked, preventing bugs from being caught early and without extensive testing. We show that
more » ... We show that writing type-safe transformations in such embeddings is in fact not just hard, but impossible without using unsafe casts or significantly restricting extensibility: Declaration-site variance opens GADTs representing typed ASTs not only to desirable extensions, but also to extensions that introduce exotic terms. We make the problem concrete on an embedding of λ<: through covariant GADTs. We discuss solution approaches, and sketch a Scala extension to address this problem without either introducing unsafe casts or restricting extensibility. We believe a complete solution would significantly ease writing transformations by allowing type-checking to verify them, and thus would ease their development.
doi:10.1145/2489837.2489842 dblp:conf/ecoop/Giarrusso13 fatcat:qghjqcnhungp5m23jh62pftocy