Three Applications of Strictness in the Efficient Implementaton of Higher-Order Terms

Similar documents
Lecture Notes on Data Representation

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

Verifying Program Invariants with Refinement Types

CLF: A logical framework for concurrent systems

λ calculus is inconsistent

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

Let Arguments Go First

The Metalanguage λprolog and Its Implementation

Language Techniques for Provably Safe Mobile Code

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

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Symmetry in Type Theory

CSE-321 Programming Languages 2011 Final

Certificates for incremental type checking

Let Arguments Go First

Overview. A Compact Introduction to Isabelle/HOL. Tobias Nipkow. System Architecture. Overview of Isabelle/HOL

Embedding logics in Dedukti

Lecture 3: Typed Lambda Calculus and Curry-Howard

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

Refinement Types as Proof Irrelevance. William Lovas with Frank Pfenning

MLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen

Concepts of programming languages

Calculus of Inductive Constructions

Introduction to the Lambda Calculus

Lecture Notes on Aggregate Data Structures

CSE-321 Programming Languages 2012 Midterm

Automated Reasoning. Natural Deduction in First-Order Logic

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CITS3211 FUNCTIONAL PROGRAMMING

Type checking in the presence of meta-variables

Formally Certified Satisfiability Solving

CSE-321 Programming Languages 2014 Final

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Combining Programming with Theorem Proving

Inductive Definitions, continued

First-Class Type Classes

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 :=

B2. The Logical Framework

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

HIGHER-ORDER ABSTRACT SYNTAX IN TYPE THEORY

Introduction to dependent types in Coq

The Typed λ Calculus and Type Inferencing in ML

Mechanized metatheory revisited

Lambda Calculus and Type Inference

Introduction to Type Theory August 2007 Types Summer School Bertinoro, It. Herman Geuvers Nijmegen NL

The Polymorphic Blame Calculus and Parametricity

Combining Proofs and Programs in a Dependently Typed Language. Certification of High-level and Low-level Programs

Higher-Order Logic. Specification and Verification with Higher-Order Logic

Lecture Notes on Computational Interpretations of Modalities

Typed Lambda Calculus

Autosubst Manual. May 20, 2016

Names are (mostly) Useless: Encoding Nominal Logic Programming Techniques with Use-counting and Dependent Types

Programming Up-to-Congruence, Again. WG 2.8 Estes Park

Formal editing: jedit-mmt. Narrative editing: LaTeX-MMT. Browsing: MMT web server. Building: MMT scripting language. The MMT API: A Generic MKM System

Foundational Aspects of Syntax 1

Where is ML type inference headed?

Programming Languages Lecture 14: Sum, Product, Recursive Types

Pure Lambda Calculus. Lecture 17

Typed Lambda Calculus for Syntacticians

Efficient Substitution in Hoare Logic Expressions

Functional programming languages

Theorem Proving Principles, Techniques, Applications Recursion

An Introduction to Martin-Lof s Constructive Type Theory and a computer implementation of it. p.1/??

Verifying Uniqueness in a Logical Framework

Tracing Ambiguity in GADT Type Inference

Practical Programming with Higher-Order Encodings and Dependent Types

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Type Theory. Lecture 2: Dependent Types. Andreas Abel. Department of Computer Science and Engineering Chalmers and Gothenburg University

Meta-programming with Names and Necessity p.1

From Types to Sets in Isabelle/HOL

Mutable References. Chapter 1

Basic Foundations of Isabelle/HOL

CSE-321 Programming Languages 2010 Final

1. M,M sequential composition: try tactic M; if it succeeds try tactic M. sequential composition (, )

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

Inductive datatypes in HOL. lessons learned in Formal-Logic Engineering

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

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

CSE-321 Programming Languages 2010 Midterm

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

The design of a programming language for provably correct programs: success and failure

Normalization by hereditary substitutions

On the Logical Foundations of Staged Computation

Covering all bases: design and implementation of case analysis for contextual objects

Compiler Verification in LF

Simple Unification-based Type Inference for GADTs

Eta-equivalence in Core Dependent Haskell

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

CSE505, Fall 2012, Midterm Examination October 30, 2012

Exercise 1 ( = 24 points)

Inductive Beluga: Programming Proofs

Algebraic Processors

Dynamic Types , Spring March 21, 2017

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Coq with Classes. Matthieu Sozeau. Journées PPS 2011 September 5th 2011 Trouville, France. Project Team πr 2 INRIA Paris

Introduction to Co-Induction in Coq

Transcription:

Three Applications of Strictness in the Efficient Implementaton of Higher-Order Terms Frank Pfenning Workshop on Implementation of Logic Réunion Island, France November 11, 2000 Joint work with Carsten Schürmann and Kevin Watkins 1

Outline Notational Definitions Strictness 1 Unification with Definitions 2 Matching and Rewriting 3 Syntactic Redundancy Elimination Conclusion 2

