src/HOL/Library/Phantom_Type.thy
author Andreas Lochbihler
Thu, 28 Jun 2012 09:14:57 +0200
changeset 48163 f0ecc1550998
child 51301 6822aa82aafa
permissions -rw-r--r--
add generic phantom type
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48163
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     1
(*  Title:      HOL/Library/Phantom_Type.thy
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     2
    Author:     Andreas Lochbihler
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     3
*)
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     4
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     5
header {* A generic phantom type *}
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     6
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     7
theory Phantom_Type imports "~~/src/HOL/Main" begin
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     8
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
     9
datatype ('a, 'b) phantom = phantom 'b
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    10
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    11
primrec of_phantom :: "('a, 'b) phantom \<Rightarrow> 'b" 
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    12
where "of_phantom (phantom x) = x"
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    13
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    14
lemma of_phantom_phantom [simp]: "phantom (of_phantom x) = x"
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    15
by(cases x) simp
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    16
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    17
lemma type_definition_phantom': "type_definition of_phantom phantom UNIV"
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    18
by(unfold_locales) simp_all
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    19
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    20
setup_lifting (no_code) type_definition_phantom'
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    21
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    22
lemma phantom_comp_of_phantom [simp]: "phantom \<circ> of_phantom = id"
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    23
  and of_phantom_comp_phantom [simp]: "of_phantom \<circ> phantom = id"
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    24
by(simp_all add: o_def id_def)
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    25
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    26
syntax "_Phantom" :: "type \<Rightarrow> logic" ("(1Phantom/(1'(_')))")
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    27
translations
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    28
  "Phantom('t)" => "CONST phantom :: _ \<Rightarrow> ('t, _) phantom"
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    29
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    30
typed_print_translation (advanced) {*
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    31
let
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    32
  fun phantom_tr' ctxt 
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    33
      (Type (@{type_name fun}, [_, Type (@{type_name phantom}, [T, _])])) ts =
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    34
    Term.list_comb (Syntax.const @{syntax_const "_Phantom"} $ Syntax_Phases.term_of_typ ctxt T, ts)
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    35
  | phantom_tr' _ _ _ = raise Match;
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    36
in [(@{const_syntax phantom}, phantom_tr')] end
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    37
*}
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    38
f0ecc1550998 add generic phantom type
Andreas Lochbihler
parents:
diff changeset
    39
end