src/HOL/Datatype_Examples/Misc_Primrec.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 58309 a09ec6daaa19
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
blanchet@58309
     1
(*  Title:      HOL/Datatype_Examples/Misc_Primrec.thy
blanchet@53303
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@53303
     3
    Copyright   2013
blanchet@53303
     4
blanchet@53303
     5
Miscellaneous primitive recursive function definitions.
blanchet@53303
     6
*)
blanchet@53303
     7
blanchet@53303
     8
header {* Miscellaneous Primitive Recursive Function Definitions *}
blanchet@53303
     9
blanchet@53303
    10
theory Misc_Primrec
blanchet@53304
    11
imports Misc_Datatype
blanchet@53303
    12
begin
blanchet@53303
    13
blanchet@55530
    14
primrec nat_of_simple :: "simple \<Rightarrow> nat" where
blanchet@53303
    15
  "nat_of_simple X1 = 1" |
blanchet@53303
    16
  "nat_of_simple X2 = 2" |
blanchet@54193
    17
  "nat_of_simple X3 = 3" |
blanchet@53303
    18
  "nat_of_simple X4 = 4"
blanchet@53303
    19
blanchet@55530
    20
primrec simple_of_simple' :: "simple' \<Rightarrow> simple" where
blanchet@53303
    21
  "simple_of_simple' (X1' _) = X1" |
blanchet@53303
    22
  "simple_of_simple' (X2' _) = X2" |
blanchet@53303
    23
  "simple_of_simple' (X3' _) = X3" |
blanchet@53303
    24
  "simple_of_simple' (X4' _) = X4"
blanchet@53303
    25
blanchet@55530
    26
primrec inc_simple'' :: "nat \<Rightarrow> simple'' \<Rightarrow> simple''" where
blanchet@53303
    27
  "inc_simple'' k (X1'' n i) = X1'' (n + k) (i + int k)" |
blanchet@53303
    28
  "inc_simple'' _ X2'' = X2''"
blanchet@53303
    29
blanchet@55530
    30
primrec myapp :: "'a mylist \<Rightarrow> 'a mylist \<Rightarrow> 'a mylist" where
blanchet@53303
    31
  "myapp MyNil ys = ys" |
blanchet@53303
    32
  "myapp (MyCons x xs) ys = MyCons x (myapp xs ys)"
blanchet@53303
    33
blanchet@55530
    34
primrec myrev :: "'a mylist \<Rightarrow> 'a mylist" where
blanchet@53303
    35
  "myrev MyNil = MyNil" |
blanchet@53303
    36
  "myrev (MyCons x xs) = myapp (myrev xs) (MyCons x MyNil)"
blanchet@53303
    37
blanchet@58231
    38
primrec shuffle_sp :: "('a \<Colon> ord, 'b \<Colon> ord, 'c, 'd) some_passive \<Rightarrow> ('d, 'a, 'b, 'c) some_passive" where
blanchet@53303
    39
  "shuffle_sp (SP1 sp) = SP1 (shuffle_sp sp)" |
blanchet@53303
    40
  "shuffle_sp (SP2 a) = SP3 a" |
blanchet@53303
    41
  "shuffle_sp (SP3 b) = SP4 b" |
blanchet@53303
    42
  "shuffle_sp (SP4 c) = SP5 c" |
blanchet@53303
    43
  "shuffle_sp (SP5 d) = SP2 d"
blanchet@53303
    44
blanchet@55530
    45
primrec
blanchet@53303
    46
  hf_size :: "hfset \<Rightarrow> nat"
blanchet@53303
    47
where
blanchet@54019
    48
  "hf_size (HFset X) = 1 + setsum id (fset (fimage hf_size X))"
blanchet@53303
    49
blanchet@55530
    50
primrec rename_lam :: "(string \<Rightarrow> string) \<Rightarrow> lambda \<Rightarrow> lambda" where
blanchet@53303
    51
  "rename_lam f (Var s) = Var (f s)" |
