src/HOL/Typerep.thy
author wenzelm
Mon Mar 16 18:24:30 2009 +0100 (2009-03-16)
changeset 30549 d2d7874648bd
parent 29574 5897b2688ccc
child 31031 cbec39ebf8f2
permissions -rw-r--r--
simplified method setup;
haftmann@29574
     1
(*  Title:      HOL/Typerep.thy
haftmann@26168
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@26168
     3
*)
haftmann@26168
     4
haftmann@26168
     5
header {* Reflecting Pure types into HOL *}
haftmann@26168
     6
haftmann@28952
     7
theory Typerep
haftmann@28952
     8
imports Plain List Code_Message
haftmann@26168
     9
begin
haftmann@26168
    10
haftmann@28335
    11
datatype typerep = Typerep message_string "typerep list"
haftmann@26168
    12
haftmann@28335
    13
class typerep =
haftmann@28335
    14
  fixes typerep :: "'a\<Colon>{} itself \<Rightarrow> typerep"
haftmann@26168
    15
begin
haftmann@26168
    16
haftmann@29574
    17
definition typerep_of :: "'a \<Rightarrow> typerep" where
haftmann@28335
    18
  [simp]: "typerep_of x = typerep TYPE('a)"
haftmann@26168
    19
haftmann@26168
    20
end
haftmann@26168
    21
haftmann@26168
    22
setup {*
haftmann@26168
    23
let
haftmann@28335
    24
  fun typerep_tr (*"_TYPEREP"*) [ty] =
haftmann@28335
    25
        Lexicon.const @{const_syntax typerep} $ (Lexicon.const "_constrain" $ Lexicon.const "TYPE" $
haftmann@26168
    26
          (Lexicon.const "itself" $ ty))
haftmann@28335
    27
    | typerep_tr (*"_TYPEREP"*) ts = raise TERM ("typerep_tr", ts);
haftmann@28335
    28
  fun typerep_tr' show_sorts (*"typerep"*)
haftmann@26168
    29
          (Type ("fun", [Type ("itself", [T]), _])) (Const (@{const_syntax TYPE}, _) :: ts) =
haftmann@28335
    30
        Term.list_comb (Lexicon.const "_TYPEREP" $ Syntax.term_of_typ show_sorts T, ts)
haftmann@28335
    31
    | typerep_tr' _ T ts = raise Match;
haftmann@26168
    32
in
haftmann@26168
    33
  Sign.add_syntax_i
haftmann@28335
    34
    [("_TYPEREP", SimpleSyntax.read_typ "type => logic", Delimfix "(1TYPEREP/(1'(_')))")]
haftmann@28335
    35
  #> Sign.add_trfuns ([], [("_TYPEREP", typerep_tr)], [], [])
haftmann@28335
    36
  #> Sign.add_trfunsT [(@{const_syntax typerep}, typerep_tr')]
haftmann@26168
    37
end
haftmann@26168
    38
*}
haftmann@26168
    39
haftmann@26168
    40
ML {*
haftmann@28335
    41
structure Typerep =
haftmann@26168
    42
struct
haftmann@26168
    43
haftmann@26168
    44
fun mk f (Type (tyco, tys)) =
haftmann@28335
    45
      @{term Typerep} $ Message_String.mk tyco
haftmann@28335
    46
        $ HOLogic.mk_list @{typ typerep} (map (mk f) tys)
haftmann@26168
    47
  | mk f (TFree v) =
haftmann@26168
    48
      f v;
haftmann@26168
    49
haftmann@28335
    50
fun typerep ty =
haftmann@28335
    51
  Const (@{const_name typerep}, Term.itselfT ty --> @{typ typerep})
haftmann@26168
    52
    $ Logic.mk_type ty;
haftmann@26168
    53
haftmann@26168
    54
fun add_def tyco thy =
haftmann@26168
    55
  let
haftmann@28335
    56
    val sorts = replicate (Sign.arity_number thy tyco) @{sort typerep};
haftmann@26168
    57
    val vs = Name.names Name.context "'a" sorts;
haftmann@26168
    58
    val ty = Type (tyco, map TFree vs);
haftmann@28335
    59
    val lhs = Const (@{const_name typerep}, Term.itselfT ty --> @{typ typerep})
haftmann@26168
    60
      $ Free ("T", Term.itselfT ty);
haftmann@28335
    61
    val rhs = mk (typerep o TFree) ty;
haftmann@26168
    62
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@26168
    63
  in
haftmann@26168
    64
    thy
haftmann@28335
    65
    |> TheoryTarget.instantiation ([tyco], vs, @{sort typerep})
haftmann@26168
    66
    |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@28965
    67
    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
haftmann@26168
    68
    |> snd
haftmann@26168
    69
    |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
wenzelm@28394
    70
    |> LocalTheory.exit_global
haftmann@26168
    71
  end;
haftmann@26168
    72
haftmann@26168
    73
fun perhaps_add_def tyco thy =
haftmann@26168
    74
  let
haftmann@28335
    75
    val inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep}
haftmann@26168
    76
  in if inst then thy else add_def tyco thy end;
haftmann@26168
    77
haftmann@26168
    78
end;
haftmann@26168
    79
*}
haftmann@26168
    80
haftmann@26168
    81
setup {*
haftmann@28335
    82
  Typerep.add_def @{type_name prop}
haftmann@28335
    83
  #> Typerep.add_def @{type_name fun}
haftmann@28335
    84
  #> Typerep.add_def @{type_name itself}
haftmann@28335
    85
  #> Typerep.add_def @{type_name bool}
haftmann@28335
    86
  #> TypedefPackage.interpretation Typerep.perhaps_add_def
haftmann@26168
    87
*}
haftmann@26168
    88
haftmann@28562
    89
lemma [code]:
haftmann@28346
    90
  "eq_class.eq (Typerep tyco1 tys1) (Typerep tyco2 tys2) \<longleftrightarrow> eq_class.eq tyco1 tyco2
haftmann@28346
    91
     \<and> list_all2 eq_class.eq tys1 tys2"
haftmann@28346
    92
  by (auto simp add: equals_eq [symmetric] list_all2_eq [symmetric])
haftmann@26168
    93
haftmann@28335
    94
code_type typerep
haftmann@26168
    95
  (SML "Term.typ")
haftmann@26168
    96
haftmann@28335
    97
code_const Typerep
haftmann@26168
    98
  (SML "Term.Type/ (_, _)")
haftmann@26168
    99
haftmann@26168
   100
code_reserved SML Term
haftmann@26168
   101
haftmann@28335
   102
hide (open) const typerep Typerep
haftmann@26168
   103
haftmann@26168
   104
end