481 | mk_tagged_type (false, T) = T; |
481 | mk_tagged_type (false, T) = T; |
482 |
482 |
483 fun dest_tagged_type (Type ("", [T])) = (true, T) |
483 fun dest_tagged_type (Type ("", [T])) = (true, T) |
484 | dest_tagged_type T = (false, T); |
484 | dest_tagged_type T = (false, T); |
485 |
485 |
486 val untag_term = map_types (snd o dest_tagged_type); |
486 val fastype_of_tagged_term = fastype_of o map_types (snd o dest_tagged_type); |
487 |
487 |
488 fun tag_term (proj_sort, _) eqngr = |
488 fun tag_term (proj_sort, _) eqngr = |
489 let |
489 let |
490 val has_sort_constraints = exists (not o null) o map proj_sort o Code_Preproc.sortargs eqngr; |
490 val has_sort_constraints = exists (not o null) o map proj_sort o Code_Preproc.sortargs eqngr; |
491 fun tag (Const (_, T')) (Const (c, T)) = |
491 fun tag (Const (_, T')) (Const (c, T)) = |
720 end |
720 end |
721 and translate_app_const ctxt algbr eqngr permissive some_thm ((c_ty, ts), some_abs) = |
721 and translate_app_const ctxt algbr eqngr permissive some_thm ((c_ty, ts), some_abs) = |
722 translate_const ctxt algbr eqngr permissive some_thm (c_ty, some_abs) |
722 translate_const ctxt algbr eqngr permissive some_thm (c_ty, some_abs) |
723 ##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) ts |
723 ##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) ts |
724 #>> (fn (t, ts) => t `$$ ts) |
724 #>> (fn (t, ts) => t `$$ ts) |
725 and translate_constr ctxt algbr eqngr permissive some_thm ty_case (c, t) = |
|
726 let |
|
727 val n = Code.args_number (Proof_Context.theory_of ctxt) c; |
|
728 val ty = (untag_term #> fastype_of #> binder_types #> take n) t ---> ty_case; |
|
729 in |
|
730 translate_const ctxt algbr eqngr permissive some_thm ((c, ty), NONE) |
|
731 #>> rpair n |
|
732 end |
|
733 and translate_case ctxt algbr eqngr permissive some_thm (t_pos, []) (c_ty, ts) = |
725 and translate_case ctxt algbr eqngr permissive some_thm (t_pos, []) (c_ty, ts) = |
734 let |
726 let |
735 fun project_term xs = nth xs t_pos; |
727 fun project_term xs = nth xs t_pos; |
736 val project_clause = the_single o nth_drop t_pos; |
728 val project_clause = the_single o nth_drop t_pos; |
737 val ty_case = project_term (binder_types (snd c_ty)); |
729 val ty_case = project_term (binder_types (snd c_ty)); |
753 xs |
745 xs |
754 |> nth_drop t_pos |
746 |> nth_drop t_pos |
755 |> curry (op ~~) case_pats |
747 |> curry (op ~~) case_pats |
756 |> map_filter (fn (NONE, _) => NONE | (SOME _, x) => SOME x); |
748 |> map_filter (fn (NONE, _) => NONE | (SOME _, x) => SOME x); |
757 val ty_case = project_term (binder_types (snd c_ty)); |
749 val ty_case = project_term (binder_types (snd c_ty)); |
758 val constrs = map_filter I case_pats ~~ project_cases ts; |
750 val constrs = map_filter I case_pats ~~ project_cases ts |
|
751 |> map (fn ((c, n), t) => |
|
752 ((c, (take n o binder_types o fastype_of_tagged_term) t ---> ty_case), n)); |
759 fun distill_clauses constrs ts_clause = |
753 fun distill_clauses constrs ts_clause = |
760 maps (fn ((constr as IConst { dom = tys, ... }, n), t) => |
754 maps (fn ((constr as IConst { dom = tys, ... }, n), t) => |
761 map (fn (pat_args, body) => (constr `$$ pat_args, body)) |
755 map (fn (pat_args, body) => (constr `$$ pat_args, body)) |
762 (distill_minimized_clause (take n tys) t)) |
756 (distill_minimized_clause (take n tys) t)) |
763 (constrs ~~ ts_clause); |
757 (constrs ~~ ts_clause); |
764 in |
758 in |
765 translate_const ctxt algbr eqngr permissive some_thm (c_ty, NONE) |
759 translate_const ctxt algbr eqngr permissive some_thm (c_ty, NONE) |
766 ##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) ts |
760 ##>> fold_map (translate_term ctxt algbr eqngr permissive some_thm o rpair NONE) ts |
767 ##>> translate_typ ctxt algbr eqngr permissive ty_case |
761 ##>> translate_typ ctxt algbr eqngr permissive ty_case |
768 ##>> fold_map (translate_constr ctxt algbr eqngr permissive some_thm ty_case) constrs |
762 ##>> fold_map (fn (c_ty, n) => |
|
763 translate_const ctxt algbr eqngr permissive some_thm (c_ty, NONE) #>> rpair n) constrs |
769 #>> (fn (((t_app, ts), ty_case), constrs) => |
764 #>> (fn (((t_app, ts), ty_case), constrs) => |
770 ICase { term = project_term ts, typ = ty_case, |
765 ICase { term = project_term ts, typ = ty_case, |
771 clauses = (filter_out (is_undefined_clause ctxt) o distill_clauses constrs o project_cases) ts, |
766 clauses = (filter_out (is_undefined_clause ctxt) o distill_clauses constrs o project_cases) ts, |
772 primitive = t_app `$$ ts }) |
767 primitive = t_app `$$ ts }) |
773 end |
768 end |