src/HOL/Library/Types_To_Sets/local_typedef.ML
author kuncar
Sun, 30 Oct 2016 13:15:14 +0100
changeset 64431 ae53f4d901a3
permissions -rw-r--r--
types to sets: initial commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64431
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     1
(*  Title:      local_typedef.ML
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     2
    Author:     Ondřej Kunčar, TU München
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     3
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     4
    Implements the Local Typedef Rule and extends the logic by the rule.
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     5
*)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     6
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     7
signature LOCAL_TYPEDEF =
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     8
sig
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     9
  val cancel_type_definition: thm -> thm
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    10
  val cancel_type_definition_attr: attribute
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    11
end;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    12
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    13
structure Local_Typedef : LOCAL_TYPEDEF =
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    14
struct
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    15
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    16
(*
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    17
Local Typedef Rule (LT)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    18
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    19
  \<Gamma> \<turnstile> (\<exists>(Rep::\<beta> \<Rightarrow> \<tau>) Abs. type_definition Rep Abs A) \<Longrightarrow> \<phi>
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    20
------------------------------------------------------------- [\<beta> not in \<phi>, \<Gamma>, A;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    21
                       \<Gamma> \<turnstile> A \<noteq> \<emptyset> \<Longrightarrow> \<phi>                        sort(\<beta>)=HOL.type]
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    22
*)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    23
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    24
(** BEGINNING OF THE CRITICAL CODE **)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    25
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    26
fun dest_typedef (Const (@{const_name Ex}, _) $ Abs (_, _, 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    27
      (Const (@{const_name Ex}, _) $ Abs (_, Abs_type,  
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    28
      (Const (@{const_name type_definition}, _)) $ Bound 1 $ Bound 0 $ set)))) = 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    29
    (Abs_type, set)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    30
   | dest_typedef t = raise TERM ("dest_typedef", [t]);
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    31
  
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    32
fun cancel_type_definition_cterm thm =
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    33
  let
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    34
    fun err msg = raise THM ("cancel_type_definition: " ^ msg, 0, [thm]);
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    35
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    36
    val thy = Thm.theory_of_thm thm;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    37
    val prop = Thm.prop_of thm;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    38
    val hyps = Thm.hyps_of thm;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    39
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    40
    val _ = null (Thm.tpairs_of thm) orelse err "the theorem contains unsolved flex-flex pairs";
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    41
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    42
    val (typedef_assm, phi) = Logic.dest_implies prop
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    43
      handle TERM _ => err "the theorem is not an implication";
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    44
    val (abs_type, set) = typedef_assm |> HOLogic.dest_Trueprop |> dest_typedef
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    45
      handle TERM _ => err ("the assumption is not of the form " 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    46
        ^ quote "\<exists>Rep Abs. type_definition Rep Abs A");
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    47
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    48
    val (repT, absT) = Term.dest_funT abs_type;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    49
    val _ = Term.is_TVar absT orelse err "the abstract type is not a schematic type variable";
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    50
    val (absT_name, sorts) = Term.dest_TVar absT;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    51
    
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    52
    val typeS = Sign.defaultS thy;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    53
    val _ = sorts = typeS orelse err ("the type " ^ quote (fst absT_name) ^ " is not of the sort " 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    54
      ^ quote (Syntax.string_of_sort_global thy typeS));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    55
     
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    56
    fun contains_absT tm = member (op=) (Term.add_tvars tm []) (absT_name, sorts);
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    57
    fun err_contains_absT_in msg = err (msg ^ " contains the forbidden type " 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    58
      ^ quote (fst absT_name));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    59
    val _ = not (contains_absT phi) orelse err_contains_absT_in "the conclusion";
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    60
    val _ = not (contains_absT set) orelse err_contains_absT_in "the set term";
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    61
    (* the following test is superfluous; the meta hypotheses cannot currently contain TVars *)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    62
    val _ = not (exists contains_absT hyps) orelse err_contains_absT_in "one of the hypotheses";
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    63
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    64
    val not_empty_assm = HOLogic.mk_Trueprop
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    65
      (HOLogic.mk_not (HOLogic.mk_eq (set, HOLogic.mk_set repT [])));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    66
    val prop = Logic.list_implies (hyps @ [not_empty_assm], phi);
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    67
  in
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    68
    Thm.global_cterm_of thy prop |> Thm.weaken_sorts (Thm.shyps_of thm)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    69
  end;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    70
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    71
(** END OF THE CRITICAL CODE **)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    72
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    73
val (_, cancel_type_definition_oracle) = Context.>>> (Context.map_theory_result
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    74
  (Thm.add_oracle (@{binding cancel_type_definition}, cancel_type_definition_cterm)));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    75
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    76
fun cancel_type_definition thm =  
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    77
  Drule.implies_elim_list (cancel_type_definition_oracle thm) (map Thm.assume (Thm.chyps_of thm));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    78
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    79
val cancel_type_definition_attr = Thm.rule_attribute [] (K cancel_type_definition);
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    80
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    81
val _ = Context.>> (Context.map_theory (Attrib.setup @{binding cancel_type_definition} 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    82
  (Scan.succeed cancel_type_definition_attr) "cancel a local type definition"));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    83
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    84
end;