src/HOL/Codatatype/Examples/TreeFI.thy
author blanchet
Fri, 21 Sep 2012 16:34:40 +0200
changeset 49509 163914705f8d
parent 49508 1e205327f059
permissions -rw-r--r--
renamed top-level theory from "Codatatype" to "BNF"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49509
163914705f8d renamed top-level theory from "Codatatype" to "BNF"
blanchet
parents: 49508
diff changeset
     1
(*  Title:      HOL/BNF/Examples/TreeFI.thy
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     2
    Author:     Dmitriy Traytel, TU Muenchen
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     3
    Author:     Andrei Popescu, TU Muenchen
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     4
    Copyright   2012
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     5
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     6
Finitely branching possibly infinite trees.
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     7
*)
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     8
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
     9
header {* Finitely Branching Possibly Infinite Trees *}
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    10
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    11
theory TreeFI
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    12
imports ListF
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    13
begin
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    14
49092
5eddc9aaebf1 hide newly introduced constant Sublist.sub to allow for name sub in TreeFsetI
Christian Sternagel
parents: 48975
diff changeset
    15
hide_const (open) Sublist.sub
5eddc9aaebf1 hide newly introduced constant Sublist.sub to allow for name sub in TreeFsetI
Christian Sternagel
parents: 48975
diff changeset
    16
49074
d8af889dcbe3 renamed three BNF/(co)datatype-related commands
blanchet
parents: 48975
diff changeset
    17
codata_raw treeFI: 'tree = "'a \<times> 'tree listF"
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    18
49220
a6260b4fb410 imported patch debugging
blanchet
parents: 49128
diff changeset
    19
lemma pre_treeFI_listF_set[simp]: "pre_treeFI_set2 (i, xs) = listF_set xs"
a6260b4fb410 imported patch debugging
blanchet
parents: 49128
diff changeset
    20
unfolding pre_treeFI_set2_def collect_def[abs_def] prod_set_defs
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    21
by (auto simp add: listF.set_natural')
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    22
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    23
(* selectors for trees *)
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    24
definition "lab tr \<equiv> fst (treeFI_dtor tr)"
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    25
definition "sub tr \<equiv> snd (treeFI_dtor tr)"
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    26
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    27
lemma dtor[simp]: "treeFI_dtor tr = (lab tr, sub tr)"
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    28
unfolding lab_def sub_def by simp
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    29
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    30
definition pair_fun (infixr "\<odot>" 50) where
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    31
  "f \<odot> g \<equiv> \<lambda>x. (f x, g x)"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    32
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    33
lemma unfold_pair_fun_lab: "lab (treeFI_dtor_unfold (f \<odot> g) t) = f t"
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    34
unfolding lab_def pair_fun_def treeFI.dtor_unfolds pre_treeFI_map_def by simp
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    35
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    36
lemma unfold_pair_fun_sub: "sub (treeFI_dtor_unfold (f \<odot> g) t) = listF_map (treeFI_dtor_unfold (f \<odot> g)) (g t)"
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    37
unfolding sub_def pair_fun_def treeFI.dtor_unfolds pre_treeFI_map_def by simp
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    38
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    39
(* Tree reverse:*)
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    40
definition "trev \<equiv> treeFI_dtor_unfold (lab \<odot> lrev o sub)"
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    41
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    42
lemma trev_simps1[simp]: "lab (trev t) = lab t"
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    43
unfolding trev_def by (simp add: unfold_pair_fun_lab)
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    44
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    45
lemma trev_simps2[simp]: "sub (trev t) = listF_map trev (lrev (sub t))"
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    46
unfolding trev_def by (simp add: unfold_pair_fun_sub)
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    47
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    48
lemma treeFI_coinduct:
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    49
assumes *: "phi x y"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    50
and step: "\<And>a b. phi a b \<Longrightarrow>
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    51
   lab a = lab b \<and>
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    52
   lengthh (sub a) = lengthh (sub b) \<and>
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    53
   (\<forall>i < lengthh (sub a). phi (nthh (sub a) i) (nthh (sub b) i))"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    54
shows "x = y"
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    55
proof (rule mp[OF treeFI.dtor_coinduct, of phi, OF _ *])
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    56
  fix a b :: "'a treeFI"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    57
  let ?zs = "zipp (sub a) (sub b)"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    58
  let ?z = "(lab a, ?zs)"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    59
  assume "phi a b"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    60
  with step have step': "lab a = lab b" "lengthh (sub a) = lengthh (sub b)"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    61
    "\<forall>i < lengthh (sub a). phi (nthh (sub a) i) (nthh (sub b) i)" by auto
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    62
  hence "pre_treeFI_map id fst ?z = treeFI_dtor a" "pre_treeFI_map id snd ?z = treeFI_dtor b"
49220
a6260b4fb410 imported patch debugging
blanchet
parents: 49128
diff changeset
    63
    unfolding pre_treeFI_map_def by auto
a6260b4fb410 imported patch debugging
blanchet
parents: 49128
diff changeset
    64
  moreover have "\<forall>(x, y) \<in> pre_treeFI_set2 ?z. phi x y"
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    65
  proof safe
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    66
    fix z1 z2
49220
a6260b4fb410 imported patch debugging
blanchet
parents: 49128
diff changeset
    67
    assume "(z1, z2) \<in> pre_treeFI_set2 ?z"
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    68
    hence "(z1, z2) \<in> listF_set ?zs" by auto
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    69
    hence "\<exists>i < lengthh ?zs. nthh ?zs i = (z1, z2)" by auto
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    70
    with step'(2) obtain i where "i < lengthh (sub a)"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    71
      "nthh (sub a) i = z1" "nthh (sub b) i = z2" by auto
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    72
    with step'(3) show "phi z1 z2" by auto
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    73
  qed
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    74
  ultimately show "\<exists>z.
49508
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    75
    (pre_treeFI_map id fst z = treeFI_dtor a \<and>
1e205327f059 adapted examples to renamings
blanchet
parents: 49463
diff changeset
    76
    pre_treeFI_map id snd z = treeFI_dtor b) \<and>
49220
a6260b4fb410 imported patch debugging
blanchet
parents: 49128
diff changeset
    77
    (\<forall>x y. (x, y) \<in> pre_treeFI_set2 z \<longrightarrow> phi x y)" by blast
48975
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    78
qed
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    79
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    80
lemma trev_trev: "trev (trev tr) = tr"
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    81
by (rule treeFI_coinduct[of "%a b. trev (trev b) = a"]) auto
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    82
7f79f94a432c added new (co)datatype package + theories of ordinals and cardinals (with Dmitriy and Andrei)
blanchet
parents:
diff changeset
    83
end