Dependently Typed Pattern Matching Hongwei Xi Boston University.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Modern Programming Languages, 2nd ed.
Closures & Environments CS153: Compilers Greg Morrisett.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Dependent Types in Practical Programming Hongwei Xi Oregon Graduate Institute.
Imperative Programming with Dependent Types Hongwei Xi University of Cincinnati.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
Distributed Meta- Programming Rui Shi, Chiyan Chen and Hongwei Xi Boston University.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
1 CPS Transform for Dependent ML Hongwei Xi University of Cincinnati and Carsten Schürmann Yale University.
May 1, 2003May 1, Imperative Programming with Dependent Types Hongwei Xi Boston University.
Facilitating Program Verification with Dependent Types Hongwei Xi Boston University.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University.
1 A Dependently Typed Assembly Language Hongwei Xi University of Cincinnati and Robert Harper Carnegie Mellon University.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
10 September Implementing Staged Computation Chiyan Chen and Hongwei Xi Boston University.
1 Dependent Types in Practical Programming Hongwei Xi University of Cincinnati.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Facilitating Programming Verification with Dependent Types Hongwei Xi University of Cincinnati.
Dependently Typed Data Structures Hongwei Xi presented by James Hook Pacific Software Research Center Oregon Graduate.
Compiling with Dependent Types Hongwei Xi University of Cincinnati.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Staging in Haskell What is Staging What does it Mean Using Template Haskell.
GADTs GADTs in Haskell. ADT vs GADT Algebraic Datatype Data List a = Nil | Cons a (List a) Data Tree a b = Tip a | Node (Tree a b) b | Fork (Tree a b)
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Advanced Formal Methods Lecture 4: Isabelle – Types and Terms Mads Dam KTH/CSC Course 2D1453, Some material from Paulson.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
More Data Types CSCE 314 Spring CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.
CSE-321 Programming Languages Dependent Types POSTECH June 5, 2006 박성우.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Advanced Functional Programming
Advanced Functional Programming
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
The Metacircular Evaluator (Continued)
Types and Classes in Haskell
PROGRAMMING IN HASKELL
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

Dependently Typed Pattern Matching Hongwei Xi Boston University

Datatypes Available in various functional programming languages such as SML and Haskell Convenience in programming Clarity in code

An Example: Random-Access Lists Cons: O(log n) (Amortized: O(1)) Uncons: O(log n) (Amortized: O(1)) Lookup operation: O(log n) Update operation: O(log n)

Datatype for Random Lists datatype ‘a ralist = Nil | One of ‘a | Even of ‘a ralist * ‘a ralist | Odd of ‘a ralist * ‘a ralist L1: x 1, …,x n ; L2: y 1, …, y n Even(L1, L2): x 1, y 1, …, x n, y n L1: x 1, …,x n, x n+1 ; L2: y 1, …, y n Odd(L1, L2): x 1, y 1, …, x n, y n, x n+1

Some Inadequacies Even should only be applied to two nonempty lists of equal length Odd should only be applied to two nonempty lists where the first list contains exactly one more element than the second one Unfortunately, these invariants cannot be captured by the type system of ML

Dependent Datatypes for Random Lists datatype ‘a ralist with nat = Nil(0) | One(1) of ‘a | {n:pos} Even(n+n) of ‘a ralist(n) * ‘a ralist(n) | {n:pos} Odd(n+n+1) of ‘a ralist(n+1) * ‘a ralist(n) For instance, Even is given the type: {n:pos} ‘a ralist(n) * ‘a ralist(n) -> ‘a ralist(n+n)

