src/Pure/Tools/codegen_consts.ML
author haftmann
Sat, 03 Mar 2007 09:27:01 +0100
changeset 22400 cb0b1bbf7e91
parent 22197 461130ccfef4
child 22508 6ee2edbce31c
permissions -rw-r--r--
clarified error message
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20387
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/codegen_consts.ML
haftmann
parents:
diff changeset
     2
    ID:         $Id$
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann
parents:
diff changeset
     4
20855
9f60d493c8fe clarified header comments
haftmann
parents: 20704
diff changeset
     5
Identifying constants by name plus normalized type instantantiation schemes.
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
     6
Convenient data structures for constants.  Auxiliary.
20387
haftmann
parents:
diff changeset
     7
*)
haftmann
parents:
diff changeset
     8
haftmann
parents:
diff changeset
     9
signature CODEGEN_CONSTS =
haftmann
parents:
diff changeset
    10
sig
haftmann
parents:
diff changeset
    11
  type const = string * typ list (*type instantiations*)
haftmann
parents:
diff changeset
    12
  val const_ord: const * const -> order
haftmann
parents:
diff changeset
    13
  val eq_const: const * const -> bool
haftmann
parents:
diff changeset
    14
  structure Consttab: TABLE
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    15
  val inst_of_typ: theory -> string * typ -> const
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    16
  val typ_of_inst: theory -> const -> string * typ
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    17
  val norm: theory -> const -> const
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    18
  val norm_of_typ: theory -> string * typ -> const
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    19
  val typ_sort_inst: Sorts.algebra -> typ * sort -> sort Vartab.table -> sort Vartab.table
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    20
  val typargs: theory -> string * typ -> typ list
22183
0e6c0aeb04ec dropped useless stuff
haftmann
parents: 22049
diff changeset
    21
  val find_def: theory -> const -> (string (*theory name*) * thm) option
21118
d9789a87825d cleanup
haftmann
parents: 20855
diff changeset
    22
  val consts_of: theory -> term -> const list
20387
haftmann
parents:
diff changeset
    23
  val read_const: theory -> string -> const
haftmann
parents:
diff changeset
    24
  val string_of_const: theory -> const -> string
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    25
  val raw_string_of_const: const -> string
20387
haftmann
parents:
diff changeset
    26
  val string_of_const_typ: theory -> string * typ -> string
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    27
  val string_of_typ: theory -> typ -> string
20387
haftmann
parents:
diff changeset
    28
end;
haftmann
parents:
diff changeset
    29
haftmann
parents:
diff changeset
    30
structure CodegenConsts: CODEGEN_CONSTS =
haftmann
parents:
diff changeset
    31
struct
haftmann
parents:
diff changeset
    32
haftmann
parents:
diff changeset
    33
haftmann
parents:
diff changeset
    34
(* basic data structures *)
haftmann
parents:
diff changeset
    35
haftmann
parents:
diff changeset
    36
type const = string * typ list (*type instantiations*);
haftmann
parents:
diff changeset
    37
val const_ord = prod_ord fast_string_ord (list_ord Term.typ_ord);
haftmann
parents:
diff changeset
    38
val eq_const = is_equal o const_ord;
haftmann
parents:
diff changeset
    39
haftmann
parents:
diff changeset
    40
structure Consttab =
haftmann
parents:
diff changeset
    41
  TableFun(
haftmann
parents:
diff changeset
    42
    type key = string * typ list;
haftmann
parents:
diff changeset
    43
    val ord = const_ord;
haftmann
parents:
diff changeset
    44
  );
haftmann
parents:
diff changeset
    45
haftmann
parents:
diff changeset
    46
21463
42dd50268c8b completed class parameter handling in axclass.ML
haftmann
parents: 21118
diff changeset
    47
(* type instantiations, overloading, dictionary values *)
20387
haftmann
parents:
diff changeset
    48
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    49
fun string_of_typ thy = setmp show_sorts true (Sign.string_of_typ thy);
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    50
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    51
fun inst_of_typ thy (c_ty as (c, ty)) =
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    52
  (c, Sign.const_typargs thy c_ty);
20387
haftmann
parents:
diff changeset
    53
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    54
fun typ_of_inst thy (c_tys as (c, tys)) =
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    55
  (c, Sign.const_instance thy c_tys);
20387
haftmann
parents:
diff changeset
    56
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
    57
fun find_def thy (c, tys) =
20387
haftmann
parents:
diff changeset
    58
  let
haftmann
parents:
diff changeset
    59
    val specs = Defs.specifications_of (Theory.defs_of thy) c;
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
    60
    val typ_instance = case AxClass.class_of_param thy c
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
    61
     of SOME _ => let
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
    62
          fun instance_tycos (Type (tyco1, _), Type (tyco2, _)) = tyco1 = tyco2
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
    63
            | instance_tycos (_ , TVar _) = true
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
    64
            | instance_tycos ty_ty = Sign.typ_instance thy ty_ty;
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
    65
        in instance_tycos end
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    66
      | NONE =>  Sign.typ_instance thy;
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    67
    fun get_def (_, { is_def, thyname, name, lhs, rhs }) =
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    68
      if is_def andalso forall typ_instance (tys ~~ lhs) then
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    69
        case try (Thm.get_axiom_i thy) name
22183
0e6c0aeb04ec dropped useless stuff
haftmann
parents: 22049
diff changeset
    70
         of SOME thm => SOME (thyname, thm)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    71
          | NONE => NONE
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    72
      else NONE
