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