src/HOL/Tools/datatype_codegen.ML
changeset 18247 b17724cae935
parent 18220 43cf5767f992
child 18334 a41ce9c10b73
equal deleted inserted replaced
18246:676d2e625d98 18247:b17724cae935
     8 signature DATATYPE_CODEGEN =
     8 signature DATATYPE_CODEGEN =
     9 sig
     9 sig
    10   val is_datatype: theory -> string -> bool
    10   val is_datatype: theory -> string -> bool
    11   val get_datatype: theory -> string -> (string list * string list) option
    11   val get_datatype: theory -> string -> (string list * string list) option
    12   val get_datacons: theory -> string * string -> typ list option
    12   val get_datacons: theory -> string * string -> typ list option
       
    13   val get_case_const_data: theory -> string -> (string * int) list option;
    13   val setup: (theory -> theory) list
    14   val setup: (theory -> theory) list
    14 end;
    15 end;
    15 
    16 
    16 structure DatatypeCodegen : DATATYPE_CODEGEN =
    17 structure DatatypeCodegen : DATATYPE_CODEGEN =
    17 struct
    18 struct
   262 
   263 
   263 (**** code generators for terms and types ****)
   264 (**** code generators for terms and types ****)
   264 
   265 
   265 fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of
   266 fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of
   266    (c as Const (s, T), ts) =>
   267    (c as Const (s, T), ts) =>
   267        (case find_first (fn (_, {index, descr, case_name, ...}) =>
   268        (case Library.find_first (fn (_, {index, descr, case_name, ...}) =>
   268          s = case_name orelse
   269          s = case_name orelse
   269            AList.defined (op =) ((#3 o the o AList.lookup (op =) descr) index) s)
   270            AList.defined (op =) ((#3 o the o AList.lookup (op =) descr) index) s)
   270              (Symtab.dest (DatatypePackage.get_datatypes thy)) of
   271              (Symtab.dest (DatatypePackage.get_datatypes thy)) of
   271           NONE => NONE
   272           NONE => NONE
   272         | SOME (tname, {index, descr, ...}) =>
   273         | SOME (tname, {index, descr, ...}) =>
   273            if isSome (get_assoc_code thy s T) then NONE else
   274            if is_some (get_assoc_code thy s T) then NONE else
   274            let val SOME (_, _, constrs) = AList.lookup (op =) descr index
   275            let val SOME (_, _, constrs) = AList.lookup (op =) descr index
   275            in (case (AList.lookup (op =) constrs s, strip_type T) of
   276            in (case (AList.lookup (op =) constrs s, strip_type T) of
   276                (NONE, _) => SOME (pretty_case thy defs gr dep module brack
   277                (NONE, _) => SOME (pretty_case thy defs gr dep module brack
   277                  (#3 (valOf (AList.lookup (op =) descr index))) c ts)
   278                  ((#3 o the o AList.lookup (op =) descr) index) c ts)
   278              | (SOME args, (_, Type _)) => SOME (pretty_constr thy defs
   279              | (SOME args, (_, Type _)) => SOME (pretty_constr thy defs
   279                  (fst (invoke_tycodegen thy defs dep module false
   280                  (fst (invoke_tycodegen thy defs dep module false
   280                     (gr, snd (strip_type T))))
   281                     (gr, snd (strip_type T))))
   281                  dep module brack args c ts)
   282                  dep module brack args c ts)
   282              | _ => NONE)
   283              | _ => NONE)
   340           |> Option.map (map (DatatypeAux.typ_of_dtyp descr (map (rpair [])
   341           |> Option.map (map (DatatypeAux.typ_of_dtyp descr (map (rpair [])
   341                (map DatatypeAux.dest_DtTFree vs)))))
   342                (map DatatypeAux.dest_DtTFree vs)))))
   342     else NONE
   343     else NONE
   343   end;
   344   end;
   344 
   345 
       
   346 fun get_case_const_data thy f =
       
   347   case Library.find_first (fn (_, {index, descr, case_name, ...}) =>
       
   348       case_name = f
       
   349     ) ((Symtab.dest o DatatypePackage.get_datatypes) thy)
       
   350    of NONE => NONE
       
   351     | SOME (_, {index, descr, ...}) =>
       
   352         (SOME o map (apsnd length) o #3 o the o AList.lookup (op =) descr) index;
       
   353 
       
   354 
   345 val setup = [
   355 val setup = [
   346   add_codegen "datatype" datatype_codegen,
   356   add_codegen "datatype" datatype_codegen,
   347   add_tycodegen "datatype" datatype_tycodegen,
   357   add_tycodegen "datatype" datatype_tycodegen,
   348   CodegenPackage.set_is_datatype
   358   CodegenPackage.set_is_datatype
   349     is_datatype,
   359     is_datatype,
   350   CodegenPackage.add_defgen 
   360   CodegenPackage.add_defgen
   351     ("datatype", CodegenPackage.defgen_datatype get_datatype),
   361     ("datatype", CodegenPackage.defgen_datatype get_datatype),
   352   CodegenPackage.add_defgen
   362   CodegenPackage.add_defgen
   353     ("datacons", CodegenPackage.defgen_datacons get_datacons)
   363     ("datacons", CodegenPackage.defgen_datacons get_datacons),
       
   364   CodegenPackage.add_codegen_expr
       
   365     ("case", CodegenPackage.codegen_case get_case_const_data)
   354 ];
   366 ];
   355 
   367 
   356 end;
   368 end;