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