src/HOL/Library/case_converter.ML
author haftmann
Wed Jul 18 20:51:21 2018 +0200 (11 months ago)
changeset 68658 16cc1161ad7f
parent 68301 fb5653a7a879
child 69568 de09a7261120
permissions -rw-r--r--
tuned equation
     1 (* Author: Pascal Stoop, ETH Zurich
     2    Author: Andreas Lochbihler, Digital Asset *)
     3 
     4 signature CASE_CONVERTER =
     5 sig
     6   val to_case: Proof.context -> (string * string -> bool) -> (string * typ -> int) ->
     7     thm list -> thm list option
     8 end;
     9 
    10 structure Case_Converter : CASE_CONVERTER =
    11 struct
    12 
    13 fun lookup_remove _ _ [] = (NONE, [])
    14   | lookup_remove eq k ((k', v) :: kvs) =
    15     if eq (k, k') then (SOME (k', v), kvs)
    16     else apsnd (cons (k', v)) (lookup_remove eq k kvs)
    17 
    18 fun mk_abort msg t =
    19   let 
    20     val T = fastype_of t
    21     val abort = Const (@{const_name missing_pattern_match}, HOLogic.literalT --> (HOLogic.unitT --> T) --> T)
    22   in
    23     abort $ HOLogic.mk_literal msg $ absdummy HOLogic.unitT t
    24   end
    25 
    26 (* fold_term : (string * typ -> 'a) ->
    27                (string * typ -> 'a) ->
    28                (indexname * typ -> 'a) ->
    29                (int -> 'a) ->
    30                (string * typ * 'a -> 'a) ->
    31                ('a * 'a -> 'a) ->
    32                term ->
    33                'a *)
    34 fun fold_term const_fun free_fun var_fun bound_fun abs_fun dollar_fun term =
    35   let
    36     fun go x = case x of
    37       Const (s, T) => const_fun (s, T)
    38       | Free (s, T) => free_fun (s, T)
    39       | Var (i, T) => var_fun (i, T)
    40       | Bound n => bound_fun n
    41       | Abs (s, T, term) => abs_fun (s, T, go term)
    42       | term1 $ term2 => dollar_fun (go term1, go term2)
    43   in
    44     go term
    45   end;
    46 
    47 datatype term_coordinate = End of typ
    48   | Coordinate of (string * (int * term_coordinate)) list;
    49 
    50 fun term_coordinate_merge (End T) _ = End T
    51   | term_coordinate_merge _ (End T) = End T
    52   | term_coordinate_merge (Coordinate xs) (Coordinate ys) =
    53   let
    54     fun merge_consts xs [] = xs
    55       | merge_consts xs ((s1, (n, y)) :: ys) = 
    56         case List.partition (fn (s2, (m, _)) => s1 = s2 andalso n = m) xs of
    57             ([], xs') => (s1, (n, y)) :: (merge_consts xs' ys)
    58           | ((_, (_, x)) :: _, xs') => (s1, (n, term_coordinate_merge x y)) :: (merge_consts xs' ys)
    59   in
    60     Coordinate (merge_consts xs ys)
    61   end;
    62 
    63 fun term_to_coordinates P term = 
    64   let
    65     val (ctr, args) = strip_comb term
    66   in
    67     case ctr of Const (s, T) =>
    68       if P (body_type T |> dest_Type |> fst, s)
    69       then SOME (End (body_type T))
    70       else
    71         let
    72           fun f (i, t) = term_to_coordinates P t |> Option.map (pair i)
    73           val tcos = map_filter I (map_index f args)
    74         in
    75           if null tcos then NONE
    76           else SOME (Coordinate (map (pair s) tcos))
    77         end
    78     | _ => NONE
    79   end;
    80 
    81 fun coordinates_to_list (End x) = [(x, [])]
    82   | coordinates_to_list (Coordinate xs) = 
    83   let
    84     fun f (s, (n, xss)) = map (fn (T, xs) => (T, (s, n) :: xs)) (coordinates_to_list xss)
    85   in flat (map f xs) end;
    86 
    87 
    88 (* AL: TODO: change from term to const_name *)
    89 fun find_ctr ctr1 xs =
    90   let
    91     val const_name = fst o dest_Const
    92     fun const_equal (ctr1, ctr2) = const_name ctr1 = const_name ctr2
    93   in
    94     lookup_remove const_equal ctr1 xs
    95   end;
    96 
    97 datatype pattern 
    98   = Wildcard
    99   | Value
   100   | Split of int * (term * pattern) list * pattern;
   101 
   102 fun pattern_merge Wildcard pat' = pat'
   103   | pattern_merge Value _ = Value
   104   | pattern_merge (Split (n, xs, pat)) Wildcard =
   105     Split (n, map (apsnd (fn pat'' => pattern_merge pat'' Wildcard)) xs, pattern_merge pat Wildcard)
   106   | pattern_merge (Split _) Value = Value
   107   | pattern_merge (Split (n, xs, pat)) (Split (m, ys, pat'')) =
   108     let 
   109       fun merge_consts xs [] = map (apsnd (fn pat => pattern_merge pat Wildcard)) xs
   110         | merge_consts xs ((ctr, y) :: ys) =
   111           (case find_ctr ctr xs of
   112               (SOME (ctr, x), xs) => (ctr, pattern_merge x y) :: merge_consts xs ys
   113             | (NONE, xs) => (ctr, y) :: merge_consts xs ys
   114           )
   115      in
   116        Split (if n <= 0 then m else n, merge_consts xs ys, pattern_merge pat pat'')
   117      end
   118      
   119 fun pattern_intersect Wildcard _ = Wildcard
   120   | pattern_intersect Value pat2 = pat2
   121   | pattern_intersect (Split _) Wildcard = Wildcard
   122   | pattern_intersect (Split (n, xs', pat1)) Value =
   123     Split (n,
   124       map (apsnd (fn pat1 => pattern_intersect pat1 Value)) xs',
   125       pattern_intersect pat1 Value)
   126   | pattern_intersect (Split (n, xs', pat1)) (Split (m, ys, pat2)) =
   127     Split (if n <= 0 then m else n, 
   128       intersect_consts xs' ys pat1 pat2,
   129       pattern_intersect pat1 pat2)
   130 and
   131     intersect_consts xs [] _ default2 = map (apsnd (fn pat => pattern_intersect pat default2)) xs
   132   | intersect_consts xs ((ctr, pat2) :: ys) default1 default2 = case find_ctr ctr xs of
   133     (SOME (ctr, pat1), xs') => 
   134       (ctr, pattern_merge (pattern_merge (pattern_intersect pat1 pat2) (pattern_intersect default1 pat2))
   135               (pattern_intersect pat1 default2)) ::
   136       intersect_consts xs' ys default1 default2
   137     | (NONE, xs') => (ctr, pattern_intersect default1 pat2) :: (intersect_consts xs' ys default1 default2)
   138         
   139 fun pattern_lookup _ Wildcard = Wildcard
   140   | pattern_lookup _ Value = Value
   141   | pattern_lookup [] (Split (n, xs, pat)) = 
   142     Split (n, map (apsnd (pattern_lookup [])) xs, pattern_lookup [] pat)
   143   | pattern_lookup (term :: terms) (Split (n, xs, pat)) =
   144   let
   145     val (ctr, args) = strip_comb term
   146     fun map_ctr (term, pat) =
   147       let
   148         val args = term |> dest_Const |> snd |> binder_types |> map (fn T => Free ("x", T))
   149       in
   150         pattern_lookup args pat
   151       end
   152   in
   153     if is_Const ctr then
   154        case find_ctr ctr xs of (SOME (_, pat'), _) => 
   155            pattern_lookup terms (pattern_merge (pattern_lookup args pat') (pattern_lookup [] pat))
   156          | (NONE, _) => pattern_lookup terms pat
   157     else if length xs < n orelse n <= 0 then
   158       pattern_lookup terms pat
   159     else pattern_lookup terms
   160       (pattern_merge
   161         (fold pattern_intersect (map map_ctr (tl xs)) (map_ctr (hd xs)))
   162         (pattern_lookup [] pat))
   163   end;
   164 
   165 fun pattern_contains terms pat = case pattern_lookup terms pat of
   166     Wildcard => false
   167   | Value => true
   168   | Split _ => raise Match;
   169 
   170 fun pattern_create _ [] = Wildcard
   171   | pattern_create ctr_count (term :: terms) = 
   172     let
   173       val (ctr, args) = strip_comb term
   174     in
   175       if is_Const ctr then
   176         Split (ctr_count ctr, [(ctr, pattern_create ctr_count (args @ terms))], Wildcard)
   177       else Split (0, [], pattern_create ctr_count terms)
   178     end;
   179 
   180 fun pattern_insert ctr_count terms pat =
   181   let
   182     fun new_pattern terms = pattern_insert ctr_count terms (pattern_create ctr_count terms)
   183     fun aux _ false Wildcard = Wildcard
   184       | aux terms true Wildcard = if null terms then Value else new_pattern terms
   185       | aux _ _ Value = Value
   186       | aux terms modify (Split (n, xs', pat)) =
   187       let
   188         val unmodified = (n, map (apsnd (aux [] false)) xs', aux [] false pat)
   189       in case terms of [] => Split unmodified
   190         | term :: terms =>
   191         let
   192           val (ctr, args) = strip_comb term
   193           val (m, ys, pat') = unmodified
   194         in
   195           if is_Const ctr
   196             then case find_ctr ctr xs' of
   197               (SOME (ctr, pat''), xs) =>
   198                 Split (m, (ctr, aux (args @ terms) modify pat'') :: map (apsnd (aux [] false)) xs, pat')
   199               | (NONE, _) => if modify
   200                 then if m <= 0
   201                   then Split (ctr_count ctr, (ctr, new_pattern (args @ terms)) :: ys, pat')
   202                   else Split (m, (ctr, new_pattern (args @ terms)) :: ys, pat')
   203                 else Split unmodified
   204             else Split (m, ys, aux terms modify pat)
   205         end
   206       end
   207   in
   208     aux terms true pat
   209   end;
   210 
   211 val pattern_empty = Wildcard;
   212 
   213 fun replace_frees lhss rhss typ_list ctxt =
   214   let
   215     fun replace_frees_once (lhs, rhs) ctxt =
   216       let
   217         val add_frees_list = fold_rev Term.add_frees
   218         val frees = add_frees_list lhs []
   219         val (new_frees, ctxt1) = (Ctr_Sugar_Util.mk_Frees "x" (map snd frees) ctxt)
   220         val (new_frees1, ctxt2) =
   221           let
   222             val (dest_frees, types) = split_list (map dest_Free new_frees)
   223             val (new_frees, ctxt2) = Variable.variant_fixes dest_frees ctxt1
   224           in
   225             (map Free (new_frees ~~ types), ctxt2)
   226           end
   227         val dict = frees ~~ new_frees1
   228         fun free_map_fun (s, T) =
   229           case AList.lookup (op =) dict (s, T) of
   230               NONE => Free (s, T)
   231             | SOME x => x
   232         val map_fun = fold_term Const free_map_fun Var Bound Abs (op $)
   233       in
   234         ((map map_fun lhs, map_fun rhs), ctxt2)
   235       end
   236 
   237     fun variant_fixes (def_frees, ctxt) =
   238       let
   239         val (dest_frees, types) = split_list (map dest_Free def_frees)
   240         val (def_frees, ctxt1) = Variable.variant_fixes dest_frees ctxt
   241       in
   242         (map Free (def_frees ~~ types), ctxt1)
   243       end
   244     val (def_frees, ctxt1) = variant_fixes (Ctr_Sugar_Util.mk_Frees "x" typ_list ctxt)
   245     val (rhs_frees, ctxt2) = variant_fixes (Ctr_Sugar_Util.mk_Frees "x" typ_list ctxt1)
   246     val (case_args, ctxt3) = variant_fixes (Ctr_Sugar_Util.mk_Frees "x"
   247       (map fastype_of (hd lhss)) ctxt2)
   248     val (new_terms1, ctxt4) = fold_map replace_frees_once (lhss ~~ rhss) ctxt3
   249     val (lhss1, rhss1) = split_list new_terms1
   250   in
   251     (lhss1, rhss1, def_frees ~~ rhs_frees, case_args, ctxt4)
   252   end;
   253 
   254 fun add_names_in_type (Type (name, Ts)) = 
   255     List.foldr (op o) (Symtab.update (name, ())) (map add_names_in_type Ts)
   256   | add_names_in_type (TFree _) = I
   257   | add_names_in_type (TVar _) = I
   258 
   259 fun add_names_in_term (Const (_, T)) = add_names_in_type T
   260   | add_names_in_term (Free (_, T)) = add_names_in_type T
   261   | add_names_in_term (Var (_, T)) = add_names_in_type T
   262   | add_names_in_term (Bound _) = I
   263   | add_names_in_term (Abs (_, T, body)) =
   264     add_names_in_type T o add_names_in_term body
   265   | add_names_in_term (t1 $ t2) = add_names_in_term t1 o add_names_in_term t2
   266 
   267 fun add_type_names terms =
   268   fold (fn term => fn f => add_names_in_term term o f) terms I
   269 
   270 fun get_split_theorems ctxt =
   271   Symtab.keys
   272   #> map_filter (Ctr_Sugar.ctr_sugar_of ctxt)
   273   #> map #split;
   274 
   275 fun match (Const (s1, _)) (Const (s2, _)) = if s1 = s2 then SOME I else NONE
   276   | match (Free y) x = SOME (fn z => if z = Free y then x else z)
   277   | match (pat1 $ pattern2) (t1 $ t2) =
   278     (case (match pat1 t1, match pattern2 t2) of
   279        (SOME f, SOME g) => SOME (f o g)
   280        | _ => NONE
   281      )
   282   | match _ _ = NONE;
   283 
   284 fun match_all patterns terms =
   285   let
   286     fun combine _ NONE = NONE
   287       | combine (f_opt, f_opt') (SOME g) = 
   288         case match f_opt f_opt' of SOME f => SOME (f o g) | _ => NONE
   289   in
   290     fold_rev combine (patterns ~~ terms) (SOME I)
   291   end
   292 
   293 fun matches (Const (s1, _)) (Const (s2, _)) = s1 = s2
   294   | matches (Free _) _ = true 
   295   | matches (pat1 $ pat2) (t1 $ t2) = matches pat1 t1 andalso matches pat2 t2
   296   | matches _ _ = false;
   297 fun matches_all patterns terms = forall (uncurry matches) (patterns ~~ terms)
   298 
   299 fun terms_to_case_at ctr_count ctxt (fun_t : term) (default_lhs : term list)
   300     (pos, (lazy_case_arg, rhs_free))
   301     ((lhss : term list list), (rhss : term list), type_name_fun) =
   302   let
   303     fun abort t =
   304       let
   305         val fun_name = head_of t |> dest_Const |> fst
   306         val msg = "Missing pattern in " ^ fun_name ^ "."
   307       in
   308         mk_abort msg t
   309       end;
   310 
   311     (* Step 1 : Eliminate lazy pattern *)
   312     fun replace_pat_at (n, tcos) pat pats =
   313       let
   314         fun map_at _ _ [] = raise Empty
   315           | map_at n f (x :: xs) = if n > 0
   316             then apfst (cons x) (map_at (n - 1) f xs)
   317             else apfst (fn x => x :: xs) (f x)
   318         fun replace [] pat term = (pat, term)
   319           | replace ((s1, n) :: tcos) pat term =
   320             let
   321               val (ctr, args) = strip_comb term
   322             in
   323               case ctr of Const (s2, _) =>
   324                   if s1 = s2
   325                   then apfst (pair ctr #> list_comb) (map_at n (replace tcos pat) args)
   326                   else (term, rhs_free)
   327                 | _ => (term, rhs_free)
   328             end
   329         val (part1, (old_pat, part2)) = chop n pats ||> (fn xs => (hd xs, tl xs))
   330         val (new_pat, old_pat1) = replace tcos pat old_pat
   331       in
   332         (part1 @ [new_pat] @ part2, old_pat1)
   333       end                               
   334     val (lhss1, lazy_pats) = map (replace_pat_at pos lazy_case_arg) lhss
   335       |> split_list
   336 
   337     (* Step 2 : Split patterns *)
   338     fun split equs =
   339       let
   340         fun merge_pattern (Const (s1, T1), Const (s2, _)) =
   341             if s1 = s2 then SOME (Const (s1, T1)) else NONE
   342           | merge_pattern (t, Free _) = SOME t
   343           | merge_pattern (Free _, t) = SOME t
   344           | merge_pattern (t1l $ t1r, t2l $ t2r) =
   345             (case (merge_pattern (t1l, t2l), merge_pattern (t1r, t2r)) of
   346               (SOME t1, SOME t2) => SOME (t1 $ t2)
   347               | _ => NONE)
   348           | merge_pattern _ = NONE
   349         fun merge_patterns pats1 pats2 = case (pats1, pats2) of
   350           ([], []) => SOME []
   351           | (x :: xs, y :: ys) =>
   352             (case (merge_pattern (x, y), merge_patterns xs ys) of
   353               (SOME x, SOME xs) => SOME (x :: xs)
   354               | _ => NONE
   355             )
   356           | _ => raise Match
   357         fun merge_insert ((lhs1, case_pat), _) [] =
   358             [(lhs1, pattern_empty |> pattern_insert ctr_count [case_pat])]
   359           | merge_insert ((lhs1, case_pat), rhs) ((lhs2, pat) :: pats) =
   360             let
   361               val pats = merge_insert ((lhs1, case_pat), rhs) pats
   362               val (first_equ_needed, new_lhs) = case merge_patterns lhs1 lhs2 of
   363                 SOME new_lhs => (not (pattern_contains [case_pat] pat), new_lhs)
   364                 | NONE => (false, lhs2)
   365               val second_equ_needed = not (matches_all lhs1 lhs2)
   366                 orelse not first_equ_needed
   367               val first_equ = if first_equ_needed
   368                 then [(new_lhs, pattern_insert ctr_count [case_pat] pat)]
   369                 else []
   370               val second_equ = if second_equ_needed
   371                 then [(lhs2, pat)]
   372                 else []
   373             in
   374               first_equ @ second_equ @ pats
   375             end
   376         in
   377           (fold merge_insert equs []
   378             |> split_list
   379             |> fst) @ [default_lhs]
   380         end
   381     val lhss2 = split ((lhss1 ~~ lazy_pats) ~~ rhss)
   382 
   383     (* Step 3 : Remove redundant patterns *)
   384     fun remove_redundant_lhs lhss =
   385       let
   386         fun f lhs pat = if pattern_contains lhs pat
   387           then ((lhs, false), pat)
   388           else ((lhs, true), pattern_insert ctr_count lhs pat)
   389       in
   390         fold_map f lhss pattern_empty
   391         |> fst
   392         |> filter snd
   393         |> map fst
   394       end
   395     fun remove_redundant_rhs rhss =
   396       let
   397         fun f (lhs, rhs) pat = if pattern_contains [lhs] pat
   398           then (((lhs, rhs), false), pat)
   399           else (((lhs, rhs), true), pattern_insert ctr_count [lhs] pat)
   400       in
   401         map fst (filter snd (fold_map f rhss pattern_empty |> fst))
   402       end
   403     val lhss3 = remove_redundant_lhs lhss2
   404 
   405     (* Step 4 : Compute right hand side *)
   406     fun subs_fun f = fold_term
   407       Const
   408       (f o Free)
   409       Var
   410       Bound
   411       Abs
   412       (fn (x, y) => f x $ f y)
   413     fun find_rhss lhs =
   414       let
   415         fun f (lhs1, (pat, rhs)) = 
   416           case match_all lhs1 lhs of NONE => NONE
   417           | SOME f => SOME (pat, subs_fun f rhs)
   418       in
   419         remove_redundant_rhs
   420           (map_filter f (lhss1 ~~ (lazy_pats ~~ rhss)) @
   421             [(lazy_case_arg, list_comb (fun_t, lhs) |> abort)]
   422           )
   423       end
   424 
   425     (* Step 5 : make_case of right hand side *)
   426     fun make_case ctxt case_arg cases = case cases of
   427       [(Free x, rhs)] => subs_fun (fn y => if y = Free x then case_arg else y) rhs
   428       | _ => Case_Translation.make_case
   429         ctxt
   430         Case_Translation.Warning
   431         Name.context
   432         case_arg
   433         cases
   434     val type_name_fun = add_type_names lazy_pats o type_name_fun
   435     val rhss3 = map ((make_case ctxt lazy_case_arg) o find_rhss) lhss3
   436   in
   437     (lhss3, rhss3, type_name_fun)
   438   end;
   439 
   440 fun terms_to_case ctxt ctr_count (head : term) (lhss : term list list)
   441     (rhss : term list) (typ_list : typ list) (poss : (int * (string * int) list) list) =
   442   let
   443     val (lhss1, rhss1, def_frees, case_args, ctxt1) = replace_frees lhss rhss typ_list ctxt
   444     val exec_list = poss ~~ def_frees
   445     val (lhss2, rhss2, type_name_fun) = fold_rev
   446       (terms_to_case_at ctr_count ctxt1 head case_args) exec_list (lhss1, rhss1, I)
   447     fun make_eq_term (lhss, rhs) = (list_comb (head, lhss), rhs)
   448       |> HOLogic.mk_eq
   449       |> HOLogic.mk_Trueprop
   450   in
   451     (map make_eq_term (lhss2 ~~ rhss2),
   452       get_split_theorems ctxt1 (type_name_fun Symtab.empty),
   453       ctxt1)
   454   end;
   455 
   456 fun build_case_t replace_ctr ctr_count head lhss rhss ctxt =
   457   let
   458     val num_eqs = length lhss
   459     val _ = if length rhss = num_eqs andalso num_eqs > 0 then ()
   460       else raise Fail
   461         ("expected same number of left-hand sides as right-hand sides\n"
   462           ^ "and at least one equation")
   463     val n = length (hd lhss)
   464     val _ = if forall (fn m => length m = n) lhss then ()
   465       else raise Fail "expected equal number of arguments"
   466 
   467     fun to_coordinates (n, ts) = case map_filter (term_to_coordinates replace_ctr) ts of
   468         [] => NONE
   469       | (tco :: tcos) => SOME (n, fold term_coordinate_merge tcos tco |> coordinates_to_list)
   470     fun add_T (n, xss) = map (fn (T, xs) => (T, (n, xs))) xss
   471     val (typ_list, poss) = lhss
   472       |> Ctr_Sugar_Util.transpose
   473       |> map_index to_coordinates
   474       |> map_filter (Option.map add_T)
   475       |> flat
   476       |> split_list 
   477   in
   478     if null poss then ([], [], ctxt)
   479     else terms_to_case ctxt (dest_Const #> ctr_count) head lhss rhss typ_list poss
   480   end;
   481 
   482 fun tac ctxt {splits, intros, defs} =
   483   let
   484     val split_and_subst = 
   485       split_tac ctxt splits 
   486       THEN' REPEAT_ALL_NEW (
   487         resolve_tac ctxt [@{thm conjI}, @{thm allI}]
   488         ORELSE'
   489         (resolve_tac ctxt [@{thm impI}] THEN' hyp_subst_tac_thin true ctxt))
   490   in
   491     (REPEAT_ALL_NEW split_and_subst ORELSE' K all_tac)
   492     THEN' (K (Local_Defs.unfold_tac ctxt [@{thm missing_pattern_match_def}]))
   493     THEN' (K (Local_Defs.unfold_tac ctxt defs))
   494     THEN_ALL_NEW (SOLVED' (resolve_tac ctxt (@{thm refl} :: intros)))
   495   end;
   496 
   497 fun to_case _ _ _ [] = NONE
   498   | to_case ctxt replace_ctr ctr_count ths =
   499     let
   500       val strip_eq = Thm.prop_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq
   501       fun import [] ctxt = ([], ctxt)
   502         | import (thm :: thms) ctxt =
   503           let
   504             val fun_ct = strip_eq #> fst #> head_of #> Logic.mk_term #> Thm.cterm_of ctxt
   505             val ct = fun_ct thm
   506             val cts = map fun_ct thms
   507             val pairs = map (fn s => (s,ct)) cts
   508             val thms' = map (fn (th,p) => Thm.instantiate (Thm.match p) th) (thms ~~ pairs)
   509           in
   510             Variable.import true (thm :: thms') ctxt |> apfst snd
   511           end
   512 
   513       val (iths, ctxt') = import ths ctxt
   514       val head = hd iths |> strip_eq |> fst |> head_of
   515       val eqs = map (strip_eq #> apfst (snd o strip_comb)) iths
   516 
   517       fun hide_rhs ((pat, rhs), name) lthy =
   518         let
   519           val frees = fold Term.add_frees pat []
   520           val abs_rhs = fold absfree frees rhs
   521           val (f, def, lthy') = case lthy
   522             |> Local_Defs.define [((Binding.name name, NoSyn), (Binding.empty_atts, abs_rhs))] of
   523               ([(f, (_, def))], lthy') => (f, def, lthy')
   524               | _ => raise Match
   525         in
   526           ((list_comb (f, map Free (rev frees)), def), lthy')
   527         end
   528 
   529       val rhs_names = Name.invent (Variable.names_of ctxt') "rhs" (length eqs)
   530       val ((def_ts, def_thms), ctxt2) =
   531         fold_map hide_rhs (eqs ~~ rhs_names) ctxt' |> apfst split_list
   532       val (ts, split_thms, ctxt3) = build_case_t replace_ctr ctr_count head
   533         (map fst eqs) def_ts ctxt2
   534       fun mk_thm t = Goal.prove ctxt3 [] [] t
   535           (fn {context=ctxt, ...} => tac ctxt {splits=split_thms, intros=ths, defs=def_thms} 1)
   536     in
   537       if null ts then NONE
   538       else
   539         ts
   540         |> map mk_thm
   541         |> Proof_Context.export ctxt3 ctxt
   542         |> map (Goal.norm_result ctxt)
   543         |> SOME
   544     end;
   545 
   546 end