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