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