blanchet@53303
    52
  "rename_lam f (App l l') = App (rename_lam f l) (rename_lam f l')" |
blanchet@53303
    53
  "rename_lam f (Abs s l) = Abs (f s) (rename_lam f l)" |
blanchet@55932
    54
  "rename_lam f (Let SL l) = Let (fimage (map_prod f (rename_lam f)) SL) (rename_lam f l)"
blanchet@53303
    55
blanchet@55530
    56
primrec
blanchet@53303
    57
  sum_i1 :: "('a\<Colon>{zero,plus}) I1 \<Rightarrow> 'a" and
blanchet@53303
    58
  sum_i2 :: "'a I2 \<Rightarrow> 'a"
blanchet@53303
    59
where
blanchet@53303
    60
  "sum_i1 (I11 n i) = n + sum_i1 i" |
blanchet@53303
    61
  "sum_i1 (I12 n i) = n + sum_i2 i" |
blanchet@53303
    62
  "sum_i2 I21 = 0" |
blanchet@53303
    63
  "sum_i2 (I22 i j) = sum_i1 i + sum_i2 j"
blanchet@53303
    64
blanchet@55530
    65
primrec forest_of_mylist :: "'a tree mylist \<Rightarrow> 'a forest" where
blanchet@53303
    66
  "forest_of_mylist MyNil = FNil" |
blanchet@53303
    67
  "forest_of_mylist (MyCons t ts) = FCons t (forest_of_mylist ts)"
blanchet@53303
    68
blanchet@55530
    69
primrec mylist_of_forest :: "'a forest \<Rightarrow> 'a tree mylist" where
blanchet@53303
    70
  "mylist_of_forest FNil = MyNil" |
blanchet@53303
    71
  "mylist_of_forest (FCons t ts) = MyCons t (mylist_of_forest ts)"
blanchet@53303
    72
blanchet@53303
    73
definition frev :: "'a forest \<Rightarrow> 'a forest" where
blanchet@53622
    74
  "frev = forest_of_mylist \<circ> myrev \<circ> mylist_of_forest"
blanchet@53303
    75
blanchet@55530
    76
primrec
blanchet@53303
    77
  mirror_tree :: "'a tree \<Rightarrow> 'a tree" and
blanchet@53303
    78
  mirror_forest :: "'a forest \<Rightarrow> 'a forest"
blanchet@53303
    79
where
blanchet@53303
    80
  "mirror_tree TEmpty = TEmpty" |
blanchet@53303
    81
  "mirror_tree (TNode x ts) = TNode x (mirror_forest ts)" |
blanchet@53303
    82
  "mirror_forest FNil = FNil" |
blanchet@53303
    83
  "mirror_forest (FCons t ts) = frev (FCons (mirror_tree t) (mirror_forest ts))"
blanchet@53303
    84
blanchet@55530
    85
primrec
blanchet@53303
    86
  mylist_of_tree' :: "'a tree' \<Rightarrow> 'a mylist" and
blanchet@53303
    87
  mylist_of_branch :: "'a branch \<Rightarrow> 'a mylist"
blanchet@53303
    88
where
blanchet@53303
    89
  "mylist_of_tree' TEmpty' = MyNil" |
blanchet@53303
    90
  "mylist_of_tree' (TNode' b b') = myapp (mylist_of_branch b) (mylist_of_branch b')" |
blanchet@53303
    91
  "mylist_of_branch (Branch x t) = MyCons x (mylist_of_tree' t)"
blanchet@53303
    92
blanchet@55530
    93
primrec
blanchet@55484
    94
  id_tree :: "'a bin_rose_tree \<Rightarrow> 'a bin_rose_tree" and
blanchet@55484
    95
  id_trees1 :: "'a bin_rose_tree mylist \<Rightarrow> 'a bin_rose_tree mylist" and
blanchet@55484
    96
  id_trees2 :: "'a bin_rose_tree mylist \<Rightarrow> 'a bin_rose_tree mylist"
