src/HOL/Typerep.thy
author haftmann
Mon Feb 22 11:10:20 2010 +0100 (2010-02-22)
changeset 35299 4f4d5bf4ea08
parent 35115 446c5063e4fd
child 35363 09489d8ffece
permissions -rw-r--r--
proper distinction of code datatypes and abstypes
     1 (* Author: Florian Haftmann, TU Muenchen *)
     2 
     3 header {* Reflecting Pure types into HOL *}
     4 
     5 theory Typerep
     6 imports Plain String
     7 begin
     8 
     9 datatype typerep = Typerep String.literal "typerep list"
    10 
    11 class typerep =
    12   fixes typerep :: "'a itself \<Rightarrow> typerep"
    13 begin
    14 
    15 definition typerep_of :: "'a \<Rightarrow> typerep" where
    16   [simp]: "typerep_of x = typerep TYPE('a)"
    17 
    18 end
    19 
    20 syntax
    21   "_TYPEREP" :: "type => logic"  ("(1TYPEREP/(1'(_')))")
    22 
    23 parse_translation {*
    24 let
    25   fun typerep_tr (*"_TYPEREP"*) [ty] =
    26         Syntax.const @{const_syntax typerep} $
    27           (Syntax.const @{syntax_const "_constrain"} $ Syntax.const @{const_syntax "TYPE"} $
    28             (Syntax.const "itself" $ ty))  (* FIXME @{type_syntax} *)
    29     | typerep_tr (*"_TYPEREP"*) ts = raise TERM ("typerep_tr", ts);
    30 in [(@{syntax_const "_TYPEREP"}, typerep_tr)] end
    31 *}
    32 
    33 typed_print_translation {*
    34 let
    35   fun typerep_tr' show_sorts (*"typerep"*)  (* FIXME @{type_syntax} *)
    36           (Type ("fun", [Type ("itself", [T]), _])) (Const (@{const_syntax TYPE}, _) :: ts) =
    37         Term.list_comb
    38           (Syntax.const @{syntax_const "_TYPEREP"} $ Syntax.term_of_typ show_sorts T, ts)
    39     | typerep_tr' _ T ts = raise Match;
    40 in [(@{const_syntax typerep}, typerep_tr')] end
    41 *}
    42 
    43 setup {*
    44 let
    45 
    46 fun add_typerep tyco thy =
    47   let
    48     val sorts = replicate (Sign.arity_number thy tyco) @{sort typerep};
    49     val vs = Name.names Name.context "'a" sorts;
    50     val ty = Type (tyco, map TFree vs);
    51     val lhs = Const (@{const_name typerep}, Term.itselfT ty --> @{typ typerep})
    52       $ Free ("T", Term.itselfT ty);
    53     val rhs = @{term Typerep} $ HOLogic.mk_literal tyco
    54       $ HOLogic.mk_list @{typ typerep} (map (HOLogic.mk_typerep o TFree) vs);
    55     val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
    56   in
    57     thy
    58     |> Theory_Target.instantiation ([tyco], vs, @{sort typerep})
    59     |> `(fn lthy => Syntax.check_term lthy eq)
    60     |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
    61     |> snd
    62     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
    63   end;
    64 
    65 fun ensure_typerep tyco thy = if not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep})
    66   andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort type}
    67   then add_typerep tyco thy else thy;
    68 
    69 in
    70 
    71 add_typerep @{type_name fun}
    72 #> Typedef.interpretation ensure_typerep
    73 #> Code.datatype_interpretation (ensure_typerep o fst)
    74 #> Code.abstype_interpretation (ensure_typerep o fst)
    75 
    76 end
    77 *}
    78 
    79 lemma [code]:
    80   "eq_class.eq (Typerep tyco1 tys1) (Typerep tyco2 tys2) \<longleftrightarrow> eq_class.eq tyco1 tyco2
    81      \<and> list_all2 eq_class.eq tys1 tys2"
    82   by (auto simp add: equals_eq [symmetric] list_all2_eq [symmetric])
    83 
    84 code_type typerep
    85   (Eval "Term.typ")
    86 
    87 code_const Typerep
    88   (Eval "Term.Type/ (_, _)")
    89 
    90 code_reserved Eval Term
    91 
    92 hide (open) const typerep Typerep
    93 
    94 end