Type-checking on Heterogeneous Sequences

Similar documents
Extra Numerical Types for Common Lisp

Common Lisp Object System Specification. 1. Programmer Interface Concepts

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

MIDTERM EXAMINATION - CS130 - Spring 2005

Strategies for typecase optimization

LISP - SEQUENCES. The function make-sequence allows you to create a sequence of any type. The syntax for this function is:

Imperative, OO and Functional Languages A C program is

Functions, Conditionals & Predicates

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Typed Racket: Racket with Static Types

The Typed Racket Guide

A Small Interpreted Language

Symbolic Computation and Common Lisp

INF4820. Common Lisp: Closures and Macros

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

Allegro CL Certification Program

Common LISP Tutorial 1 (Basic)

The Typed Racket Reference

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Typed Scheme: Scheme with Static Types

19 Machine Learning in Lisp

Functional programming with Common Lisp

Common Lisp. Blake McBride

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Implementing Symmetric Multiprocessing in LispWorks

Lisp Basic Example Test Questions

CMSC 330: Organization of Programming Languages

Common LISP-Introduction

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Recursion & Iteration

Robot Programming with Lisp

A little bit of Lisp

Functional Programming Languages (FPL)

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional Programming. Pure Functional Languages

Allegro CL Certification Program

Principles of Programming Languages

Lecture #5 Kenneth W. Flynn RPI CS

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CS4215 Programming Language Implementation

9.5 Equivalence Relations

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Pierce Ch. 3, 8, 11, 15. Type Systems

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

A Genetic Algorithm Implementation

Lecture Notes on Lisp A Brief Introduction

Functional Programming. Pure Functional Languages

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

15 Unification and Embedded Languages in Lisp

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

CSCI337 Organisation of Programming Languages LISP

CMPUT325 Extensions to Pure Lisp. Extensions to Pure Lisp. Bob Price and Russ Greiner. 5th October 2004

Chapter 1. Fundamentals of Higher Order Programming

Midterm Examination (Sample Solutions), Cmput 325

Lecture #24: Programming Languages and Programs

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Types and Programming Languages. Lecture 5. Extensions of simple types

Organization of Programming Languages CS3200/5200N. Lecture 11

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

PL Recitation 9/21/2010

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Review of Sets. Review. Philippe B. Laval. Current Semester. Kennesaw State University. Philippe B. Laval (KSU) Sets Current Semester 1 / 16

Lisp. Versions of LISP

Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Sets Fall / 31

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Streams and Lazy Evaluation in Lisp

Project 2: Scheme Interpreter

PLanCompS. Peter D Mosses Swansea University. SSLF12: Summer School on Language Frameworks Sinaia, Romania, July 2012

Example: Haskell algebraic data types (1)

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

LP/LispLite: Trivial Lisp Org Mode Conversion

CS558 Programming Languages

Lecture #2 Kenneth W. Flynn RPI CS

Associative Database Managment WIlensky Chapter 22

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Debugging in LISP. trace causes a trace to be printed for a function when it is called

Topology I Test 1 Solutions October 13, 2008

Associative Database Managment

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Introduction. Sets and the Real Number System

CS 842 Ben Cassell University of Waterloo

CMSC 331 Final Exam Section 0201 December 18, 2000

A brief survey of programming languages: motivation and design/ continued. Lecture 3. Even more, Algol, Cobol. Are they really different?

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

XLISP PLUS. Reference Manual. Version 3.0

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Structure and Interpretation of Computer Programs

Chapter 3 Linear Structures: Lists

Scheme: Expressions & Procedures

Transcription:

Type-checking on Heterogeneous Sequences in Common Lisp Jim Newton EPITA/LRDE May 9, 2016 Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 1 / 31

Overview 1 Introduction Common Lisp Types 2 Type Sequences Limitations Rational Type Expression Generated Code Example: destructuring-case Overlapping Types 3 Conclusion Difficulties Encountered Summary Questions Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 2 / 31

Introduction Common Lisp Types Common Lisp Types Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 3 / 31

Introduction Common Lisp Types What is a type in Common Lisp? Definition (from CL specification) A (possibly infinite) set of objects. Definition (type specifier) An expression that denotes a type. Atomic examples t, integer, number, asdf:component Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 4 / 31

Introduction Common Lisp Types Type specifiers come in several forms. Compound type specifiers (eql 12) (member :x :y :z) (satisfies oddp) (and (or number string) (not (satisfies MY-FUN))) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 5 / 31

