src/Pure/defs.ML
author wenzelm
Mon, 22 May 2006 22:29:15 +0200
changeset 19697 423af2e013b8
parent 19695 7706aeac6cf1
child 19701 c07c31ac689b
permissions -rw-r--r--
specifications_of: lhs/rhs represented as typargs; export pretty_const; export dest; more precise checking of lhs patterns; more precise normalization; misc cleanup;
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
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
     5
Global well-formedness checks for constant definitions.  Covers plain
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
     6
definitions and simple sub-structural overloading (depending on a
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
     7
single type argument).
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
     8
*)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
     9
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    10
signature DEFS =
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
    11
sig
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    12
  val pretty_const: Pretty.pp -> string * typ list -> Pretty.T
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    13
  type T
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    14
  val specifications_of: T -> string -> (serial * {is_def: bool, module: string, name: string,
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    15
    lhs: typ list, rhs: (string * typ list) list}) list
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    16
  val dest: T ->
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    17
   {restricts: ((string * typ list) * string) list,
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    18
    reducts: ((string * typ list) * (string * typ list) list) list}
19590
12af4942923d simple substructure test for typargs (independent type constructors);
wenzelm
parents: 19569
diff changeset
    19
  val empty: T
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    20
  val merge: Pretty.pp -> T * T -> T
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    21
  val define: Pretty.pp -> Consts.T ->
19628
de019ddcd89e actually reject malformed defs;
wenzelm
parents: 19624
diff changeset
    22
    bool -> bool -> string -> string -> string * typ -> (string * typ) list -> 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
    23
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
    24
17711
c16cbe73798c activate signature constraints;
wenzelm
parents: 17707
diff changeset
    25
structure Defs: DEFS =
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    26
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
    27
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    28
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    29
(* type arguments *)
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
    30
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    31
type args = typ list;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    32
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    33
fun pretty_const pp (c, args) =
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
    34
  let
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    35
    val prt_args =
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    36
      if null args then []
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    37
      else [Pretty.list "(" ")" (map (Pretty.typ pp o Type.freeze_type) args)];
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    38
  in Pretty.block (Pretty.str c :: prt_args) end;
