Extensible Pattern Matching

Similar documents
Languages as Libraries

Languages as Libraries

Typed Racket: Racket with Static Types

The Typed Racket Guide

Module 10: Imperative Programming, Modularization, and The Future

Functional Programming. Pure Functional Languages

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Functional Programming. Pure Functional Languages

Functional Programming - 2. Higher Order Functions

Programming Languages: Application and Interpretation

Typed Scheme: Scheme with Static Types

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 314 Principles of Programming Languages

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Graph reduction. Simple graph reduction with visualisation

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

User-defined Functions. Conditional Expressions in Scheme

Programming Languages: Application and Interpretation

Scheme Quick Reference

Box-and-arrow Diagrams

Why Macros? Scheme-Style Macros: Patterns and Lexical Scope. Macros versus Arbitrary Program Generators. Macros versus Arbitrary Program Generators

Expression Values Operators. (string-append a b c ) a, b, c string-append. (substring abcd 0 2) abcd, 0, 2 substring

Scheme as implemented by Racket

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

Module 8: Local and functional abstraction

Designing Precise Pushdown Higher-Order Flow Analyses

CS 314 Principles of Programming Languages. Lecture 16

Scheme Quick Reference

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

Scheme-Style Macros: Patterns and Lexical Scope

CS115 - Module 9 - filter, map, and friends

Racket: Modules, Contracts, Languages

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

Typed Scheme From Scripts to Programs. Sam Tobin-Hochstadt Northeastern University

Functional Programming Lecture 1: Introduction

Functional Data Structures for Typed Racket. Hari Prashanth and Sam Tobin-Hochstadt Northeastern University

Functions & First Class Function Values

CMSC 330: Organization of Programming Languages. Rust Basics

ECE570 Lecture 2: Types and Conventions

ECE473 Lecture 2: Types and Conventions

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

The Typed Racket Guide

Symbolic Computation

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Imperative languages

Vectors in Scheme. Data Structures in Scheme

CS61A Midterm 2 Review (v1.1)

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

CS 360 Programming Languages Interpreters

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

How to Design Programs

Comp 311: Sample Midterm Examination

FP Foundations, Scheme

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CSE 341 Section Handout #6 Cheat Sheet

Logic - CM0845 Introduction to Haskell

What is tail position?

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Lecture 5: Lazy Evaluation and Infinite Data Structures

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Space War Class Diagram. Elements of OOP. How to design interactions between objects. Space War Class Diagram with Inheritance

Building a system for symbolic differentiation

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

ormap, andmap, and filter

Programming Languages

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Pattern Matching for Scheme

Essentials of Programming Languages Language

COP4020 Programming Assignment 1 - Spring 2011

Essentials of Programming Languages Language

Functional Programming and Haskell

Fun in the Web: Web based programming environments for functional programs. Danny Yoo WPI

Building a system for symbolic differentiation

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

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

A Brief Introduction to Scheme (II)

It s Turtles All the Way Up: Self-Extensible Programming Languages in PLT Scheme

Scheme: Expressions & Procedures

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

CS115 - Module 10 - General Trees

(add1 3) 4 (check-expect (add1 3) 4)

CS 320 Homework One Due 2/5 11:59pm

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

LECTURE 16. Functional Programming

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Pattern Matching and Abstract Data Types

Modules, Structs, Hashes, and Operational Semantics

CS2500 Exam 2 Fall 2011

