src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author hoelzl
Thu Jan 07 17:43:35 2010 +0100 (2010-01-07)
changeset 34290 1edf0f223c6e
parent 34289 c9c14c72d035
child 34291 4e896680897e
permissions -rw-r--r--
added syntax translation to automatically add finite typeclass to index type of cartesian product type
himmelma@33175
     1
(* Title:      HOL/Library/Finite_Cartesian_Product
himmelma@33175
     2
   Author:     Amine Chaieb, University of Cambridge
himmelma@33175
     3
*)
himmelma@33175
     4
himmelma@33175
     5
header {* Definition of finite Cartesian product types. *}
himmelma@33175
     6
himmelma@33175
     7
theory Finite_Cartesian_Product
himmelma@33175
     8
imports Main (*FIXME: ATP_Linkup is only needed for metis at a few places. We could dispense of that by changing the proofs.*)
himmelma@33175
     9
begin
himmelma@33175
    10
himmelma@33175
    11
subsection {* Finite Cartesian products, with indexing and lambdas. *}
himmelma@33175
    12
himmelma@33175
    13
typedef (open Cart)
himmelma@33175
    14
  ('a, 'b) "^" (infixl "^" 15)
himmelma@34289
    15
    = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
himmelma@33175
    16
  morphisms Cart_nth Cart_lambda ..
himmelma@33175
    17
himmelma@33175
    18
notation Cart_nth (infixl "$" 90)
himmelma@33175
    19
himmelma@33175
    20
notation (xsymbols) Cart_lambda (binder "\<chi>" 10)
himmelma@33175
    21
hoelzl@34290
    22
(*
hoelzl@34290
    23
  Translate "'b ^ 'n" into "'b ^ ('n :: finite)". When 'n needs more than one
hoelzl@34290
    24
  type class write "cart 'b ('n::{finite, ...})"
hoelzl@34290
    25
*)
hoelzl@34290
    26
hoelzl@34290
    27
syntax "_finite_cart" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
hoelzl@34290
    28
hoelzl@34290
    29
parse_translation {*
hoelzl@34290
    30
let
hoelzl@34290
    31
  fun cart t u = Syntax.const @{type_name cart} $ t $ u
hoelzl@34290
    32
  fun finite_cart_tr [t, u as Free (x, _)] =
hoelzl@34290
    33
        if Syntax.is_tid x
hoelzl@34290
    34
        then cart t (Syntax.const "_ofsort" $ u $ Syntax.const (hd @{sort finite}))
hoelzl@34290
    35
        else cart t u
hoelzl@34290
    36
    | finite_cart_tr [t, u] = cart t u
hoelzl@34290
    37
in
hoelzl@34290
    38
  [("_finite_cart", finite_cart_tr)]
hoelzl@34290
    39
end
hoelzl@34290
    40
*}
hoelzl@34290
    41
himmelma@33175
    42
lemma stupid_ext: "(\<forall>x. f x = g x) \<longleftrightarrow> (f = g)"
himmelma@33175
    43
  apply auto
himmelma@33175
    44
  apply (rule ext)
himmelma@33175
    45
  apply auto
himmelma@33175
    46
  done
himmelma@33175
    47
himmelma@34289
    48
lemma Cart_eq: "((x:: 'a ^ 'b::finite) = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
himmelma@33175
    49
  by (simp add: Cart_nth_inject [symmetric] expand_fun_eq)
himmelma@33175
    50
himmelma@33175
    51
lemma Cart_lambda_beta [simp]: "Cart_lambda g $ i = g i"
himmelma@33175
    52
  by (simp add: Cart_lambda_inverse)
himmelma@33175
    53
himmelma@33175
    54
lemma Cart_lambda_unique:
himmelma@34289
    55
  fixes f :: "'a ^ 'b::finite"
himmelma@33175
    56
  shows "(\<forall>i. f$i = g i) \<longleftrightarrow> Cart_lambda g = f"
himmelma@33175
    57
  by (auto simp add: Cart_eq)
himmelma@33175
    58
himmelma@33175
    59
