src/Pure/defs.ML
author obua
Tue Sep 27 14:41:41 2005 +0200 (2005-09-27)
changeset 17670 bf4f2c1b26cc
parent 17669 94dbbffbf94b
child 17707 bc0270e9d27f
permissions -rw-r--r--
corrected spelling bug
obua@16108
     1
(*  Title:      Pure/General/defs.ML
obua@16108
     2
    ID:         $Id$
obua@16108
     3
    Author:     Steven Obua, TU Muenchen
obua@16108
     4
wenzelm@16982
     5
Checks if definitions preserve consistency of logic by enforcing that
wenzelm@16982
     6
there are no cyclic definitions. The algorithm is described in "An
wenzelm@16982
     7
Algorithm for Determining Definitional Cycles in Higher-Order Logic
wenzelm@16982
     8
with Overloading", Steven Obua, technical report, to be written :-)
obua@17669
     9
obua@17669
    10
ATTENTION:
obua@17670
    11
Currently this implementation of the cycle test contains a bug of which the author is fully aware.
obua@17669
    12
This bug makes it possible to introduce inconsistent definitional cycles in Isabelle. 
obua@16108
    13
*)
obua@16108
    14
wenzelm@16877
    15
signature DEFS =
wenzelm@16877
    16
sig
wenzelm@16982
    17
  (*true: record the full chain of definitions that lead to a circularity*)
wenzelm@16982
    18
  val chain_history: bool ref
obua@16198
    19
  type graph
wenzelm@16877
    20
  val empty: graph
wenzelm@16982
    21
  val declare: theory -> string * typ -> graph -> graph
wenzelm@16982
    22
  val define: theory -> string * typ -> string -> (string * typ) list -> graph -> graph
wenzelm@16982
    23
  val finalize: theory -> string * typ -> graph -> graph
wenzelm@16877
    24
  val merge: Pretty.pp -> graph -> graph -> graph
wenzelm@16982
    25
  val finals: graph -> typ list Symtab.table
obua@16743
    26
  datatype overloadingstate = Open | Closed | Final
wenzelm@16982
    27
  val overloading_info: graph -> string -> (typ * (string*typ) list * overloadingstate) option
wenzelm@16982
    28
  val monomorphic: graph -> string -> bool
obua@16108
    29
end
obua@16108
    30
obua@16108
    31
structure Defs :> DEFS = struct
obua@16108
    32
obua@16108
    33
type tyenv = Type.tyenv
obua@16108
    34
type edgelabel = (int * typ * typ * (typ * string * string) list)
obua@16108
    35
obua@16743
    36
datatype overloadingstate = Open | Closed | Final
obua@16361
    37
obua@16108
    38
datatype node = Node of
obua@16308
    39
         typ  (* most general type of constant *)
obua@16361
    40
         * defnode Symtab.table
wenzelm@16877
    41
             (* a table of defnodes, each corresponding to 1 definition of the
obua@16308
    42
                constant for a particular type, indexed by axiom name *)
wenzelm@16877
    43
         * (unit Symtab.table) Symtab.table
wenzelm@16877
    44
             (* a table of all back referencing defnodes to this node,
obua@16308
    45
                indexed by node name of the defnodes *)
obua@16198
    46
         * typ list (* a list of all finalized types *)
obua@16743
    47
         * overloadingstate
wenzelm@16877
    48
obua@16198
    49
     and defnode = Defnode of
obua@16198
    50
         typ  (* type of the constant in this particular definition *)
obua@16308
    51
         * (edgelabel list) Symtab.table (* The edges, grouped by nodes. *)
obua@16108
    52
wenzelm@17412
    53
fun getnode graph = the o Symtab.lookup graph
obua@16361
    54
fun get_nodedefs (Node (_, defs, _, _, _)) = defs
wenzelm@17412
    55
fun get_defnode (Node (_, defs, _, _, _)) defname = Symtab.lookup defs defname
wenzelm@17223
    56
fun get_defnode' graph noderef =
wenzelm@17412
    57
  Symtab.lookup (get_nodedefs (the (Symtab.lookup graph noderef)))
obua@16108
    58
wenzelm@17223
    59
fun table_size table = Symtab.fold (K (fn x => x + 1)) table 0;
wenzelm@16877
    60
wenzelm@16982
    61
datatype graphaction =
wenzelm@16982
    62
    Declare of string * typ
wenzelm@16982
    63
  | Define of string * typ * string * string * (string * typ) list
wenzelm@16982
    64
  | Finalize of string * typ
wenzelm@16877
    65
wenzelm@16982
    66
type graph = int * string Symtab.table * graphaction list * node Symtab.table
obua@16308
    67
wenzelm@16982
    68
val chain_history = ref true
obua@16308
    69
obua@16384
    70
val empty = (0, Symtab.empty, [], Symtab.empty)
obua@16108
    71
obua@16108
    72
exception DEFS of string;
obua@16108
    73
exception CIRCULAR of (typ * string * string) list;
obua@16113
    74
exception INFINITE_CHAIN of (typ * string * string) list;
obua@16108
    75
exception CLASH of string * string * string;
obua@16158
    76
exception FINAL of string * typ;
obua@16108
    77
obua@16108
    78
fun def_err s = raise (DEFS s)
obua@16108
    79
wenzelm@16877
    80
fun no_forwards defs =
wenzelm@16877
    81
    Symtab.foldl
wenzelm@16877
    82
    (fn (closed, (_, Defnode (_, edges))) =>
obua@16361
    83
        if not closed then false else Symtab.is_empty edges)
obua@16361
    84
    (true, defs)
obua@16361
    85
wenzelm@16877
    86
fun checkT' (Type (a, Ts)) = Type (a, map checkT' Ts)
obua@17669
    87
  | checkT' (TFree (a, _)) = TVar ((a, 0), [])        
