src/Doc/Datatypes/Datatypes.thy
changeset 53332 c97a05a26dd6
parent 53331 20440c789759
child 53335 585b2fee55e5
equal deleted inserted replaced
53331:20440c789759 53332:c97a05a26dd6
   244 
   244 
   245 text {*
   245 text {*
   246 simplest recursive type: copy of the natural numbers:
   246 simplest recursive type: copy of the natural numbers:
   247 *}
   247 *}
   248 
   248 
       
   249     datatype_new nat = Zero | Suc nat
   249 (*<*)
   250 (*<*)
   250     (* FIXME: remove "rep_compat" once "datatype_new" is integrated with "fun" *)
   251     (* FIXME: remove once "datatype_new" is integrated with "fun" *)
       
   252     datatype_new_compat nat
   251 (*>*)
   253 (*>*)
   252     datatype_new (*<*)(rep_compat) (*>*)nat = Zero | Suc nat
       
   253 
   254 
   254 text {*
   255 text {*
   255 lists were shown in the introduction; terminated lists are a variant:
   256 lists were shown in the introduction; terminated lists are a variant:
   256 *}
   257 *}
   257 
   258 
   636 
   637 
   637 text {*
   638 text {*
   638 we don't like the confusing name @{const nth}:
   639 we don't like the confusing name @{const nth}:
   639 *}
   640 *}
   640 
   641 
   641     (* FIXME *)
   642     primrec_new at :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" where
   642     primrec_new_notyet at :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" where
       
   643       "at (a # as) j =
   643       "at (a # as) j =
   644          (case j of
   644          (case j of
   645             Zero \<Rightarrow> a
   645             Zero \<Rightarrow> a
   646           | Suc j' \<Rightarrow> at as j')"
   646           | Suc j' \<Rightarrow> at as j')"
   647 
       
   648     primrec_new at :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" where
       
   649       "at (a # as) j = nat_case a (at as) j"
       
   650 
   647 
   651     primrec_new tfold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) tlist \<Rightarrow> 'b" where
   648     primrec_new tfold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) tlist \<Rightarrow> 'b" where
   652       "tfold _ (TNil b) = b" |
   649       "tfold _ (TNil b) = b" |
   653       "tfold f (TCons a as) = f a (tfold f as)"
   650       "tfold f (TCons a as) = f a (tfold f as)"
   654 
   651 
   672 
   669 
   673 text {*
   670 text {*
   674 Mutual recursion is be possible within a single type, but currently only using fun:
   671 Mutual recursion is be possible within a single type, but currently only using fun:
   675 *}
   672 *}
   676 
   673 
   677 (*<*)
       
   678     (* FIXME: remove hack once "primrec_new" is in place *)
       
   679     rep_datatype Zero Suc
       
   680     by (erule nat.induct, assumption) auto
       
   681 (*>*)
       
   682     fun
   674     fun
   683       even :: "nat \<Rightarrow> bool" and
   675       even :: "nat \<Rightarrow> bool" and
   684       odd :: "nat \<Rightarrow> bool"
   676       odd :: "nat \<Rightarrow> bool"
   685     where
   677     where
   686       "even Zero = True" |
   678       "even Zero = True" |