Framework to extract Coq terms to -terms Semi-automatic - - PowerPoint PPT Presentation

framework to extract coq terms to terms semi automatic
SMART_READER_LITE
LIVE PREVIEW

Framework to extract Coq terms to -terms Semi-automatic - - PowerPoint PPT Presentation

V ERIFIED E XTRACTION FROM C OQ TO A L AMBDA -C ALCULUS C OQ W ORKSHOP TALK Yannick Forster and Fabian Kunze S AARLAND U NIVERSITY Programming Systems Lab saarland university computer science The Language Encodings Representation


slide-1
SLIDE 1

VERIFIED EXTRACTION FROM COQ

TO A LAMBDA-CALCULUS

COQ WORKSHOP TALK

Yannick Forster and Fabian Kunze

SAARLAND UNIVERSITY Programming Systems Lab

computer science

saarland

university

slide-2
SLIDE 2

The Language Encodings Representation Internalization In Practice

A VERIFIED SELF INTERPRETER IN THE λ-CALCULUS

computer science

saarland

university

Definition Eva := R (λ (λ (λ ((0 (λ none)) (λ (λ (3 none) (λ (((5 0) 2) (λ (((6 1) 2) (λ ((1 (λ none)) (λ (λ none))) (λ (8 3) (((Subst 0) Zero) 1)))) none)) none)))) (λ some (Lam 0))))) Lemma Eva_correct k s : Eva (enc k) (tenc s) ≡ oenc (eva k s). Proof. (∗ including lemmas: 75 lines correctness proof ∗) Qed.

2

slide-3
SLIDE 3

The Language Encodings Representation Internalization In Practice

... AND WITH OUR FRAMEWORK

computer science

saarland

university

Instance term_eva : internalized eva. Proof.

  • internalizeR. revert y0. induction y; intros[]; recStep P; crush.

repeat (destruct _ ; crush). Defined.

3

slide-4
SLIDE 4

The Language Encodings Representation Internalization In Practice

◮ Framework to extract Coq terms to λ-terms ◮ Semi-automatic verification (only briefly mentioned in this

talk)

◮ Development of computability theory in this framework 4

slide-5
SLIDE 5

The Language Encodings Representation Internalization In Practice

SYNTAX AND SEMANTICS OF OUR λ-CALCULUS

computer science

saarland

university

De Bruijn Terms: s, t ::= n | s t | λs (n ∈ N) Reduction: (λs)(λt) ≻ s0

λt

s ≻ s′ st ≻ s′t t ≻ t′ st ≻ st′ ≻∗ denotes the reflexive, transitive closure of ≻. ≡ the equivalence closure.

5 [Plotkin, 1975], [Niehren, 1996], [Dal Lago & Martini, 2008]

slide-6
SLIDE 6

The Language Encodings Representation Internalization In Practice

BOOLEANS AND NATURAL NUMBERS

SCOTT ENCODING:

computer science

saarland

university

true := λ x y.x false := λ x y.y

if b then s else t =

⇒ b s t 0 := λ z s.z Sn := λ z s.s n

match n with O ⇒s | S n’ ⇒t =

⇒ n s(λn′.t)

6 [Curry, Hindley, Seldin, 1972]

slide-7
SLIDE 7

The Language Encodings Representation Internalization In Practice

EXTRACTION

EXAMPLE: ADDITION

computer science

saarland

university

fix plus (n m : N) {struct n} : N:= match n with | 0 ⇒m | S p ⇒S (plus p m) end

S := λ n z s. s n plus := ρ(λ A n m.n m (λp.S (A p m)))

7

slide-8
SLIDE 8

The Language Encodings Representation Internalization In Practice

EXISITING EXTRACTIONS

computer science

saarland

university

  • 1. Write down a Coq term
  • 2. Prove it to be correct (or use dependent type)
  • 3. Extract to programming language

How to know that the extracted term is correct? Trust or prove the extraction mechanism!

8

slide-9
SLIDE 9

The Language Encodings Representation Internalization In Practice

OUR EXTRACTION

computer science

saarland

university

  • 1. Write down a Coq term
  • 2. Prove it to be correct (or use dependent type)
  • 3. Extract to lambda-calculus
  • 4. Use semi-automatic verification to verify the correctness in

Coq? How to know that the extracted term is correct? It’s proven in Coq!

9

slide-10
SLIDE 10

