src/HOL/datatype.ML
changeset 3308 da002cef7090
parent 3292 8b143c196d42
child 3534 c245c88194ff
equal deleted inserted replaced
3307:a106a557d704 3308:da002cef7090
   141     end ;
   141     end ;
   142 
   142 
   143 in
   143 in
   144   fun add_datatype (typevars, tname, cons_list') thy = 
   144   fun add_datatype (typevars, tname, cons_list') thy = 
   145     let
   145     let
   146       val dummy = if length cons_list' < dtK then ()
   146       val dummy = require_thy thy "Arith" "datatype definitions";
   147                   else require_thy thy "Nat" "datatype definitions";
       
   148       
   147       
   149       fun typid(dtRek(_,id)) = id
   148       fun typid(dtRek(_,id)) = id
   150         | typid(dtVar s) = implode (tl (explode s))
   149         | typid(dtVar s) = implode (tl (explode s))
   151         | typid(dtTyp(_,id)) = id;
   150         | typid(dtTyp(_,id)) = id;
   152 
   151 
   342          NoSyn);
   341          NoSyn);
   343 
   342 
   344       val rules_rec = rec_rules 1 cons_list
   343       val rules_rec = rec_rules 1 cons_list
   345 
   344 
   346 (* -------------------------------------------------------------------- *)
   345 (* -------------------------------------------------------------------- *)
       
   346 (* The size function                                                    *) 
       
   347 
       
   348       fun size_eqn(_,name,types,vns,_) =
       
   349         let fun sum((T,vn)::args) =
       
   350                   if is_dtRek T then "size(" ^ vn ^ ") + " ^ sum(args)
       
   351                   else sum args
       
   352               | sum [] = "1"
       
   353             val rhs = if exists is_dtRek types then sum(types~~vns) else "0"
       
   354         in ("", "size(" ^ C_exp name vns ^ ") = " ^ rhs) end;
       
   355 
       
   356       val size_eqns  = map size_eqn cons_list;
       
   357 
       
   358 (* -------------------------------------------------------------------- *)
   347       val consts = 
   359       val consts = 
   348         map const_type cons_list
   360         map const_type cons_list
   349         @ (if num_of_cons < dtK then []
   361         @ (if num_of_cons < dtK then []
   350            else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)])
   362            else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)])
   351         @ [case_const,rec_const];
   363         @ [case_const,rec_const];
   411     in
   423     in
   412       (thy |> add_types types
   424       (thy |> add_types types
   413            |> add_arities arities
   425            |> add_arities arities
   414            |> add_consts consts
   426            |> add_consts consts
   415            |> add_trrules xrules
   427            |> add_trrules xrules
   416            |> add_axioms rules, add_primrec)
   428            |> add_axioms rules, add_primrec, size_eqns)
   417     end
   429     end
   418 
   430 
   419 (*Check if the (induction) variable occurs among the premises, which
   431 (*Check if the (induction) variable occurs among the premises, which
   420   usually signals a mistake *)
   432   usually signals a mistake *)
   421 fun occs_in_prems a i state =
   433 fun occs_in_prems a i state =