19624
wenzelm
parents: 19620
diff changeset
    39
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    40
fun disjoint_args (Ts, Us) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    41
  not (Type.could_unifys (Ts, Us)) orelse
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    42
    ((Type.raw_unifys (Ts, map (Logic.incr_tvar (maxidx_of_typs Ts + 1)) Us) Vartab.empty; false)
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    43
      handle Type.TUNIFY => true);
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    44
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    45
fun match_args (Ts, Us) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    46
  Option.map Envir.typ_subst_TVars
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    47
    (SOME (Type.raw_matches (Ts, Us) Vartab.empty) handle Type.TYPE_MATCH => NONE);
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    48
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    49
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    50
(* datatype defs *)
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    51
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    52
type spec = {is_def: bool, module: string, name: string, lhs: args, rhs: (string * args) list};
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    53
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    54
type def =
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    55
 {specs: spec Inttab.table,
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    56
  restricts: (args * string) list,
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    57
  reducts: (args * (string * args) list) list};
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    58
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    59
fun make_def (specs, restricts, reducts) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    60
  {specs = specs, restricts = restricts, reducts = reducts}: def;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    61
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    62
fun map_def c f =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    63
  Symtab.default (c, make_def (Inttab.empty, [], [])) #>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    64
  Symtab.map_entry c (fn {specs, restricts, reducts}: def =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    65
    make_def (f (specs, restricts, reducts)));
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    66
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    67
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    68
datatype T = Defs of def Symtab.table;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    69
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    70
fun lookup_list which (Defs defs) c =
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    71
  (case Symtab.lookup defs c of
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    72
    SOME def => which def
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    73
  | NONE => []);
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    74
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    75
val specifications_of = lookup_list (Inttab.dest o #specs);
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    76
val restricts_of = lookup_list #restricts;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    77
val reducts_of = lookup_list #reducts;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    78
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    79
fun dest (Defs defs) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    80
  let
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    81
    val restricts = Symtab.fold (fn (c, {restricts, ...}) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    82
      fold (fn (args, name) => cons ((c, args), name)) restricts) defs [];
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    83
    val reducts = Symtab.fold (fn (c, {reducts, ...}) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    84
      fold (fn (args, deps) => cons ((c, args), deps)) reducts) defs [];
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    85
  in {restricts = restricts, reducts = reducts} end;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    86
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    87
val empty = Defs Symtab.empty;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    88
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    89
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    90
(* specifications *)
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    91
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    92
fun disjoint_specs c (i, {lhs = Ts, name = a, ...}: spec) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    93
  Inttab.forall (fn (j, {lhs = Us, name = b, ...}: spec) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    94
    i = j orelse disjoint_args (Ts, Us) orelse
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    95
      error ("Type clash in specifications " ^ quote a ^ " and " ^ quote b ^
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    96
        " for constant " ^ quote c));
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    97
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    98
fun join_specs c ({specs = specs1, restricts, reducts}, {specs = specs2, ...}: def) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    99
  let
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   100
    val specs' =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   101
      Inttab.fold (fn spec2 => (disjoint_specs c spec2 specs1; Inttab.update spec2)) specs2 specs1;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   102
  in make_def (specs', restricts, reducts) end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   103
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   104
fun update_specs c spec = map_def c (fn (specs, restricts, reducts) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   105
  (disjoint_specs c spec specs; (Inttab.update spec specs, restricts, reducts)));
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   106
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   107
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   108
(* normalization: reduction and well-formedness check *)
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   109
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   110
local
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   111
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   112
fun reduction reds_of deps =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   113
  let
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   114
    fun reduct Us (Ts, rhs) =
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   115
      (case match_args (Ts, Us) of
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   116
        NONE => NONE
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   117
      | SOME subst => SOME (map (apsnd (map subst)) rhs));
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   118
    fun reducts (d: string, Us) = get_first (reduct Us) (reds_of d);
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   119
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   120
    fun add (NONE, dp) = insert (op =) dp
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   121
      | add (SOME dps, _) = fold (insert (op =)) dps;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   122
    val deps' = map (`reducts) deps;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   123
  in
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   124
    if forall (is_none o #1) deps' then NONE
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   125
    else SOME (fold_rev add deps' [])
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   126
  end;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   127
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   128
fun reductions reds_of deps =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   129
  (case reduction reds_of deps of
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   130
    SOME deps' => reductions reds_of deps'
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   131
  | NONE => deps);
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   132
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   133
fun contained U (Type (_, Ts)) = exists (fn T => T = U orelse contained U T) Ts
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   134
  | contained _ _ = false;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   135
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   136
fun wellformed pp rests_of (c, args) (d, Us) =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   137
  let
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   138
    val prt = Pretty.string_of o pretty_const pp;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   139
    fun err s1 s2 =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   140
      error (s1 ^ " dependency of constant " ^ prt (c, args) ^ " -> " ^ prt (d, Us) ^ s2);
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   141
  in
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   142
    exists (fn U => exists (contained U) args) Us orelse
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   143
    (c <> d andalso exists (member (op =) args) Us) orelse
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   144
      (case find_first (fn (Ts, _) => not (disjoint_args (Ts, Us))) (rests_of d) of
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   145
        NONE =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   146
          c <> d orelse is_none (match_args (args, Us)) orelse err "Circular" ""
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   147
      | SOME (Ts, name) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   148
          if c = d then err "Circular" ("\n(via " ^ quote name ^ ")")
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   149
          else
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   150
            err "Malformed" ("\n(restriction " ^ prt (d, Ts) ^ " from " ^ quote name ^ ")"))
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   151
  end;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   152
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   153
fun normalize pp rests_of reds_of (c, args) deps =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   154
  let
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   155
    val deps' = reductions reds_of deps;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   156
    val _ = forall (wellformed pp rests_of (c, args)) deps';
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   157
  in deps' end;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   158
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   159
fun normalize_all pp (c, args) deps defs =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   160
  let
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   161
    val norm = normalize pp (restricts_of (Defs defs));
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   162
    val norm_rule = norm (fn c' => if c' = c then [(args, deps)] else []);
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   163
    val norm_defs = norm (reducts_of (Defs defs));
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   164
    fun norm_update (c', {reducts, ...}: def) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   165
      let val reducts' = reducts
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   166
        |> map (fn (args', deps') => (args', norm_defs (c', args') (norm_rule (c', args') deps')))
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   167
      in
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   168
        K (reducts <> reducts') ?
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   169
          map_def c' (fn (specs, restricts, reducts) => (specs, restricts, reducts'))
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   170
      end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   171
  in Symtab.fold norm_update defs defs end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   172
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   173
in
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   174
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   175
fun dependencies pp (c, args) restr deps (Defs defs) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   176
  let
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   177
    val deps' = normalize pp (restricts_of (Defs defs)) (reducts_of (Defs defs)) (c, args) deps;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   178
    val defs' = defs
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   179
      |> map_def c (fn (specs, restricts, reducts) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   180
        (specs, Library.merge (op =) (restricts, restr), reducts))
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   181
      |> normalize_all pp (c, args) deps';
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   182
    val deps'' =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   183
      normalize pp (restricts_of (Defs defs')) (reducts_of (Defs defs')) (c, args) deps';
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   184
    val defs'' = defs'
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   185
      |> map_def c (fn (specs, restricts, reducts) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   186
        (specs, restricts, insert (op =) (args, deps'') reducts));
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   187
  in Defs defs'' end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   188
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   189
end;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   190
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   191
19624
wenzelm
parents: 19620
diff changeset
   192
(* merge *)
wenzelm
parents: 19620
diff changeset
   193
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   194
fun merge pp (Defs defs1, Defs defs2) =
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   195
  let
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   196
    fun add_deps (c, args) restr deps defs =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   197
      if AList.defined (op =) (reducts_of defs c) args then defs
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   198
      else dependencies pp (c, args) restr deps defs;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   199
    fun add_def (c, {restricts, reducts, ...}: def) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   200
      fold (fn (args, deps) => add_deps (c, args) restricts deps) reducts;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   201
  in Defs (Symtab.join join_specs (defs1, defs2)) |> Symtab.fold add_def defs2 end;
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   202
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   203
local  (* FIXME *)
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   204
  val merge_aux = merge
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   205
  val acc = Output.time_accumulator "Defs.merge"
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   206
in fun merge pp = acc (merge_aux pp) end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   207
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   208
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   209
(* define *)
19590
12af4942923d simple substructure test for typargs (independent type constructors);
wenzelm
parents: 19569
diff changeset
   210
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   211
fun plain_args args =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   212
  forall Term.is_TVar args andalso not (has_duplicates (op =) args);
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   213
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   214
fun define pp consts unchecked is_def module name lhs rhs (Defs defs) =
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
   215
  let
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   216
    fun typargs const = (#1 const, Consts.typargs consts const);
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   217
    val (c, args) = typargs lhs;
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   218
    val deps = map typargs rhs;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   219
    val restr =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   220
      if plain_args args orelse
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   221
        (case args of [Type (a, rec_args)] => plain_args rec_args | _ => false)
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   222
      then [] else [(args, name)];
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   223
    val spec =
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   224
      (serial (), {is_def = is_def, module = module, name = name, lhs = args, rhs = deps});
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   225
    val defs' = defs |> update_specs c spec;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   226
  in Defs defs' |> (if unchecked then I else dependencies pp (c, args) restr deps) end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   227
19628
de019ddcd89e actually reject malformed defs;
wenzelm
parents: 19624
diff changeset
   228
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   229
local  (* FIXME *)
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   230
  val define_aux = define
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   231
  val acc = Output.time_accumulator "Defs.define"
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   232
in
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   233
  fun define pp consts unchecked is_def module name lhs rhs =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   234
    acc (define_aux pp consts unchecked is_def module name lhs rhs)
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   235
end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   236
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
   237
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
   238
end;