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