• Aucun résultat trouvé

Example: pure λ-calculus

N/A
N/A
Protected

Academic year: 2022

Partager "Example: pure λ-calculus"

Copied!
24
0
0

Texte intégral

(1)

An overview of alphaCaml

Franc¸ois Pottier

September 2005

(2)

Introduction

A specification language

Implementation techniques

Translating specifications

Conclusion

(3)

Motivation

Our programming languages do not support abstract syntax with binders in a satisfactory way.

Hand-coding the operations that deal with lexical scope

(capture-avoiding substitution, etc.) is tedious and error-prone.

How about a more declarative, robust, automated approach?

– cf. Shinwell’s Fresh O’Caml, Cheney’s FreshLib.

(4)

Three facets

Let’s distinguish three facets of the problem:

I a specification language,

I an implementation technique,

I an automated translation of the former to the latter.

In this talk, I emphasize the first aspect.

(5)

Introduction

A specification language

Implementation techniques

Translating specifications

Conclusion

(6)

Prior art

There have been a few proposals to enrich algebraic specification languages with names and abstractions.

An abstraction usually takes the form haie, or ha1, . . . , anie, or, as in Fresh Objective Caml, he1ie2.

Abstraction is always binary: the names (or atoms) a that appear on the left-hand side are bound, and their scope is the expression e that appears on the right-hand side.

(7)

Example: pure λ-calculus

Pure λ-calculus:

M:=a|M M|λa.M is modelled in Fresh Objective Caml as follows:

bindable type var

type term =

| EVar of var

| EApp of term ∗ term

| ELam of hvariterm

(8)

A more delicate example

Let’s add simultaneous definitions:

M::=. . .|leta1=M1and . . . andan=Mn inM

The atoms ai are bound, so they must lie within the abstraction’s left-hand side. The terms Mi are outside the abstraction’s lexical scope, so they must lie outside of the abstraction:

type term =

| ...

| ELet of term list ∗ hvar listiterm

(9)

Another delicate example

Simultaneous recursive definitions pose a similar problem:

M::=. . .|letreca1=M1and . . . andan =Mn inM

The terms Mi are now inside the abstraction’s lexical scope, so they must lie within the abstraction’s right-hand side:

type term =

| ...

| ELetRec ofhvar listi(term list ∗ term)

(10)

The problem

The root of the problem is the assumption that lexical and physical structure should coincide.

(11)

A solution

Within an abstraction, alphaCaml distinguishes three basic

components: binding occurrences of names, expressions that lie within the abstraction’s lexical scope, and expressions that lie outside the scope.

These components are assembled using sums and products, giving rise to a syntactic category of so-called patterns. Abstraction becomes unary and holds a pattern.

t::=unit|t×t|t+t|atom|hui Expression types u::=unit|u×u|u+u|atom|innert|outert Pattern types

(12)

Back to pure λ-calculus

Pure λ-calculus is modelled in alphaCaml as follows:

sort var

type term =

| EVar of atom var

| EApp of term ∗ term

| ELam of hlampi type lamp binds var =

atom var ∗ inner term

(13)

A second look at simultaneous definitions

Simultaneous definitions are modelled without difficulty:

type term =

| ...

| ELet of hletpi

type letp binds var =

binding list ∗ inner term type binding binds var =

atom var ∗ outer term

(14)

More advanced examples

Abstract syntax for patterns in an Objective Caml-like programming language could be declared like this:

type pattern binds var =

| PWildcard

| PVar of atom var

| PRecord of pattern StringMap.t

| PInjection of [ constructor ] ∗ pattern list

| PAnd of pattern ∗ pattern

| POr of pattern ∗ pattern

(15)

Introduction

A specification language

Implementation techniques

Translating specifications

Conclusion

(16)

Three known techniques

1. de Bruijn indices. Require shifting, which is fragile. No freshening.

Generic equality and hashing functions respect α-equivalence.

2. Atoms. Require freshening upon opening abstractions. No shifting. Require custom equality and hashing functions.

3. Pollack mix: free names as atoms and bound names as indices.

Analogous to 2, except generic equality and hashing respect α-equivalence.

alphaCaml follows 2.

(17)

Some more details

Atoms are represented as pairs of an integer and a string. The latter is used only as a hint for display.

Sets of atoms and renamings are encoded as Patricia trees.

Renamings are suspended and composed at abstractions, which allows linear-time term traversals.

Even though the fresh atom generator has state, closed terms can safely be marshalled to disk.

(18)

Introduction

A specification language

Implementation techniques

Translating specifications

Conclusion

(19)

Types

The specification of pure λ-calculus is translated down to Objective Caml as follows. Atoms and abstractions are abstract.

type var = Var.Atom.t

type term =

| EVar of var

| EApp of term ∗ term

| ELam of opaque lamp and lamp =

var ∗ term

(20)

Code

Opening an abstraction automatically freshens its bound atoms.

val open lamp : opaque lamp →lamp val create lamp : lamp →opaque lamp

This enforces Barendregt’s informal convention.

More boilerplate is generated for computing sets of free or bound atoms, applying renamings, helping clients succinctly define

transformations (such as capture-avoiding substitution), etc.

(21)

Introduction

A specification language

Implementation techniques

Translating specifications

Conclusion

(22)

Status

alphaCaml is available. There are very few known users so far.

The distribution comes with two demos:

I a na¨ıve typechecker and evaluator for F

I a na¨ıve evaluator for a calculus of mixins (Hirschowitz et al.) These limited experiments are encouraging.

(23)

Limitations

One must go through open functions to examine abstractions. Deep pattern matching is impossible.

Clients can write meaningless code, such as a function that pretends to collect the bound atoms in an expression.

(24)

Towards alpha-(your-favorite-prover-here)?

How about translating a specification language like alphaCaml’s into theorems (recursion and induction principles) and proofs?

– cf. Pitts, Urban and Tasson, Norrish...

Références

Documents relatifs

[1] addresses two main issues: the links between belowground and aboveground plant traits and the links between plant strategies (as defined by these traits) and the

We define a partition of the set of integers k in the range [1, m−1] prime to m into two or three subsets, where one subset consists of those integers k which are < m/2,

[1] Godement R., Th´ eorie des faisceaux, Hermann, 1973 [2] Iversen B., Cohomology of sheaves, Springer 1986. [3] Kashiwara M., Schapira P., Sheaves on Manifolds, Grundlehren

Asian Early Pleistocene Hominins: Handedness of East Asian Hominin.. Fig.1: Soda-lime glass replica used in the experiment. Fig.2: Documentation of the type, orientation and position

Discretization by means of edge finite elements leads to a system with a new right hand side (Ψ, curl w h ) that belongs to the image of the matrix K, and the curl curl operator

Water mass transformation in the North Atlantic is examined in an eddy-permitting simulation with the OC- CAM ocean general circulation model, forced by realis- tic surface fluxes

Key words: Mulholland inequality, Hilbert-type inequality, weight function, equiv- alent form, the best possible constant factor.. The above inequality includes the best

L’accès aux bas-fonds (repousses d’herbe) et la distribution modérée de concentrés permettent d’obtenir un prélèvement important de lait et les résultats économiques les