Introduction Common Lisp Types Type specifiers come in several forms. Compound type specifiers (eql 12) (member :x :y :z) (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 (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 5 / 31

Introduction Common Lisp Types Using types with sequences Compile time (lambda (x y) (declare (type (vector float) x y)) (list x y)) Run time (typep my-list (cons t (cons t (cons string)))) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 6 / 31

Limitations Limitations Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 7 / 31

Limitations Limited capability for specifying heterogeneous sequences. You can t specify the following. An arbitrary length, non-empty, list of floats: (1.0 2.0 3.0) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 8 / 31

Limitations Limited capability for specifying heterogeneous sequences. You can t specify the following. An arbitrary length, non-empty, list of floats: (1.0 2.0 3.0) A plist such as: (:x 0 :y 2 :z 3) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 8 / 31

Rational Type Expression The Rational Type Expression Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 9 / 31

Rational Type Expression Introducing the RTE type Rational type expression vs. RTE type specifier number + (RTE (:+ number)) Example: (1.0 2.0 3.0) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 10 / 31

Rational Type Expression Introducing the RTE type Rational type expression vs. RTE type specifier number + (RTE (:+ number)) Example: (1.0 2.0 3.0) (keyword integer) (RTE (:* (:cat keyword integer))) Example: (:x 0 :y 2 :z 3) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 10 / 31

Rational Type Expression Use RTE anywhere CL expects a type specifier. (typedef plist (type) (and list (RTE (:* keyword,type)))) (defun foo (A B) (declare (type (RTE (:+ number)) A) (type (plist float) B))...) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 11 / 31

Rational Type Expression An RTE can be expressed as a finite state machine. (symbol (number + string + )) + (:+ symbol (:or (:+ number) (:+ string))) number number 2 symbol 0 1 symbol symbol string 3 string Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 12 / 31

Generated Code Generated Code Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 13 / 31

Generated Code State machine can be expressed in CL code (lambda (seq) (declare (optimize (speed 3) (debug 0) (safety 0))) (typecase seq (list...) (simple-vector...) (vector...) (sequence...) (t nil))) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 14 / 31

Generated Code Code generating implementing state machine (tagbody 0 (unless seq (return nil)) (typecase (pop seq) (symbol (go 1)) (t (return nil))) 1 (unless seq (return nil)) (typecase (pop seq) (number (go 2)) (string (go 3)) (t (return nil))) 2 (unless seq (return t)) (typecase (pop seq) (number (go 2)) (symbol (go 1)) (t (return nil))) 3 symbol 0 1 number string (unless seq (return t)) (typecase (pop seq) (string (go 3)) (symbol (go 1)) (t (return nil))))) number 2 symbol symbol 3 string Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 15 / 31

Example: destructuring-case destructuring-case Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 16 / 31

Example: destructuring-case Example of destructuring-case (destructuring-case DATA ;; Case-1 ((a b &optional (c "")) (declare (type integer a) (type string b c))...) ;; Case-2 ((a (b c) &key (x t) (y "") z &allow-other-keys) (declare (type fixnum a b c) (type symbol x) (type string y) (type list z))...)) (typecase DATA ;; Case-1 ((rte (:cat integer string (:? string)))...destructuring-bind...) ;; Case-2 ((rte...complicated...)...destructuring-bind......)) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 17 / 31

Example: destructuring-case Regular type expression denoting Case-2 ( : c a t ( : c a t fixnum ( : and l i s t ( r t e ( : c a t fixnum fixnum ) ) ) ) ( : and ( : keyword t ) ( : c a t ( : ( not ( member : x : y : z ) ) t ) ( : o r : empty word ( : c a t ( e q l : z ) fixnum ( : ( not ( member : x : y ) ) t ) ( :? ( e q l : y ) s t r i n g ( : ( not ( e q l : x ) ) t ) ( :? ( e q l : x ) symbol ( : t t ) ) ) ) ( : c a t ( e q l : z ) fixnum ( : ( not ( member : x : y ) ) t ) ( :? ( e q l : x ) symbol ( : ( not ( e q l : y ) ) t ) ( :? ( e q l : y ) s t r i n g ( : t t ) ) ) ) ( : c a t ( e q l : y ) s t r i n g ( : ( not ( member : x : z ) ) t ) ( :? ( e q l : z ) fixnum ( : ( not ( e q l : x ) ) t ) ( :? ( e q l : x ) symbol ( : t t ) ) ) ) ( : c a t ( e q l : x ) symbol ( : ( not ( member : y : z ) ) t ) ( :? ( e q l : z ) fixnum ( : ( not ( e q l : y ) ) t ) ( :? ( e q l : y ) s t r i n g ( : t t ) ) ) ) ( : c a t ( e q l : y ) s t r i n g ( : ( not ( member : x : z ) ) t ) ( :? ( e q l : x ) symbol ( : ( not ( e q l : z ) ) t ) ( :? ( e q l : z ) fixnum ( : t t ) ) ) ) ( : c a t ( e q l : x ) symbol ( : ( not ( member : y : z ) ) t ) ( :? ( e q l : y ) s t r i n g ( : ( not ( e q l : z ) ) t ) ( :? ( e q l : z ) fixnum ( : t t ) ) ) ) ) ) ) ) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 18 / 31

Example: destructuring-case Finite State Machine of Case-2 of destructuring-case T1 T20 19 T21 16 T4 18 T9 T10 20 21 T6 T3 T17 T1 8 0 T3 1 T7 2 T1 T8 T9 T10 17 3 25 T6 T3 4 26 T8 T19 T1 T10 T8 T9 T1 6 5 12 28 29 T4 T3 T4 T6 7 22 13 T10 T16 T1 T9 T15 T1 T8 9 23 24 14 15 T3 T6 T4 10 T1 T5 11 T18 27 Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 19 / 31

Overlapping Types Overlapping Types Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 20 / 31

Overlapping Types Rational type expression with overlapping types ((integer number) (number integer)) (:or (:cat integer number) (:cat number integer)) integer P 3 number P 0 number P 2 P 1 integer Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 21 / 31

Overlapping Types Overlapping types must decomposed into disjoint types ((integer number) ((number integer) integer)) (:or (:cat integer number) (:cat (and number (not integer)) integer)) integer P 3 number P 0 number integer P 2 P 1 integer Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 22 / 31

Overlapping Types Overlapping types considered harmful Disjoint Set Decomposed Expression { 1 } A B C D F H { 2 } B C D { 3 } B C D { 4 } C B D { 5 } B C D { 6 } B D C { 7 } C D B { 8 } D B C H { 9 } E { 10 } F { 11 } G { 12 } H D { 13 } D H E G 11 D A B C 3 2 4 5 6 7 8 E 9 12 13 H 1 F 10 Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 23 / 31

Overlapping Types How to calculate type disjoint-ness and equivalence. (defun type-intersection (T1 T2) (and,t1,t2)) (defun types-disjoint-p (T1 T2) (subtypep (type-intersection T1 T2) nil)) (defun types-equivalent-p (T1 T2) (multiple-value-bind (T1<=T2 okt1t2) (subtypep T1 T2) (multiple-value-bind (T2<=T1 okt2t2) (subtypep T2 T1) (values (and T1<=T2 T2<=T1) (and okt1t2 okt2t2))))) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 24 / 31

Conclusion Difficulties Encountered Interesting Difficulties Encountered Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 25 / 31

Conclusion Difficulties Encountered Performance and correctness problems with SUBTYPEP (subtypep (and integer (or (eql 1) (satisfies F))) (and integer (or (eql 0) (satisfies G)))) = NIL, T (should be NIL, NIL) (subtypep compiled-function nil) = NIL, NIL (should be NIL, T) (subtypep (eql :x) keyword) = NIL, NIL (should be T, T) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 26 / 31

Conclusion Difficulties Encountered Recursive types forbidden Neither the CL type system nor the RTE extension are expressive enough to specify recursive types such as: (deftype singleton (type) (or (cons,type nil) (cons (singleton,type)))) (deftype proper-list (type) (cons,type (or null (proper-list,type)))) Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 27 / 31

Conclusion Difficulties Encountered Missing CL API for type reflection and extension Can t ask whether a particular type exists? I.e., is there a type foo? E.g., Given two RTE type specifiers, we can calculate whether one is a subtype of the other. Unfortunately, CL provides no SUBTYPE hook allowing me to make this calculation. Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 28 / 31

Conclusion Difficulties Encountered Future Research Static analysis of destructuring-case to detect unreachable code or overlapping cases. Investigate performance of type decomposition (disjoint-izing). Apply to other dynamic languages (e.g., Python, Scala/JVM, Julia/LLVM). Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 29 / 31

Conclusion Summary Summary Regular expression style type-based pattern matching on CL sequences. RTE type allows O(n) type checking of CL sequences. Non-linear complexity moved to compile time. Source-code available at https://www.lrde.epita.fr/wiki/publications/newton.16.els Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 30 / 31

Conclusion Questions Q/A Questions? Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 31 / 31