ect : Contexts in reFL A Theorem Proving Meta-Language Jim Grundy - - PowerPoint PPT Presentation

ect contexts in refl a theorem proving meta language
SMART_READER_LITE
LIVE PREVIEW

ect : Contexts in reFL A Theorem Proving Meta-Language Jim Grundy - - PowerPoint PPT Presentation

ect : Contexts in reFL A Theorem Proving Meta-Language Jim Grundy Intel Corporation, Strategic CAD Labs Tom Melham Oxford University, Computing Laboratory John OLeary Intel Corporation, Strategic CAD Labs Sava Krsti c Intel


slide-1
SLIDE 1

Contexts in reFL ect: A Theorem Proving Meta-Language

Jim Grundy Intel Corporation, Strategic CAD Labs Tom Melham Oxford University, Computing Laboratory John O’Leary Intel Corporation, Strategic CAD Labs Sava Krsti´ c Intel Corporation, Strategic CAD Labs October 2005

slide-2
SLIDE 2

Language Overview

reFL ect is

◮ 2nd version of FL with reflection ◮ a dialect of ML used at Intel for applications including

◮ correctness preserving design transformations ◮ interactive theorem proving of design properties

slide-3
SLIDE 3

Language Overview

reFL ect is

◮ 2nd version of FL with reflection ◮ a dialect of ML used at Intel for applications including

◮ correctness preserving design transformations ◮ interactive theorem proving of design properties

◮ reFL

ect is typed λ-calculus +

◮ A quotation mechanism, like:

  • |

| |this| | |

  • ◮ An anti-quotation mechanism, like:

ˆ ˆ ˆthis

slide-4
SLIDE 4

Language Overview

reFL ect is

◮ 2nd version of FL with reflection ◮ a dialect of ML used at Intel for applications including

◮ correctness preserving design transformations ◮ interactive theorem proving of design properties

◮ reFL

ect is typed λ-calculus +

◮ A quotation mechanism, like:

  • |

| |this| | |

  • ◮ An anti-quotation mechanism, like:

ˆ ˆ ˆthis

◮ Quoted expressions denote values of type term

slide-5
SLIDE 5

Language Overview

reFL ect is

◮ 2nd version of FL with reflection ◮ a dialect of ML used at Intel for applications including

◮ correctness preserving design transformations ◮ interactive theorem proving of design properties

◮ reFL

ect is typed λ-calculus +

◮ A quotation mechanism, like:

  • |

| |this| | |

  • ◮ An anti-quotation mechanism, like:

ˆ ˆ ˆthis

◮ Quoted expressions denote values of type term

◮ Values of type term are ASTs of well-typed expressions

slide-6
SLIDE 6

Language Overview

reFL ect is

◮ 2nd version of FL with reflection ◮ a dialect of ML used at Intel for applications including

◮ correctness preserving design transformations ◮ interactive theorem proving of design properties

◮ reFL

ect is typed λ-calculus +

◮ A quotation mechanism, like:

  • |

| |this| | |

  • ◮ An anti-quotation mechanism, like:

ˆ ˆ ˆthis

◮ Quoted expressions denote values of type term

◮ Values of type term are ASTs of well-typed expressions ◮ 1 + 2 and 2 + 1 are equal, they both describe the number 3

slide-7
SLIDE 7

Language Overview

reFL ect is

◮ 2nd version of FL with reflection ◮ a dialect of ML used at Intel for applications including

◮ correctness preserving design transformations ◮ interactive theorem proving of design properties

◮ reFL

ect is typed λ-calculus +

◮ A quotation mechanism, like:

  • |

| |this| | |

  • ◮ An anti-quotation mechanism, like:

ˆ ˆ ˆthis

◮ Quoted expressions denote values of type term

◮ Values of type term are ASTs of well-typed expressions ◮ 1 + 2 and 2 + 1 are equal, they both describe the number 3 ◮

  • |

| |1 + 2| | |

  • and
  • |

| |2 + 1| | |

  • are not equal, they are different ASTs
slide-8
SLIDE 8

Language Overview

reFL ect is

◮ 2nd version of FL with reflection ◮ a dialect of ML used at Intel for applications including

◮ correctness preserving design transformations ◮ interactive theorem proving of design properties

◮ reFL

