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