src/Pure/defs.ML
author wenzelm
Wed, 15 Feb 2006 21:35:00 +0100
changeset 19050 527bc006f2b6
parent 19040 88d25a6c346a
child 19135 2de31ba562d7
permissions -rw-r--r--
specifications_of: avoid partiality;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
     1
(*  Title:      Pure/defs.ML
16108
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
     2
    ID:         $Id$
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
     3
    Author:     Makarius
16108
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
     4
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
     5
Global well-formedness checks for constant definitions.  Dependencies
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
     6
are only tracked for non-overloaded definitions!
16108
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
     7
*)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
     8
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
     9
signature DEFS =
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    10
sig
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    11
  type T
17712
46c2091e5187 make signature constraint actually work;
wenzelm
parents: 17711
diff changeset
    12
  val define: (string -> typ) -> string -> string * typ -> (string * typ) list -> T -> T
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    13
  val empty: T
19040
88d25a6c346a exported specifications_of
haftmann
parents: 19025
diff changeset
    14
  val specifications_of: T -> string -> typ list
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    15
  val merge: Pretty.pp -> T * T -> T
16108
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    16
end
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    17
17711
c16cbe73798c activate signature constraints;
wenzelm
parents: 17707
diff changeset
    18
structure Defs: DEFS =
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    19
struct
16108
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    20
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    21
(** datatype T **)
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    22
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    23
datatype T = Defs of
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    24
 {consts: typ Graph.T,                                 (*constant declarations and dependencies*)
17994
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    25
  specified: (string * typ) Inttab.table Symtab.table}; (*specification name and const type*)
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    26
17994
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    27
fun make_defs (consts, specified) = Defs {consts = consts, specified = specified};
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    28
fun map_defs f (Defs {consts, specified}) = make_defs (f (consts, specified));
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    29
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    30
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    31
(* specified consts *)
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    32
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    33
fun disjoint_types T U =
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    34
  (Type.raw_unify (T, Logic.incr_tvar (maxidx_of_typ T + 1) U) Vartab.empty; false)
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    35
    handle Type.TUNIFY => true;
16308
636a1a84977a 1) Fixed bug in Defs.merge_edges_1.
obua
parents: 16198
diff changeset
    36
17803
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    37
fun check_specified c (i, (a, T)) = Inttab.forall (fn (j, (b, U)) =>
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    38
  i = j orelse not (Type.could_unify (T, U)) orelse disjoint_types T U orelse
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    39
    error ("Type clash in specifications " ^ quote a ^ " and " ^ quote b ^
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    40
      " for constant " ^ quote c));
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    41
16982
4600e74aeb0d chain_history: turned into runtime flag;
wenzelm
parents: 16936
diff changeset
    42
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    43
(* define consts *)
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    44
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    45
fun err_cyclic cycles =
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    46
  error ("Cyclic dependency of constants:\n" ^
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    47
    cat_lines (map (space_implode " -> " o map quote o rev) cycles));
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    48
17994
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    49
fun define const_type name lhs rhs = map_defs (fn (consts, specified) =>
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    50
  let
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    51
    fun declare (a, _) = Graph.default_node (a, const_type a);
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    52
    fun add_deps (a, bs) G = Graph.add_deps_acyclic (a, bs) G
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    53
      handle Graph.CYCLES cycles => err_cyclic cycles;
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    54
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    55
    val (c, T) = lhs;
17803
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    56
    val spec = (serial (), (name, T));
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    57
    val no_overloading = Type.raw_instance (const_type c, T);
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    58
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    59
    val consts' =
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    60
      consts |> declare lhs |> fold declare rhs
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    61
      |> K no_overloading ? add_deps (c, map #1 rhs);
17803
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    62
    val specified' = specified
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    63
      |> Symtab.default (c, Inttab.empty)
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    64
      |> Symtab.map_entry c (fn specs =>
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    65
        (check_specified c spec specs; Inttab.update spec specs));
17994
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    66
  in (consts', specified') end);
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    67
19050
527bc006f2b6 specifications_of: avoid partiality;
wenzelm
parents: 19040
diff changeset
    68
fun specifications_of (Defs {specified, ...}) c =
527bc006f2b6 specifications_of: avoid partiality;
wenzelm
parents: 19040
diff changeset
    69
  (map (snd o snd) o Inttab.dest o the_default Inttab.empty o Symtab.lookup specified) c;
527bc006f2b6 specifications_of: avoid partiality;
wenzelm
parents: 19040
diff changeset
    70
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    71
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    72
(* empty and merge *)
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    73
17994
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    74
val empty = make_defs (Graph.empty, Symtab.empty);
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    75
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    76
fun merge pp
17994
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    77
   (Defs {consts = consts1, specified = specified1},
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    78
    Defs {consts = consts2, specified = specified2}) =
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    79
  let
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    80
    val consts' = (consts1, consts2) |> Graph.merge_acyclic (K true)
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    81
      handle Graph.CYCLES cycles => err_cyclic cycles;
17803
e235a57651a1 more efficient check_specified, much less invocations;
wenzelm
parents: 17712
diff changeset
    82
    val specified' = (specified1, specified2) |> Symtab.join (fn c => fn (orig_specs1, specs2) =>
19025
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 17994
diff changeset
    83
      Inttab.fold (fn spec2 => fn specs1 =>
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 17994
diff changeset
    84
        (check_specified c spec2 orig_specs1; Inttab.update spec2 specs1)) specs2 orig_specs1);
17994
6a1a49cba5b3 removed inappropriate monomorphic test;
wenzelm
parents: 17803
diff changeset
    85
  in make_defs (consts', specified') end;
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    86
16108
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    87
end;