ect is typed λ-calculus +

◮ A quotation mechanism, like:

  • |

| |this| | |

  • ◮ An anti-quotation mechanism, like:

ˆ ˆ ˆthis

◮ Quoted expressions denote values of type term

◮ Values of type term are ASTs of well-typed expressions ◮ 1 + 2 and 2 + 1 are equal, they both describe the number 3 ◮

  • |

| |1 + 2| | |

  • and
  • |

| |2 + 1| | |

  • are not equal, they are different ASTs

  • |

| |ˆ ˆ ˆ

  • |

| |1| | |

  • + 2|

| |

  • and
  • |

| |1 + ˆ ˆ ˆ

  • |

| |2| | |

  • |

| |

  • are equal, they describe
  • |

| |1 + 2| | |

slide-9
SLIDE 9

Example

  • letrec

comm

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • =
  • |

| |ˆ ˆ ˆ(comm y) + ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |ˆ ˆ ˆf ˆ ˆ ˆx| | |

  • =
  • |

| |ˆ ˆ ˆ(comm f) ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆb| | |

  • =
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆ(comm b)| | |

  • | ...

| comm x = x;

slide-10
SLIDE 10

Example

  • letrec

comm

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • =
  • |

| |ˆ ˆ ˆ(comm y) + ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |ˆ ˆ ˆf ˆ ˆ ˆx| | |

  • =
  • |

| |ˆ ˆ ˆ(comm f) ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆb| | |

  • =
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆ(comm b)| | |

  • | ...

| comm x = x; comm: term→term

slide-11
SLIDE 11

Example

  • letrec

comm

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • =
  • |

| |ˆ ˆ ˆ(comm y) + ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |ˆ ˆ ˆf ˆ ˆ ˆx| | |

  • =
  • |

| |ˆ ˆ ˆ(comm f) ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆb| | |

  • =
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆ(comm b)| | |

  • | ...

| comm x = x; comm: term→term

  • comm
  • |

| |y = m*x + c| | |

  • ;
slide-12
SLIDE 12

Example

  • letrec

comm

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • =
  • |

| |ˆ ˆ ˆ(comm y) + ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |ˆ ˆ ˆf ˆ ˆ ˆx| | |

  • =
  • |

| |ˆ ˆ ˆ(comm f) ˆ ˆ ˆ(comm x)| | |

  • | comm
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆb| | |

  • =
  • |

| |λˆ ˆ ˆp. ˆ ˆ ˆ(comm b)| | |

  • | ...

| comm x = x; comm: term→term

  • comm
  • |

| |y = m*x + c| | |

  • ;
  • |

| |y = c + m*x| | |

  • : term
slide-13
SLIDE 13

The Higher Order Logic of reFL ect

The HOL Logic                    λ − calculus + constants: =, true, false + axioms, inference rules + definitions

slide-14
SLIDE 14

The Higher Order Logic of reFL ect

The HOL Logic                    λ − calculus + constants: =, true, false + axioms, inference rules + definitions The reFL ect Logic                    reFL ect + constants: =, true, false + axioms, inference rules + definitions

slide-15
SLIDE 15

The Higher Order Logic of reFL ect

The HOL Logic                    λ − calculus + constants: =, true, false + axioms, inference rules + definitions The reFL ect Logic                    reFL ect + constants: =, true, false + axioms, inference rules + definitions

Common to Both

◮ Not everything that may be discussed may be executed

◮ let ∀ f = f = (λx. true)

◮ Reductions in the language are valid inferences in the logic

◮ If Λ → true, then ⊢ Λ

slide-16
SLIDE 16

Levels and Their Relationships

◮ A deep embedding of LTL in HOL:

0: ML 1: HOL logic, deeply embedded in ML 2: LTL logic, deeply embedded in HOL

Use the prover (level 0 program) to reason about what HOL functions (level 1) do to LTL expressions (level 2)

slide-17
SLIDE 17

Levels and Their Relationships

◮ A deep embedding of LTL in HOL:

0: ML 1: HOL logic, deeply embedded in ML 2: LTL logic, deeply embedded in HOL

Use the prover (level 0 program) to reason about what HOL functions (level 1) do to LTL expressions (level 2)

◮ A shallow embedding of LTL in reFL

ect