lemma Cart_lambda_eta: "(\<chi> i. (g$i)) = g"
himmelma@33175
    60
  by (simp add: Cart_eq)
himmelma@33175
    61
himmelma@33175
    62
text{* A non-standard sum to "paste" Cartesian products. *}
himmelma@33175
    63
himmelma@34289
    64
definition pastecart :: "'a ^ 'm::finite \<Rightarrow> 'a ^ 'n::finite \<Rightarrow> 'a ^ ('m + 'n)" where
himmelma@33175
    65
  "pastecart f g = (\<chi> i. case i of Inl a \<Rightarrow> f$a | Inr b \<Rightarrow> g$b)"
himmelma@33175
    66
himmelma@34289
    67
definition fstcart:: "'a ^('m::finite + 'n::finite) \<Rightarrow> 'a ^ 'm" where
himmelma@33175
    68
  "fstcart f = (\<chi> i. (f$(Inl i)))"
himmelma@33175
    69
himmelma@34289
    70
definition sndcart:: "'a ^('m::finite + 'n::finite) \<Rightarrow> 'a ^ 'n" where
himmelma@33175
    71
  "sndcart f = (\<chi> i. (f$(Inr i)))"
himmelma@33175
    72
himmelma@33175
    73
lemma nth_pastecart_Inl [simp]: "pastecart f g $ Inl a = f$a"
himmelma@33175
    74
  unfolding pastecart_def by simp
himmelma@33175
    75
himmelma@33175
    76
lemma nth_pastecart_Inr [simp]: "pastecart f g $ Inr b = g$b"
himmelma@33175
    77
  unfolding pastecart_def by simp
himmelma@33175
    78
himmelma@33175
    79
lemma nth_fstcart [simp]: "fstcart f $ i = f $ Inl i"
himmelma@33175
    80
  unfolding fstcart_def by simp
himmelma@33175
    81
himmelma@33175
    82
lemma nth_sndtcart [simp]: "sndcart f $ i = f $ Inr i"
himmelma@33175
    83
  unfolding sndcart_def by simp
himmelma@33175
    84
himmelma@33175
    85
lemma finite_sum_image: "(UNIV::('a + 'b) set) = range Inl \<union> range Inr"
himmelma@33175
    86
by (auto, case_tac x, auto)
himmelma@33175
    87
himmelma@34289
    88
lemma fstcart_pastecart: "fstcart (pastecart (x::'a ^'m::finite ) (y:: 'a ^ 'n::finite)) = x"
himmelma@33175
    89
  by (simp add: Cart_eq)
himmelma@33175
    90
himmelma@34289
    91
lemma sndcart_pastecart: "sndcart (pastecart (x::'a ^'m::finite ) (y:: 'a ^ 'n::finite)) = y"
himmelma@33175
    92
  by (simp add: Cart_eq)
himmelma@33175
    93
himmelma@33175
    94
lemma pastecart_fst_snd: "pastecart (fstcart z) (sndcart z) = z"
himmelma@33175
    95
  by (simp add: Cart_eq pastecart_def fstcart_def sndcart_def split: sum.split)
himmelma@33175
    96
himmelma@33175
    97
lemma pastecart_eq: "(x = y) \<longleftrightarrow> (fstcart x = fstcart y) \<and> (sndcart x = sndcart y)"
himmelma@33175
    98
  using pastecart_fst_snd[of x] pastecart_fst_snd[of y] by metis
himmelma@33175
    99
himmelma@33175
   100
lemma forall_pastecart: "(\<forall>p. P p) \<longleftrightarrow> (\<forall>x y. P (pastecart x y))"
himmelma@33175
   101
  by (metis pastecart_fst_snd fstcart_pastecart sndcart_pastecart)
himmelma@33175
   102
himmelma@33175
   103
lemma exists_pastecart: "(\<exists>p. P p)  \<longleftrightarrow> (\<exists>x y. P (pastecart x y))"
himmelma@33175
   104
  by (metis pastecart_fst_snd fstcart_pastecart sndcart_pastecart)
himmelma@33175
   105
himmelma@33175
   106
end