src/Pure/consts.ML
author haftmann
Mon, 07 Nov 2005 12:06:11 +0100
changeset 18103 7a524bfa8d65
parent 18065 16608ab6ed84
child 18117 61a430a67d7c
permissions -rw-r--r--
added proper fillin_mixfix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/consts.ML
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     4
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     5
Polymorphic constants.
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     6
*)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     7
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     8
signature CONSTS =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     9
sig
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    10
  type T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    11
  val dest: T -> {declarations: typ NameSpace.table, constraints: typ NameSpace.table}
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    12
  val space: T -> NameSpace.T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    13
  val declaration: T -> string -> typ  (*exception TYPE*)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    14
  val constraint: T -> string -> typ    (*exception TYPE*)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    15
  val monomorphic: T -> string -> bool
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    16
  val typargs: T -> string -> typ -> typ list
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    17
  val declare: NameSpace.naming -> bstring * typ -> T -> T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    18
  val constrain: string * typ -> T -> T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    19
  val hide: bool -> string -> T -> T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    20
  val empty: T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    21
  val merge: T * T -> T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    22
end
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    23
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    24
structure Consts: CONSTS =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    25
struct
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    26
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    27
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    28
(** datatype T **)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    29
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    30
type arg = (indexname * sort) * int list;  (*type variable with first occurrence*)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    31
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    32
datatype T = Consts of
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    33
 {declarations: ((typ * arg list) * serial) NameSpace.table,
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    34
  constraints: typ Symtab.table};
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    35
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    36
fun make_consts (declarations, constraints) =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    37
  Consts {declarations = declarations, constraints = constraints};
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    38
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    39
fun map_consts f (Consts {declarations, constraints}) =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    40
  make_consts (f (declarations, constraints));
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    41
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    42
fun dest (Consts {declarations = (space, decls), constraints}) =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    43
 {declarations = (space, Symtab.map (#1 o #1) decls),
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    44
  constraints = (space, constraints)};
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    45
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    46
fun space (Consts {declarations = (space, _), ...}) = space;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    47
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    48
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    49
(* lookup consts *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    50
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    51
fun the_const (Consts {declarations = (_, decls), ...}) c =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    52
  (case Symtab.lookup decls c of SOME (decl, _) => decl
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    53
  | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    54
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    55
fun declaration consts c = #1 (the_const consts c);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    56
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    57
fun constraint (consts as Consts {constraints, ...}) c =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    58
  (case Symtab.lookup constraints c of
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    59
    SOME T => T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    60
  | NONE => declaration consts c);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    61
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    62
fun monomorphic consts c = null (#2 (the_const consts c));
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    63
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    64
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    65
(* typargs -- view actual const type as instance of declaration *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    66
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    67
fun sub (Type (_, Ts)) (i :: is) = sub (nth Ts i) is
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    68
  | sub T [] = T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    69
  | sub T _ = raise Subscript;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    70
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    71
fun typargs consts c =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    72
  let val (_, args) = the_const consts c
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    73
  in fn T => map (sub T o #2) args end;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    74
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    75
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    76
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    77
(** build consts **)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    78
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    79
fun err_dup_consts cs =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    80
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    81
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    82
fun err_inconsistent_constraints cs =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    83
  error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    84
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    85
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    86
(* declarations etc. *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    87
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    88
fun args_of declT =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    89
  let
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    90
    fun args (Type (_, Ts)) pos = args_list Ts 0 pos
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    91
      | args (TVar v) pos = insert (eq_fst op =) (v, rev pos)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    92
      | args (TFree _) _ = I
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    93
    and args_list (T :: Ts) i is = args T (i :: is) #> args_list Ts (i + 1) is
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    94
      | args_list [] _ _ = I;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    95
  in rev (args declT [] []) end;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    96
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    97
fun declare naming (c, T) = map_consts (apfst (fn declarations =>
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    98
  let
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    99
    val decl = (c, ((T, args_of T), serial ()));
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   100
    val declarations' = NameSpace.extend_table naming (declarations, [decl])
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   101
      handle Symtab.DUPS cs => err_dup_consts cs;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   102
  in declarations' end));
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   103
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   104
val constrain = map_consts o apsnd o Symtab.update;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   105
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   106
fun hide fully c = map_consts (apfst (apfst (NameSpace.hide fully c)));
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   107
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   108
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   109
(* empty and merge *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   110
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   111
val empty = make_consts (NameSpace.empty_table, Symtab.empty);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   112
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   113
fun merge
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   114
  (Consts {declarations = declarations1, constraints = constraints1},
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   115
   Consts {declarations = declarations2, constraints = constraints2}) =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   116
  let
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   117
    val declarations = NameSpace.merge_tables (eq_snd (op =)) (declarations1, declarations2)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   118
      handle Symtab.DUPS cs => err_dup_consts cs;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   119
    val constraints = Symtab.merge (op =) (constraints1, constraints2)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   120
      handle Symtab.DUPS cs => err_inconsistent_constraints cs;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   121
  in make_consts (declarations, constraints) end;
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   122
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   123
end;