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