20387
haftmann
parents:
diff changeset
    73
  in
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    74
    get_first get_def specs
20387
haftmann
parents:
diff changeset
    75
  end;
haftmann
parents:
diff changeset
    76
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    77
fun norm thy (c, insts) =
20387
haftmann
parents:
diff changeset
    78
  let
22183
0e6c0aeb04ec dropped useless stuff
haftmann
parents: 22049
diff changeset
    79
    fun disciplined class [ty as Type (tyco, _)] =
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    80
          let
22400
cb0b1bbf7e91 clarified error message
haftmann
parents: 22197
diff changeset
    81
            val sorts = Sorts.mg_domain (Sign.classes_of thy) tyco [class]
cb0b1bbf7e91 clarified error message
haftmann
parents: 22197
diff changeset
    82
              handle CLASS_ERROR => error ("No such instance: " ^ tyco ^ " :: " ^ class
cb0b1bbf7e91 clarified error message
haftmann
parents: 22197
diff changeset
    83
                ^ ",\nrequired for overloaded constant " ^ c);
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    84
            val vs = Name.invents Name.context "'a" (length sorts);
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    85
          in
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    86
            (c, [Type (tyco, map (fn v => TVar ((v, 0), [])) vs)])
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    87
          end
22183
0e6c0aeb04ec dropped useless stuff
haftmann
parents: 22049
diff changeset
    88
      | disciplined class _ =
0e6c0aeb04ec dropped useless stuff
haftmann
parents: 22049
diff changeset
    89
          (c, [TVar (("'a", 0), [class])]);
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    90
  in case AxClass.class_of_param thy c
22183
0e6c0aeb04ec dropped useless stuff
haftmann
parents: 22049
diff changeset
    91
   of SOME class => disciplined class insts
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    92
    | NONE => inst_of_typ thy (c, Sign.the_const_type thy c)
20387
haftmann
parents:
diff changeset
    93
  end;
haftmann
parents:
diff changeset
    94
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    95
fun norm_of_typ thy (c, ty) =
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
    96
  norm thy (c, Sign.const_typargs thy (c, ty));
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
    97
20704
a56f0743b3ee cleaned up
haftmann
parents: 20600
diff changeset
    98
fun consts_of thy t =
21118
d9789a87825d cleanup
haftmann
parents: 20855
diff changeset
    99
  fold_aterms (fn Const c => cons (norm_of_typ thy c) | _ => I) t []
20704
a56f0743b3ee cleaned up
haftmann
parents: 20600
diff changeset
   100
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   101
fun typ_sort_inst algebra =
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   102
  let
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   103
    val inters = Sorts.inter_sort algebra;
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   104
    fun match _ [] = I
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   105
      | match (TVar (v, S)) S' = Vartab.map_default (v, []) (fn S'' => inters (S, inters (S', S'')))
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   106
      | match (Type (a, Ts)) S =
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   107
          fold2 match Ts (Sorts.mg_domain algebra a S)
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   108
  in uncurry match end;
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   109
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   110
fun typargs thy (c_ty as (c, ty)) =
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   111
  let
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   112
    val opt_class = AxClass.class_of_param thy c;
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   113
    val tys = Sign.const_typargs thy (c, ty);
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   114
  in case (opt_class, tys)
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   115
   of (SOME _, [Type (_, tys')]) => tys'
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   116
    | _ => tys
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   117
  end;
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   118
20387
haftmann
parents:
diff changeset
   119
haftmann
parents:
diff changeset
   120
(* reading constants as terms *)
haftmann
parents:
diff changeset
   121
haftmann
parents:
diff changeset
   122
fun read_const_typ thy raw_t =
haftmann
parents:
diff changeset
   123
  let
haftmann
parents:
diff changeset
   124
    val t = Sign.read_term thy raw_t
haftmann
parents:
diff changeset
   125
  in case try dest_Const t
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
   126
   of SOME c_ty => (typ_of_inst thy o norm_of_typ thy) c_ty
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20387
diff changeset
   127
    | NONE => error ("Not a constant: " ^ Sign.string_of_term thy t)
20387
haftmann
parents:
diff changeset
   128
  end;
haftmann
parents:
diff changeset
   129
haftmann
parents:
diff changeset
   130
fun read_const thy =
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
   131
  norm_of_typ thy o read_const_typ thy;
20387
haftmann
parents:
diff changeset
   132
haftmann
parents:
diff changeset
   133
22197
461130ccfef4 clarified code
haftmann
parents: 22183
diff changeset
   134
(* printing *)
20387
haftmann
parents:
diff changeset
   135
haftmann
parents:
diff changeset
   136
fun string_of_const thy (c, tys) =
haftmann
parents:
diff changeset
   137
  space_implode " " (Sign.extern_const thy c
haftmann
parents:
diff changeset
   138
    :: map (enclose "[" "]" o Sign.string_of_typ thy) tys);
haftmann
parents:
diff changeset
   139
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
   140
fun raw_string_of_const (c, tys) =
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
   141
  space_implode " " (c
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
   142
    :: map (enclose "[" "]" o Display.raw_string_of_typ) tys);
6d75e02ed285 added codegen_data
haftmann
parents: 20456
diff changeset
   143
20387
haftmann
parents:
diff changeset
   144
fun string_of_const_typ thy (c, ty) =
haftmann
parents:
diff changeset
   145
  string_of_const thy (c, Consts.typargs (Sign.consts_of thy) (c, ty));
haftmann
parents:
diff changeset
   146
haftmann
parents:
diff changeset
   147
end;