0: reFL ect 1: quoted reFL ect expressions 2: twice quoted reFL ect expressions

Use the prover (level 0 program) to reason about what reFL ect functions (level 1) do to reFL ect expressions (level 2)

slide-18
SLIDE 18

Levels are Separate

We want the same relationship between level n and n + 1 reFL ect expressions as between ML and HOL (or between HOL and LTL, the deeply embedded language)

◮ Level n expressions can manipulate level n + 1 expressions

slide-19
SLIDE 19

Levels are Separate

We want the same relationship between level n and n + 1 reFL ect expressions as between ML and HOL (or between HOL and LTL, the deeply embedded language)

◮ Level n expressions can manipulate level n + 1 expressions ◮ Level n expressions don’t interpret those above level n + 1

(We don’t implement LTL reasoning directly in ML.)

slide-20
SLIDE 20

Levels are Separate

We want the same relationship between level n and n + 1 reFL ect expressions as between ML and HOL (or between HOL and LTL, the deeply embedded language)

◮ Level n expressions can manipulate level n + 1 expressions ◮ Level n expressions don’t interpret those above level n + 1

(We don’t implement LTL reasoning directly in ML.)

◮ They do not, usually, become level n + 1 expressions

(ML does not become HOL)

slide-21
SLIDE 21

Levels are Separate

We want the same relationship between level n and n + 1 reFL ect expressions as between ML and HOL (or between HOL and LTL, the deeply embedded language)

◮ Level n expressions can manipulate level n + 1 expressions ◮ Level n expressions don’t interpret those above level n + 1

(We don’t implement LTL reasoning directly in ML.)

◮ They do not, usually, become level n + 1 expressions

(ML does not become HOL)

◮ Level n + 1 expressions do not, usually,

become level n expressions (HOL does not become ML)

slide-22
SLIDE 22

Levels are Separate

We want the same relationship between level n and n + 1 reFL ect expressions as between ML and HOL (or between HOL and LTL, the deeply embedded language)

◮ Level n expressions can manipulate level n + 1 expressions ◮ Level n expressions don’t interpret those above level n + 1

(We don’t implement LTL reasoning directly in ML.)

◮ They do not, usually, become level n + 1 expressions

(ML does not become HOL)

◮ Level n + 1 expressions do not, usually,

become level n expressions (HOL does not become ML)

◮ Variables are bound within a level, not across levels

◮ Want

  • |

| |x| | |

  • different to
  • |

| |1| | |

  • ◮ Want usual quantifier rules

◮ Do not want this

☛ ⊢ ¬(

  • |

| |x| | |

  • =
  • |

| |1| | |

  • )

⊢ ∀x. ¬(

  • |

| |x| | |

  • =
  • |

| |1| | |

  • )

⊢ ¬(

  • |

| |1| | |

  • =
  • |

| |1| | |

  • )

[∀E] [∀I]

slide-23
SLIDE 23

reFL ect Abstract Syntax

Λ, M, N : : = k – Constant | v – Variable | λΛ. M – Abstraction | λΛ. M | N – Alternation | ΛM – Application |

  • |

| |Λ| | |

  • – Quotation

| ˆ ˆ ˆΛ – Anti-quotation

Note:

◮ Arbitrary expressions may be patterns ◮ Lambda abstractions may have match alternatives ◮ Omitting whole story about type annotations checking

slide-24
SLIDE 24

reFL ect Abstract Syntax

Λ, M, N : : = k – Constant | v – Variable | λΛ. M – Abstraction | λΛ. M | N – Alternation | ΛM – Application |

  • |

| |Λ| | |

  • – Quotation

| ˆ ˆ ˆΛ – Anti-quotation On the path from the root of an AST to some subexpression:

◮ the level of the subexpression is the number of quotations on

the path − the number of antiquotes

◮ an expression is well formed if no subexpression has

negative level

slide-25
SLIDE 25

We Don’t Do This

We could make values of term appear as if defined as follows: lettype term = VAR string // v | CONST val // k | APPLY term term // ΛM | ABS term term // λΛ. M | ALT term term term // λΛ. M | N | QUOTE term //

  • |

| |Λ| | |

  • | ANTIQ term

// ˆ ˆ ˆΛ

slide-26
SLIDE 26

We Don’t Do This

