13 val mk_func: thm -> (CodegenConsts.const * thm) list |
13 val mk_func: thm -> (CodegenConsts.const * thm) list |
14 val mk_head: thm -> CodegenConsts.const * thm |
14 val mk_head: thm -> CodegenConsts.const * thm |
15 val dest_func: thm -> (string * typ) * term list |
15 val dest_func: thm -> (string * typ) * term list |
16 val typ_func: thm -> typ |
16 val typ_func: thm -> typ |
17 |
17 |
|
18 val inst_thm: sort Vartab.table -> thm -> thm |
18 val expand_eta: int -> thm -> thm |
19 val expand_eta: int -> thm -> thm |
19 val rewrite_func: thm list -> thm -> thm |
20 val rewrite_func: thm list -> thm -> thm |
20 end; |
21 end; |
21 |
22 |
22 structure CodegenFunc : CODEGEN_FUNC = |
23 structure CodegenFunc : CODEGEN_FUNC = |
81 val _ = |
82 val _ = |
82 if has_duplicates (op =) |
83 if has_duplicates (op =) |
83 ((fold o fold_aterms) (fn Var (v, _) => cons v |
84 ((fold o fold_aterms) (fn Var (v, _) => cons v |
84 | _ => I |
85 | _ => I |
85 ) args []) |
86 ) args []) |
86 then bad_thm "Repeated variables on left hand side of function equation" thm |
87 then bad_thm "Repeated variables on left hand side of defining equation" thm |
87 else () |
88 else () |
88 fun no_abs (Abs _) = bad_thm "Abstraction on left hand side of function equation" thm |
89 fun no_abs (Abs _) = bad_thm "Abstraction on left hand side of defining equation" thm |
89 | no_abs (t1 $ t2) = (no_abs t1; no_abs t2) |
90 | no_abs (t1 $ t2) = (no_abs t1; no_abs t2) |
90 | no_abs _ = (); |
91 | no_abs _ = (); |
91 val _ = map no_abs args; |
92 val _ = map no_abs args; |
92 in thm end |
93 in thm end |
93 | NONE => bad_thm "Not a function equation" thm; |
94 | NONE => bad_thm "Not a defining equation" thm; |
94 |
95 |
95 val mk_func = map (mk_head o assert_func) o mk_rew; |
96 val mk_func = map (mk_head o assert_func) o mk_rew; |
96 |
97 |
97 |
98 |
98 (* utilities *) |
99 (* utilities *) |
|
100 |
|
101 fun inst_thm tvars' thm = |
|
102 let |
|
103 val thy = Thm.theory_of_thm thm; |
|
104 val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
105 fun mk_inst (tvar as (v, _)) = case Vartab.lookup tvars' v |
|
106 of SOME sort => SOME (pairself (Thm.ctyp_of thy o TVar) (tvar, (v, sort))) |
|
107 | NONE => NONE; |
|
108 val insts = map_filter mk_inst tvars; |
|
109 in Thm.instantiate (insts, []) thm end; |
99 |
110 |
100 fun expand_eta k thm = |
111 fun expand_eta k thm = |
101 let |
112 let |
102 val thy = Thm.theory_of_thm thm; |
113 val thy = Thm.theory_of_thm thm; |
103 val (lhs, rhs) = (Logic.dest_equals o Drule.plain_prop_of) thm; |
114 val (lhs, rhs) = (Logic.dest_equals o Drule.plain_prop_of) thm; |
137 val (ct_f, ct_args) = Drule.strip_comb ct_lhs; |
148 val (ct_f, ct_args) = Drule.strip_comb ct_lhs; |
138 val rhs' = rewrite ct_rhs; |
149 val rhs' = rewrite ct_rhs; |
139 val args' = map rewrite ct_args; |
150 val args' = map rewrite ct_args; |
140 val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1) |
151 val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1) |
141 args' (Thm.reflexive ct_f)); |
152 args' (Thm.reflexive ct_f)); |
142 in |
153 in Thm.transitive (Thm.transitive lhs' thm) rhs' end; |
143 Thm.transitive (Thm.transitive lhs' thm) rhs' |
|
144 end handle Bind => raise ERROR "rewrite_func" |
|
145 |
154 |
146 end; |
155 end; |