Meta-programming with Names and Necessity p.1

Similar documents
On the Logical Foundations of Staged Computation

Modal Logic: Implications for Design of a Language for Distributed Computation p.1/53

Meta-programming with Names and Necessity

Meta-programming with Names and Necessity

Lecture Notes on Computational Interpretations of Modalities

Extracting the Range of cps from Affine Typing

4.5 Pure Linear Functional Programming

Kripke-Style Contextual Modal Type Theory

CLF: A logical framework for concurrent systems

CSE 341: Programming Languages

System Description: Delphin A Functional Programming Language for Deductive Systems

Closed Types for a Safe Imperative MetaML

Incremental Rebinding

Rule Formats for Nominal Modal Transition Systems

Lambda Calculus. Lecture 4 CS /26/10

A Pronominal Account of Binding and Computation

Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description)

(Refer Slide Time: 4:00)

A Polymorphic Type System for Multi-Staged Languages

Formal Systems and their Applications

Modal Logic Revisited

An Implementation of the Language Lambda Prolog Organized around Higher-Order Pattern Unification

Towards a Semantic Web Modeling Language

A Simple Supercompiler Formally Verified in Coq

The Metalanguage λprolog and Its Implementation

Introduction to OCaml

From the λ-calculus to Functional Programming Drew McDermott Posted

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Static Name Control for FreshML

MetaML: structural reflection for multi-stage programming (CalcagnoC, MoggiE, in collaboration with TahaW)

Lecture Notes on Data Representation

Lecture Notes on Aggregate Data Structures

Objects as Session-Typed Processes

Lambda Calculus. Variables and Functions. cs3723 1

Introduction to Lambda Calculus. Lecture 7 CS /08/09

How to reify fresh type variables?

Denotational Semantics. Domain Theory

Deriving Compilers and Virtual Machines for a Multi-Level Language

Lecture Notes on Static and Dynamic Semantics

Lambda Calculus and Type Inference

Functional Programming with Names and Necessity

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

HIGHER-ORDER ABSTRACT SYNTAX IN TYPE THEORY

Assistant for Language Theory. SASyLF: An Educational Proof. Corporation. Microsoft. Key Shin. Workshop on Mechanizing Metatheory

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Functional Languages. Hwansoo Han

Functional Programming. Big Picture. Design of Programming Languages

CSE-321 Programming Languages 2010 Final

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

Gradual Parametricity, Revisited

Typing Multi-Staged Programs and Beyond

MPRI course 2-4 Functional programming languages Exercises

A Canonical 1 Locally Named Representation of Binding. α -equivalence is identity. Randy Pollack. Masahiko Sato. LFCS, University of Edinburgh

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Urmas Tamm Tartu 2013

Chapter 11 :: Functional Languages

1.3. Conditional expressions To express case distinctions like

An Explicit-Continuation Metacircular Evaluator

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Fundamental Concepts. Chapter 1

Fundamentals and lambda calculus

MMT Objects. Florian Rabe. Computer Science, Jacobs University, Bremen, Germany

CS4215 Programming Language Implementation. Martin Henz

CSCI-GA Scripting Languages

Typing & Static Analysis of Multi-Staged Programs

Statically-Scoped Exceptions: a Typed Foundation for Aspect-Oriented Error Handling

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Mechanized metatheory revisited

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

A Type Theory for Memory Allocation and Data Layout (Extended Version)

CSE505, Fall 2012, Midterm Examination October 30, 2012

A Logical View of Effects

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

Constrained Types and their Expressiveness

Comp 311: Sample Midterm Examination

Mutable References. Chapter 1

System Description: Twelf A Meta-Logical Framework for Deductive Systems

A Self-Hosting Evaluator using HOAS

CS153: Compilers Lecture 15: Local Optimization

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Elf: A Meta-Language for Deductive Systems (System Description)

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

An experiment with variable binding, denotational semantics, and logical relations in Coq. Adam Chlipala University of California, Berkeley

1. Abstract syntax: deep structure and binding. 2. Static semantics: typing rules. 3. Dynamic semantics: execution rules.

c constructor P, Q terms used as propositions G, H hypotheses scope identifier for a notation scope M, module identifiers t, u arbitrary terms

Coq quick reference. Category Example Description. Inductive type with instances defined by constructors, including y of type Y. Inductive X : Univ :=

Mechanizing Metatheory with LF and Twelf

Chapter 5: The Untyped Lambda Calculus

CHAPTER ONE OVERVIEW. 1.1 Continuation-passing style

Programming type-safe transformations using higher-order abstract syntax