We could make values of term appear as if defined as follows: lettype term = VAR string // v | CONST val // k | APPLY term term // ΛM | ABS term term // λΛ. M | ALT term term term // λΛ. M | N | QUOTE term //

  • |

| |Λ| | |

  • | ANTIQ term

// ˆ ˆ ˆΛ Consider how to find the free variables in a term

slide-27
SLIDE 27

We Don’t Do This

We could make values of term appear as if defined as follows: lettype term = VAR string // v | CONST val // k | APPLY term term // ΛM | ABS term term // λΛ. M | ALT term term term // λΛ. M | N | QUOTE term //

  • |

| |Λ| | |

  • | ANTIQ term

// ˆ ˆ ˆΛ Consider how to find the free variables in a term

◮ just those at level 0 ◮ variables at higher level are somebody else’s problem

slide-28
SLIDE 28

Example: What We Don’t Do

let frees trm = letrec f 0 (VAR nam) = {VAR nam} | f (n+1) (VAR nam) = {} | f n (CONST idn) = {} | f n (APP fun arg) = f n fun ∪ f n arg | f 0 (ABS pat bod) = f 0 bod - f 0 pat | f (n+1) (ABS pat bod) = f (n+1) pat ∪ f (n+1) bod ... | f n (QUOTE quo) = f (n+1) quo | f (n+1) (ANTIQ ant) = f n ant in f 0 trm;

slide-29
SLIDE 29

Why Don’t We Do It?

◮ The definition of frees was overly complex

◮ It had to be careful to remember what to look at and what not to ◮ It traversed regions it didn’t need to look at

slide-30
SLIDE 30

Why Don’t We Do It?

◮ The definition of frees was overly complex

◮ It had to be careful to remember what to look at and what not to ◮ It traversed regions it didn’t need to look at

◮ QUOTE and ANTIQ move expressions up and down levels

without restriction

slide-31
SLIDE 31

Why Don’t We Do It?

◮ The definition of frees was overly complex

◮ It had to be careful to remember what to look at and what not to ◮ It traversed regions it didn’t need to look at

◮ QUOTE and ANTIQ move expressions up and down levels

without restriction

◮ Programs can, and must, inspect arbitrarily higher levels

slide-32
SLIDE 32

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ( + )
  • |

| | + 1| | |

ˆ ˆx + )

slide-33
SLIDE 33

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ( + )
  • |

| | + 1| | |

ˆ ˆx + )

slide-34
SLIDE 34

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )
  • |

| | + 1| | |

ˆ ˆx + )

slide-35
SLIDE 35

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )

  • |

| | + 1| | |

ˆ ˆx + )

slide-36
SLIDE 36

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )

  • |

| | + 1| | |

  • ✘ (ˆ

ˆ ˆx + )

slide-37
SLIDE 37

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )

  • |

| | + 1| | |

  • ✘ (ˆ

ˆ ˆx + ) We assume the usual hole filling operation on contexts

slide-38
SLIDE 38

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )

  • |

| | + 1| | |

  • ✘ (ˆ

ˆ ˆx + ) We assume the usual hole filling operation on contexts ( + )[2,1] is

slide-39
SLIDE 39

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )

  • |

| | + 1| | |

  • ✘ (ˆ

ˆ ˆx + ) We assume the usual hole filling operation on contexts ( + )[2,1] is 2 + 1

slide-40
SLIDE 40

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )

  • |

| | + 1| | |

  • ✘ (ˆ

ˆ ˆx + ) We assume the usual hole filling operation on contexts ( + )[2,1] is 2 + 1

  • |

| |ˆ ˆ ˆ + 1| | |

  • [
  • |

| |2| | |

  • ]

is

slide-41
SLIDE 41

What We Do Instead: Contexts

Λ, M, N : : = . . . – as in terms | – hole A context is well formed only if:

◮ all holes are at level 0 ◮ no portion of the context has negative level

  • |

| |ˆ ˆ ˆ + 1| | |

  • ✔ ( + )

  • |

| | + 1| | |

  • ✘ (ˆ

ˆ ˆx + ) We assume the usual hole filling operation on contexts ( + )[2,1] is 2 + 1

  • |

| |ˆ ˆ ˆ + 1| | |

  • [
  • |

| |2| | |

  • ]

is

  • |

