src/HOL/Library/Types_To_Sets/internalize_sort.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:      internalize_sort.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 a derived rule (by using Thm.unconstrainT) that internalizes
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     5
    type class annotations.
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     6
*)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     7
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     8
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
     9
(*
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    10
                     \<phi>['a::{c_1, ..., c_n} / 'a]
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    11
---------------------------------------------------------------------
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    12
  class.c_1 ops_1 \<Longrightarrow> ... \<Longrightarrow> class.c_n ops_n \<Longrightarrow> \<phi>['a::type / 'a]
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    13
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    14
where class.c is the locale predicate associated with type class c and
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    15
ops are operations associated with type class c. For example:
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    16
If c = semigroup_add, then ops = op-, op+, 0, uminus.
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    17
If c = finite, then ops = TYPE('a::type).
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    18
*)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    19
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    20
signature INTERNALIZE_SORT =
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    21
sig
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    22
  val internalize_sort:  ctyp -> thm -> typ * thm
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    23
  val internalize_sort_attr: typ -> attribute
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    24
end;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    25
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    26
structure Internalize_Sort : INTERNALIZE_SORT =
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    27
struct
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    28
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    29
fun internalize_sort ctvar thm =
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    30
  let
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    31
    val thy = Thm.theory_of_thm thm;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    32
    val tvar = Thm.typ_of ctvar;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    33
    
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    34
    val ((_, assms, classes),_) = Logic.unconstrainT [] (Thm.prop_of thm);
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    35
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    36
    fun is_proper_class thy = can (Axclass.get_info thy); (* trick by FH *)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    37
    fun reduce_to_non_proper_sort (TVar (name, sort)) = 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    38
        TVar (name, Sign.minimize_sort thy (filter_out (is_proper_class thy) (Sign.complete_sort thy sort)))
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    39
      | reduce_to_non_proper_sort _ = error "not supported";
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    40
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    41
    val data = (map fst classes) ~~ assms;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    42
    
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    43
    val new_tvar = get_first (fn (tvar', ((ren_tvar, _), _)) => if tvar = tvar' 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    44
      then SOME (reduce_to_non_proper_sort ren_tvar) else NONE) data
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    45
      |> the_default tvar;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    46
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    47
    fun localify class = Class.rules thy class |> snd |> Thm.transfer thy;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    48
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    49
    fun discharge_of_class tvar class = Thm.of_class (Thm.global_ctyp_of thy tvar, class);
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    50
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    51
    val rules = map (fn (tvar', ((ren_tvar, class), _)) => if tvar = tvar' 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    52
      then (if Class.is_class thy class then localify class else discharge_of_class ren_tvar class)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    53
      else discharge_of_class ren_tvar class) data;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    54
  in
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    55
    (new_tvar, (Thm.unconstrainT (Thm.strip_shyps thm) OF rules) |> Drule.zero_var_indexes)
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    56
  end;
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    57
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    58
val tvar = Args.context -- Args.typ >> (fn (_, v as TFree _) => Logic.varifyT_global v 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    59
  | (ctxt, t) => error ("Not a type variable: " ^ Syntax.string_of_typ ctxt t));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    60
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    61
fun internalize_sort_attr tvar = 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    62
  Thm.rule_attribute [] (fn context => fn thm => 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    63
    (snd (internalize_sort (Thm.ctyp_of (Context.proof_of context) tvar) thm)));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    64
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    65
val _ = Context.>> (Context.map_theory (Attrib.setup @{binding internalize_sort} 
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    66
  (tvar >> internalize_sort_attr) "internalize a sort"));
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    67
ae53f4d901a3 types to sets: initial commit
kuncar
parents:
diff changeset
    68
end;