blanchet@55484
    97
where
blanchet@55484
    98
  "id_tree (BRTree a ts ts') = BRTree a (id_trees1 ts) (id_trees2 ts')" |
blanchet@55484
    99
  "id_trees1 MyNil = MyNil" |
blanchet@55484
   100
  "id_trees1 (MyCons t ts) = MyCons (id_tree t) (id_trees1 ts)" |
blanchet@55484
   101
  "id_trees2 MyNil = MyNil" |
blanchet@55484
   102
  "id_trees2 (MyCons t ts) = MyCons (id_tree t) (id_trees2 ts)"
blanchet@55484
   103
blanchet@55530
   104
primrec
blanchet@55774
   105
  trunc_tree :: "'a bin_rose_tree \<Rightarrow> 'a bin_rose_tree" and
blanchet@55774
   106
  trunc_trees1 :: "'a bin_rose_tree mylist \<Rightarrow> 'a bin_rose_tree mylist" and
blanchet@55774
   107
  trunc_trees2 :: "'a bin_rose_tree mylist \<Rightarrow> 'a bin_rose_tree mylist"
blanchet@55774
   108
where
blanchet@55774
   109
  "trunc_tree (BRTree a ts ts') = BRTree a (trunc_trees1 ts) (trunc_trees2 ts')" |
blanchet@55774
   110
  "trunc_trees1 MyNil = MyNil" |
blanchet@55774
   111
  "trunc_trees1 (MyCons t ts) = MyCons (id_tree t) MyNil" |
blanchet@55774
   112
  "trunc_trees2 MyNil = MyNil" |
blanchet@55774
   113
  "trunc_trees2 (MyCons t ts) = MyCons (id_tree t) MyNil"
blanchet@55774
   114
blanchet@55774
   115
primrec
blanchet@53303
   116
  is_ground_exp :: "('a, 'b) exp \<Rightarrow> bool" and
blanchet@53303
   117
  is_ground_trm :: "('a, 'b) trm \<Rightarrow> bool" and
blanchet@53303
   118
  is_ground_factor :: "('a, 'b) factor \<Rightarrow> bool"
blanchet@53303
   119
where
blanchet@53303
   120
  "is_ground_exp (Term t) \<longleftrightarrow> is_ground_trm t" |
blanchet@53303
   121
  "is_ground_exp (Sum t e) \<longleftrightarrow> is_ground_trm t \<and> is_ground_exp e" |
blanchet@53303
   122
  "is_ground_trm (Factor f) \<longleftrightarrow> is_ground_factor f" |
blanchet@53303
   123
  "is_ground_trm (Prod f t) \<longleftrightarrow> is_ground_factor f \<and> is_ground_trm t" |
blanchet@53303
   124
  "is_ground_factor (C _) \<longleftrightarrow> True" |
blanchet@53303
   125
  "is_ground_factor (V _) \<longleftrightarrow> False" |
blanchet@53303
   126
  "is_ground_factor (Paren e) \<longleftrightarrow> is_ground_exp e"
blanchet@53303
   127
blanchet@55530
   128
primrec map_ftreeA :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a ftree \<Rightarrow> 'a ftree" where
blanchet@53303
   129
  "map_ftreeA f (FTLeaf x) = FTLeaf (f x)" |
blanchet@53622
   130
  "map_ftreeA f (FTNode g) = FTNode (map_ftreeA f \<circ> g)"
blanchet@53303
   131
blanchet@55530
   132
primrec map_ftreeB :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a ftree \<Rightarrow> 'b ftree" where
blanchet@53303
   133
  "map_ftreeB f (FTLeaf x) = FTLeaf (f x)" |
blanchet@53622
   134
  "map_ftreeB f (FTNode g) = FTNode (map_ftreeB f \<circ> g \<circ> the_inv f)"
blanchet@53303
   135
blanchet@53303
   136
end