exp ::= (match exp clause :::) j clause :::) j * clause :::) j (match-let ([pat exp] :::) body) j (match-let* ([pat exp] :::) body) j (match-letrec ([

Transcription:

Extensible Pattern Matching Sam Tobin-Hochstadt PLT @ Northeastern University IFL, September 3, 2010

Extensible Pattern Matching in an Extensible Language Sam Tobin-Hochstadt PLT @ Northeastern University IFL, September 3, 2010

(: magnitude : Complex -> Real) (define (magnitude n) (cond [(eq? (first n) 'cart) (sqrt (+ (sqr (second n)) (sqr (third n))))] [(eq? (first n) 'polar) (second n)]))

(: magnitude : Complex -> Real) (define (magnitude n) (if (not (pair? n)) (error 'bad-input) (let ([t1 (first n)] [t1* (rest n)]) (if (not (pair? t1*)) (error 'bad-input) (let ([t2 (first t1*)] [t2* (rest t1*)]) (if (not (pair? t3)) (error 'bad-input) (let ([t3 (first t2*)] [t3* (rest t2*)]) (if (not (null? t3)) (error 'bad-input)) (cond [(eq? t1 'cart) (sqrt (+ (sqr t2) (sqr t3)))] [(eq? t1 'polar) t2] [else (error 'bad-input)]))))))) )

(: magnitude : Complex -> Real) (define (magnitude n) (match n [(list 'cart x y) (sqrt (+ (sqr x) (sqr y)))] [(list 'polar r theta) r]))

(: magnitude : Complex -> Real) (define (magnitude n) (match n [(list 'cart xs...) (sqrt (apply + (map sqr xs)))] [(list 'polar r theta...) r]))

(: magnitude : Complex -> Real) (define (magnitude n) (match n [(cart xs...) (sqrt (apply + (map sqr xs)))] [(polar r theta...) r]))

(: magnitude : Complex -> Real) (define (magnitude n) (match n [(polar r theta...) r]))

Pattern Matching in Racket

match works for arbitrary data (match e [(list a b) (+ a b)] [(? string? a) (string-length a)] [(? number? a) a])

match provides expressive patterns (match e [(app add1 n) n])

match is an optimizer (match e [(list (? B?)) do-something-else]) [Le Fessant & Maranget]

match supports recursive patterns (match (list 2 4 6 8 10) [(list (? even? y)...) (foldr + 0 y)])

match supports recursive patterns (match '(3 2 1 3) [(list-no-order 1 2 3...) 'yes] [_ 'no])

Extensible Languages

Simple Language Extension (define-syntax (let ([x e]...) body) ((lambda (x...) body) e...)) (let ([x 1] [y 2]) (+ x y))

Simple Language Extension (define-syntax (let ([x e]...) body) ((lambda (x...) body) e...)) (let ([x 1] [y 2]) (+ x y)) ((lambda (x y) (+ x y)) 1 2)

Simple Language Extension (define-syntax (let ([x e]...) body) ((lambda (x...) body) e...)) (let ([x 1] [y 2]) (+ x y)) ((lambda (x y) (+ x y)) 1 2) [Kohlbecker et al, 1980s]

Adding Computation (define-syntax (numbers start end) (list (in-range start end))) (numbers 1 10)

Adding Computation (define-syntax (numbers start end) (list (in-range start end))) (numbers 1 10) (list 1 2 3 4 5 6 7 8 9 10)

Adding Computation (define-syntax (numbers start end) (list (in-range start end))) (numbers 1 10) (list 1 2 3 4 5 6 7 8 9 10) [Dybvig et al, 1990s]

Racket Modular Language Extension Compiler API Arbitrary Language Rewriting...

Racket Modular Language Extension Compiler API Arbitrary Language Rewriting... [Flatt et al, 2000s]

(define-syntax x 1) (define-syntax (get-x) (syntax-value x)) (get-x)

(define-syntax x 1) (define-syntax (get-x) (syntax-value x)) (get-x) 1

Extensible Pattern Matching

(define-syntax (let ([x e]...) b) ((lambda (x...) b) e...))

(define-syntax (let ([x e]...) b) ((lambda (x...) b) e...)) (define-matcher (not-false p) (? (compose not false?) p))

The core of match (define (parse-pattern pat) (syntax-case pat [(cons pat1 pat2)...] [(? pred pat)...]...))

The extended core (define (parse-pattern pat) (syntax-case pat [(id pats...) ] [(cons pat1 pat2)...] [(? pred pat)...]...))

The extended core (define (parse-pattern pat) (syntax-case pat [(id pats...) #:when (bound-to-match-expander? id) [(cons pat1 pat2)...] [(? pred pat)...]...)) ]

The extended core (define (parse-pattern pat) (syntax-case pat [(id pats...) #:when (bound-to-match-expander? id) [(cons pat1 pat2)...] [(? pred pat)...]...)) (syntax-value id) ]

The extended core (define (parse-pattern pat) (syntax-case pat [(id pats...) #:when (bound-to-match-expander? id) (match-expander-fn (syntax-value id)) [(cons pat1 pat2)...] [(? pred pat)...]...)) ]

The extended core (define (parse-pattern pat) (syntax-case pat [(id pats...) #:when (bound-to-match-expander? id) (let ([transformer (match-expander-fn (syntax-value id))]) [(cons pat1 pat2)...] [(? pred pat)...]...)) )]

The extended core (define (parse-pattern pat) (syntax-case pat [(id pats...) #:when (bound-to-match-expander? id) (let ([transformer (match-expander-fn (syntax-value id))]) (transformer (id pats...)) )] [(cons pat1 pat2)...] [(? pred pat)...]...))

The extended core (define (parse-pattern pat) (syntax-case pat [(id pats...) #:when (bound-to-match-expander? id) (let ([transformer (match-expander-fn (syntax-value id))]) (parse-pattern (transformer (id pats...))))] [(cons pat1 pat2)...] [(? pred pat)...]...))

An Example (define-matcher (not-false p)...) (match (list 7 #f) [(list (not-false x)... y) x])

An Example (define-syntax not-false (match-expander...)) (match (list 7 #f) [(list (not-false x)... y) x])

An Example (define-syntax not-false (match-expander...)) (match (list 7 #f) [(list (not-false z)... y) z]) (let ([transformer (match-expander-fn (syntax-value not-false))]) (parse-pattern (transformer (not-false z))))

An Example (define-syntax not-false (match-expander...)) (match (list 7 #f) [(list (not-false z)... y) z]) (? (compose not false?) z)

An Example (define-syntax not-false (match-expander...)) (match (list 7 #f) [(list (? (compose not false?) z)... y) z])

Applications

Views [Wadler 87] as a library (require (planet cobbe/views/views)) (define-view Zero zero? ()) (define-view Succ exact-positive-integer? (sub1)) (define (even? n) (match n [(Zero) true] [(Succ (Zero)) false] [(Succ (Succ n)) (even? n)]))

Web Server Dispatching (dispatch-rules [("") list-posts] [("posts" (string-arg)) review-post] [("archive" (integer-arg) (integer-arg)) review-archive] [else list-posts])

Other Extensible Systems View Patterns [Peyton-Jones et al]: app patterns Views [Wadler]: define-matcher and app Active Patterns [Syme et al]: Multiple uses of define-matcher, app, and?

Pattern matching is great Extensible pattern matching is even better An expressive and extensible language can give us both

Thanks! Available at racket-lang.org