1 (* Title: HOL/Tools/datatype_realizer.ML |
1 (* Title: HOL/Tools/Datatype/datatype_realizer.ML |
2 Author: Stefan Berghofer, TU Muenchen |
2 Author: Stefan Berghofer, TU Muenchen |
3 |
3 |
4 Porgram extraction from proofs involving datatypes: |
4 Program extraction from proofs involving datatypes: |
5 Realizers for induction and case analysis |
5 realizers for induction and case analysis. |
6 *) |
6 *) |
7 |
7 |
8 signature DATATYPE_REALIZER = |
8 signature DATATYPE_REALIZER = |
9 sig |
9 sig |
10 val add_dt_realizers: Datatype.config -> string list -> theory -> theory |
10 val add_dt_realizers: Datatype.config -> string list -> theory -> theory |
11 val setup: theory -> theory |
11 val setup: theory -> theory |
12 end; |
12 end; |
13 |
13 |
14 structure DatatypeRealizer : DATATYPE_REALIZER = |
14 structure Datatype_Realizer : DATATYPE_REALIZER = |
15 struct |
15 struct |
16 |
16 |
17 open DatatypeAux; |
17 open Datatype_Aux; |
18 |
18 |
19 fun subsets i j = if i <= j then |
19 fun subsets i j = if i <= j then |
20 let val is = subsets (i+1) j |
20 let val is = subsets (i+1) j |
21 in map (fn ks => i::ks) is @ is end |
21 in map (fn ks => i::ks) is @ is end |
22 else [[]]; |
22 else [[]]; |
58 |
58 |
59 val (prems, rec_fns) = split_list (flat (fst (fold_map |
59 val (prems, rec_fns) = split_list (flat (fst (fold_map |
60 (fn ((i, (_, _, constrs)), T) => fold_map (fn (cname, cargs) => fn j => |
60 (fn ((i, (_, _, constrs)), T) => fold_map (fn (cname, cargs) => fn j => |
61 let |
61 let |
62 val Ts = map (typ_of_dtyp descr sorts) cargs; |
62 val Ts = map (typ_of_dtyp descr sorts) cargs; |
63 val tnames = Name.variant_list pnames (DatatypeProp.make_tnames Ts); |
63 val tnames = Name.variant_list pnames (Datatype_Prop.make_tnames Ts); |
64 val recs = filter (is_rec_type o fst o fst) (cargs ~~ tnames ~~ Ts); |
64 val recs = filter (is_rec_type o fst o fst) (cargs ~~ tnames ~~ Ts); |
65 val frees = tnames ~~ Ts; |
65 val frees = tnames ~~ Ts; |
66 |
66 |
67 fun mk_prems vs [] = |
67 fun mk_prems vs [] = |
68 let |
68 let |
95 fun mk_proj j [] t = t |
95 fun mk_proj j [] t = t |
96 | mk_proj j (i :: is) t = if null is then t else |
96 | mk_proj j (i :: is) t = if null is then t else |
97 if (j: int) = i then HOLogic.mk_fst t |
97 if (j: int) = i then HOLogic.mk_fst t |
98 else mk_proj j is (HOLogic.mk_snd t); |
98 else mk_proj j is (HOLogic.mk_snd t); |
99 |
99 |
100 val tnames = DatatypeProp.make_tnames recTs; |
100 val tnames = Datatype_Prop.make_tnames recTs; |
101 val fTs = map fastype_of rec_fns; |
101 val fTs = map fastype_of rec_fns; |
102 val ps = map (fn ((((i, _), T), U), s) => Abs ("x", T, make_pred i U T |
102 val ps = map (fn ((((i, _), T), U), s) => Abs ("x", T, make_pred i U T |
103 (list_comb (Const (s, fTs ---> T --> U), rec_fns) $ Bound 0) (Bound 0))) |
103 (list_comb (Const (s, fTs ---> T --> U), rec_fns) $ Bound 0) (Bound 0))) |
104 (descr ~~ recTs ~~ rec_result_Ts ~~ rec_names); |
104 (descr ~~ recTs ~~ rec_result_Ts ~~ rec_names); |
105 val r = if null is then Extraction.nullt else |
105 val r = if null is then Extraction.nullt else |
130 |> Sign.root_path |
130 |> Sign.root_path |
131 |> PureThy.store_thm |
131 |> PureThy.store_thm |
132 (Binding.qualified_name (space_implode "_" (ind_name :: vs @ ["correctness"])), thm) |
132 (Binding.qualified_name (space_implode "_" (ind_name :: vs @ ["correctness"])), thm) |
133 ||> Sign.restore_naming thy; |
133 ||> Sign.restore_naming thy; |
134 |
134 |
135 val ivs = rev (Term.add_vars (Logic.varify (DatatypeProp.make_ind [descr] sorts)) []); |
135 val ivs = rev (Term.add_vars (Logic.varify (Datatype_Prop.make_ind [descr] sorts)) []); |
136 val rvs = rev (Thm.fold_terms Term.add_vars thm' []); |
136 val rvs = rev (Thm.fold_terms Term.add_vars thm' []); |
137 val ivs1 = map Var (filter_out (fn (_, T) => |
137 val ivs1 = map Var (filter_out (fn (_, T) => |
138 tname_of (body_type T) mem ["set", "bool"]) ivs); |
138 tname_of (body_type T) mem ["set", "bool"]) ivs); |
139 val ivs2 = map (fn (ixn, _) => Var (ixn, the (AList.lookup (op =) rvs ixn))) ivs; |
139 val ivs2 = map (fn (ixn, _) => Var (ixn, the (AList.lookup (op =) rvs ixn))) ivs; |
140 |
140 |
167 val rT' = TVar (("'P", 0), HOLogic.typeS); |
167 val rT' = TVar (("'P", 0), HOLogic.typeS); |
168 |
168 |
169 fun make_casedist_prem T (cname, cargs) = |
169 fun make_casedist_prem T (cname, cargs) = |
170 let |
170 let |
171 val Ts = map (typ_of_dtyp descr sorts) cargs; |
171 val Ts = map (typ_of_dtyp descr sorts) cargs; |
172 val frees = Name.variant_list ["P", "y"] (DatatypeProp.make_tnames Ts) ~~ Ts; |
172 val frees = Name.variant_list ["P", "y"] (Datatype_Prop.make_tnames Ts) ~~ Ts; |
173 val free_ts = map Free frees; |
173 val free_ts = map Free frees; |
174 val r = Free ("r" ^ Long_Name.base_name cname, Ts ---> rT) |
174 val r = Free ("r" ^ Long_Name.base_name cname, Ts ---> rT) |
175 in (r, list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop |
175 in (r, list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop |
176 (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))), |
176 (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))), |
177 HOLogic.mk_Trueprop (Free ("P", rT --> HOLogic.boolT) $ |
177 HOLogic.mk_Trueprop (Free ("P", rT --> HOLogic.boolT) $ |