Operational Semantics Using the Partiality Monad

Similar documents
Normalization by Evaluation in the Delay Monad A Case Study for Coinduction via Copatterns and Sized Types

Normalization by Evaluation

THE UNIVERSITY OF CALGARY FACULTY OF SCIENCE FINAL EXAMINATION COMPUTER SCIENCE 521

Subtyping, Declaratively

Subtyping, Declaratively

From Operational Semantics to Abstract Machines

Abstracting Definitional Interpreters. David Van Horn

Internship report Testing judgements of type theory Chalmers Tekniska Högskola, Göteborg

THE UNIVERSITY OF CALGARY FACULTY OF SCIENCE FINAL EXAMINATION COMPUTER SCIENCE 521

Coinductive big-step semantics and Hoare logics for nontermination

The Lifting Lemma. Ralf Hinze

Strong Normalization for Guarded Types

Nunchaku: Flexible Model Finding for Higher-Order Logic

Normalisation by Evaluation for Dependent Types

Focusing on Binding and Computation

Typed Arithmetic Expressions

Modular Bisimulation Theory for Computations and Values

Coinductive big-step operational semantics

A proof checking kernel for the λπ-calculus modulo

Mathematical Synthesis of Equational Deduction Systems. Marcelo Fiore. Computer Laboratory University of Cambridge

Coalgebras and Codata in Agda

The Curry-Howard Isomorphism

Simply Typed Lambda Calculus

Internship Report Game Semantics and Normalization by Evaluation for Brouwer Ordinals

First Class Traversal Idioms with Nested Attribute Grammars

Streams and Coalgebra Lecture 2

Meta-reasoning in the concurrent logical framework CLF

Subtyping and Intersection Types Revisited

F-in Coq: A Deep Embedding

A Calculus of Definitions

Realizability Semantics of Parametric Polymorphism, General References, and Recursive Types

Proofs and computations

A Denotational Approach to Measuring Complexity in Functional Programs

Program Testing and Constructive Validity

Groups and higher groups in homotopy type theory

Depending on equations

Logic for Computational Effects: work in progress

Indexed Induction-Recursion

Meta-Reasoning in a Concurrent Logical Framework

Normalisation by Evaluation for Dependent Types

The lambda calculus with constructors

Copatterns Programming Infinite Objects by Observations

λ-calculus and types

Higher Order Containers

CSCI 490 problem set 6

arxiv: v1 [cs.lo] 23 Jan 2019

NORMALISATION BY EVALUATION FOR TYPE THEORY, IN TYPE THEORY

Tutorial on Semantics Part I

The syntactic guard condition of Coq

Indexed Containers. Thorsten Altenkirch Peter Morris University of Nottingham Abstract. 1 Introduction

A New Look At Generalized Rewriting in Type Theory

Monadic Refinements for Relational Cost Analysis (Appendix)

Roy L. Crole. Operational Semantics Abstract Machines and Correctness. University of Leicester, UK

Equivalence of Regular Expressions and FSMs

Towards Operations on Operational Semantics

Monads for Relations. Jeremy G. Siek University of Colorado at Boulder SPLS, June Jeremy Siek Monads for Relations 1 / 20

Verified Characteristic Formulae for CakeML. Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 18, 2017

The Continuity of Monadic Stream Functions

Automated Reasoning Lecture 17: Inductive Proof (in Isabelle)

Foundations of Computation. Ana Bove

Functional Big-step Semantics

The Continuity of Monadic Stream Functions

Order Sorted Algebra. Japan Advanced Institute of Science and Technology. March 8, 2008

Bisimulation and coinduction in higher-order languages

Deterministic Finite Automaton (DFA)

Towards a general framework for metareasoning on HOAS encodings

Simply Typed Lambda Calculus

Weak ω-groupoids in Type Theory

A probabilistic lambda calculus - Some preliminary investigations

Operational Semantics

Notes from Yesterday s Discussion. Big Picture. CIS 500 Software Foundations Fall November 1. Some lessons.

Reasoning with Higher-Order Abstract Syntax and Contexts: A Comparison

Principles of Program Analysis: Control Flow Analysis

Operationally-Based Theories of Program Equivalence

Bicubical Directed Type Theory

Parametric Polymorphism and Operational Improvement

Applied Logic. Lecture 1 - Propositional logic. Marcin Szczuka. Institute of Informatics, The University of Warsaw

Technical Report No Proofs Accompanying Fast and Loose Reasoning is Morally Correct NILS ANDERS DANIELSSON

Semantic Labelling for Proving Termination of Combinatory Reduction Systems. Makoto Hamana