Notational Definitions Common in mathematical practice Some examples: A = A (A B) = (A B) (B A)!x. A(x) = x. A(x) y. A(y) x. = y A B = Πx:A. B where x not free in B nat = µα. 1+α zero = fold (inl ) Based on higher-order abstract syntax, not concrete syntax [Griffin 89] 3

Derived Rules of Inference as Notational Definitions andelr D = andel (ander D) D A (B C) ELR B = D A (B C) ER B C EL B trans DE= impi (λu. impe E (impe D u)) D E A B B C trans A C =... 4

Other Kinds of Definitions By cases pred(0) = 0, pred(n +1)=n Recursive double(0) = 0, double(n + 1) = double(n) + 2 Syntactic sugar [1, 2, 3] = 1::2::3::nil Admissible rules of inference Sometimes, these may be thought of as notational definitions at a different semantic level. pred = lam (λx. case x 0(λn. n)) 5

Setting Logical framework or type theory (LF, Coq, Isabelle, Nuprl) All support notational definitions Framework tasks: (in Twelf system based on LF) representation and checking (LF) (terms, formulas, proofs,...) search and meta-programming (Elf) (theorem proving, logic programming, proof transformation,...) meta-theorem proving (Twelf) (logical interpretations, soundness & completeness, type preservation,...) 6

Core Operations Type checking Γ M : A requires convertibility Γ M. = N : A. Search Γ?:A and type inference require unification θ. Γ θm. = θn : θa Meta-theorem proving splits cases (also requiring unification). All of these require β-reduction (use debruijn indices and explicit substitutition [Dowek,Hardin,Kirchner,Pf 96]). 7

Language Types A ::= am 1...M n A A Πx:A. A Objects M ::= hm 1...M n λx:a. M Heads h ::= x variables c constructors d defined constants Signatures Σ ::= Σ,c:A Σ,d:A = M... Type-checking is easy except for convertibility. δ(d) =M if d:a = M in Σ Will omit types. Consider only βη-long normal forms. 8

Semantics of Definitions Definitions must be semantically transparent. For human interface: preserve definitions! For efficiency: preserve definitions! How do we reconcile these? 9

βηδ-convertibility Huet s algorithm for definitions in (early?) Coq (ignoring some issues of control): cm. = cn M. = N cm. = c N fails for c c dm. = cn δ(d) M. = cn cm. = dn cm. = δ(d) N dm. = dn M. = N or dm. = d N else else for d d or prev. case fails δ(d) M. = d N dm. = δ(d ) N δ(d) M. = δ(d ) N 10

Example: Must Expand Definitions k = λx. λy. x k 12 =. k 13 1 =1. 2 =3. fails δ(k) 12 =. k 13 1 =. k 13 1 =. δ(k) 13 1 =1. Even identical defined constants need to be expanded. 11

Example: Need Not Expand Definitions A = A (literally: = λa. A ).. A 0 = B 0 for A 0 = B 0. A 0 = B 0 fails (A 0 ) =(B. 0 ). A 0 = B 0 fails again Expanding identical defined constants if often redundant. 12

Analysis of Huet s Algorithm Preserves definitions as much as possible. Inefficient mostly during failure. Type-checking mostly succeeds. Tactic-based search often fails because of constructor clashes. No unification supported! 13

Injectivity d is injective if for all appropriate M and N, dm =. dn implies M =. N Injectivity of d implies: whenever. M = N then. dm = dn Allows early failure, avoids expansion of definitions. k is not injective (must expand). is injective (need not expand). 14

Strictness Strictness is a syntactic criterion on δ(d) that guarantees injectivity. A definition d:a = λx.hm is strict if every parameter x i has at least one strict occurrence in M. An occurence of x of the form xy 1...y n in M is strict if 1. all heads on the path from the root to the occurrence of x are either constructors c, strict definitions d, or locally bound variables y, but not definition parameters x i ; 2. y 1,...,y n are distinct locally bound variables (x occurs as a pattern variable). 15

Examples Revisited A = A strict in A nimp AB = B A strict in A and B (A B) = (A B) (B A) strict in A and B!x. A(x) = x. A(x) y. A(y) x. = y! = λa:i o. x. A x y. A y x. = y two strict occurrences of A 16

Counterexamples Revisited kxy = x no strict occurrence of x or y π 1 Ce 1 e 2 = rew (C(fst e 1,e 2 )) (C(e 1 )) no strict occurrences of C, e 1 or e 2 inst ta = infer ( (λx. A x)) (A t) strict in A, not strict in t These are often declared as constructors, not defined. 17

Optimization of Convertibility Mark definition as strict if it is strict in all arguments. Avoid retry in Huet s algorithm: commit to dm =. dn M =. N Practical value depends on percentage of strict definitions. In applications, most definitions are strict. Non-strictness mostly for derived rules of inference. Find appropriate level for definitions: k : exp exp exp = λx:exp.λy:exp.x not strict k : exp = lam(λx:exp. lam(λy:exp.x)) strict 18

