A Type System for MetaML MetaML types as an Omega program Lecture 12.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
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 =
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.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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 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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Copyright © 2010 Pearson Education, Inc
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Staging in Haskell What is Staging What does it Mean Using Template Haskell.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
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)
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Chapter 9: Functional Programming in a Typed Language.
Advanced Functional Programming Tim Sheard 1 Lecture 6 Functional Programming Tim Sheard & Mark Jones Monads & Interpreters.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Advanced Functional Programming Tim Sheard 1 Lecture 18 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Lecture 5 1 CSP tools for verification of Sec Prot Overview of the lecture The Casper interface Refinement checking and FDR Model checking Theorem proving.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
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:
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Environments, Stores, and Interpreters. Overview As we study languages we will build small languages that illustrate language features We will use two.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
6.001 SICP Compilation Context: special purpose vs. universal machines
CS 326 Programming Languages, Concepts and Implementation
A lightening tour in 45 minutes
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
On the Structure of Interpreters
Corky Cartwright January 18, 2017
Env. Model Implementation
Tim Sheard Oregon Graduate Institute
Advanced Functional Programming
The Metacircular Evaluator
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Procedures App B: SLLGEN 1.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
Environments, Stores, and Interpreters
Assignments and Procs w/Params
Recursive Procedures and Scopes
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

A Type System for MetaML MetaML types as an Omega program Lecture 12

Assignments No programming assignment this week. The paper –“ML Like Inference for Classifiers” –To be discussed today Feb 17, 2005 New Paper Reading Assignment –Search-Based Binding Time Analysis using Type-Directed Pruning –Tim Sheard and Nathan Linger –Available on class web site. Project proposal –A 1 page written project proposal is due next Tuesday, Feb. 22.

How do we describe type systems? How do we type: 5 or 12 ? –All Integer constants have type int How do we type f x ? –If f has type (a -> b) –And x has type a –Then f x has type b

Type Derivation notation Programming language researchers have developed a notation to describe type systems, sometimes called a sequent or derivation f x :: b f :: a -> bx :: a

Handling Variables What type does x have? –It depends on context –The notation uses a s Greek symbol (usually Γ or  ) to represent a function that encodes the context. Γ├ x : a Γ x ═ a We treat Γ as a function from variables to types

Extending Contexts If a context is a function, we can build a new function which knows about more variables Γ( x, a ) is a function that maps x to a and every other variable to the type that Γ maps it to Γ ├ (fn x => e) :: a -> b Γ(x,a) ├ e :: b