Towards Correctness of Program Transformations Through Unification and Critical Pair Computation

Specification of Core Agda Subtitle

On Coinduction and Quantum Lambda Calculi

Lecture 4: Effect Handlers

Wolfgang Jeltsch. Seminar talk at the Institute of Cybernetics Tallinn, Estonia

Theory of Computer Science. Theory of Computer Science. D7.1 Introduction. D7.2 Turing Machines as Words. D7.3 Special Halting Problem

On the Correctness of the Krivine Machine

Formalising the Completeness Theorem of Classical Propositional Logic in Agda (Proof Pearl)

ON FIRST-ORDER CONS-FREE TERM REWRITING AND PTIME

Denoting computation

Dynamic Semantics. Dynamic Semantics. Operational Semantics Axiomatic Semantics Denotational Semantic. Operational Semantics

Pola: a language for PTIME Programming. Mike Burrell, Robin Cockett, Brian Redmond LCC 2009

Formal SOS-Proofs for the Lambda-Calculus

Modeling and Analysis of Communicating Systems

Type Soundness for Path Polymorphism

Constructive Formalization of Regular Languages

Introduction to Turing Machines. Reading: Chapters 8 & 9

RANK HIERARCHIES FOR GENERALIZED QUANTIFIERS

HORSes: format, termination and confluence

A note on coinduction and weak bisimilarity for while programs

Syntax. Notation Throughout, and when not otherwise said, we assume a vocabulary V = C F P.

Transcription:

page.1 Operational Semantics Using the Partiality Monad Nils Anders Danielsson (Göteborg) Shonan Meeting 026: Coinduction for computation structures and programming languages The research leading to these results has received funding from the European Research Council under the European Union s Seventh Framework Programme (FP7/2007-2013)/ERC grant agreement n 247219. This presentation does not necessarily reflect the views of the ERC or the EU. The EU is not liable for any use of the presented information.

page.2 Total Definitional Interpreters Nils Anders Danielsson (Göteborg) Shonan Meeting 026: Coinduction for computation structures and programming languages The research leading to these results has received funding from the European Research Council under the European Union s Seventh Framework Programme (FP7/2007-2013)/ERC grant agreement n 247219. This presentation does not necessarily reflect the views of the ERC or the EU. The EU is not liable for any use of the presented information.

page.3 Outline Using partiality monad to: Define semantics of partial language: total definitional interpreter. Prove type soundness. Prove compiler correctness.

page.4 A partial language A language with two effects: non-termination and crashes. t = c x λx.t t t Represented using well-scoped de Bruijn indices: data Tm (n N) Set where con N Tm n var Fin n Tm n lam Tm (1 + n) Tm n app Tm n Tm n Tm n

page.5 A partial language A language with two effects: non-termination and crashes. t = c x λx.t t t Represented using well-scoped de Bruijn indices: data Tm (n N) Set where con N Tm n var Fin n Tm n lam Tm (1 + n) Tm n app Tm n Tm n Tm n

page.6 Environments and values Based on closures: Env N Set Env n = Vec Value n data Value Set where con N Value clo Tm (1 + n) Env n Value

page.7 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ)

page.8 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ)

page.9 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ)

page.10 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ)

page.11 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ)

page.12 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ)

page.13 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ) Does not work in Agda, Coq Call-by-value? Call-by-name?

page.14 Relational, big-step semantics Can define inductive big-step semantics: ρ t v Very similar to definitional interpreter, but we cannot run the semantics. Does not distinguish between non-termination and crashes.

page.15 Relational, big-step semantics Can add coinductive big-step semantics: ρ t Problem: Duplication of rules. Problem: Have we forgotten a rule?

page.16 Alternative Definitional interpreter + partiality monad functional, big-step semantics.

page.17 Partiality monad

page.18 Partiality monad codata (A Set) Set where now A A later A A Constructive partiality monad, not maybe monad or exception monad. A ν C. A + C.

page.19 Partiality monad codata (A Set) Set where now A A later A A Constructive partiality monad, not maybe monad or exception monad. A ν C. A + C.

page.20 Partiality monad codata (A Set) Set where now A A later A A Constructive partiality monad, not maybe monad or exception monad. A ν C. A + C.

page.21 Partiality monad codata (A Set) Set where now A A later A A never A never = later never >= A (A B ) B now x >= f = f x later x >= f = later (x >= f)

page.22 Partiality monad codata (A Set) Set where now A A later A A What is the right notion of equality for A? later (later (now 5)) now 5 later never never now 5 never Equality up to finite differences in the number of later constructors.

page.23 Total definitional interpreter

