Full reduction at full throttle Extension to the Calculus of - - PowerPoint PPT Presentation

full reduction at full throttle
SMART_READER_LITE
LIVE PREVIEW

Full reduction at full throttle Extension to the Calculus of - - PowerPoint PPT Presentation

Full reduction at full throttle Extension to the Calculus of Inductive Constructions Gerco van Heerdt December 19, 2014 1 / 18 Symbolic CIC Term t , T , P ::= x | t 1 t 2 | v | C i ( t ) | case P t of ( C i ( x i ) t i )


slide-1
SLIDE 1

Full reduction at full throttle

Extension to the Calculus of Inductive Constructions Gerco van Heerdt December 19, 2014

1 / 18

slide-2
SLIDE 2

Symbolic CIC

Term ∋ t, T, P ::= x | t1 t2 | v | Ci( t) | casePt of (Ci( xi) → ti)i∈I | fixm(f : T := t) Val ∋ v ::= λx.t | [a v] | Ci( v) Atom ∋ a ::= ˜ x | s | Πx : T1.T2 | casePa of (Ci( xi) → ti)i∈I | fixm(f : T := t)

2 / 18

slide-3
SLIDE 3

Additional reduction rules

casePCi( v) of (Ci( xi) → ti)i∈I →ι ti{ xi ← v} caseP[a] of (Ci( xi) → ti)i∈I →ι [casePa of (Ci( xi) → ti)i∈I] fixm(f : T := t) v1 . . . vm−1 Ci( v) →ι t{f ← fixm(f : T := t)} v1 . . . vm−1 Ci( v) fixm(f : T := t) v1 . . . vm−1 [a] →ι [fixm(f : T := t) v1 . . . vm−1 a]

3 / 18

slide-4
SLIDE 4

Readback for pattern matching and fixpoints

RA(casePa of (Ci( xi) → ti)i∈I) = casePRA(a) of (Ci( xi) → N (f (Ci(# » [˜ xi]))))i∈I where f = λx.casePx of (Ci( xi) → ti)i∈I RA(fixm(f : T := t)) = fixm(f : T := N ((λf .t)[˜ f ]))

4 / 18

slide-5
SLIDE 5

Extended value module

module type Values = module type type head = | ... | Construct of int * t array type atom = | Var of var | Sort of sort | Prod of t * t | Match of annot * t * t * (t -> t) | Fix of (t -> t) * t * int ... val mkConstruct : int -> t array

  • > t

end

5 / 18

slide-6
SLIDE 6

Compilation scheme

Sorts, products, and constructors

sB = mkAccu (Sort s) Πx : T.UB = mkAccu (Prod(TB, λx.UB)) Ci( t)B = mkConstruct i [| tB|]

6 / 18

slide-7
SLIDE 7

Compilation scheme

Pattern matching

casePt of (Ci( xi) → ti)i∈IB = let rec case c = match c with | C1( x1)B∪{

x1} ->

t1B∪{

x1}

| ... | Cn( xn)B∪{

xn} ->

tnB∪{

xn}

| _ -> mkAccu (Match (˜ I ,c,PB ,case )) in case tB

7 / 18

slide-8
SLIDE 8

Compilation scheme

Fixpoints

fixm(f : T := t)B = let fnorm f = tB∪{f } in let rec f = mkLam (fun x1 -> ... -> mkLam(fun xm -> if is_accu xm then mkAccu (Fix (fnorm ,TB ,m)) x1 ... xm else fnorm f x1 ... xm ) ... ) in f let is_accu v = match head v with | Accu _ -> true | _ -> false

8 / 18

slide-9
SLIDE 9

Compilation scheme

Inductive types

The inductive type Inductive I := C1 : T1 | ... | Cn : Tn is translated to type I = Accu_I of t | C1 of t * · · · * t | ... | Cn of t * · · · * t where the signatures match the arity of each constructor. mkConstruct i v = Obj.magic Ci( v)

9 / 18

slide-10
SLIDE 10

Example

b = λm.λn.case m of (C1() → n, C2(p) → C2(add p n)) fix1(add := b)∅ = let norm_add add = b{add} in let rec add = mkLam (fun x1 -> if is_accu x1 then mkAccu (Fix (norm_add ,1)) x1 else norm_add add x1) in add

10 / 18

slide-11
SLIDE 11

Example

b = λm.λn.case m of (C1() → n, C2(p) → C2(add p n)) fix1(add := b)∅ = let norm_add add = b{add} in let rec add = (fun x1 -> if is_accu x1 then mkAccu (Fix (norm_add ,1)) x1 else norm_add add x1) in add

11 / 18

slide-12
SLIDE 12

Example

b = case m of (C1() → n, C2(p) → C2(add p n)) λm.λn.b{add} = mkLam(fun m -> λn.b{add,m}) = mkLam(fun m -> mkLam(fun n -> b{add,m,n})) = fun m -> fun n -> b{add,m,n} b{add,m,n} = let rec case c = match c with | C1(){add,m,n} -> n{add,m,n} | C2(p){add,m,n,p} -> C2(add p n)){add,m,n,p} | _ -> mkAccu (Match (c,case )) in case m{add,m,n}

12 / 18

slide-13
SLIDE 13

Example

let rec case c = match c with | C1(){add,m,n} -> n{add,m,n} | C2(p){add,m,n,p} -> C2(add p n){add,m,n,p} | _ -> mkAccu (Match (c,case )) in case m = let rec case c = match c with | C1() -> n | C2(p) -> C2(add p n{add,m,n,p}) | _ -> mkAccu (Match (c,case )) in case m

13 / 18

slide-14
SLIDE 14

Example

let rec case c = match c with | C1() -> n | C2(p) -> C2(add p n{add,m,n,p}) | _ -> mkAccu (Match (c,case )) in case m = let rec case c = match c with | C1() -> n | C2(p) -> C2(add p n) | _ -> mkAccu (Match (c,case )) in case m

14 / 18

slide-15
SLIDE 15

Example

let norm_add add = fun m -> fun n -> let rec case c = match c with | C1() -> n | C2(p) -> C2(add p n) | _ -> mkAccu (Match (c,case )) in case m in let rec add = (fun x1 -> if is_accu x1 then mkAccu (Fix (norm_add ,1)) x1 else norm_add add x1) in add

15 / 18

slide-16
SLIDE 16

Example

let norm_add add m n = let rec case c = match c with | C1() -> n | C2(p) -> C2(add p n) | _ -> mkAccu (Match (c,case )) in case m in let rec add m = if is_accu m then mkAccu (Fix (norm_add ,1)) m else norm_add add m in add

16 / 18

slide-17
SLIDE 17

Example

Their version

let norm_add f m n = let rec case c = match c with | Accu_nat _ -> mk_sw_accu [...] (cast_accu m) pred_add (case f n) | C1() -> n | C2(p) -> C2(f p n) in let rec add m n = if is_accu m then mk_fix_accu [...] fixtype_add normtbl_add m n else case m n in add

17 / 18

slide-18
SLIDE 18

Example

Optimization

let rec case f n m = match m with | Accu_nat _ -> mk_sw_accu [...] (cast_accu m) pred_add (case f n) | C1() -> n | C2(p) -> C2(f p n) let norm_add f m n = case_add f n m let rec add m n = match m with | Accu_nat _ -> mk_fix_accu [...] fixtype_add normtbl_add m n | C1() -> n | C2(p) -> C2(f p n)

18 / 18