wenzelm@16877
    88
  | checkT' (TVar ((a, 0), _)) = TVar ((a, 0), [])
wenzelm@16877
    89
  | checkT' (T as TVar _) = raise TYPE ("Illegal schematic type variable encountered", [T], []);
obua@16384
    90
wenzelm@16982
    91
fun checkT thy = Compress.typ thy o checkT';
obua@16384
    92
wenzelm@16877
    93
fun rename ty1 ty2 = Logic.incr_tvar ((maxidx_of_typ ty1)+1) ty2;
obua@16108
    94
obua@16108
    95
fun subst_incr_tvar inc t =
wenzelm@17223
    96
    if inc > 0 then
obua@16198
    97
      let
wenzelm@16877
    98
        val tv = typ_tvars t
wenzelm@16877
    99
        val t' = Logic.incr_tvar inc t
wenzelm@17223
   100
        fun update_subst ((n, i), _) =
wenzelm@17412
   101
          Vartab.update ((n, i), ([], TVar ((n, i + inc), [])));
obua@16198
   102
      in
wenzelm@17223
   103
        (t', fold update_subst tv Vartab.empty)
wenzelm@16877
   104
      end
obua@16108
   105
    else
obua@16198
   106
      (t, Vartab.empty)
wenzelm@16877
   107
obua@16108
   108
fun subst s ty = Envir.norm_type s ty
wenzelm@16877
   109
obua@16108
   110
fun subst_history s history = map (fn (ty, cn, dn) => (subst s ty, cn, dn)) history
wenzelm@16877
   111
obua@16108
   112
fun is_instance instance_ty general_ty =
wenzelm@16936
   113
    Type.raw_instance (instance_ty, general_ty)
wenzelm@16877
   114
obua@16108
   115
fun is_instance_r instance_ty general_ty =
obua@16108
   116
    is_instance instance_ty (rename instance_ty general_ty)
wenzelm@16877
   117
wenzelm@16877
   118
fun unify ty1 ty2 =
wenzelm@16936
   119
    SOME (Type.raw_unify (ty1, ty2) Vartab.empty)
obua@16108
   120
    handle Type.TUNIFY => NONE
wenzelm@16877
   121
wenzelm@16877
   122
(*
wenzelm@16877
   123
   Unifies ty1 and ty2, renaming ty1 and ty2 so that they have greater indices than max and
wenzelm@16877
   124
   so that they are different. All indices in ty1 and ty2 are supposed to be less than or
obua@16308
   125
   equal to max.
wenzelm@16877
   126
   Returns SOME (max', s1, s2), so that s1(ty1) = s2(ty2) and max' is greater or equal than
obua@16308
   127
   all indices in s1, s2, ty1, ty2.
obua@16108
   128
*)
wenzelm@16877
   129
fun unify_r max ty1 ty2 =
obua@16108
   130
    let
obua@16308
   131
      val max = Int.max(max, 0)
obua@16198
   132
      val max1 = max (* >= maxidx_of_typ ty1 *)
obua@16198
   133
      val max2 = max (* >= maxidx_of_typ ty2 *)
obua@16198
   134
      val max = Int.max(max, Int.max (max1, max2))
obua@16308
   135
      val (ty1, s1) = subst_incr_tvar (max + 1) ty1
obua@16308
   136
      val (ty2, s2) = subst_incr_tvar (max + max1 + 2) ty2
wenzelm@16877
   137
      val max = max + max1 + max2 + 2
obua@16198
   138
      fun merge a b = Vartab.merge (fn _ => false) (a, b)
obua@16108
   139
    in
obua@16198
   140
      case unify ty1 ty2 of
wenzelm@16877
   141
        NONE => NONE
obua@16198
   142
      | SOME s => SOME (max, merge s1 s, merge s2 s)
obua@16108
   143
    end
wenzelm@16877
   144
wenzelm@16982
   145
fun can_be_unified_r ty1 ty2 = is_some (unify ty1 (rename ty1 ty2))
wenzelm@16982
   146
fun can_be_unified ty1 ty2 = is_some (unify ty1 ty2)
wenzelm@16877
   147
obua@16308
   148
fun normalize_edge_idx (edge as (maxidx, u1, v1, history)) =
obua@16308
   149
    if maxidx <= 1000000 then edge else
obua@16308
   150
    let
wenzelm@16877
   151
wenzelm@16877
   152
      fun idxlist idx extract_ty inject_ty (tab, max) ts =
wenzelm@16877
   153
          foldr
wenzelm@16877
   154
            (fn (e, ((tab, max), ts)) =>
obua@16308
   155
                let
obua@16308
   156
                  val ((tab, max), ty) = idx (tab, max) (extract_ty e)
obua@16308
   157
                  val e = inject_ty (ty, e)
obua@16308
   158
                in
obua@16308
   159
                  ((tab, max), e::ts)
obua@16308
   160
                end)
obua@16308
   161
            ((tab,max), []) ts
wenzelm@16877
   162
wenzelm@16877
   163
      fun idx (tab,max) (TVar ((a,i),_)) =
wenzelm@17412
   164
          (case Inttab.lookup tab i of
obua@16308
   165
             SOME j => ((tab, max), TVar ((a,j),[]))
wenzelm@17412
   166
           | NONE => ((Inttab.update (i, max) tab, max + 1), TVar ((a,max),[])))
wenzelm@16877
   167
        | idx (tab,max) (Type (t, ts)) =
wenzelm@16877
   168
          let
obua@16308
   169
            val ((tab, max), ts) = idxlist idx I fst (tab, max) ts
obua@16308
   170
          in
obua@16308
   171
            ((tab,max), Type (t, ts))
obua@16308
   172
          end
obua@16308
   173
        | idx (tab, max) ty = ((tab, max), ty)
wenzelm@16877
   174
obua@16308
   175
      val ((tab,max), u1) = idx (Inttab.empty, 0) u1
obua@16308
   176
      val ((tab,max), v1) = idx (tab, max) v1
wenzelm@16877
   177
      val ((tab,max), history) =
obua@16308
   178
          idxlist idx
wenzelm@16877
   179
            (fn (ty,_,_) => ty)
wenzelm@16877
   180
            (fn (ty, (_, s1, s2)) => (ty, s1, s2))
obua@16308
   181
            (tab, max) history
obua@16308
   182
    in
obua@16308
   183
      (max, u1, v1, history)
obua@16308
   184
    end
wenzelm@16877
   185
obua@16108
   186
fun compare_edges (e1 as (maxidx1, u1, v1, history1)) (e2 as (maxidx2, u2, v2, history2)) =
obua@16108
   187
    let
obua@16198
   188
      val t1 = u1 --> v1
wenzelm@16877
   189
      val t2 = Logic.incr_tvar (maxidx1+1) (u2 --> v2)
obua@16108
   190
    in
obua@16308
   191
      if (is_instance t1 t2) then
wenzelm@16877
   192
        (if is_instance t2 t1 then
wenzelm@16877
   193
           SOME (int_ord (length history2, length history1))
wenzelm@16877
   194
         else
wenzelm@16877
   195
           SOME LESS)
obua@16308
   196
      else if (is_instance t2 t1) then
wenzelm@16877
   197
        SOME GREATER
obua@16198
   198
      else
wenzelm@16877
   199
        NONE
obua@16108
   200
    end
obua@16308
   201
obua@16308
   202
fun merge_edges_1 (x, []) = [x]
wenzelm@16877
   203
  | merge_edges_1 (x, (y::ys)) =
obua@16108
   204
    (case compare_edges x y of
obua@16198
   205
       SOME LESS => (y::ys)
obua@16198
   206
     | SOME EQUAL => (y::ys)
obua@16198
   207
     | SOME GREATER => merge_edges_1 (x, ys)
obua@16198
   208
     | NONE => y::(merge_edges_1 (x, ys)))
wenzelm@16877
   209
obua@16108
   210
fun merge_edges xs ys = foldl merge_edges_1 xs ys
obua@16108
   211
obua@16384
   212
fun declare' (g as (cost, axmap, actions, graph)) (cty as (name, ty)) =
wenzelm@16877
   213
    (cost, axmap, (Declare cty)::actions,
wenzelm@17412
   214
     Symtab.update_new (name, Node (ty, Symtab.empty, Symtab.empty, [], Open)) graph)
wenzelm@16877
   215
    handle Symtab.DUP _ =>
obua@16361
   216
           let
wenzelm@17412
   217
             val (Node (gty, _, _, _, _)) = the (Symtab.lookup graph name)
obua@16361
   218
           in
obua@16361
   219
             if is_instance_r ty gty andalso is_instance_r gty ty then
obua@16361
   220
               g
obua@16361
   221
             else
obua@16361
   222
               def_err "constant is already declared with different type"
obua@16361
   223
           end
obua@16361
   224
wenzelm@16982
   225
fun declare'' thy g (name, ty) = declare' g (name, checkT thy ty)
obua@16361
   226
obua@16384
   227
val axcounter = ref (IntInf.fromInt 0)
obua@16384
   228
fun newaxname axmap axname =
obua@16384
   229
    let
obua@16384
   230
      val c = !axcounter
obua@16384
   231
      val _ = axcounter := c+1
obua@16384
   232
      val axname' = axname^"_"^(IntInf.toString c)
obua@16384
   233
    in
wenzelm@17412
   234
      (Symtab.update (axname', axname) axmap, axname')
obua@16384
   235
    end
obua@16384
   236
wenzelm@16877
   237
fun translate_ex axmap x =
obua@16384
   238
    let
wenzelm@16877
   239
      fun translate (ty, nodename, axname) =
wenzelm@17412
   240
          (ty, nodename, the (Symtab.lookup axmap axname))
obua@16384
   241
    in
obua@16384
   242
      case x of
obua@16384
   243
        INFINITE_CHAIN chain => raise (INFINITE_CHAIN (map translate chain))
obua@16384
   244
      | CIRCULAR cycle => raise (CIRCULAR (map translate cycle))
obua@16384
   245
      | _ => raise x
obua@16384
   246
    end
obua@16384
   247
obua@16826
   248
fun define' (cost, axmap, actions, graph) (mainref, ty) axname orig_axname body =
obua@16108
   249
    let
wenzelm@17412
   250
      val mainnode  = (case Symtab.lookup graph mainref of
wenzelm@16877
   251
                         NONE => def_err ("constant "^mainref^" is not declared")
wenzelm@16877
   252
                       | SOME n => n)
obua@16361
   253
      val (Node (gty, defs, backs, finals, _)) = mainnode
wenzelm@16877
   254
      val _ = (if is_instance_r ty gty then ()
obua@16308
   255
               else def_err "type of constant does not match declared type")
wenzelm@16877
   256
      fun check_def (s, Defnode (ty', _)) =
wenzelm@16877
   257
          (if can_be_unified_r ty ty' then
wenzelm@16877
   258
             raise (CLASH (mainref, axname, s))
wenzelm@16877
   259
           else if s = axname then
wenzelm@16877
   260
             def_err "name of axiom is already used for another definition of this constant"
wenzelm@16877
   261
           else false)
obua@16198
   262
      val _ = Symtab.exists check_def defs
wenzelm@16877
   263
      fun check_final finalty =
wenzelm@16877
   264
          (if can_be_unified_r finalty ty then
wenzelm@16877
   265
             raise (FINAL (mainref, finalty))
wenzelm@16877
   266
           else
wenzelm@16877
   267
             true)
obua@16198
   268
      val _ = forall check_final finals
wenzelm@16877
   269
wenzelm@16877
   270
      (* now we know that the only thing that can prevent acceptance of the definition
obua@16308
   271
         is a cyclic dependency *)
wenzelm@16877
   272
obua@16308
   273
      fun insert_edges edges (nodename, links) =
wenzelm@16877
   274
          (if links = [] then
obua@16308
   275
             edges
obua@16308
   276
           else
obua@16308
   277
             let
obua@16308
   278
               val links = map normalize_edge_idx links
obua@16308
   279
             in
wenzelm@17412
   280
               Symtab.update (nodename,
wenzelm@17412
   281
                               case Symtab.lookup edges nodename of
wenzelm@16877
   282
                                 NONE => links
wenzelm@17223
   283
                               | SOME links' => merge_edges links' links) edges
obua@16308
   284
             end)
wenzelm@16877
   285
obua@16308
   286
      fun make_edges ((bodyn, bodyty), edges) =
wenzelm@16877
   287
          let
wenzelm@16877
   288
            val bnode =
wenzelm@17412
   289
                (case Symtab.lookup graph bodyn of
wenzelm@16877
   290
                   NONE => def_err "body of constant definition references undeclared constant"
wenzelm@16877
   291
                 | SOME x => x)
wenzelm@16877
   292
            val (Node (general_btyp, bdefs, bbacks, bfinals, closed)) = bnode
wenzelm@16877
   293
          in
obua@16361
   294
            if closed = Final then edges else
wenzelm@16877
   295
            case unify_r 0 bodyty general_btyp of
wenzelm@16877
   296
              NONE => edges
wenzelm@16877
   297
            | SOME (maxidx, sigma1, sigma2) =>
wenzelm@16877
   298
              if exists (is_instance_r bodyty) bfinals then
obua@16308
   299
                edges
obua@16308
   300
              else
wenzelm@16877
   301
                let
wenzelm@16877
   302
                  fun insert_trans_edges ((step1, edges), (nodename, links)) =
obua@16308
   303
                      let
obua@16308
   304
                        val (maxidx1, alpha1, beta1, defname) = step1
wenzelm@16877
   305
                        fun connect (maxidx2, alpha2, beta2, history) =
wenzelm@16877
   306
                            case unify_r (Int.max (maxidx1, maxidx2)) beta1 alpha2 of
wenzelm@16877
   307
                              NONE => NONE
wenzelm@16877
   308
                            | SOME (max, sleft, sright) =>
wenzelm@16877
   309
                              SOME (max, subst sleft alpha1, subst sright beta2,
wenzelm@16982
   310
                                    if !chain_history then
wenzelm@16877
   311
                                      ((subst sleft beta1, bodyn, defname)::
wenzelm@16877
   312
                                       (subst_history sright history))
wenzelm@16877
   313
                                    else [])
obua@16308
   314
                        val links' = List.mapPartial connect links
obua@16308
   315
                      in
obua@16308
   316
                        (step1, insert_edges edges (nodename, links'))
obua@16308
   317
                      end
wenzelm@16877
   318
obua@16308
   319
                  fun make_edges' ((swallowed, edges),
obua@16308
   320
                                   (def_name, Defnode (def_ty, def_edges))) =
wenzelm@16877
   321
                      if swallowed then
wenzelm@16877
   322
                        (swallowed, edges)
wenzelm@16877
   323
                      else
wenzelm@16877
   324
                        (case unify_r 0 bodyty def_ty of
wenzelm@16877
   325
                           NONE => (swallowed, edges)
wenzelm@16877
   326
                         | SOME (maxidx, sigma1, sigma2) =>
wenzelm@16877
   327
                           (is_instance_r bodyty def_ty,
wenzelm@16877
   328
                            snd (Symtab.foldl insert_trans_edges
obua@16308
   329
                              (((maxidx, subst sigma1 ty, subst sigma2 def_ty, def_name),
obua@16308
   330
                                edges), def_edges))))
wenzelm@16877
   331
                  val (swallowed, edges) = Symtab.foldl make_edges' ((false, edges), bdefs)
wenzelm@16877
   332
                in
wenzelm@16877
   333
                  if swallowed then
wenzelm@16877
   334
                    edges
wenzelm@16877
   335
                  else
wenzelm@16877
   336
                    insert_edges edges
obua@16308
   337
                    (bodyn, [(maxidx, subst sigma1 ty, subst sigma2 general_btyp,[])])
wenzelm@16877
   338
                end
wenzelm@16877
   339
          end
wenzelm@16877
   340
obua@16308
   341
      val edges = foldl make_edges Symtab.empty body
wenzelm@16877
   342
wenzelm@16877
   343
      (* We also have to add the backreferences that this new defnode induces. *)
obua@16308
   344
      fun install_backrefs (graph, (noderef, links)) =
obua@16308
   345
          if links <> [] then
obua@16308
   346
            let
obua@16361
   347
              val (Node (ty, defs, backs, finals, closed)) = getnode graph noderef
wenzelm@16877
   348
              val _ = if closed = Final then
wenzelm@16877
   349
                        sys_error ("install_backrefs: closed node cannot be updated")
obua@16361
   350
                      else ()
obua@16308
   351
              val defnames =
wenzelm@17412
   352
                  (case Symtab.lookup backs mainref of
obua@16308
   353
                     NONE => Symtab.empty
obua@16308
   354
                   | SOME s => s)
wenzelm@17412
   355
              val defnames' = Symtab.update_new (axname, ()) defnames
wenzelm@17412
   356
              val backs' = Symtab.update (mainref, defnames') backs
obua@16308
   357
            in
wenzelm@17412
   358
              Symtab.update (noderef, Node (ty, defs, backs', finals, closed)) graph
obua@16308
   359
            end
obua@16308
   360
          else
obua@16308
   361
            graph
wenzelm@16877
   362
obua@16198
   363
      val graph = Symtab.foldl install_backrefs (graph, edges)
wenzelm@16877
   364
obua@16361
   365
      val (Node (_, _, backs, _, closed)) = getnode graph mainref
wenzelm@16877
   366
      val closed =
wenzelm@16877
   367
          if closed = Final then sys_error "define: closed node"
obua@16361
   368
          else if closed = Open andalso is_instance_r gty ty then Closed else closed
obua@16361
   369
obua@16308
   370
      val thisDefnode = Defnode (ty, edges)
wenzelm@17412
   371
      val graph = Symtab.update (mainref, Node (gty, Symtab.update_new
wenzelm@17223
   372
        (axname, thisDefnode) defs, backs, finals, closed)) graph
wenzelm@16877
   373
wenzelm@16877
   374
      (* Now we have to check all backreferences to this node and inform them about
obua@16308
   375
         the new defnode. In this section we also check for circularity. *)
obua@16308
   376
      fun update_backrefs ((backs, graph), (noderef, defnames)) =
wenzelm@16877
   377
          let
wenzelm@16877
   378
            fun update_defs ((defnames, graph),(defname, _)) =
wenzelm@16877
   379
                let
wenzelm@16877
   380
                  val (Node (nodety, nodedefs, nodebacks, nodefinals, closed)) =
obua@16361
   381
                      getnode graph noderef
obua@16361
   382
                  val _ = if closed = Final then sys_error "update_defs: closed node" else ()
wenzelm@16877
   383
                  val (Defnode (def_ty, defnode_edges)) =
wenzelm@17412
   384
                      the (Symtab.lookup nodedefs defname)
wenzelm@17412
   385
                  val edges = the (Symtab.lookup defnode_edges mainref)
obua@16361
   386
                  val refclosed = ref false
wenzelm@16877
   387
wenzelm@16877
   388
                  (* the type of thisDefnode is ty *)
wenzelm@16877
   389
                  fun update (e as (max, alpha, beta, history), (changed, edges)) =
wenzelm@16877
   390
                      case unify_r max beta ty of
wenzelm@16877
   391
                        NONE => (changed, e::edges)
wenzelm@16877
   392
                      | SOME (max', s_beta, s_ty) =>
wenzelm@16877
   393
                        let
wenzelm@16877
   394
                          val alpha' = subst s_beta alpha
wenzelm@16877
   395
                          val ty' = subst s_ty ty
wenzelm@16877
   396
                          val _ =
wenzelm@16877
   397
                              if noderef = mainref andalso defname = axname then
wenzelm@16877
   398
                                (case unify alpha' ty' of
wenzelm@16877
   399
                                   NONE =>
wenzelm@16877
   400
                                   if (is_instance_r ty' alpha') then
wenzelm@16877
   401
                                     raise (INFINITE_CHAIN (
wenzelm@16877
   402
                                            (alpha', mainref, axname)::
wenzelm@16877
   403
                                            (subst_history s_beta history)@
wenzelm@16877
   404
                                            [(ty', mainref, axname)]))
wenzelm@16877
   405
                                   else ()
wenzelm@16877
   406
                                 | SOME s =>
obua@16308
   407
                                   raise (CIRCULAR (
wenzelm@16877
   408
                                          (subst s alpha', mainref, axname)::
wenzelm@16877
   409
                                          (subst_history s (subst_history s_beta history))@
wenzelm@16877
   410
                                          [(subst s ty', mainref, axname)])))
wenzelm@16877
   411
                              else ()
wenzelm@16877
   412
                        in
wenzelm@16877
   413
                          if is_instance_r beta ty then
wenzelm@16877
   414
                            (true, edges)
wenzelm@16877
   415
                          else
wenzelm@16877
   416
                            (changed, e::edges)
wenzelm@16877
   417
                        end
wenzelm@16877
   418
obua@16308
   419
                  val (changed, edges') = foldl update (false, []) edges
wenzelm@16877
   420
                  val defnames' = if edges' = [] then
wenzelm@16877
   421
                                    defnames
wenzelm@16877
   422
                                  else
wenzelm@17412
   423
                                    Symtab.update (defname, ()) defnames
obua@16308
   424
                in
obua@16308
   425
                  if changed then
obua@16308
   426
                    let
wenzelm@16877
   427
                      val defnode_edges' =
obua@16308
   428
                          if edges' = [] then
obua@16308
   429
                            Symtab.delete mainref defnode_edges
obua@16308
   430
                          else
wenzelm@17412
   431
                            Symtab.update (mainref, edges') defnode_edges
obua@16308
   432
                      val defnode' = Defnode (def_ty, defnode_edges')
wenzelm@17412
   433
                      val nodedefs' = Symtab.update (defname, defnode') nodedefs
obua@16361
   434
                      val closed = if closed = Closed andalso Symtab.is_empty defnode_edges'
wenzelm@16877
   435
                                      andalso no_forwards nodedefs'
obua@16361
   436
                                   then Final else closed
wenzelm@16877
   437
                      val graph' =
wenzelm@17412
   438
                        Symtab.update
wenzelm@17223
   439
                          (noderef, Node (nodety, nodedefs', nodebacks, nodefinals, closed)) graph
obua@16308
   440
                    in
obua@16308
   441
                      (defnames', graph')
obua@16308
   442
                    end
obua@16308
   443
                  else
obua@16308
   444
                    (defnames', graph)
obua@16308
   445
                end
wenzelm@16877
   446
wenzelm@16877
   447
            val (defnames', graph') = Symtab.foldl update_defs
obua@16308
   448
                                                   ((Symtab.empty, graph), defnames)
wenzelm@16877
   449
          in
wenzelm@16877
   450
            if Symtab.is_empty defnames' then
wenzelm@16877
   451
              (backs, graph')
wenzelm@16877
   452
            else
wenzelm@16877
   453
              let
wenzelm@17412
   454
                val backs' = Symtab.update_new (noderef, defnames') backs
wenzelm@16877
   455
              in
wenzelm@16877
   456
                (backs', graph')
wenzelm@16877
   457
              end
wenzelm@16877
   458
          end
wenzelm@16877
   459
obua@16308
   460
      val (backs, graph) = Symtab.foldl update_backrefs ((Symtab.empty, graph), backs)
wenzelm@16877
   461
obua@16198
   462
      (* If a Circular exception is thrown then we never reach this point. *)
obua@16361
   463
      val (Node (gty, defs, _, finals, closed)) = getnode graph mainref
obua@16361
   464
      val closed = if closed = Closed andalso no_forwards defs then Final else closed
wenzelm@17412
   465
      val graph = Symtab.update (mainref, Node (gty, defs, backs, finals, closed)) graph
obua@16826
   466
      val actions' = (Define (mainref, ty, axname, orig_axname, body))::actions
wenzelm@16877
   467
    in
obua@16384
   468
      (cost+3, axmap, actions', graph)
obua@16384
   469
    end handle ex => translate_ex axmap ex
wenzelm@16877
   470
wenzelm@16982
   471
fun define'' thy (g as (cost, axmap, actions, graph)) (mainref, ty) orig_axname body =
obua@16308
   472
    let
wenzelm@16982
   473
      val ty = checkT thy ty
wenzelm@16877
   474
      fun checkbody (n, t) =
wenzelm@16877
   475
          let
obua@16361
   476
            val (Node (_, _, _,_, closed)) = getnode graph n
obua@16361
   477
          in
obua@16361
   478
            case closed of
obua@16361
   479
              Final => NONE
wenzelm@16982
   480
            | _ => SOME (n, checkT thy t)
obua@16361
   481
          end
obua@16361
   482
      val body = distinct (List.mapPartial checkbody body)
obua@16826
   483
      val (axmap, axname) = newaxname axmap orig_axname
obua@16308
   484
    in
obua@16826
   485
      define' (cost, axmap, actions, graph) (mainref, ty) axname orig_axname body
obua@16308
   486
    end
obua@16308
   487
wenzelm@16877
   488
fun finalize' (cost, axmap, history, graph) (noderef, ty) =
wenzelm@17412
   489
    case Symtab.lookup graph noderef of
obua@16308
   490
      NONE => def_err ("cannot finalize constant "^noderef^"; it is not declared")
obua@16361
   491
    | SOME (Node (nodety, defs, backs, finals, closed)) =>
wenzelm@16877
   492
      let
wenzelm@16877
   493
        val _ =
obua@16308
   494
            if (not (is_instance_r ty nodety)) then
wenzelm@16877
   495
              def_err ("only type instances of the declared constant "^
obua@16308
   496
                       noderef^" can be finalized")
wenzelm@16877
   497
            else ()
wenzelm@16877
   498
        val _ = Symtab.exists
wenzelm@16877
   499
                  (fn (def_name, Defnode (def_ty, _)) =>
wenzelm@16877
   500
                      if can_be_unified_r ty def_ty then
wenzelm@16877
   501
                        def_err ("cannot finalize constant "^noderef^
obua@16308
   502
                                 "; clash with definition "^def_name)
wenzelm@16877
   503
                      else
wenzelm@16877
   504
                        false)
wenzelm@16877
   505
                  defs
wenzelm@16877
   506
obua@16198
   507
        fun update_finals [] = SOME [ty]
wenzelm@16877
   508
          | update_finals (final_ty::finals) =
obua@16198
   509
            (if is_instance_r ty final_ty then NONE
obua@16198
   510
             else
obua@16198
   511
               case update_finals finals of
obua@16198
   512
                 NONE => NONE
obua@16198
   513
               | (r as SOME finals) =>
obua@16198
   514
                 if (is_instance_r final_ty ty) then
obua@16198
   515
                   r
obua@16198
   516
                 else
wenzelm@16877
   517
                   SOME (final_ty :: finals))
wenzelm@16877
   518
      in
obua@16198
   519
        case update_finals finals of
obua@16384
   520
          NONE => (cost, axmap, history, graph)
wenzelm@16877
   521
        | SOME finals =>
wenzelm@16877
   522
          let
wenzelm@16877
   523
            val closed = if closed = Open andalso is_instance_r nodety ty then
wenzelm@16877
   524
                           Closed else
obua@16361
   525
                         closed
wenzelm@17412
   526
            val graph = Symtab.update (noderef, Node (nodety, defs, backs, finals, closed)) graph
wenzelm@16877
   527
wenzelm@16877
   528
            fun update_backref ((graph, backs), (backrefname, backdefnames)) =
wenzelm@16877
   529
                let
wenzelm@16877
   530
                  fun update_backdef ((graph, defnames), (backdefname, _)) =
wenzelm@16877
   531
                      let
wenzelm@16877
   532
                        val (backnode as Node (backty, backdefs, backbacks,
wenzelm@16877
   533
                                               backfinals, backclosed)) =
obua@16308
   534
                            getnode graph backrefname
wenzelm@16877
   535
                        val (Defnode (def_ty, all_edges)) =
obua@16308
   536
                            the (get_defnode backnode backdefname)
obua@16308
   537
wenzelm@16877
   538
                        val (defnames', all_edges') =
wenzelm@17412
   539
                            case Symtab.lookup all_edges noderef of
wenzelm@16877
   540
                              NONE => sys_error "finalize: corrupt backref"
wenzelm@16877
   541
                            | SOME edges =>
wenzelm@16877
   542
                              let
wenzelm@16877
   543
                                val edges' = List.filter (fn (_, _, beta, _) =>
obua@16308
   544
                                                             not (is_instance_r beta ty)) edges
wenzelm@16877
   545
                              in
wenzelm@16877
   546
                                if edges' = [] then
wenzelm@16877
   547
                                  (defnames, Symtab.delete noderef all_edges)
wenzelm@16877
   548
                                else
wenzelm@17412
   549
                                  (Symtab.update (backdefname, ()) defnames,
wenzelm@17412
   550
                                   Symtab.update (noderef, edges') all_edges)
wenzelm@16877
   551
                              end
wenzelm@16877
   552
                        val defnode' = Defnode (def_ty, all_edges')
wenzelm@17412
   553
                        val backdefs' = Symtab.update (backdefname, defnode') backdefs
wenzelm@16877
   554
                        val backclosed' = if backclosed = Closed andalso
obua@16361
   555
                                             Symtab.is_empty all_edges'
obua@16361
   556
                                             andalso no_forwards backdefs'
obua@16361
   557
                                          then Final else backclosed
wenzelm@16877
   558
                        val backnode' =
obua@16361
   559
                            Node (backty, backdefs', backbacks, backfinals, backclosed')
wenzelm@16877
   560
                      in
wenzelm@17412
   561
                        (Symtab.update (backrefname, backnode') graph, defnames')
wenzelm@16877
   562
                      end
wenzelm@16877
   563
wenzelm@16877
   564
                  val (graph', defnames') =
obua@16308
   565
                      Symtab.foldl update_backdef ((graph, Symtab.empty), backdefnames)
wenzelm@16877
   566
                in
wenzelm@16877
   567
                  (graph', if Symtab.is_empty defnames' then backs
wenzelm@17412
   568
                           else Symtab.update (backrefname, defnames') backs)
wenzelm@16877
   569
                end
wenzelm@16877
   570
            val (graph', backs') = Symtab.foldl update_backref ((graph, Symtab.empty), backs)
wenzelm@16877
   571
            val Node ( _, defs, _, _, closed) = getnode graph' noderef
obua@16361
   572
            val closed = if closed = Closed andalso no_forwards defs then Final else closed
wenzelm@17412
   573
            val graph' = Symtab.update (noderef, Node (nodety, defs, backs',
wenzelm@17223
   574
                                                        finals, closed)) graph'
obua@16361
   575
            val history' = (Finalize (noderef, ty)) :: history
wenzelm@16877
   576
          in
wenzelm@16877
   577
            (cost+1, axmap, history', graph')
wenzelm@16877
   578
          end
obua@16198
   579
      end
wenzelm@16877
   580
wenzelm@16982
   581
fun finalize'' thy g (noderef, ty) = finalize' g (noderef, checkT thy ty)
obua@16308
   582
obua@16826
   583
fun update_axname ax orig_ax (cost, axmap, history, graph) =
wenzelm@17412
   584
  (cost, Symtab.update (ax, orig_ax) axmap, history, graph)
obua@16826
   585
obua@16361
   586
fun merge' (Declare cty, g) = declare' g cty
wenzelm@16877
   587
  | merge' (Define (name, ty, axname, orig_axname, body), g as (cost, axmap, history, graph)) =
wenzelm@17412
   588
    (case Symtab.lookup graph name of
obua@16826
   589
       NONE => define' (update_axname axname orig_axname g) (name, ty) axname orig_axname body
wenzelm@16877
   590
     | SOME (Node (_, defs, _, _, _)) =>
wenzelm@17412
   591
       (case Symtab.lookup defs axname of
wenzelm@16877
   592
          NONE => define' (update_axname axname orig_axname g) (name, ty) axname orig_axname body
wenzelm@16877
   593
        | SOME _ => g))
wenzelm@16877
   594
  | merge' (Finalize finals, g) = finalize' g finals
wenzelm@16877
   595
wenzelm@16877
   596
fun merge'' (g1 as (cost1, _, actions1, _)) (g2 as (cost2, _, actions2, _)) =
obua@16308
   597
    if cost1 < cost2 then
obua@16308
   598
      foldr merge' g2 actions1
obua@16308
   599
    else
obua@16308
   600
      foldr merge' g1 actions2
wenzelm@16877
   601
wenzelm@16877
   602
fun finals (_, _, history, graph) =
wenzelm@16877
   603
    Symtab.foldl
wenzelm@16877
   604
      (fn (finals, (name, Node(_, _, _, ftys, _))) =>
wenzelm@17412
   605
          Symtab.update_new (name, ftys) finals)
obua@16198
   606
      (Symtab.empty, graph)
obua@16158
   607
wenzelm@16877
   608
fun overloading_info (_, axmap, _, graph) c =
obua@16743
   609
    let
wenzelm@17412
   610
      fun translate (ax, Defnode (ty, _)) = (the (Symtab.lookup axmap ax), ty)
obua@16743
   611
    in
wenzelm@17412
   612
      case Symtab.lookup graph c of
obua@16743
   613
        NONE => NONE
obua@16743
   614
      | SOME (Node (ty, defnodes, _, _, state)) =>
obua@16743
   615
        SOME (ty, map translate (Symtab.dest defnodes), state)
obua@16743
   616
    end
wenzelm@16877
   617
wenzelm@16982
   618
wenzelm@16982
   619
(* monomorphic consts -- neither parametric nor ad-hoc polymorphism *)
wenzelm@16982
   620
wenzelm@16982
   621
fun monomorphicT (Type (_, Ts)) = forall monomorphicT Ts
wenzelm@16982
   622
  | monomorphicT _ = false
wenzelm@16982
   623
wenzelm@16982
   624
fun monomorphic (_, _, _, graph) c =
wenzelm@17412
   625
  (case Symtab.lookup graph c of
wenzelm@16982
   626
    NONE => true
wenzelm@16982
   627
  | SOME (Node (ty, defnodes, _, _, _)) =>
wenzelm@16982
   628
      Symtab.min_key defnodes = Symtab.max_key defnodes andalso
wenzelm@16982
   629
      monomorphicT ty);
obua@16743
   630
wenzelm@16877
   631
wenzelm@16877
   632
wenzelm@16877
   633
(** diagnostics **)
wenzelm@16877
   634
wenzelm@16877
   635
fun pretty_const pp (c, T) =
wenzelm@16877
   636
 [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16936
   637
  Pretty.quote (Pretty.typ pp (Type.freeze_type (Term.zero_var_indexesT T)))];
wenzelm@16877
   638
wenzelm@16877
   639
fun pretty_path pp path = fold_rev (fn (T, c, def) =>
wenzelm@16877
   640
  fn [] => [Pretty.block (pretty_const pp (c, T))]
wenzelm@16877
   641
   | prts => Pretty.block (pretty_const pp (c, T) @
wenzelm@16877
   642
      [Pretty.brk 1, Pretty.str ("depends via " ^ quote def ^ " on")]) :: prts) path [];
wenzelm@16877
   643
wenzelm@16877
   644
fun defs_circular pp path =
wenzelm@16982
   645
  Pretty.str "Cyclic dependency of definitions: " :: pretty_path pp path
wenzelm@16877
   646
  |> Pretty.chunks |> Pretty.string_of;
wenzelm@16877
   647
wenzelm@16877
   648
fun defs_infinite_chain pp path =
wenzelm@16982
   649
  Pretty.str "Infinite chain of definitions: " :: pretty_path pp path
wenzelm@16877
   650
  |> Pretty.chunks |> Pretty.string_of;
wenzelm@16877
   651
wenzelm@16877
   652
fun defs_clash def1 def2 = "Type clash in definitions " ^ quote def1 ^ " and " ^ quote def2;
wenzelm@16877
   653
wenzelm@16877
   654
fun defs_final pp const =
wenzelm@16877
   655
  (Pretty.str "Attempt to define final constant" :: Pretty.brk 1 :: pretty_const pp const)
wenzelm@16877
   656
  |> Pretty.block |> Pretty.string_of;
wenzelm@16877
   657
wenzelm@16877
   658
wenzelm@16877
   659
(* external interfaces *)
wenzelm@16877
   660
wenzelm@16982
   661
fun declare thy const defs =
wenzelm@16982
   662
  if_none (try (declare'' thy defs) const) defs;
wenzelm@16877
   663
wenzelm@16982
   664
fun define thy const name rhs defs =
wenzelm@16982
   665
  define'' thy defs const name rhs
wenzelm@16877
   666
    handle DEFS msg => sys_error msg
wenzelm@16982
   667
      | CIRCULAR path => error (defs_circular (Sign.pp thy) path)
wenzelm@16982
   668
      | INFINITE_CHAIN path => error (defs_infinite_chain (Sign.pp thy) path)
wenzelm@16877
   669
      | CLASH (_, def1, def2) => error (defs_clash def1 def2)
wenzelm@16982
   670
      | FINAL const => error (defs_final (Sign.pp thy) const);
wenzelm@16877
   671
wenzelm@16982
   672
fun finalize thy const defs =
wenzelm@16982
   673
  finalize'' thy defs const handle DEFS msg => sys_error msg;
wenzelm@16877
   674
wenzelm@16877
   675
fun merge pp defs1 defs2 =
wenzelm@16877
   676
  merge'' defs1 defs2
wenzelm@16877
   677
    handle CIRCULAR namess => error (defs_circular pp namess)
wenzelm@16877
   678
      | INFINITE_CHAIN namess => error (defs_infinite_chain pp namess);
wenzelm@16877
   679
obua@16108
   680
end;
wenzelm@16877
   681
obua@16308
   682
(*
obua@16108
   683
obua@16308
   684
fun tvar name = TVar ((name, 0), [])
obua@16108
   685
obua@16108
   686
val bool = Type ("bool", [])
obua@16108
   687
val int = Type ("int", [])
obua@16308
   688
val lam = Type("lam", [])
obua@16108
   689
val alpha = tvar "'a"
obua@16108
   690
val beta = tvar "'b"
obua@16108
   691
val gamma = tvar "'c"
obua@16108
   692
fun pair a b = Type ("pair", [a,b])
obua@16308
   693
fun prm a = Type ("prm", [a])
obua@16308
   694
val name = Type ("name", [])
obua@16108
   695
obua@16108
   696
val _ = print "make empty"
wenzelm@16877
   697
val g = Defs.empty
obua@16108
   698
obua@16308
   699
val _ = print "declare perm"
obua@16308
   700
val g = Defs.declare g ("perm", prm alpha --> beta --> beta)
obua@16308
   701
obua@16308
   702
val _ = print "declare permF"
obua@16308
   703
val g = Defs.declare g ("permF", prm alpha --> lam --> lam)
obua@16308
   704
obua@16308
   705
val _ = print "define perm (1)"
wenzelm@16877
   706
val g = Defs.define g ("perm", prm alpha --> (beta --> gamma) --> (beta --> gamma)) "perm_fun"
obua@16308
   707
        [("perm", prm alpha --> gamma --> gamma), ("perm", prm alpha --> beta --> beta)]
obua@16108
   708
obua@16308
   709
val _ = print "define permF (1)"
obua@16308
   710
val g = Defs.define g ("permF", prm alpha --> lam --> lam) "permF_app"
obua@16308
   711
        ([("perm", prm alpha --> lam --> lam),
obua@16308
   712
         ("perm", prm alpha --> lam --> lam),
obua@16308
   713
         ("perm", prm alpha --> lam --> lam),
obua@16308
   714
         ("perm", prm alpha --> name --> name)])
obua@16108
   715
obua@16308
   716
val _ = print "define perm (2)"
obua@16308
   717
val g = Defs.define g ("perm", prm alpha --> lam --> lam) "perm_lam"
obua@16308
   718
        [("permF", (prm alpha --> lam --> lam))]
obua@16108
   719
wenzelm@16877
   720
*)