src/Pure/defs.ML
author wenzelm
Thu, 10 Apr 2014 11:06:45 +0200
changeset 56507 5f6f2576a836
parent 56050 fdccbb97915a
child 57520 3ad1b289f21b
permissions -rw-r--r--
more contributors;
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
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
     2
    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
     3
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
     4
Global well-formedness checks for constant definitions.  Covers plain
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
     5
definitions and simple sub-structural overloading.
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
     6
*)
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
16877
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
     8
signature DEFS =
e92cba1d4842 tuned interfaces declare, define, finalize, merge:
wenzelm
parents: 16838
diff changeset
     9
sig
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
    10
  val pretty_const: Proof.context -> string * typ list -> Pretty.T
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
    11
  val plain_args: typ list -> bool
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    12
  type T
33712
cffc97238102 tuned signature;
wenzelm
parents: 33701
diff changeset
    13
  type spec =
55544
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    14
   {def: string option,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    15
    description: string,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    16
    pos: Position.T,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    17
    lhs: typ list,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    18
    rhs: (string * typ list) list}
33712
cffc97238102 tuned signature;
wenzelm
parents: 33701
diff changeset
    19
  val all_specifications_of: T -> (string * spec list) list
cffc97238102 tuned signature;
wenzelm
parents: 33701
diff changeset
    20
  val specifications_of: T -> string -> spec list
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    21
  val dest: T ->
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    22
   {restricts: ((string * typ list) * string) list,
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    23
    reducts: ((string * typ list) * (string * typ list) list) list}
19590
12af4942923d simple substructure test for typargs (independent type constructors);
wenzelm
parents: 19569
diff changeset
    24
  val empty: T
42389
b2c6033fc7e4 pass plain Proof.context for pretty printing;
wenzelm
parents: 42384
diff changeset
    25
  val merge: Proof.context -> T * T -> T
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
    26
  val define: Proof.context -> bool -> string option -> string ->
19727
wenzelm
parents: 19713
diff changeset
    27
    string * typ list -> (string * typ list) 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
    28
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
    29
17711
c16cbe73798c activate signature constraints;
wenzelm
parents: 17707
diff changeset
    30
structure Defs: DEFS =
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
    31
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
    32
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    33
(* type arguments *)
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
    34
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    35
type args = typ list;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    36
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
    37
fun pretty_const ctxt (c, args) =
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
    38
  let
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    39
    val prt_args =
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    40
      if null args then []
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
    41
      else [Pretty.list "(" ")" (map (Syntax.pretty_typ ctxt o Logic.unvarifyT_global) args)];
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    42
  in Pretty.block (Pretty.str c :: prt_args) end;
19624
wenzelm
parents: 19620
diff changeset
    43
19707
wenzelm
parents: 19701
diff changeset
    44
fun plain_args args =
wenzelm
parents: 19701
diff changeset
    45
  forall Term.is_TVar args andalso not (has_duplicates (op =) args);