Application 1: Unification with Definitions Unification derived from convertibility. Definitions much harder: strictness indispensible. Huet s algorithm for convertibility incomplete for unification. Example: occurs-check with definitions Y. = k 1 Y occurs-check fails, but unification should succeed 19

Counterexample: Keeping Definitions Consider k 12 =. k 1 Y Y =3. Y =2. Y =3. fails but Y = 3 is the most general unifier! Problem: unification of k 12 =. k 1 Y succeeds, but unifier is not most general. No natural backtrack points as for convertibility. 20

Strict Unification Let d be a strict definition, U(E) the set of unifiers of E. Then U(d M. = dn)=u(m. = N) from properties of convertibility and substitution. Generalized occurs-check: X. = hm fails the occurs-check if there is a strict occurrence of X in M. Note: id = λx. x is not strict because otherwise X = id(id(id X)) would fail the generalized occurs-check. 21

Implementation of Strict Unification Definitions are classified as strict or non-strict ( abbreviations ). Strict definitions are preserved as much as possible. Non-strict definitions are expanded during parsing. Critical cases of algorithm: dm. = cn δ(d) M. = cn cm. = dn cm. = δ(d) N dm. = dn M. = N dm. = d N δ(d) M. = δ(d ) N for d d 22

Assessment Unification is central in logical framework (type reconstruction, logic programming and search) Strict unification seems to work well in practice. Exploits interactions with other features of implementation (de Bruijn indices, explicit substitutions). 23

Refinements of Strictness Refinement (a): strictness per parameter inst ta = infer ( (λx. A x)) (A t) strict in A, not strict in t inst t A = infer ( (λx. A x)) (A t) strict in remaining argument A Refinement (b): hereditary analysis inst ta = infer ( (λx. A x)) (A t) strict in t is A = λx. A x is strict in x. Context-dependent. Practical value questionable: too few non-strict definitions. 24

Application 2: Matching and Rewriting Higher-order matching used in several contexts (functional logic programming, higher-order rewriting, meta-programming) Theorem: θ. Γ θm. = N : A has unique solutions if every free variable X has one strict occurrence in M. Obtain higher-order patterns if all occurrences must be strict [Miller 91] [Nipkow 91]. More general case arises in practice [Virga 99]. Of interest with and without dependent types. 25

Application 3: Syntactic Redundancy Elimination LF representations with dependent types carry significant redundant information for simplicity of type-checking. Inflates proof terms, slows down checking. Syntactic redundancy elimination: drop redundant information, retain decidability of type-checking. Important for proof compression (proof-carrying code [Necula 98], non-linear compression). Important for efficient checking and unification with dependencies. 26

Example A B I A B andi :ΠA. ΠB. pf (A) pf (B) pf (A B) Consider andi A B D E : pf (A B) :pf (A) :pf (B) A is redundant if D can synthesize its type. B is redundant if E and synthesize its type. A and B are redundant we know the type of the whole because A and B occur strict in pf (A B)! 27

Another Example x. A(x) E A(t) foralle :Πt:i. ΠA:i o. pf ( (λx. A x)) pf (A t) Consider foralle t A D : pf (A t) :pf ( (λx. A x)) A is redundant if D can synthesize its type. A is not redundant if we only inherit atype (A is not strict in pf (A t)). t is not redundant either way. 28

Bi-Directional Type Checking Annotate each constant occurrence as synthesizing or inheriting a type. Annotations are by no means unique. Result of strictness analysis. Redundant quantifiers are bracketed. andi : [ΠA. ][ΠB. ] pf (A) pf (B) pf (A B) andi : [ΠA. ][ΠB. ] pf (A) pf (B) pf (A B) andel : [ΠA. ]ΠB. pf (A B) pf (A) andel : [ΠA. ][ΠB. ] pf (A B) pf (A) foralle : Πt. ΠA. pf ( (λx. A x)) pf (At) foralle : Πt. [ΠA. ] pf ( (λx. A x)) pf (At) 29

Complex Annotations ore : [ΠA. ][ΠB. ][ΠC. ] pf (A B) (pf (A) pf (C)) (pf (B) pf (C)) pf (C) Generally, no best annotation (depends on usage). Strictness is critical. Hand-crafted annotations based on knowledge about object theory may be best. Automatic annotations for second-order case practical (non-linear proof size compression in LF i [Necula 98]). 30

Avoiding Redundant Unification Maintain invariant that in we have θ. Γ θm. = θn : θa Γ M : A and Γ N : A Then we don t need to unify inherited arguments. Empirical study: 50% improvement in logic programming efficiency of 13 case studies [Michaylov & Pf 93]. No gain for simple types. 31

Summary Strictness, motivated from notational definitions 1 unification with definitions 2 matching and rewriting (with or without definitions) 3 proof compression (with or without definitions) Further empirical evaluation? Refined analysis? 32