src/HOL/Tools/datatype_codegen.ML
changeset 17521 0f1c48de39f5
parent 17412 e26cb20ef0cc
child 18220 43cf5767f992
equal deleted inserted replaced
17520:8581c151adea 17521:0f1c48de39f5
    24 
    24 
    25 (* find shortest path to constructor with no recursive arguments *)
    25 (* find shortest path to constructor with no recursive arguments *)
    26 
    26 
    27 fun find_nonempty (descr: DatatypeAux.descr) is i =
    27 fun find_nonempty (descr: DatatypeAux.descr) is i =
    28   let
    28   let
    29     val (_, _, constrs) = valOf (assoc (descr, i));
    29     val (_, _, constrs) = valOf (AList.lookup (op =) descr i);
    30     fun arg_nonempty (_, DatatypeAux.DtRec i) = if i mem is then NONE
    30     fun arg_nonempty (_, DatatypeAux.DtRec i) = if i mem is then NONE
    31           else Option.map (curry op + 1 o snd) (find_nonempty descr (i::is) i)
    31           else Option.map (curry op + 1 o snd) (find_nonempty descr (i::is) i)
    32       | arg_nonempty _ = SOME 0;
    32       | arg_nonempty _ = SOME 0;
    33     fun max xs = Library.foldl
    33     fun max xs = Library.foldl
    34       (fn (NONE, _) => NONE
    34       (fn (NONE, _) => NONE
   158                    Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"]
   158                    Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"]
   159                else if null cs2 then
   159                else if null cs2 then
   160                  [Pretty.block [Pretty.str "(case", Pretty.brk 1,
   160                  [Pretty.block [Pretty.str "(case", Pretty.brk 1,
   161                    Pretty.str "i", Pretty.brk 1, Pretty.str "of",
   161                    Pretty.str "i", Pretty.brk 1, Pretty.str "of",
   162                    Pretty.brk 1, Pretty.str "0 =>", Pretty.brk 1,
   162                    Pretty.brk 1, Pretty.str "0 =>", Pretty.brk 1,
   163                    mk_constr "0" true (cname, valOf (assoc (cs, cname))),
   163                    mk_constr "0" true (cname, valOf (AList.lookup (op =) cs cname)),
   164                    Pretty.brk 1, Pretty.str "| _ =>", Pretty.brk 1,
   164                    Pretty.brk 1, Pretty.str "| _ =>", Pretty.brk 1,
   165                    mk_choice cs1, Pretty.str ")"]]
   165                    mk_choice cs1, Pretty.str ")"]]
   166                else [mk_choice cs2])) ::
   166                else [mk_choice cs2])) ::
   167             (if null cs1 then []
   167             (if null cs1 then []
   168              else [Pretty.blk (4, separate (Pretty.brk 1) 
   168              else [Pretty.blk (4, separate (Pretty.brk 1) 
   261 
   261 
   262 fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of
   262 fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of
   263    (c as Const (s, T), ts) =>
   263    (c as Const (s, T), ts) =>
   264        (case find_first (fn (_, {index, descr, case_name, ...}) =>
   264        (case find_first (fn (_, {index, descr, case_name, ...}) =>
   265          s = case_name orelse
   265          s = case_name orelse
   266            isSome (assoc (#3 (valOf (assoc (descr, index))), s)))
   266            AList.defined (op =) ((#3 o the o AList.lookup (op =) descr) index) s)
   267              (Symtab.dest (DatatypePackage.get_datatypes thy)) of
   267              (Symtab.dest (DatatypePackage.get_datatypes thy)) of
   268           NONE => NONE
   268           NONE => NONE
   269         | SOME (tname, {index, descr, ...}) =>
   269         | SOME (tname, {index, descr, ...}) =>
   270            if isSome (get_assoc_code thy s T) then NONE else
   270            if isSome (get_assoc_code thy s T) then NONE else
   271            let val SOME (_, _, constrs) = assoc (descr, index)
   271            let val SOME (_, _, constrs) = AList.lookup (op =) descr index
   272            in (case (assoc (constrs, s), strip_type T) of
   272            in (case (AList.lookup (op =) constrs s, strip_type T) of
   273                (NONE, _) => SOME (pretty_case thy defs gr dep module brack
   273                (NONE, _) => SOME (pretty_case thy defs gr dep module brack
   274                  (#3 (valOf (assoc (descr, index)))) c ts)
   274                  (#3 (valOf (AList.lookup (op =) descr index))) c ts)
   275              | (SOME args, (_, Type _)) => SOME (pretty_constr thy defs
   275              | (SOME args, (_, Type _)) => SOME (pretty_constr thy defs
   276                  (fst (invoke_tycodegen thy defs dep module false
   276                  (fst (invoke_tycodegen thy defs dep module false
   277                     (gr, snd (strip_type T))))
   277                     (gr, snd (strip_type T))))
   278                  dep module brack args c ts)
   278                  dep module brack args c ts)
   279              | _ => NONE)
   279              | _ => NONE)