Abstract syntax for variable binders: An overview

What does my program mean?

Introduction to lambda calculus Part 3

Accurate Step Counting

Typed Lambda Calculus for Syntacticians

LECTURE 16. Functional Programming

Types for binding. Paul Stansifer. April 13, 2012

Tracing Ambiguity in GADT Type Inference

Transcription:

Meta-programming with Names and Necessity Aleksandar Nanevski Carnegie Mellon University ICFP, Pittsburgh, 05 October 2002 Meta-programming with Names and Necessity p.1

Meta-programming Manipulation of (source) programs of an object language Input object program Meta program Output object program Examples: compilers, partial evaluators, symbolic computation systems, meta-logical frameworks Meta-programming with Names and Necessity p.2

Typed meta-programming Typed meta- and object-language Well-typed meta-programs can construct only well-typed source object-language programs Source object-language programs higher-order syntax trees Object-language types Meta-language types Here, we name the inclusion as a modal type constructor object-language types meta-language types Example: programs of type type of (source) object-language Meta-programming with Names and Necessity p.3

Representation of source programs Must handle programs with binding structure built-in notion of equivalence modulo -renaming variables Enable type-safe evaluation of closed object-language programs. Admit programs with free variables (as already noticed by MetaML community). Provide a way to destruct source object-language programs and recurse over their structure! (and this is why we need extra expressiveness over MetaML). Meta-programming with Names and Necessity p.4

Outline Introduction Background on S4-necessity Combining necessity with names Theorems Future work and conclusions Meta-programming with Names and Necessity p.5

-calculus Proof-term calculus for necessity fragment of intuitionistic modal S4 (Pfenning and Davies 00) Types values of this type encode closed source (i.e. syntactic) expressions of type Typing judgment Two kinds of variables: context for ordinary variables (binding compiled code) context for expression variables (binding source expressions) Meta-programming with Names and Necessity p.6

-calculus (cont d) Meta-programming with Names and Necessity p.7 Terms in Lisp behaves like Local reduction

Example 1 produces source expression - fun if then let box = box = box = else in box end; - val val = = box ; (* syntax *) can be pattern-matched against and/or evaluated: - let box val = = in ; Meta-programming with Names and Necessity p.8

Necessity limitations How to manipulate expressions with binding structure? Code analysis restricted subterms of a closed term are not necessarily closed Allowing only closed expressions will contain unnecessary redexes output expressions Need a type of open syntactic expressions or code schemas Meta-programming with Names and Necessity p.9

Outline Introduction Background on S4-necessity Combining necessity with names Theorems Future work and conclusions Meta-programming with Names and Necessity p.10

Code schemas Syntactic expressions with indeterminates (also called atoms, symbols or names ) Treatment of indeterminates (names) inspired by Nominal Logic and FreshML (Pitts and Gabbay 01) Names occurring in a boxed syntactic expression are listed in its type closed syntactic expressions of type with indeterminates Example: assuming are names, then Meta-programming with Names and Necessity p.11

Support of a term Support of a term set of names which should be defined before the term can be evaluated Example: assuming are names, then term type support Support of a term can be arbitrarily extended Meta-programming with Names and Necessity p.12

Typing code schemas Types Typing judgment is the support of Context Context Context, and for ordinary variables for expression variables with their support for names Meta-programming with Names and Necessity p.13

Typing code schemas (cont d) -Introduction rule -Elimination rule Meta-programming with Names and Necessity p.14

Typing code schemas (cont d) -Introduction rule -Elimination rule Meta-programming with Names and Necessity p.14

Typing code schemas (cont d) -Introduction rule -Elimination rule Meta-programming with Names and Necessity p.14

Typing code schemas (cont d) Meta-programming with Names and Necessity p.15 Terms Name rule

Explicit name substitution Terms Example = box - let box in box end = box - val does not bind Notice: the term constructor Meta-programming with Names and Necessity p.16