| |ˆ ˆ ˆ

  • |

| |2| | |

  • + 1|

| |

slide-42
SLIDE 42

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

slide-43
SLIDE 43

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

slide-44
SLIDE 44

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • ( + )

[x, y]

slide-45
SLIDE 45

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • ( + )

[x, y]

  • |

| |x + y| | |

slide-46
SLIDE 46

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • ( + )

[x, y]

  • |

| |x + y| | |

  • (x + y)

[]

slide-47
SLIDE 47

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • ( + )

[x, y]

  • |

| |x + y| | |

  • (x + y)

[]

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆ

  • |

| |y| | |

  • |

| |

slide-48
SLIDE 48

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • ( + )

[x, y]

  • |

| |x + y| | |

  • (x + y)

[]

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆ

  • |

| |y| | |

  • |

| |

  • ( + )

[x,

  • |

| |y| | |

  • ]
slide-49
SLIDE 49

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • ( + )

[x, y]

  • |

| |x + y| | |

  • (x + y)

[]

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆ

  • |

| |y| | |

  • |

| |

  • ( + )

[x,

  • |

| |y| | |

  • ]
  • |

| |ˆ ˆ ˆf

  • |

| |ˆ ˆ ˆˆ ˆ ˆx + ˆ ˆ ˆˆ ˆ ˆy| | |

  • |

| |

slide-50
SLIDE 50

The Context Property

All well-formed expressions of the form

  • |

| |Λ| | |

  • have a unique factorization into:

◮ a well-formed context C ◮ a list of well-formed expressions M1, . . . Mn

such that

  • |

| |C[ˆ ˆ ˆM1, . . .ˆ ˆ ˆMn]| | |

  • is
  • |

| |Λ| | |

  • Example

Expression Factors

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • ( + )

[x, y]

  • |

| |x + y| | |

  • (x + y)

[]

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆ

  • |

| |y| | |

  • |

| |

  • ( + )

[x,

  • |

| |y| | |

  • ]
  • |

| |ˆ ˆ ˆf

  • |

| |ˆ ˆ ˆˆ ˆ ˆx + ˆ ˆ ˆˆ ˆ ˆy| | |

  • |

| |

  • (
  • |

| |ˆ ˆ ˆ + ˆ ˆ ˆ | | |

  • )

[f, x, y]

slide-51
SLIDE 51

A Context Centric Term View

lettype term = VAR string | CONST val // v | k | APPLY term term // Λ M | ABS term term // λΛ. M | ALT term term term // λΛ. M | QUOTE context (term list) //

  • |

| |C[ˆ ˆ ˆΛ1, . . .ˆ ˆ ˆΛn]| | |

  • ◮ No term ever changes level with these constructions
slide-52
SLIDE 52

A Context Centric Term View

lettype term = VAR string | CONST val // v | k | APPLY term term // Λ M | ABS term term // λΛ. M | ALT term term term // λΛ. M | QUOTE context (term list) //

  • |

| |C[ˆ ˆ ˆΛ1, . . .ˆ ˆ ˆΛn]| | |

  • ◮ No term ever changes level with these constructions

◮ From level n I can construct any level n + 1 expression I want

slide-53
SLIDE 53

A Context Centric Term View

lettype term = VAR string | CONST val // v | k | APPLY term term // Λ M | ABS term term // λΛ. M | ALT term term term // λΛ. M | QUOTE context (term list) //

  • |

| |C[ˆ ˆ ˆΛ1, . . .ˆ ˆ ˆΛn]| | |

  • ◮ No term ever changes level with these constructions

◮ From level n I can construct any level n + 1 expression I want ◮ All I can do with expressions above n + 1 is access the n + 1

subexpressions

slide-54
SLIDE 54

Free Variables Revisited

letrec frees (VAR nam) = {VAR nam} | frees (CONST idn) = {} | frees (APP fun arg) = frees fun ∪ frees arg | frees (ABS pat bod) = frees bod - frees pat | frees (ALT pat bod alt) = (frees bod - frees pat) ∪ (frees alt) | frees (QUOTE ctx tms) = fold (∪) {} (map frees tms);

slide-55
SLIDE 55

Free Variables Revisited