The Language Encodings Representation Internalization In Practice

TYPICAL EXTRACTION PROCESS

computer science

saarland

university

Preliminaries:

  • 1. Register relevant encoding functions
  • 2. Extract all occuring functions

Automated extraction:

  • 1. Generate an inductive representation from a Coq term
  • 2. Eliminate non-computational parts
  • 3. Extract to L-term
  • 4. Generate correctness statement

Verification:

  • 1. Verify the term semi-automatically

10

slide-11
SLIDE 11

The Language Encodings Representation Internalization In Practice

SEEN THIS BEFORE?

computer science

saarland

university

Definition dec (X : Prop) : Type :={X} + {¬ X}. Existing Class dec. Definition decision (X : Prop) (D : dec X) : dec X :=D. Arguments decision X {D}.

11

slide-12
SLIDE 12

The Language Encodings Representation Internalization In Practice

SEEN THIS BEFORE?

computer science

saarland

university

Essentially the same:

Typeclass dec (X : Prop) : Type :=mk_dec { decider (X : Prop) : Type :={X} + {¬ X} }. Definition decision (X : Prop) (D : dec X) : dec X :=decider. Arguments decision X {D}.

12

slide-13
SLIDE 13

The Language Encodings Representation Internalization In Practice

A TYPECLASS FOR ENCODINGS

computer science

saarland

university

Class registered (X : Type) :=mk_registered { enc_f : X → term ; (∗ the encoding function for X ∗) proc_enc : ∀ x, proc (enc_f x) (∗ encodings need to be a procedure ∗) }. Arguments enc_f X {registered} _.

13

slide-14
SLIDE 14

The Language Encodings Representation Internalization In Practice

REGISTRATION OF BOOL AND NAT

computer science

saarland

university

Instance register_bool : registered bool. Proof. register bool_enc. Defined. Instance register_N : registered N. Proof. register N_enc. Defined.

14

slide-15
SLIDE 15

The Language Encodings Representation Internalization In Practice

THE SAME TRICK AGAIN

computer science

saarland

university

Definition enc (X : Type) (H:registered X) : X → term :=enc_f X. Global Arguments enc {X} {H} _ : simpl never. Compute (enc 0, enc false, enc 2). ((λ (λ 1)), (λ (λ 0)), (λ (λ O (λ (λ O (λ (λ 1))))))) : term ∗ term ∗ term

15

slide-16
SLIDE 16

The Language Encodings Representation Internalization In Practice

TEMPLATE COQ

computer science

saarland

university

“Template Coq is a quoting library for Coq. It takes Coq terms and constructs a representation of their syntax tree as a Coq inductive data type.”

16 [Malecha, 2014]

slide-17
SLIDE 17

The Language Encodings Representation Internalization In Practice

TEMPLATE COQ’S REPRESENTATION

computer science

saarland

university

Inductive term : Type := | tRel : N→ term | tVar : ident → term | tMeta : N→ term | tEvar : N→ term | tSort : sort → term | tCast : term → cast_kind → term → term | tProd : name → term (∗∗ the type ∗∗) → term → term | tLambda : name → term (∗∗ the type ∗∗) → term → term | tLetIn : name → term (∗∗ the type ∗∗) → term → term → term | tApp : term → list term → term | tConst : string → term | tInd : inductive → term | tConstruct : inductive → N→ term | tCase : N→ term → term → list term → term | tFix : mfixpoint term → N→ term | tUnknown : string → term.

17

slide-18
SLIDE 18

The Language Encodings Representation Internalization In Practice

INTERMEDIATE REPRESENTATION

computer science

saarland

university

Inductive iTerm : Prop := iApp : iTerm → iTerm → iTerm (∗ application of two terms ∗) | iLam : iTerm → iTerm (∗ fun ∗) | iFix : iTerm → iTerm (∗ fix ∗) | iConst (X:Type) : X → iTerm (∗ not unfolded constants ∗) | iMatch : iTerm → list iTerm → iTerm (∗ matches with all the cases ∗) | iVar : N→ N→ iTerm (∗ variables ∗) | iType : iTerm. (∗ eliminated terms ∗)

18

slide-19
SLIDE 19

The Language Encodings Representation Internalization In Practice

Straightforward/seen in the introduction:

◮ fun ◮ var ◮ app ◮ match ◮ eliminated terms 19

slide-20
SLIDE 20

The Language Encodings Representation Internalization In Practice

