128 case SINGLE tac (Goal.init cgoal) of |
128 case SINGLE tac (Goal.init cgoal) of |
129 NONE => Fail |
129 NONE => Fail |
130 | SOME st => if Thm.no_prems st then Solved (Goal.finish st) else Stuck st |
130 | SOME st => if Thm.no_prems st then Solved (Goal.finish st) else Stuck st |
131 |
131 |
132 |
132 |
|
133 fun dest_binop_list cn (t as (Const (n, _) $ a $ b)) = |
|
134 if cn = n then dest_binop_list cn a @ dest_binop_list cn b else [ t ] |
|
135 | dest_binop_list _ t = [ t ] |
|
136 |
|
137 |
|
138 (* separate two parts in a +-expression: |
|
139 "a + b + c + d + e" --> "(a + b + d) + (c + e)" |
|
140 |
|
141 Here, + can be any binary operation that is AC. |
|
142 |
|
143 cn - The name of the binop-constructor (e.g. @{const_name "op Un"}) |
|
144 ac - the AC rewrite rules for cn |
|
145 is - the list of indices of the expressions that should become the first part |
|
146 (e.g. [0,1,3] in the above example) |
|
147 *) |
|
148 |
|
149 fun regroup_conv neu cn ac is ct = |
|
150 let |
|
151 val mk = HOLogic.mk_binop cn |
|
152 val t = term_of ct |
|
153 val xs = dest_binop_list cn t |
|
154 val js = 0 upto (length xs) - 1 \\ is |
|
155 val ty = fastype_of t |
|
156 val thy = theory_of_cterm ct |
|
157 in |
|
158 Goal.prove_internal [] |
|
159 (cterm_of thy |
|
160 (Logic.mk_equals (t, |
|
161 if is = [] |
|
162 then mk (Const (neu, ty), foldr1 mk (map (nth xs) js)) |
|
163 else if js = [] |
|
164 then mk (foldr1 mk (map (nth xs) is), Const (neu, ty)) |
|
165 else mk (foldr1 mk (map (nth xs) is), foldr1 mk (map (nth xs) js))))) |
|
166 (K (MetaSimplifier.rewrite_goals_tac ac |
|
167 THEN rtac Drule.reflexive_thm 1)) |
|
168 end |
|
169 |
|
170 (* instance for unions *) |
|
171 fun regroup_union_conv t = |
|
172 regroup_conv (@{const_name "{}"}) |
|
173 @{const_name "op Un"} |
|
174 (map (fn t => t RS eq_reflection) (@{thms "Un_ac"} @ |
|
175 @{thms "Un_empty_right"} @ |
|
176 @{thms "Un_empty_left"})) t |
|
177 |
|
178 |
133 end |
179 end |