page.24 Definitional interpreter Tm n Env n Value con i ρ = con i var x ρ = lookup x ρ lam t ρ = clo t ρ app t 1 t 2 ρ = t 1 ρ t 2 ρ Value Value Value clo t 1 ρ v 2 = t 1 (v 2 ρ)

page.25 Definitional interpreter With Maybe monad: Tm n Env n Maybe Value con i ρ = return (con i) var x ρ = return (lookup x ρ) lam t ρ = return (clo t ρ) app t 1 t 2 ρ = t 1 ρ >= λ v 1 t 2 ρ >= λ v 2 v 1 v 2 Value Value Maybe Value clo t 1 ρ v 2 = t 1 (v 2 ρ) con i 1 v 2 = fail

page.26 Definitional interpreter With Maybe monad: Tm n Env n Maybe Value con i ρ = return (con i) var x ρ = return (lookup x ρ) lam t ρ = return (clo t ρ) app t 1 t 2 ρ = t 1 ρ >= λ v 1 t 2 ρ >= λ v 2 v 1 v 2 Value Value Maybe Value clo t 1 ρ v 2 = t 1 (v 2 ρ) con i 1 v 2 = fail

page.27 Total definitional interpreter With Maybe + : Tm n Env n (Maybe Value) con i ρ = return (con i) var x ρ = return (lookup x ρ) lam t ρ = return (clo t ρ) app t 1 t 2 ρ = t 1 ρ >= λ v 1 t 2 ρ >= λ v 2 v 1 v 2 Value Value (Maybe Value) clo t 1 ρ v 2 = later ( t 1 (v 2 ρ)) con i 1 v 2 = fail

page.28 Total definitional interpreter With Maybe + : Tm n Env n (Maybe Value) con i ρ = return (con i) var x ρ = return (lookup x ρ) lam t ρ = return (clo t ρ) app t 1 t 2 ρ = t 1 ρ >= λ v 1 t 2 ρ >= λ v 2 v 1 v 2 Value Value (Maybe Value) clo t 1 ρ v 2 = later ( t 1 (v 2 ρ)) con i 1 v 2 = fail

page.29 Total definitional interpreter Tm n Env n (Maybe Value) Type signature deterministic. Type signature computable. No duplication of rules. Impossible to forget a case. Classically equivalent to ρ t v plus ρ t.

page.30 Type soundness

page.31 Types Coinductive simple types (to allow non-termination): codata Ty Set where nat Ty Ty Ty Ty τ Ty τ = τ nat Typing relation: Γ t σ

page.32 Type soundness Statement of type soundness: [ ] t σ t [ ] fail Proof: Generalise, then nested corecursion/ structural recursion.

page.33 Compiler correctness

page.34 Virtual machine data State Set where data Result Set where continue State Result done VM-Value Result crash Result step State Result step = step = crash

page.35 Virtual machine Small-step, total, functional semantics: exec State (Maybe VM-Value) exec s = case step s of continue s later (exec s ) done v return v crash fail Type signature deterministic. Type signature computable. Possible to forget a case.

page.36 Compiler correctness Small-step, total, functional semantics: exec State (Maybe VM-Value) Compilers: comp comp v Tm 0 State Value VM-Value

page.37 Compiler correctness Small-step, total, functional semantics: exec State (Maybe VM-Value) Compilers: comp comp v Tm 0 State Value VM-Value Compiler correctness: (t Tm 0) exec (comp t) ( t [ ] >= λ v return (comp v v))

page.38 Conclusions Definitional interpreter + partiality monad functional, total, big-step semantics. Can mechanise (some) meta-theory. See paper for non-determinism, contextual equivalence, applicative bisimilarity.

page.39 Bonus slides

page.40 Operational semantics? Very close to usual, relational big-step operational semantics. Not compositional. Values contain closures.

page.41 Typing rules Γ con i nat Γ var x lookup x Γ σ Γ t τ Γ lam t σ τ Γ t 1 σ τ Γ t 2 σ Γ app t 1 t 2 τ

page.42 Applicative bisimilarity, part 1 data (Maybe Value) (Maybe Value) Set where now u MV v now u now v later (x y) later x later y later l x y later x y later r x y x later y data MV Maybe Value Maybe Value Set where just u V v just u MV just v nothing nothing MV nothing

page.43 Applicative bisimilarity, part 2 data V Value Value Set where con con i V con i clo ( v ( t 1 (v ρ 1 ) t 2 (v ρ 2 ))) clo t 1 ρ 1 V clo t 2 ρ 2 T Tm n Tm n Set t 1 T t 2 = ρ t 1 ρ t 2 ρ