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