DeBruijn Indices DeBruijn indices are used to indicate variables by position, rather than by name. Name based (fn x => (x, fn y => (x,y)) Equivalent Index based (fn => (v0, fn => (v1,v0)) The index of a variable indicates how many lambdas to walk over to reach the binding lambda.

DeBruijn Contexts Contexts are just lists of types (we know which variable they belong to by their position in the list). Usually the lists “grow” from the right. Γ ├ (fn => fn => e) :: a -> b -> c Γ(a) ├ fn => e :: b -> c Γ(a)(b) ├ e :: c

DeBruijn Rule for variables Γ(a)├ V0 : a Γ├ V(n-1) : b Γ(a)├ Vn : b

MetaML: Type system Typing judgment contains a "sliding band" of type contexts. The lambda fragment binds and looks up variables in the "present" –Staging operations slide the pointer present future past          

Overview of Omega Object language is an algebraic datatype with type indexes It is indexed by some semantic property –Type Omega-Programs check and maintain that the index property is manipulated in a consistent manner. This lets us build and test type systems interactively.

Other features of Omega. Omega is a Haskell-like language with following additional features –Support for equality qualified types The type system automatically propagates and manipulates type equalities –Complex kind system Allows the programmer to define new kinds Polymorphic kind system –Staging MetaML-style code

Omega: An Example kind Nat = Zero | Succ Nat data List :: *0 ~> Nat ~> *0 where Nil :: List a Zero Cons :: a -> List a m -> List a (Succ m) A kind of natural numbers ( Nat ) –Classifies types Zero, Succ Zero, Succ(Succ Zero) … These types do not classify any runtime computations Think of them as Lists with length n –List a n : a list with whose length is n We call the parameter n a type index.

Indexes describe invariants or properties Function map preserves length of lists What about more complex invariants –Function append: –(List a m) -> (List a n) -> (List a (m+n)) map :: (a -> b) -> (List a n) -> (List b n) map f Nil = Nil map f (Cons x xs) = Cons (f x) (map f xs)

Append sum :: Nat ~> Nat ~> Nat {sum Zero x} = x {sum (Succ y) x} = Succ {sum y x} app :: List a n -> List a m -> List a {sum n m} app Nil ys = ys app (Cons x xs) ys = Cons x (app xs ys)

A Staged Object Langauge A staged object language MetaML –A standard lambda calculus fragment –Staging fragment Brackets, escape, run, and CSP –Sequences of contexts (or environments) Past, present, future –Use type indexes to describe types of terms and shapes of contexts

MetaML in Omega data Cd n f t = Cd (forall p. Exp p n f t) data Var:: *0 ~> *0 ~> *0 where Z:: Var (b,a) a S:: Var w x -> Var (w,y) x data Exp:: *0 ~> *0 ~> *0 ~> *0 ~> *0 where Const:: t -> Exp past now future t V:: Var now t -> Exp past now future t Abs:: Exp p (n,s) f t -> Exp p n f (s -> t) App:: Exp p n f (t1->t) -> Exp p n f t1 -> Exp p n f t Br:: Exp (p,n) c f t -> Exp p n (c,f) (Cd c f t) Esc:: Exp p b (n,f) (Cd n f t) -> Exp (p,b) n f t Csp:: Exp p b (n,f) t -> Exp (p,b) n f t Pair:: Exp p n f s -> Exp p n f t -> Exp p n f (s,t) Run:: (forall x. Exp p n (x,f) (Cd x f t)) -> Exp p n f t

Example Variables data Var:: *0 ~> *0 ~> *0 where Z:: Var (b,a) a S:: Var w x -> Var (w,y) x prompt> Z Z : (forall a b. Var (a,b) b) prompt> S Z (S Z) : (forall a b c. Var ((a,b),c) b) prompt> S(S Z) (S (S Z)) : (forall a b c d. Var (((a,b),c),d) b) prompt> V(S Z) (V (S Z)) : (forall a b c d e. Exp a ((b,c),d) e c) The “value” of the variable (Z = 0, S Z = 1, S(S Z)=2, …) indicates how many pairs to climb over

DeBruijn Notation in nested pairs ((((_,x3),x2),x1),x0) The number indicates number of “steps” to the “left” before accessing the “right” component. ( _, x0 ) ( _, x1) ( _, x2)

Example expressions x0 = V Z x1 = V(S Z) prompt> x0 (V Z) : (forall a b c d. Exp a (b,c) d b) prompt> x1 (V (S Z)) : (forall a b c d e. Exp a (b,(c,d)) e c) prompt> Abs x0 (Abs (V Z)) : (forall a b c d. Exp a b c (d -> d)) prompt> Abs x1 (Abs (V (S Z))) : (forall a b c d e. Exp a (b,c) d (e -> c)) prompt> Abs (Abs (App x1 x0)) (Abs (Abs (App (V (S Z)) (V Z)))) : (forall a b c d e. Exp a b c ((d -> e) -> d -> e))

Comparison Note how the constructor functions are like type system rules App :: Exp a b c (d -> e) -> Exp a b c d -> Exp a b c e f x :: e f :: d -> ex :: d

Contexts The second index acts like a DeBruijn context Abs :: Exp a (Γ,b) d e -> Exp a Γ d (b -> e) Γ ├ (fn => w) :: b -> e Γ(b) ├ w :: e

MetaML: Type system Typing judgment contains a "sliding band" of type contexts. The lambda fragment binds and looks up variables in the "present" –Staging operations slide the pointer –First and third index are “stacks” of contexts present future past          

Interpreter Decision: How to represent code –Code is just an Exp… –… but not just any exp data Cd n f t = Cd (forall p. Exp p n f t) Why polymorphic in the past? –Polymorphic in the past = no escapes –Polymorphic in the present = is a closed term –Polymorphic in the future = contains no brackets

Structure of the Interpreter A family of functions –Eval "level zero" Evaluates the lambda calculus fragment in the usual way –Build Performs two slightly different functions –Splices escaped code (level 1 in MetaML) –Rebuilds code keeping track of levels

The lambda fragment is standard When encountering a code bracket –Invokes the build function (bd), remembering the current environment eval :: Exp past now future t -> now -> t eval (Const n) env = n eval (V Z) (x,y) = y eval (V (S v)) (x,y) = eval (V v) x eval (App f x) env = (eval f env) (eval x env) eval (Abs e) env = \ v -> eval e (env,v) eval (Pair x y) env = (eval x env, eval y env) eval (Br e) env = Cd (bd (EnvZ env) e) eval (Run e) env = case eval e env of { Cd x -> eval x () }

eval > > = bd1 > = > = > bd1 :: Exp (a,b) n f t -> Exp z n f t bd2 :: Exp ((a,b),c) n f t -> Exp (z,c) n f t bd3 :: Exp (((a,b),c),d) n f t -> Exp ((z,c),d) n f t bd4 :: Exp ((((a,b),c),d),e) n f t -> Exp (((z,c),d),e) n f t

Building code Build takes an expression and rebuilds it –Relation (Env x y) relates the past of the argument and the result expression The base case is when env is created by invocation from eval –Store the environment with which the build was invoked to give as an argument to eval in case of escapes –The past of the result expression is polymorphic so that it can be formed into code data Env:: *0 ~> *0 ~> *0 where EnvZ:: a -> Env (b,a) c EnvS:: Env a b -> Env (a,c) (b,c) bd :: Env a z -> Exp a n f t -> Exp z n f t

(Env x y) continued: –Inductive case EnvS: (Env x y) -> (Env (x,a) (y,a)) Going under brackets enlarges the past

build Escape –Two cases depending on the environment –Bracket (increment the environment) The lambda fragment is simply rebuilt bd :: Env a z -> Exp a n f t -> Exp z n f t bd env (Const n) = Const n bd env (V z) = V z bd env (App x y) = App (bd env x) (bd env y) bd env (Abs e) = Abs(bd env e) bd env (Pair x y) = Pair (bd env x) (bd env y) bd env (Br e) = Br(bd (EnvS env) e) bd env (Run e) = Run(bd env e) bd (EnvZ env) (Esc e) = case eval e env of { Cd x -> x} bd (EnvS r) (Esc e) = case bd r e of { Br x -> x; y -> Esc y } bd (EnvZ env) (Csp e) = Const(eval e env) bd (EnvS r) (Csp e) = Csp(bd r e)

Run Polymorphism to encode run –Recall: an expression polymorphic in the present contains no free variables –Therefore it can be executed in any environment (including the empty environment) data Exp past now future t = | Run (forall n. Exp past now (n,future) (Cd n future t)) eval env (Run e) = case eval env e of { Cd x -> eval () x }

Programs rejected -- ~(run )> -- b2 = Abs (Esc (Run (Br (V Z)))) b1 = Br(V Z) b1 :: Exp a b ((c,d),e) (Cd (c,d) e d)) Run :: (forall e. Exp a b (e,c) (Cd e c d)) -> Exp a b c d Not polymorphic in “now” since it has a free variable.

Puzzle val puzzle = run ((run ~( (fn x => ) (fn w => ) ) 5>) 3); -- (fn x => ) exp1 = Abs (Br (Csp(V Z))) -- fn w => ) exp2 = Abs (Br ( (V (S Z)))) -- ~((fn x => ) (fn w => )) exp3 = Esc(App exp1 exp2) --- ~( (fn x => ) (fn w => ) ) 5> exp4 = Br(Abs(App exp3 (Const 5)))

Note type of inner term -- ~( (fn x => ) (fn w => ) ) 5> prompt> exp4 (Br (Abs (App (Esc (App (Abs (Br (Csp (V Z)))) (Abs (Br (V (S Z)))))) (Const 5)))) :: Exp a b ((c,d),e) (Cd (c,d) e (a1 -> Cd ((c,d),a1) e d)))

Further questions OK, so this is not exactly MetaML? –How do we embed MetaML into MetaL? Provide a translation How does it relate to other MetaML type systems? Are there programs it rejects/accepts that others don't? –We conjecture it is similar to environment classifiers, but need to formalize this notion Code type carries the type of the environment? –Is this really a problem? Types may grow (arbitrarily) large But the power function works!!!