wenzelm
parents: 19701
diff changeset
    46
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    47
fun disjoint_args (Ts, Us) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    48
  not (Type.could_unifys (Ts, Us)) orelse
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    49
    ((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
    50
      handle Type.TUNIFY => true);
19692
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
fun match_args (Ts, Us) =
56050
fdccbb97915a minor performance tuning via fast matching filter;
wenzelm
parents: 55544
diff changeset
    53
  if Type.could_matches (Ts, Us) then
fdccbb97915a minor performance tuning via fast matching filter;
wenzelm
parents: 55544
diff changeset
    54
    Option.map Envir.subst_type
fdccbb97915a minor performance tuning via fast matching filter;
wenzelm
parents: 55544
diff changeset
    55
      (SOME (Type.raw_matches (Ts, Us) Vartab.empty) handle Type.TYPE_MATCH => NONE)
fdccbb97915a minor performance tuning via fast matching filter;
wenzelm
parents: 55544
diff changeset
    56
  else NONE;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    57
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    58
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    59
(* datatype defs *)
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    60
33701
9dd1079cec3a primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents: 32785
diff changeset
    61
type spec =
55544
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    62
 {def: string option,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    63
  description: string,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    64
  pos: Position.T,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    65
  lhs: args,
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    66
  rhs: (string * args) list};
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    67
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    68
type def =
55544
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    69
 {specs: spec Inttab.table,  (*source specifications*)
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
    70
  restricts: (args * string) list,  (*global restrictions imposed by incomplete patterns*)
19712
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
    71
  reducts: (args * (string * args) list) list};  (*specifications as reduction system*)
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    72
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    73
fun make_def (specs, restricts, reducts) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    74
  {specs = specs, restricts = restricts, reducts = reducts}: def;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    75
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    76
fun map_def c f =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    77
  Symtab.default (c, make_def (Inttab.empty, [], [])) #>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    78
  Symtab.map_entry c (fn {specs, restricts, reducts}: def =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    79
    make_def (f (specs, restricts, reducts)));
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    80
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    81
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    82
datatype T = Defs of def Symtab.table;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    83
19712
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
    84
fun lookup_list which defs c =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    85
  (case Symtab.lookup defs c of
19713
69c71d40f8a8 made smlnj happy;
wenzelm
parents: 19712
diff changeset
    86
    SOME (def: def) => which def
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    87
  | NONE => []);
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    88
32050
wenzelm
parents: 32035
diff changeset
    89
fun all_specifications_of (Defs defs) =
wenzelm
parents: 32035
diff changeset
    90
  (map o apsnd) (map snd o Inttab.dest o #specs) (Symtab.dest defs);
wenzelm
parents: 32035
diff changeset
    91
24199
8be734b5f59f new access interface in defs.ML
haftmann
parents: 20668
diff changeset
    92
fun specifications_of (Defs defs) = lookup_list (map snd o Inttab.dest o #specs) defs;
32050
wenzelm
parents: 32035
diff changeset
    93
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    94
val restricts_of = lookup_list #restricts;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    95
val reducts_of = lookup_list #reducts;
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
    96
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    97
fun dest (Defs defs) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    98
  let
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
    99
    val restricts = Symtab.fold (fn (c, {restricts, ...}) =>
33701
9dd1079cec3a primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents: 32785
diff changeset
   100
      fold (fn (args, description) => cons ((c, args), description)) restricts) defs [];
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   101
    val reducts = Symtab.fold (fn (c, {reducts, ...}) =>
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   102
      fold (fn (args, deps) => cons ((c, args), deps)) reducts) defs [];
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   103
  in {restricts = restricts, reducts = reducts} end;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   104
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   105
val empty = Defs Symtab.empty;
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
(* specifications *)
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   109
55544
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
   110
fun disjoint_specs c (i, {description = a, pos = pos_a, lhs = Ts, ...}: spec) =
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
   111
  Inttab.forall (fn (j, {description = b, pos = pos_b, lhs = Us, ...}: spec) =>
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   112
    i = j orelse disjoint_args (Ts, Us) orelse
55544
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
   113
      error ("Clash of specifications for constant " ^ quote c ^ ":\n" ^
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
   114
        "  " ^ quote a ^ Position.here pos_a ^ "\n" ^
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
   115
        "  " ^ quote b ^ Position.here pos_b));
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   116
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   117
fun join_specs c ({specs = specs1, restricts, reducts}, {specs = specs2, ...}: def) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   118
  let
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   119
    val specs' =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   120
      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
   121
  in make_def (specs', restricts, reducts) end;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   122
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   123
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
   124
  (disjoint_specs c spec specs; (Inttab.update spec specs, restricts, reducts)));
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   125
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   126
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   127
(* normalized dependencies: reduction with well-formedness check *)
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   128
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   129
local
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   130
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   131
val prt = Pretty.string_of oo pretty_const;
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   132
fun err ctxt (c, args) (d, Us) s1 s2 =
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   133
  error (s1 ^ " dependency of constant " ^ prt ctxt (c, args) ^ " -> " ^ prt ctxt (d, Us) ^ s2);
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   134
19712
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
   135
fun contained (U as TVar _) (Type (_, Ts)) = exists (fn T => T = U orelse contained U T) Ts
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   136
  | contained _ _ = false;
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   137
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   138
fun acyclic ctxt (c, args) (d, Us) =
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   139
  c <> d orelse
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   140
  exists (fn U => exists (contained U) args) Us orelse
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   141
  is_none (match_args (args, Us)) orelse
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   142
  err ctxt (c, args) (d, Us) "Circular" "";
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   143
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   144
fun wellformed ctxt defs (c, args) (d, Us) =
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   145
  forall is_TVar Us orelse
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   146
  (case find_first (fn (Ts, _) => not (disjoint_args (Ts, Us))) (restricts_of defs d) of
33701
9dd1079cec3a primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents: 32785
diff changeset
   147
    SOME (Ts, description) =>
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   148
      err ctxt (c, args) (d, Us) "Malformed"
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   149
        ("\n(restriction " ^ prt ctxt (d, Ts) ^ " from " ^ quote description ^ ")")
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   150
  | NONE => true);
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   151
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   152
fun reduction ctxt defs const deps =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   153
  let
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   154
    fun reduct Us (Ts, rhs) =
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   155
      (case match_args (Ts, Us) of
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   156
        NONE => NONE
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   157
      | SOME subst => SOME (map (apsnd (map subst)) rhs));
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   158
    fun reducts (d, Us) = get_first (reduct Us) (reducts_of defs d);
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   159
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   160
    val reds = map (`reducts) deps;
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   161
    val deps' =
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   162
      if forall (is_none o #1) reds then NONE
20668
wenzelm
parents: 20390
diff changeset
   163
      else SOME (fold_rev
wenzelm
parents: 20390
diff changeset
   164
        (fn (NONE, dp) => insert (op =) dp | (SOME dps, _) => fold (insert (op =)) dps) reds []);
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   165
    val _ = forall (acyclic ctxt const) (the_default deps deps');
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   166
  in deps' end;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   167
19760
c7e9cc10acc8 merge: always normalize (and check!) reductions;
wenzelm
parents: 19729
diff changeset
   168
in
c7e9cc10acc8 merge: always normalize (and check!) reductions;
wenzelm
parents: 19729
diff changeset
   169
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   170
fun normalize ctxt =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   171
  let
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   172
    fun norm_update (c, {reducts, ...}: def) (changed, defs) =
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   173
      let
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   174
        val reducts' = reducts |> map (fn (args, deps) =>
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   175
          (args, perhaps (reduction ctxt defs (c, args)) deps));
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   176
      in
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   177
        if reducts = reducts' then (changed, defs)
32785
ec5292653aff eliminated redundant parameters;
wenzelm
parents: 32050
diff changeset
   178
        else (true, defs |> map_def c (fn (specs, restricts, _) => (specs, restricts, reducts')))
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   179
      end;
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   180
    fun norm_all defs =
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   181
      (case Symtab.fold norm_update defs (false, defs) of
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   182
        (true, defs') => norm_all defs'
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   183
      | (false, _) => defs);
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   184
    fun check defs (c, {reducts, ...}: def) =
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   185
      reducts |> forall (fn (args, deps) => forall (wellformed ctxt defs (c, args)) deps);
19729
cb9e2f0c7658 separate checks for acyclic/wellformed;
wenzelm
parents: 19727
diff changeset
   186
  in norm_all #> (fn defs => tap (Symtab.forall (check defs)) defs) end;
19701
c07c31ac689b export plain_args;
wenzelm
parents: 19697
diff changeset
   187
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   188
fun dependencies ctxt (c, args) restr deps =
19712
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
   189
  map_def c (fn (specs, restricts, reducts) =>
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
   190
    let
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
   191
      val restricts' = Library.merge (op =) (restricts, restr);
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
   192
      val reducts' = insert (op =) (args, deps) reducts;
3ae3cc4b1eac wellformed: be less ambitious about structural containment;
wenzelm
parents: 19707
diff changeset
   193
    in (specs, restricts', reducts') end)
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   194
  #> normalize ctxt;
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   195
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   196
end;
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   197
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   198
19624
wenzelm
parents: 19620
diff changeset
   199
(* merge *)
wenzelm
parents: 19620
diff changeset
   200
42389
b2c6033fc7e4 pass plain Proof.context for pretty printing;
wenzelm
parents: 42384
diff changeset
   201
fun merge ctxt (Defs defs1, Defs defs2) =
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   202
  let
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   203
    fun add_deps (c, args) restr deps defs =
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   204
      if AList.defined (op =) (reducts_of defs c) args then defs
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   205
      else dependencies ctxt (c, args) restr deps defs;
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   206
    fun add_def (c, {restricts, reducts, ...}: def) =
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   207
      fold (fn (args, deps) => add_deps (c, args) restricts deps) reducts;
19760
c7e9cc10acc8 merge: always normalize (and check!) reductions;
wenzelm
parents: 19729
diff changeset
   208
  in
c7e9cc10acc8 merge: always normalize (and check!) reductions;
wenzelm
parents: 19729
diff changeset
   209
    Defs (Symtab.join join_specs (defs1, defs2)
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   210
      |> normalize ctxt |> Symtab.fold add_def defs2)
19760
c7e9cc10acc8 merge: always normalize (and check!) reductions;
wenzelm
parents: 19729
diff changeset
   211
  end;
19613
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   212
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   213
9bf274ec94cf allow dependencies of disjoint collections of instances;
wenzelm
parents: 19590
diff changeset
   214
(* define *)
19590
12af4942923d simple substructure test for typargs (independent type constructors);
wenzelm
parents: 19569
diff changeset
   215
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   216
fun define ctxt unchecked def description (c, args) deps (Defs defs) =
17707
bc0270e9d27f back to simple 'defs' (cf. revision 1.79 of theory.ML);
wenzelm
parents: 17670
diff changeset
   217
  let
55544
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
   218
    val pos = Position.thread_data ();
19697
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
32785
ec5292653aff eliminated redundant parameters;
wenzelm
parents: 32050
diff changeset
   221
        (case args of [Type (_, rec_args)] => plain_args rec_args | _ => false)
33701
9dd1079cec3a primitive defs: clarified def (axiom name) vs. description;
wenzelm
parents: 32785
diff changeset
   222
      then [] else [(args, description)];
19692
bad13b32c0f3 yet another re-implementation:
wenzelm
parents: 19628
diff changeset
   223
    val spec =
55544
cf1baba89a27 more informative error;
wenzelm
parents: 42389
diff changeset
   224
      (serial (), {def = def, description = description, pos = pos, lhs = args, rhs = deps});
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   225
    val defs' = defs |> update_specs c spec;
42384
6b8e28b52ae3 pass plain Proof.context for pretty printing;
wenzelm
parents: 42383
diff changeset
   226
  in Defs (defs' |> (if unchecked then I else dependencies ctxt (c, args) restr deps)) end;
19697
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   227
423af2e013b8 specifications_of: lhs/rhs represented as typargs;
wenzelm
parents: 19695
diff changeset
   228
end;