letrec frees (VAR nam) = {VAR nam} | frees (CONST idn) = {} | frees (APP fun arg) = frees fun ∪ frees arg | frees (ABS pat bod) = frees bod - frees pat | frees (ALT pat bod alt) = (frees bod - frees pat) ∪ (frees alt) | frees (QUOTE ctx tms) = fold (∪) {} (map frees tms); Contexts hide what you don’t to see behind an SEP field.

◮ no need to for the . . . to fit it now

slide-56
SLIDE 56

Free Variables Revisited

letrec frees (VAR nam) = {VAR nam} | frees (CONST idn) = {} | frees

  • |

| |ˆ ˆ ˆfun ˆ ˆ ˆarg| | |

  • =

frees fun ∪ frees arg | frees

  • |

| |λˆ ˆ ˆabs. ˆ ˆ ˆbod| | |

  • =

frees bod - frees pat | frees

  • |

| |λˆ ˆ ˆpat. ˆ ˆ ˆbod | ˆ ˆ ˆalt| | |

  • =

(frees bod - frees pat) ∪ (frees alt) | frees (QUOTE ctx tms) = fold (∪) {} (map frees tms); Contexts hide what you don’t to see behind an SEP field.

◮ no need to for the . . . to fit it now

slide-57
SLIDE 57

Implementing reFL ect

Consider how to write an evaluator for terms in reFL ect. eval: term → term

◮ Regular language features ‘easy’, let’s assume done

  • eval
  • |

| |(λ[x,y]. x + y) [1,2]| | |

  • ;
slide-58
SLIDE 58

Implementing reFL ect

Consider how to write an evaluator for terms in reFL ect. eval: term → term

◮ Regular language features ‘easy’, let’s assume done

  • eval
  • |

| |(λ[x,y]. x + y) [1,2]| | |

  • ;
  • |

| |3| | |

  • : term
slide-59
SLIDE 59

Implementing reFL ect

Consider how to write an evaluator for terms in reFL ect. eval: term → term

◮ Regular language features ‘easy’, let’s assume done

  • eval
  • |

| |(λ[x,y]. x + y) [1,2]| | |

  • ;
  • |

| |3| | |

  • : term

◮ How do we do anti-quote based term construction?

  • eval
  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • ;
slide-60
SLIDE 60

Implementing reFL ect

Consider how to write an evaluator for terms in reFL ect. eval: term → term

◮ Regular language features ‘easy’, let’s assume done

  • eval
  • |

| |(λ[x,y]. x + y) [1,2]| | |

  • ;
  • |

| |3| | |

  • : term

◮ How do we do anti-quote based term construction?

  • eval
  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • ;
  • |

| |

  • |

| |1 + 3| | |

  • |

| |

  • : term
slide-61
SLIDE 61

Implementing reFL ect

Consider how to write an evaluator for terms in reFL ect. eval: term → term

◮ Regular language features ‘easy’, let’s assume done

  • eval
  • |

| |(λ[x,y]. x + y) [1,2]| | |

  • ;
  • |

| |3| | |

  • : term

◮ How do we do anti-quote based term construction?

  • eval
  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • ;
  • |

| |

  • |

| |1 + 3| | |

  • |

| |

  • : term
slide-62
SLIDE 62

Implementing reFL ect

Consider how to write an evaluator for terms in reFL ect. eval: term → term

◮ Regular language features ‘easy’, let’s assume done

  • eval
  • |

| |(λ[x,y]. x + y) [1,2]| | |

  • ;
  • |

| |3| | |

  • : term

◮ How do we do anti-quote based term construction?

  • eval
  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • ;
  • |

| |

  • |

| |1 + 3| | |

  • |

| |

  • : term

◮ How do we do anti-quote based term destruction?

  • eval
  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • ;
slide-63
SLIDE 63

Implementing reFL ect

Consider how to write an evaluator for terms in reFL ect. eval: term → term

◮ Regular language features ‘easy’, let’s assume done

  • eval
  • |

| |(λ[x,y]. x + y) [1,2]| | |

  • ;
  • |

| |3| | |

  • : term

◮ How do we do anti-quote based term construction?

  • eval
  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • ;
  • |

| |

  • |

| |1 + 3| | |

  • |

| |

  • : term

◮ How do we do anti-quote based term destruction?

  • eval
  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • ;
  • |

| |

  • |