Example 2, generate the function Given = - fun else then box = if ) end in box ( let box ; = = box ( ) - val val end; in box - let box = val = box Meta-programming with Names and Necessity p.17

Name creation Dynamic introduction of names into computation (version of gensym) Terms Type system ensures the value of Typing rule does not depend on Meta-programming with Names and Necessity p.18

Name abstraction Used to express that a term depends on one name, no matter which (inspired by FreshML and Nominal Logic of Pitts and Gabbay) Terms Types N pairs up Example: polynomial and the value of into a closure with one indeterminate - new in let val p = box in end end val it = box N Meta-programming with Names and Necessity p.19

Name concretion Provides a fresh name in place of the abstracted one Terms Elimination form for abstraction Example - val p = box N - val q = box N - new end; val it = true in = Expressions not fresh for and and. are not be well-typed, as is Meta-programming with Names and Necessity p.20

N Example 3 Given source for, generate source for Use pattern-matching to check if is a lambda = - fun of case box in = new in box let box box ) box ( ; box = box - val Thanks to pattern-matching, no redexes in the result Meta-programming with Names and Necessity p.21

Outline Introduction Background on S4-necessity Combining necessity with names Theorems Future work and conclusions Meta-programming with Names and Necessity p.22

Substitution principles, then 1. Ordinary substitution principle if and, then 2. Modal substitution principle if and and, then 3. Name substitution principle if Meta-programming with Names and Necessity p.23

Progress and preservation If 1. is a value, or 2. there exists is unique, and then either, such that ; furthermore, Meta-programming with Names and Necessity p.24

Future work Support polymorphism can be found in the paper Names of general types (currently names are simply typed) Type polymorphism and type-polymorphic recursion Polymorphic patterns and intensional type analysis Relation to MetaML and other meta-programming languages Extension to type theory with names Meta-programming with Names and Necessity p.25

Conclusions Type of closed syntactic program representations corresponds to modality of intuitionistic S4. Not expressive enough for intensional manipulation of programs with binding structure Type of open source programs can be obtained by adding indeterminates (names) to the language, thus creating polynomials over source expressions Names stand for free variables of source programs making it possible to destruct and analyze the source programs The distinction between compiled and source code achieved through the modality allows for typed names Since names are typed, explicit substitution can be made primitive Meta-programming with Names and Necessity p.26

Outline Introduction Background on S4-necessity Combining necessity with names Theorems Future work and conclusions Meta-programming with Names and Necessity p.27

Related work Judgmental reconstruction of modal logic (Pfenning and Davies 00) Nominal logic and FreshML (Pitts and Gabbay 01) Modeled in Fraenkel-Mostowsky set theory Uses name abstraction to represent -equivalence classes of terms Only first-order syntax Names limited to a type atm can be extended to a family of types......but still, names can be used only for bindings No distinction between variables and names of type atm Substitution must be hand-written Impossible to give substitution-style operational semantics Meta-programming with Names and Necessity p.28

Related work (cont d) Systems with type of open syntactic expressions Temporal calculus (Davies 96) object program = meta program at later time free object program variables = meta variables at later time problems: no evaluation of closed expressions no attempt at code analysis MetaML (Calcagno, Moggi, Taha, Sheard 01) + type refinement for closedness problems: no code analysis scope extrusion in presence of references Meta-programming with Names and Necessity p.29

Intensional code analysis Destructing syntactic expressions (with binding) by pattern-matching Higher-order patterns Pattern matches a syntactic expression with free variables in the set, and stores it into the pattern variable Meta-programming with Names and Necessity p.30

N N Intensional code analysis (cont d) Pattern typing judgment Lambda abstraction rule Pattern-variable rule Meta-programming with Names and Necessity p.31

Relationship with S4 Syntactic expressions can be composed Syntactic expressions are syntactic Syntactic expressions can be compiled and evaluated Meta-programming with Names and Necessity p.32

N N N Typing abstraction and concretion type constructor is a binder Name abstraction rule Name concretion rule Meta-programming with Names and Necessity p.33

Example 2 How to generate syntactic expressions with binding structure? produces source for Application = - fun else in box then let box = box end if ; - = box val! box But we want Meta-programming with Names and Necessity p.34

Example 4, generate code for Given code for Attempt with no code analysis = - fun in = let box box end ; box = box - val Unnecessary redexes again! Meta-programming with Names and Necessity p.35

Possible applications Distinguishing between extensional and intensional nature of programs algebraic simplifications in symbolic computation functions can exploit knowledge of intensional structure of arguments (examples: integration, differentiation) Higher-order Abstract Syntax Programmer-specified (source level) optimizations in run-time code generation mechanism for choosing between highly-optimized or quickly produced target programs domain-specific optimizations Meta-programming with Names and Necessity p.36

-calculus (cont d) Meta-programming with Names and Necessity p.37 Hypothesis rule Local reduction Local expansion

Explicit name substitution (cont d) Substituted name must be in context Typing rule Meta-programming with Names and Necessity p.38

Typing code schemas (cont d) Terms Name rule Hypotheses rules Meta-programming with Names and Necessity p.39

Typing code schemas (cont d) Terms Name rule Hypotheses rules Meta-programming with Names and Necessity p.39