FIX

computer science

saarland

university

Use function ρ with (ρ u) t ≻∗ u (ρ u) t

20

slide-21
SLIDE 21

The Language Encodings Representation Internalization In Practice

A TYPECLASS FOR INTERNALIZATION

computer science

saarland

university

Class internalized (X : Type) (x : X) := { internalizer : term ; proc_t : proc internalizer }. Definition int (X : Type) (x : X) (H : internalized x) :=internalizer. Global Arguments int {X} {ty} x {H} : simpl never.

21

slide-22
SLIDE 22

The Language Encodings Representation Internalization In Practice

GENERATING CORRECTNESS STATEMENTS

computer science

saarland

university

Correctness statement for plus: plus n m ≻∗ n + m Correctness statement for f with f : X → Y → Z: f x y ≻∗ f x y Idea: Correctness statement can be generated from the type

22

slide-23
SLIDE 23

The Language Encodings Representation Internalization In Practice

THE TT TYPE

computer science

saarland

university

An inductive representation for types using HOAS:

Inductive TT : Type → Type := TyB t (H : registered t) : TT t | TyElim t : TT t | TyAll t (ttt : TT t) (f : t → Type) (ftt : ∀ x : t, TT (f x)) : TT (∀ (x:t), f x). Arguments TyB _ {_}. Arguments TyAll {_} _ {_} _. Notation "X Y" :=(TyAll X (fun _ ⇒Y)) (right associativity, at level 70).

23

slide-24
SLIDE 24

The Language Encodings Representation Internalization In Practice

EXAMPLE

computer science

saarland

university

TT representation for ∀ x y : N, { x = y } + { x = y } is TyAll (TyB N) (fun x : N⇒ TyAll (TyV N) (fun y : N⇒TyB ( {x = y} + {x = y}) )) : TT (∀ x y : N, {x = y} + {x = y})

24

slide-25
SLIDE 25

The Language Encodings Representation Internalization In Practice

GENERATING CORRECTNESS STATEMENTS

computer science

saarland

university

Generate statements using a function:

Definition internalizesF (p : Lvw.term) t (ty : TT t) (f : t) : Prop. revert p. induction ty as [ t H p | t H p | t ty internalizesHyp R ftt internalizesF’]; simpl in ∗; intros. − exact (p >∗ enc f). − exact (p >∗ I). − exact (∀ (y : t) u, proc u → internalizesHyp y u → internalizesF’ _ (f y) (app p u)). Defined. 25

slide-26
SLIDE 26

The Language Encodings Representation Internalization In Practice

EXEMPLARY CORRECTNESS STATEMENTS

computer science

saarland

university

Correctness statement that t internalizes . . .

◮ . . . a term n : N:

t >∗ enc n

◮ . . . a term X : Type:

t >∗ I

◮ . . . a term f : X → Y:

∀ u (x : X), internalizesF u X _ x → internalizesF (t u) Y _ (f x)

26

slide-27
SLIDE 27

The Language Encodings Representation Internalization In Practice

INTERNALIZEDCLASS

computer science

saarland

university

Class internalizedClass (X : Type) (ty : TT X) (x : X) := { internalizer : term ; proc_t : proc internalizer ; correct_t : internalizesF internalizer ty x }. Definition int (X : Type) (ty : TT X) (x : X) (H : internalizedClass ty x) :=internalizer. Global Arguments int {X} {ty} x {H} : simpl never. 27

slide-28
SLIDE 28

The Language Encodings Representation Internalization In Practice

A FINAL HACK

computer science

saarland

university

Instance term_eva : internalizedClass (TyB NTyB term TyB (option term)) eva.

Better:

Notation "’internalized’ f" := (internalizedClass ltac:(let t :=type of f in let x :=toTT t in exact x) f) (at level 100, only parsing). Instance term_eva : internalized eva.

28

slide-29
SLIDE 29

The Language Encodings Representation Internalization In Practice

COMPUTABILITY THEORY

computer science

saarland

university

Formalization Thesis Framework Natural Numbers 110 60 Equality on terms and N 85 46 Lists 230 113 Substitution and Self Interpretation 209 74 Inverse Encoding of N 37 9 In Total 777 319

29

slide-30
SLIDE 30

The Language Encodings Representation Internalization In Practice

Thanks!

Code: ps.uni-saarland.de/~forster/coq-workshop-16/

30