| |1| | |

  • |

| |

  • : term
slide-64
SLIDE 64

Filling Context Holes

We require the following primitive function, to implement eval: fill: context → term list → term This is a version of the primitive context hole filling operation

  • c;

( + ): context

  • fill c [
  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ];
slide-65
SLIDE 65

Filling Context Holes

We require the following primitive function, to implement eval: fill: context → term list → term This is a version of the primitive context hole filling operation

  • c;

( + ): context

  • fill c [
  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ];
  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • : term
slide-66
SLIDE 66

Filling Context Holes

We require the following primitive function, to implement eval: fill: context → term list → term This is a version of the primitive context hole filling operation

  • c;

( + ): context

  • fill c [
  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ];
  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • : term

fill is similar to QUOTE: but removes quotes, doesn’t add anti-quote to balance levels

  • QUOTE c [
  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ];
slide-67
SLIDE 67

Filling Context Holes

We require the following primitive function, to implement eval: fill: context → term list → term This is a version of the primitive context hole filling operation

  • c;

( + ): context

  • fill c [
  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ];
  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • : term

fill is similar to QUOTE: but removes quotes, doesn’t add anti-quote to balance levels

  • QUOTE c [
  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ];
  • |

| |

  • |

| |ˆ ˆ ˆ

  • |

| |1| | |

  • + ˆ

ˆ ˆ

  • |

| |2| | |

  • |

| |

  • |

| |

  • :term
slide-68
SLIDE 68

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • =
  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-69
