full reduction at full throttle
play

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 )


  1. Full reduction at full throttle Extension to the Calculus of Inductive Constructions Gerco van Heerdt December 19, 2014 1 / 18

  2. 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 ) i ∈ I | fix m ( f : T := t ) Val ∋ v ::= λ x . t | [ a � v ] | C i ( � v ) Atom ∋ a ::= ˜ x | s | Π x : T 1 . T 2 | case � P � a of ( C i ( � x i ) → t i ) i ∈ I | fix m ( f : T := t ) 2 / 18

  3. Additional reduction rules case � P � C i ( � v ) of ( C i ( � x i ) → t i ) i ∈ I → ι t i { � x i ← � v } case � P � [ a ] of ( C i ( � x i ) → t i ) i ∈ I → ι [ case � P � a of ( C i ( � x i ) → t i ) i ∈ I ] fix m ( f : T := t ) v 1 . . . v m − 1 C i ( � v ) → ι t { f ← fix m ( f : T := t ) } v 1 . . . v m − 1 C i ( � v ) fix m ( f : T := t ) v 1 . . . v m − 1 [ a ] → ι [ fix m ( f : T := t ) v 1 . . . v m − 1 a ] 3 / 18

  4. Readback for pattern matching and fixpoints R A ( case � P � a of ( C i ( � x i ) → t i ) i ∈ I ) = x i ) → N ( f ( C i ( # » case � P � R A ( a ) of ( C i ( � [˜ x i ])))) i ∈ I where f = λ x . case � P � x of ( C i ( � x i ) → t i ) i ∈ I R A ( fix m ( f : T := t )) = fix m ( f : T := N (( λ f . t )[˜ f ])) 4 / 18

  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

  6. Compilation scheme Sorts, products, and constructors � s � B = mkAccu (Sort s) � Π x : T . U � B mkAccu (Prod( � T � B , � λ x . U � B )) = t ) � B t � B |] � C i ( � mkConstruct i [| � � = 6 / 18

  7. Compilation scheme Pattern matching x i ) → t i ) i ∈ I � B = � case � P � t of ( C i ( � let rec case c = match c with x 1 } -> � � x 1 ) � B ∪{ � t 1 � B ∪{ � x 1 } | � C 1 ( � | ... x n } -> � � x n ) � B ∪{ � t n � B ∪{ � x n } | � C n ( � | _ -> mkAccu (Match ( ˜ I ,c, � P � B ,case )) in case � t � B 7 / 18

  8. Compilation scheme Fixpoints � fix m ( f : T := t ) � B = let fnorm f = � t � B ∪{ f } in let rec f = mkLam ( fun x 1 -> ... -> mkLam( fun x m -> if is_accu x m then mkAccu (Fix (fnorm , � T � B ,m)) x 1 ... x m else fnorm f x 1 ... x m ) ... ) in f let is_accu v = match head v with | Accu _ -> true | _ -> false 8 / 18

  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. v = Obj.magic C i ( � v ) mkConstruct i � 9 / 18

  10. Example b = λ m .λ n . case m of ( C 1 () → n , C 2 ( p ) → C 2 ( add p n )) � fix 1 ( add := b ) � ∅ add = � b � { add } in = let norm_add let rec add = mkLam ( fun x 1 -> if is_accu x 1 then mkAccu (Fix (norm_add ,1)) x 1 else norm_add add x 1 ) in add 10 / 18

  11. Example b = λ m .λ n . case m of ( C 1 () → n , C 2 ( p ) → C 2 ( add p n )) � fix 1 ( add := b ) � ∅ add = � b � { add } in = let norm_add let rec add = ( fun x 1 -> if is_accu x 1 then mkAccu (Fix (norm_add ,1)) x 1 else norm_add add x 1 ) in add 11 / 18

  12. Example b = case m of ( C 1 () → n , C 2 ( p ) → C 2 ( 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 | � C 1 () � { add , m , n } -> � n � { add , m , n } | � C 2 ( p ) � { add , m , n , p } -> � C 2 ( add p n )) � { add , m , n , p } | _ -> mkAccu (Match (c,case )) in case � m � { add , m , n } 12 / 18

  13. Example let rec case c = match c with | � C 1 () � { add , m , n } -> � n � { add , m , n } | � C 2 ( p ) � { add , m , n , p } -> � C 2 ( add p n ) � { add , m , n , p } | _ -> mkAccu (Match (c,case )) in case m = let rec case c = match c with | C 1 () -> n | C 2 ( p ) -> C 2 ( � add p n � { add , m , n , p } ) | _ -> mkAccu (Match (c,case )) in case m 13 / 18

  14. Example let rec case c = match c with | C 1 () -> n | C 2 ( p ) -> C 2 ( � add p n � { add , m , n , p } ) | _ -> mkAccu (Match (c,case )) in case m = let rec case c = match c with | C 1 () -> n | C 2 ( p ) -> C 2 ( add p n ) | _ -> mkAccu (Match (c,case )) in case m 14 / 18

  15. Example let norm_add add = fun m -> fun n -> let rec case c = match c with | C 1 () -> n | C 2 ( p ) -> C 2 ( add p n ) | _ -> mkAccu (Match (c,case )) in case m in let rec add = ( fun x 1 -> if is_accu x 1 then mkAccu (Fix (norm_add ,1)) x 1 else norm_add add x 1 ) in add 15 / 18

  16. Example let norm_add add m n = let rec case c = match c with | C 1 () -> n | C 2 ( p ) -> C 2 ( 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

  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) | C 1 () -> n | C 2 ( p ) -> C 2 ( 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

  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) | C 1 () -> n | C 2 ( p ) -> C 2 ( 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 | C 1 () -> n | C 2 ( p ) -> C 2 ( f p n ) 18 / 18

Recommend


More recommend