src/HOL/BNF_Examples/Compat.thy
changeset 58216 e02d867dfbc6
parent 58150 2bf3ed0f62cf
child 58305 57752a91eec4
equal deleted inserted replaced
58215:cccf5445e224 58216:e02d867dfbc6
    62 
    62 
    63 datatype_compat x
    63 datatype_compat x
    64 
    64 
    65 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name x}; \<close>
    65 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name x}; \<close>
    66 
    66 
       
    67 thm x.induct x.rec
       
    68 thm compat_x.induct compat_x.rec
       
    69 
    67 datatype_new 'a tttre = TTTre 'a "'a tttre lst lst lst"
    70 datatype_new 'a tttre = TTTre 'a "'a tttre lst lst lst"
    68 
    71 
    69 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name tttre}; \<close>
    72 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name tttre}; \<close>
    70 
    73 
    71 datatype_compat tttre
    74 datatype_compat tttre
    72 
    75 
    73 ML \<open> get_descrs @{theory} (4, 4, 1) @{type_name tttre}; \<close>
    76 ML \<open> get_descrs @{theory} (4, 4, 1) @{type_name tttre}; \<close>
    74 
    77 
       
    78 thm tttre.induct tttre.rec
       
    79 thm compat_tttre.induct compat_tttre.rec
       
    80 
    75 datatype_new 'a ftre = FEmp | FTre "'a \<Rightarrow> 'a ftre lst"
    81 datatype_new 'a ftre = FEmp | FTre "'a \<Rightarrow> 'a ftre lst"
    76 
    82 
    77 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name ftre}; \<close>
    83 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name ftre}; \<close>
    78 
    84 
    79 datatype_compat ftre
    85 datatype_compat ftre
    80 
    86 
    81 ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name ftre}; \<close>
    87 ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name ftre}; \<close>
    82 
    88 
       
    89 thm ftre.induct ftre.rec
       
    90 thm compat_ftre.induct compat_ftre.rec
       
    91 
    83 datatype_new 'a btre = BTre 'a "'a btre lst" "'a btre lst"
    92 datatype_new 'a btre = BTre 'a "'a btre lst" "'a btre lst"
    84 
    93 
    85 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name btre}; \<close>
    94 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name btre}; \<close>
    86 
    95 
    87 datatype_compat btre
    96 datatype_compat btre
    88 
    97 
    89 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name btre}; \<close>
    98 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name btre}; \<close>
       
    99 
       
   100 thm btre.induct btre.rec
       
   101 thm compat_btre.induct compat_btre.rec
    90 
   102 
    91 datatype_new 'a foo = Foo | Foo' 'a "'a bar" and 'a bar = Bar | Bar' 'a "'a foo"
   103 datatype_new 'a foo = Foo | Foo' 'a "'a bar" and 'a bar = Bar | Bar' 'a "'a foo"
    92 
   104 
    93 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name foo}; \<close>
   105 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name foo}; \<close>
    94 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name bar}; \<close>
   106 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name bar}; \<close>
   104 
   116 
   105 datatype_compat tre
   117 datatype_compat tre
   106 
   118 
   107 ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name tre}; \<close>
   119 ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name tre}; \<close>
   108 
   120 
   109 fun f_tre and f_tres where
   121 thm tre.induct tre.rec
   110   "f_tre (Tre a ts) = {a} \<union> f_tres ts"
   122 thm compat_tre.induct compat_tre.rec
   111 | "f_tres Nl = {}"
       
   112 | "f_tres (Cns t ts) = f_tres ts"
       
   113 
   123 
   114 datatype_new 'a f = F 'a and 'a g = G 'a
   124 datatype_new 'a f = F 'a and 'a g = G 'a
   115 
   125 
   116 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name f}; \<close>
   126 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name f}; \<close>
   117 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name g}; \<close>
   127 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name g}; \<close>
   128 
   138 
   129 datatype_compat h
   139 datatype_compat h
   130 
   140 
   131 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name h}; \<close>
   141 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name h}; \<close>
   132 
   142 
       
   143 thm h.induct h.rec
       
   144 thm compat_h.induct compat_h.rec
       
   145 
   133 datatype_new myunit = MyUnity
   146 datatype_new myunit = MyUnity
   134 
   147 
   135 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name myunit}; \<close>
   148 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name myunit}; \<close>
   136 
   149 
   137 datatype_compat myunit
   150 datatype_compat myunit
   143 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name mylist}; \<close>
   156 ML \<open> get_descrs @{theory} (0, 1, 1) @{type_name mylist}; \<close>
   144 
   157 
   145 datatype_compat mylist
   158 datatype_compat mylist
   146 
   159 
   147 ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name mylist}; \<close>
   160 ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name mylist}; \<close>
   148 
       
   149 fun f_mylist where
       
   150   "f_mylist MyNil = 0"
       
   151 | "f_mylist (MyCons _ xs) = Suc (f_mylist xs)"
       
   152 
   161 
   153 datatype_new foo' = FooNil | FooCons bar' foo' and bar' = Bar
   162 datatype_new foo' = FooNil | FooCons bar' foo' and bar' = Bar
   154 
   163 
   155 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name foo'}; \<close>
   164 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name foo'}; \<close>
   156 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name bar'}; \<close>
   165 ML \<open> get_descrs @{theory} (0, 2, 2) @{type_name bar'}; \<close>
   158 datatype_compat bar' foo'
   167 datatype_compat bar' foo'
   159 
   168 
   160 ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name foo'}; \<close>
   169 ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name foo'}; \<close>
   161 ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name bar'}; \<close>
   170 ML \<open> get_descrs @{theory} (2, 2, 2) @{type_name bar'}; \<close>
   162 
   171 
   163 fun f_foo and f_bar where
       
   164   "f_foo FooNil = 0"
       
   165 | "f_foo (FooCons bar foo) = Suc (f_foo foo) + f_bar bar"
       
   166 | "f_bar Bar = Suc 0"
       
   167 
       
   168 datatype funky = Funky "funky tre" | Funky'
   172 datatype funky = Funky "funky tre" | Funky'
   169 
   173 
   170 ML \<open> get_descrs @{theory} (3, 3, 3) @{type_name funky}; \<close>
   174 ML \<open> get_descrs @{theory} (3, 3, 3) @{type_name funky}; \<close>
   171 
   175 
   172 datatype fnky = Fnky "nat tre"
   176 datatype fnky = Fnky "nat tre"
   179 
   183 
   180 datatype_compat tree
   184 datatype_compat tree
   181 
   185 
   182 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name tree}; \<close>
   186 ML \<open> get_descrs @{theory} (3, 3, 1) @{type_name tree}; \<close>
   183 
   187 
   184 
   188 thm tree.induct tree.rec
   185 subsection \<open> Creating a New-Style Datatype Using an Old-Style Interface \<close>
   189 thm compat_tree.induct compat_tree.rec
       
   190 
       
   191 
       
   192 subsection \<open> Creating New-Style Datatypes Using Old-Style Interfaces \<close>
   186 
   193 
   187 ML \<open>
   194 ML \<open>
   188 val l_specs =
   195 val l_specs =
   189   [((@{binding l}, [("'a", @{sort type})], NoSyn),
   196   [((@{binding l}, [("'a", @{sort type})], NoSyn),
   190    [(@{binding N}, [], NoSyn),
   197    [(@{binding N}, [], NoSyn),
   191     (@{binding C}, [@{typ 'a}, Type (Sign.full_name @{theory} @{binding l}, [@{typ 'a}])], NoSyn)])];
   198     (@{binding C}, [@{typ 'a}, Type (Sign.full_name @{theory} @{binding l}, [@{typ 'a}])], NoSyn)])];
   192 \<close>
   199 \<close>
   193 
   200 
   194 setup \<open> snd o BNF_LFP_Compat.add_datatype BNF_LFP_Compat.Unfold_Nesting l_specs \<close>
   201 setup \<open> snd o BNF_LFP_Compat.add_datatype BNF_LFP_Compat.Unfold_Nesting l_specs; \<close>
   195 
   202 
   196 ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name l}; \<close>
   203 ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name l}; \<close>
   197 
   204 
   198 thm l.exhaust l.map l.induct l.rec l.size
   205 thm l.exhaust l.map l.induct l.rec l.size
   199 
   206 
   202   [((@{binding t}, [("'b", @{sort type})], NoSyn),
   209   [((@{binding t}, [("'b", @{sort type})], NoSyn),
   203    [(@{binding T}, [@{typ 'b}, Type (@{type_name l},
   210    [(@{binding T}, [@{typ 'b}, Type (@{type_name l},
   204        [Type (Sign.full_name @{theory} @{binding t}, [@{typ 'b}])])], NoSyn)])];
   211        [Type (Sign.full_name @{theory} @{binding t}, [@{typ 'b}])])], NoSyn)])];
   205 \<close>
   212 \<close>
   206 
   213 
   207 setup \<open> snd o BNF_LFP_Compat.add_datatype BNF_LFP_Compat.Unfold_Nesting t_specs \<close>
   214 setup \<open> snd o BNF_LFP_Compat.add_datatype BNF_LFP_Compat.Unfold_Nesting t_specs; \<close>
   208 
   215 
   209 ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name t}; \<close>
   216 ML \<open> get_descrs @{theory} (2, 2, 1) @{type_name t}; \<close>
   210 
   217 
   211 thm t.exhaust t.map t.induct t.rec t.size
   218 thm t.exhaust t.map t.induct t.rec t.size
       
   219 thm compat_t.induct compat_t.rec
       
   220 
       
   221 ML \<open>
       
   222 val ft_specs =
       
   223   [((@{binding ft}, [("'a", @{sort type})], NoSyn),
       
   224    [(@{binding FT0}, [], NoSyn),
       
   225     (@{binding FT}, [@{typ 'a} --> Type (Sign.full_name @{theory} @{binding ft}, [@{typ 'a}])],
       
   226      NoSyn)])];
       
   227 \<close>
       
   228 
       
   229 setup \<open> snd o BNF_LFP_Compat.add_datatype BNF_LFP_Compat.Unfold_Nesting ft_specs; \<close>
       
   230 
       
   231 ML \<open> get_descrs @{theory} (1, 1, 1) @{type_name ft}; \<close>
       
   232 
       
   233 thm ft.exhaust ft.induct ft.rec ft.size
       
   234 thm compat_ft.induct compat_ft.rec
   212 
   235 
   213 end
   236 end