uncons in Dependent ML(DML) fun(‘a) uncons (One x) = (x, Nil) | uncons (Even (l1, l2)) = (case uncons l1 of (x, Nil) => (x, l2) | (x, l1) => (x, Odd (l2, l1)) | uncons (Odd (l1, l2)) = let val (x, l1) = uncons l1 in (x, Even (l2, l1)) end withtype {n:pos} ‘a ralist(n) -> ‘a * ‘a ralist(n-1)

Pattern Matching in DML Nondeterministic at compile-time Sequential at run-time This can cause an annoying problem in DML: the previous code for uncons does not type-check

Mutually Disjoint Patterns Note that: nondeterministic pattern matching is the same as sequential pattern matching if all patterns are disjoint We can manually expand patterns into disjoint ones, but this may be inconvenient and error-prone

An Example of Expansion (case uncons l1 of (x, Nil) => (x, l2) | (x, l1) => (x, Odd (l2, l1)) is expanded into (case uncons l1 of (x, Nil) => (x, l2) | (x, l1 as One _) => (x, Odd (l2, l1)) | (x, l1 as Even _) => (x, Odd (l2, l1)) | (x, l1 as Odd _) => (x, Odd (l2, l1))

The Problem Given patterns p, p 1, …, p n, we intend to find a list patterns p’ 1, …, p’ n’ such that a value v matches p but none of p i if and only if it matches one of p’ j. Note that p’ 1, …, p’ n’ need not be disjoint. An algorithm that generates the least n’ is said to be optimal.

The result An algorithm, which is essentially based upon Laville’s work, is presented and proven to be optimal. Note that this is an exponential algorithm. We do handle datatypes with infinitely many constructors (integers).

A Motivating Example fun restore (R(R t, y, c), z, d) = R(B t, y, B(c, z, d)) | restore (R(a, x, R(b, y, c)), z, d) = R(B (a, x, b), y, B(c, z, d)) | restore (a, x, R(R(b, y, c), z, d)) = R(B (a, x, b), y, B(c, z, d)) | restore (a, x, R(b, y, R t)) = R(B (a, x, b), y, B t) | restore t == B t (* == indicates the need for resolving sequentiality *) withtype … The last clause in the above definition needs to be expanded into 36 ones in order to type-check.

Exhaustiveness of Patterns datatype ‘a list with nat = nil(0) | {n:nat} cons(n+1) of ‘a * ‘a list(n) fun(‘a, ‘b) zip ([], []) = [] | zip (x :: xs, y :: ys) = (x, y) :: zip (xs, ys) withtype {n:nat} ‘a list(n) * ‘b list(n) -> (‘a * ‘b) list(n) The pattern matching clauses in the definition of zip is exhaustive: neither ([], _ :: _) nor (_ :: _, []) can have type ‘a list(n) * ‘b list(n) for any natural number n.

Exhaustiveness of Patterns fun(‘a) nth_safe (0, x :: _) = x | nth_safe (i, _ :: xs) = nth_safe (i-1, xs) withtype {i:nat, n:nat | i ‘a The pattern matching clauses are also exhaustive since …

Tag Check Elimination Pat = (_, _) Pos = o.0 1 Pat = (_ :: _, _) Pos = o.1 Pat = ([], _) Pos = o.1 23 Pat = ([], [])Pat = ([], _ :: _) 45 Pat = (_ :: _, _ ::_)Pat = (_ :: _, []) 67

Interpreter (I) sort typ = Int | Bool | Fun of typ * typ sort ctx = nil | :: of typ * ctx datatype exp = Int of int | Bool of bool | Add of exp * exp | Sub of exp * exp | Eq of exp * exp | If of exp * exp * exp | One | Shift of exp | lam of exp | App of exp * exp | Fix of exp

Interpreter (II) We can refine exp with a type indes expression of sort typ * ctx : Add: {c:ctx} exp(Int, c) * exp (Int, c) -> exp (Int, c) One:{t:typ,c:ctx} exp(t, t :: c)) Shift:{ta:typ,tb:typ,c:ctx} exp(ta,c) -> exp(ta, tb :: c) Lam:{ta:typ,tb:typ,c:ctx} exp(tb, ta :: c) -> exp (Fun(ta, tb), c) …

Interpreter (III) fun evaluate e = eval (e, []) withtype {t:typ} exp(t, nil) -> value(t) and eval (Zero e, env) = let val ValInt i = eval (e, env) in ValBool (i = 0) end … …

Untagged Representation Obviously, there is no need for tags if we never do tag-checking on the values of a particular datatype However, garbage collection makes things much more difficult

Conclusion Dependent datatypes can more accurately model data structures More program errors can be detected at compile-time Code becomes more robust This is a case when safer code runs faster