src/HOL/Typerep.thy
author haftmann
Thu Dec 04 14:43:33 2008 +0100 (2008-12-04)
changeset 28965 1de908189869
parent 28952 15a4b2cf8c34
child 29574 5897b2688ccc
permissions -rw-r--r--
cleaned up binding module and related code
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@28952
     8
theory Typerep
haftmann@28952
     9
imports Plain List 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)
haftmann@28965
    70
    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
haftmann@26168
    71
    |> snd
haftmann@26168
    72
    |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
wenzelm@28394
    73
    |> LocalTheory.exit_global
haftmann@26168
    74
  end;
haftmann@26168
    75
haftmann@26168
    76
fun perhaps_add_def tyco thy =
haftmann@26168
    77
  let
haftmann@28335
    78
    val inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep}
haftmann@26168
    79
  in if inst then thy else add_def tyco thy end;
haftmann@26168
    80
haftmann@26168
    81
end;
haftmann@26168
    82
*}
haftmann@26168
    83
haftmann@26168
    84
setup {*
haftmann@28335
    85
  Typerep.add_def @{type_name prop}
haftmann@28335
    86
  #> Typerep.add_def @{type_name fun}
haftmann@28335
    87
  #> Typerep.add_def @{type_name itself}
haftmann@28335
    88
  #> Typerep.add_def @{type_name bool}
haftmann@28335
    89
  #> TypedefPackage.interpretation Typerep.perhaps_add_def
haftmann@26168
    90
*}
haftmann@26168
    91
haftmann@28562
    92
lemma [code]:
haftmann@28346
    93
  "eq_class.eq (Typerep tyco1 tys1) (Typerep tyco2 tys2) \<longleftrightarrow> eq_class.eq tyco1 tyco2
haftmann@28346
    94
     \<and> list_all2 eq_class.eq tys1 tys2"
haftmann@28346
    95
  by (auto simp add: equals_eq [symmetric] list_all2_eq [symmetric])
haftmann@26168
    96
haftmann@28335
    97
code_type typerep
haftmann@26168
    98
  (SML "Term.typ")
haftmann@26168
    99
haftmann@28335
   100
code_const Typerep
haftmann@26168
   101
  (SML "Term.Type/ (_, _)")
haftmann@26168
   102
haftmann@26168
   103
code_reserved SML Term
haftmann@26168
   104
haftmann@28335
   105
hide (open) const typerep Typerep
haftmann@26168
   106
haftmann@26168
   107
end