SLIDE 69

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • = eval (QUOTE (

+ ) [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) (map eval [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) [eval

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • , eval
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

= fill ( + ) [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

=

  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-70
SLIDE 70

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • = eval (QUOTE (

+ ) [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) (map eval [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) [eval

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • , eval
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

= fill ( + ) [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

=

  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-71
SLIDE 71

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • = eval (QUOTE (

+ ) [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) (map eval [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) [eval

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • , eval
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

= fill ( + ) [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

=

  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-72
SLIDE 72

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • = eval (QUOTE (

+ ) [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) (map eval [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) [eval

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • , eval
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

= fill ( + ) [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

=

  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-73
SLIDE 73

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • = eval (QUOTE (

+ ) [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) (map eval [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) [eval

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • , eval
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

= fill ( + ) [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

=

  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-74
SLIDE 74

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • = eval (QUOTE (

+ ) [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) (map eval [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) [eval

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • , eval
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

= fill ( + ) [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

=

  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-75
SLIDE 75

Implementing Anti-quote Based Term Construction

letrec eval (QUOTE ctx tms) = fill c (map eval tms) ...; eval

  • |

| |

  • |

| |ˆ ˆ ˆ(fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )) + ˆ

ˆ ˆ

  • |

| |3| | |

  • |

| |

  • |

| |

  • = eval (QUOTE (

+ ) [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) (map eval [

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ])

= fill ( + ) [eval

  • |

| |fst (

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • )|

| |

  • , eval
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

= fill ( + ) [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |3| | |

  • |

| |

  • ]

=

  • |

| |

  • |

| |1 + 3| | |

  • |

| |

slide-76
SLIDE 76

Pattern Matching Contexts

We require the following primitive function, to implement eval: match: context → term → term list For any context c, match c inverts fill c

  • c;

( + ): context

  • match c
  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ;
slide-77
SLIDE 77

Pattern Matching Contexts

We require the following primitive function, to implement eval: match: context → term → term list For any context c, match c inverts fill c

  • c;

( + ): context

  • match c
  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ;

[

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ]: term list
  • match c
  • |

| |

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • |

| |

  • ;
slide-78
SLIDE 78

Pattern Matching Contexts

We require the following primitive function, to implement eval: match: context → term → term list For any context c, match c inverts fill c

  • c;

( + ): context

  • match c
  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ;

[

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ]: term list
  • match c
  • |

| |

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • |

| |

  • ;

[

  • |

| |

  • |

| |ˆ ˆ ˆx| | |

  • |

| |

  • ,
  • |

| |

  • |

| |ˆ ˆ ˆy| | |

  • |

| |

  • ]: term list
  • match c
  • |

| |

  • |

| |1 - 2| | |

  • |

| |

  • ;
slide-79
SLIDE 79

Pattern Matching Contexts

We require the following primitive function, to implement eval: match: context → term → term list For any context c, match c inverts fill c

  • c;

( + ): context

  • match c
  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ;

[

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ]: term list
  • match c
  • |

| |

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • |

| |

  • ;

[

  • |

| |

  • |

| |ˆ ˆ ˆx| | |

  • |

| |

  • ,
  • |

| |

  • |

| |ˆ ˆ ˆy| | |

  • |

| |

  • ]: term list
  • match c
  • |

| |

  • |

| |1 - 2| | |

  • |

| |

  • ;

error: no match

slide-80
SLIDE 80

Auxiliary Function for Term Destruction

We need an auxiliary function to transform a list of quotes to a quoted list

  • pull [
  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ,
  • |

| |3| | |

  • ];
  • |

| |[1,2,3]| | |

  • : term
slide-81
SLIDE 81

Auxiliary Function for Term Destruction

We need an auxiliary function to transform a list of quotes to a quoted list letrec pull [] =

  • |

| |[]| | |

  • | pull (h:t) =
  • |

| |ˆ ˆ ˆh:ˆ ˆ ˆ(pull t)| | |

  • ;
  • pull [
  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ,
  • |

| |3| | |

  • ];
  • |

| |[1,2,3]| | |

  • : term
slide-82
SLIDE 82

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-83
SLIDE 83

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-84
SLIDE 84

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-85
SLIDE 85

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-86
SLIDE 86

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-87
SLIDE 87

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-88
SLIDE 88

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-89
SLIDE 89

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-90
SLIDE 90

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-91
SLIDE 91

Implementing Anti-Quote based Term Destruction

letrec eval

  • |

| |(λˆ ˆ ˆ(QUOTE ctx pts). ˆ ˆ ˆbdy) ˆ ˆ ˆval| | |

  • =

eval

  • |

| |(λˆ ˆ ˆ(pull pts). ˆ ˆ ˆbdy) ˆ ˆ ˆ(pull (match ctx val))| | |

  • ...;

eval

  • |

| |(λ

  • |

| |ˆ ˆ ˆx + ˆ ˆ ˆy| | |

  • . x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(QUOTE ( + ) [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)
  • |

| |1 + 2| | |

  • |

| |

  • = eval
  • |

| |(λˆ ˆ ˆ(pull [

  • |

| |x| | |

  • ,
  • |

| |y| | |

  • ]). x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x)

ˆ ˆ ˆ(pull (match ( + )

  • |

| |

  • |

| |1 + 2| | |

  • |

| |

  • ))|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x ˆ

ˆ ˆ(pull [

  • |

| |

  • |

| |1| | |

  • |

| |

  • ,
  • |

| |

  • |

| |2| | |

  • |

| |

  • ])|

| |

  • = eval
  • |

| |(λˆ ˆ ˆ

  • |

| |[x, y]| | |

  • . x) ˆ

ˆ ˆ

  • |

| |[

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • |

| |

  • = eval
  • |

| |(λ[x, y]. x) [

  • |

| |1| | |

  • ,
  • |

| |2| | |

  • ]|

| |

  • =
  • |

| |

  • |

| |1| | |

  • |

| |

slide-92
SLIDE 92

The End

Conclusions

◮ quote/anti-quote are a convenient way to manipulate terms ◮ most common manipulations preserve the level of a term ◮ context term view makes level preserving manipulation easy ◮ implementation is straightforward

slide-93
SLIDE 93

The End

Conclusions

◮ quote/anti-quote are a convenient way to manipulate terms ◮ most common manipulations preserve the level of a term ◮ context term view makes level preserving manipulation easy ◮ implementation is straightforward

Interesting Things I Didn’t Mention

◮ The type system and run-time type checking ◮ Manipulations that don’t preserve level: true reflection

slide-94
SLIDE 94

The End

Conclusions

◮ quote/anti-quote are a convenient way to manipulate terms ◮ most common manipulations preserve the level of a term ◮ context term view makes level preserving manipulation easy ◮ implementation is straightforward

Interesting Things I Didn’t Mention

◮ The type system and run-time type checking ◮ Manipulations that don’t preserve level: true reflection

Ideas About The Future

◮ More advanced types to eliminate run-time type checking ◮ Restrictions on reflection to ensure soundness