src/Pure/defs.ML
author obua
Wed, 01 Jun 2005 21:25:35 +0200
changeset 16177 1af9f5c69745
parent 16158 2c3565b74b7a
child 16198 cfd070a2cc4d
permissions -rw-r--r--
Preliminary version of defs.ML that does not check final consts.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
(*  Title:      Pure/General/defs.ML
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$
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
    Author:     Steven Obua, TU Muenchen
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
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
     5
    Checks if definitions preserve consistency of logic by enforcing that there are no cyclic definitions.
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
    The algorithm is described in 
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
    "Cycle-free Overloading in Isabelle", Steven Obua, technical report, to be written :-)
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
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    10
signature DEFS = sig
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    11
    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    12
    type graph
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    13
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    14
    exception DEFS of string
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    15
    exception CIRCULAR of (typ * string * string) list
16113
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
    16
    exception INFINITE_CHAIN of (typ * string * string) list 
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    17
    exception FINAL of string * typ
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
    18
    exception CLASH of string * string * string
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    19
    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    20
    val empty : graph
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    21
    val declare : graph -> string * typ -> graph  (* exception DEFS *)
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    22
    val define : graph -> string * typ -> string -> (string * typ) list -> graph 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    23
      (* exception DEFS, CIRCULAR, INFINITE_CHAIN, CLASH, FINAL *)
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    24
    
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    25
    val finalize : graph -> string * typ -> graph (* exception DEFS *)
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    26
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    27
    val finals : graph -> (typ list) Symtab.table
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
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
    (* the first argument should be the smaller graph *)
16113
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
    30
    val merge : graph -> graph -> graph (* exception CIRCULAR, INFINITE_CHAIN, CLASH *)
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
    31
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
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
    33
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    34
structure Defs :> DEFS = struct
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    35
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    36
type tyenv = Type.tyenv
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    37
type edgelabel = (int * typ * typ * (typ * string * string) list)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    38
type noderef = string
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    39
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    40
datatype node = Node of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    41
       string  (* name of constant *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    42
     * typ  (* most general type of constant *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    43
     * defnode Symtab.table  (* a table of defnodes, each corresponding to 1 definition of the constant for a particular type, 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    44
                             indexed by axiom name *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    45
     * backref Symtab.table (* a table of all back references to this node, indexed by node name *)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    46
     * typ list (* a list of all finalized types *)
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
    47
     
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    48
and defnode = Defnode of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    49
       typ  (* type of the constant in this particular definition *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    50
     * ((noderef * (string option * edgelabel list) list) Symtab.table) (* The edges, grouped by nodes. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    51
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    52
and backref = Backref of
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    53
       noderef  (* the name of the node that has defnodes which reference a certain node A *)
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
    54
     * (unit Symtab.table) (* the names of the defnodes that DIRECTLY reference A. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    55
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    56
fun getnode graph noderef = the (Symtab.lookup (graph, noderef))
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    57
fun get_nodename (Node (n, _, _ ,_, _)) = n
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    58
fun get_nodedefs (Node (_, _, defs, _, _)) = defs
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    59
fun get_defnode (Node (_, _, defs, _, _)) defname = Symtab.lookup (defs, defname)
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
    60
fun get_defnode' graph noderef defname = Symtab.lookup (get_nodedefs (the (Symtab.lookup (graph, noderef))), defname)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    61
fun get_nodename (Node (n, _, _ , _, _)) = n
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
    62
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    63
datatype graphaction = Declare of string * typ 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    64
		     | Define of string * typ * string * (string * typ) list
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    65
		     | Finalize of string * typ
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
    66
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    67
type graph = (graphaction list) * (node Symtab.table)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    68
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    69
val empty = ([], Symtab.empty)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    70
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    71
exception DEFS of string;
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    72
exception CIRCULAR of (typ * string * string) list;
16113
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
    73
exception INFINITE_CHAIN of (typ * string * string) list;
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
    74
exception CLASH of string * string * string;
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    75
exception FINAL of string * typ;
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
    76
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    77
fun def_err s = raise (DEFS s)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    78
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    79
fun declare (actions, g) (cty as (name, ty)) =
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    80
    ((Declare cty)::actions, 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    81
     Symtab.update_new ((name, Node (name, Type.varifyT(Type.strip_sorts ty), Symtab.empty, Symtab.empty, [])), g))
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
    82
    handle Symtab.DUP _ => def_err "constant is already declared"
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
    83
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    84
fun rename ty1 ty2 = incr_tvar ((maxidx_of_typ ty1)+1) ty2;  
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    85
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    86
fun subst_incr_tvar inc t =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    87
    if (inc > 0) then 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    88
	let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    89
	    val tv = typ_tvars t
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    90
	    val t' = incr_tvar inc t
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    91
	    fun update_subst (((n,i), _), s) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    92
		Vartab.update (((n, i), ([], TVar ((n, i+inc), []))), s)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    93
	in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    94
	    (t',List.foldl update_subst Vartab.empty tv)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    95
	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
    96
    else
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    97
	(t, Vartab.empty)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    98
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
    99
(* Rename tys2 so that tys2 and tys1 do not have any variables in common any more.
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   100
   As a result, return the renamed tys2' and the substitution that takes tys2 to tys2'. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   101
fun subst_rename max1 ty2 =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   102
    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   103
        val max2 = (maxidx_of_typ ty2)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   104
        val (ty2', s) = subst_incr_tvar (max1 + 1) ty2                
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   105
    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   106
	(ty2', s, max1 + max2 + 1)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   107
    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
   108
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   109
fun subst s ty = Envir.norm_type s ty
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   110
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   111
fun subst_history s history = map (fn (ty, cn, dn) => (subst s ty, cn, dn)) history
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   112
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   113
fun is_instance instance_ty general_ty =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   114
    Type.typ_instance Type.empty_tsig (instance_ty, general_ty)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   115
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   116
fun is_instance_r instance_ty general_ty =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   117
    is_instance instance_ty (rename instance_ty general_ty)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   118
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   119
fun unify ty1 ty2 = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   120
    SOME (fst (Type.unify Type.empty_tsig (Vartab.empty, 0) (ty1, ty2)))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   121
    handle Type.TUNIFY => NONE
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   122
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   123
(* 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   124
   Unifies ty1 and ty2, renaming ty1 and ty2 so that they have greater indices than max and so that they
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   125
   are different. All indices in ty1 and ty2 are supposed to be less than or equal to max.
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   126
   Returns SOME (max', s1, s2), so that s1(ty1) = s2(ty2) and max' is greater or equal than all 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   127
   indices in s1, s2, ty1, ty2.
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   128
*)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   129
fun unify_r max ty1 ty2 = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   130
    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   131
	val max =  Int.max(max, 0)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   132
	val max1 = max (* >= maxidx_of_typ ty1 *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   133
	val max2 = max (* >= maxidx_of_typ ty2 *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   134
	val max = Int.max(max, Int.max (max1, max2))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   135
        val (ty1, s1) = subst_incr_tvar (max+1) ty1
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   136
	val (ty2, s2) = subst_incr_tvar (max+max1+2) ty2
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   137
        val max = max+max1+max2+2	
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   138
	fun merge a b = Vartab.merge (fn _ => false) (a, b)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   139
    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   140
	case unify ty1 ty2 of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   141
	    NONE => NONE
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   142
	  | SOME s => SOME (max, merge s1 s, merge s2 s)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   143
    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
   144
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   145
fun can_be_unified_r ty1 ty2 =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   146
    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   147
	val ty2 = rename ty1 ty2
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   148
    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   149
	case unify ty1 ty2 of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   150
	    NONE => false
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   151
	  | _ => true
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   152
    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
   153
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   154
fun can_be_unified ty1 ty2 =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   155
    case unify ty1 ty2 of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   156
	NONE => false
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   157
      | _ => true
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   158
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   159
fun checkT (Type (a, Ts)) = Type (a, map checkT Ts)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   160
  | checkT (TVar ((a, 0), _)) = TVar ((a, 0), [])
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   161
  | checkT (TVar ((a, i), _)) = def_err "type is not clean"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   162
  | checkT (TFree (a, _)) = TVar ((a, 0), [])
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   163
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   164
fun label_ord NONE NONE = EQUAL
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   165
  | label_ord NONE (SOME _) = LESS
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   166
  | label_ord (SOME _) NONE = GREATER
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   167
  | label_ord (SOME l1) (SOME l2) = string_ord (l1,l2)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   168
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   169
fun compare_edges (e1 as (maxidx1, u1, v1, history1)) (e2 as (maxidx2, u2, v2, history2)) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   170
    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   171
	val t1 = u1 --> v1
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   172
	val t2 = u2 --> v2
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   173
    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   174
	if (is_instance_r t1 t2) then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   175
	    (if is_instance_r t2 t1 then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   176
		 SOME (int_ord (length history2, length history1))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   177
	     else
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   178
		 SOME LESS)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   179
	else if (is_instance_r t2 t1) then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   180
	    SOME GREATER
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   181
	else
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   182
	    NONE
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   183
    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
   184
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   185
fun merge_edges_1 (x, []) = []
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   186
  | merge_edges_1 (x, (y::ys)) = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   187
    (case compare_edges x y of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   188
	 SOME LESS => (y::ys)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   189
       | SOME EQUAL => (y::ys)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   190
       | SOME GREATER => merge_edges_1 (x, ys)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   191
       | NONE => y::(merge_edges_1 (x, ys)))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   192
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   193
fun merge_edges xs ys = foldl merge_edges_1 xs ys
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   194
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   195
fun pack_edges xs = merge_edges [] xs
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   196
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   197
fun merge_labelled_edges [] es = es
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   198
  | merge_labelled_edges es [] = es
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   199
  | merge_labelled_edges ((l1,e1)::es1) ((l2,e2)::es2) = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   200
    (case label_ord l1 l2 of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   201
	 LESS => (l1, e1)::(merge_labelled_edges es1 ((l2, e2)::es2))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   202
       | GREATER => (l2, e2)::(merge_labelled_edges ((l1, e1)::es1) es2)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   203
       | EQUAL => (l1, merge_edges e1 e2)::(merge_labelled_edges es1 es2))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   204
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   205
fun defnode_edges_foldl f a defnode =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   206
    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   207
	val (Defnode (ty, def_edges)) = defnode
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   208
	fun g (b, (_, (n, labelled_edges))) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   209
	    foldl (fn ((s, edges), b') => 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   210
		      (foldl (fn (e, b'') => f ty n s e b'') b' edges))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   211
		  b
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   212
		  labelled_edges		  		     
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   213
    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   214
	Symtab.foldl g (a, def_edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   215
    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
   216
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   217
fun define (actions, graph) (name, ty) axname body =
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
   218
    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   219
	val ty = checkT ty
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   220
	val body = map (fn (n,t) => (n, checkT t)) body		 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   221
	val mainref = name
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   222
	val mainnode  = (case Symtab.lookup (graph, mainref) of 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   223
			     NONE => def_err ("constant "^(quote mainref)^" is not declared")
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   224
			   | SOME n => n)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   225
	val (Node (n, gty, defs, backs, finals)) = mainnode
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
   226
	val _ = (if is_instance_r ty gty then () else def_err "type of constant does not match declared type")
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   227
	fun check_def (s, Defnode (ty', _)) = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   228
	    (if can_be_unified_r ty ty' then 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   229
		 raise (CLASH (mainref, axname, s))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   230
	     else if s = axname then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   231
	         def_err "name of axiom is already used for another definition of this constant"
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   232
	     else false)	
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   233
	val _ = Symtab.exists check_def defs
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   234
(*	fun check_final finalty = 
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   235
	    (if can_be_unified_r finalty ty then
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   236
		 raise (FINAL (mainref, finalty))
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   237
	     else
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   238
		 true)
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   239
	val _ = forall check_final finals*)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   240
	
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
   241
	(* now we know that the only thing that can prevent acceptance of the definition is a cyclic dependency *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   242
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   243
	(* body contains the constants that this constant definition depends on. For each element of body,
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   244
           the function make_edges_to calculates a group of edges that connect this constant with 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   245
           the constant that is denoted by the element of the body *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   246
	fun make_edges_to (bodyn, bodyty) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   247
	    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   248
		val bnode = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   249
		    (case Symtab.lookup (graph, bodyn) of 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   250
			 NONE => def_err "body of constant definition references undeclared constant"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   251
		       | SOME x => x)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   252
		val (Node (_, general_btyp, bdefs, bbacks, bfinals)) = bnode
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
   253
	    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   254
		case unify_r 0 bodyty general_btyp of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   255
		    NONE => NONE
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   256
		  | SOME (maxidx, sigma1, sigma2) => 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   257
		    SOME (
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   258
		    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   259
			(* For each definition of the constant in the body, 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   260
			   check if the definition unifies with the type of the constant in the body. *)	                
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   261
	              fun make_edges ((swallowed, l),(def_name, Defnode (def_ty, _))) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   262
			  if swallowed then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   263
			      (swallowed, l)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   264
			  else 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   265
			      (case unify_r 0 bodyty def_ty of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   266
				   NONE => (swallowed, l)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   267
				 | SOME (maxidx, sigma1, sigma2) => 
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   268
				   (is_instance_r bodyty def_ty,
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
   269
				    merge_labelled_edges l [(SOME def_name,[(maxidx, subst sigma1 ty, subst sigma2 def_ty, [])])]))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   270
          	      val (swallowed, edges) = Symtab.foldl make_edges ((false, []), bdefs)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   271
		    in
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   272
			if swallowed (*orelse (exists (is_instance_r bodyty) bfinals)*) then 
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
   273
			    (bodyn, edges)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   274
			else 
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
   275
			    (bodyn, [(NONE, [(maxidx, subst sigma1 ty, subst sigma2 general_btyp,[])])]@edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   276
		    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
   277
	    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
   278
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   279
	fun update_edges (b as (bodyn, bodyty), edges) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   280
	    (case make_edges_to b of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   281
		 NONE => edges
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   282
	       | SOME m =>
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   283
		 (case Symtab.lookup (edges, bodyn) of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   284
		      NONE => Symtab.update ((bodyn, m), edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   285
		    | SOME (_, es') => 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   286
		      let 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   287
			  val (_, es) = m
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   288
			  val es = merge_labelled_edges es es'
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   289
		      in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   290
			  Symtab.update ((bodyn, (bodyn, es)), edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   291
		      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
   292
		 )
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   293
	    )
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   294
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   295
	val edges = foldl update_edges Symtab.empty body
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   296
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   297
	fun insert_edge edges (nodename, (defname_opt, edge)) = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   298
	    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   299
		val newlink = [(defname_opt, [edge])]
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   300
	    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   301
		case Symtab.lookup (edges, nodename) of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   302
		    NONE => Symtab.update ((nodename, (nodename, newlink)), edges)		    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   303
		  | SOME (_, links) => 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   304
		    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   305
			val links' = merge_labelled_edges links newlink
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   306
		    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   307
			Symtab.update ((nodename, (nodename, links')), edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   308
		    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
   309
	    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
   310
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   311
        (* We constructed all direct edges that this defnode has. 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   312
           Now we have to construct the transitive hull by going a single step further. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   313
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   314
        val thisDefnode = Defnode (ty, edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   315
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   316
	fun make_trans_edges _ noderef defname_opt (max1, alpha1, beta1, history1) edges = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   317
	    case defname_opt of 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   318
		NONE => edges
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   319
	      | SOME defname => 		
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   320
		let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   321
		    val defnode = the (get_defnode' graph noderef defname)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   322
		    fun make_trans_edge _ noderef2 defname_opt2 (max2, alpha2, beta2, history2) edges =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   323
			case unify_r (Int.max (max1, max2)) beta1 alpha2 of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   324
			    NONE => edges
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   325
			  | SOME (max, sleft, sright) =>
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   326
			    insert_edge edges (noderef2, 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   327
					       (defname_opt2, 							  
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   328
						(max, subst sleft alpha1, subst sright beta2, 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   329
						 (subst_history sleft history1)@
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   330
						 ((subst sleft beta1, noderef, defname)::
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   331
						  (subst_history sright history2)))))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   332
		in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   333
		    defnode_edges_foldl make_trans_edge edges defnode
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   334
		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
   335
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   336
	val edges = defnode_edges_foldl make_trans_edges edges thisDefnode
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   337
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   338
	val thisDefnode = Defnode (ty, edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   339
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   340
	(* We also have to add the backreferences that this new defnode induces. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   341
	    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   342
	fun hasNONElink ((NONE, _)::_) = true
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   343
	  | hasNONElink _ = false
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   344
	
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   345
	fun install_backref graph noderef pointingnoderef pointingdefname = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   346
	    let
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   347
		val (Node (pname, _, _, _, _)) = getnode graph pointingnoderef
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   348
		val (Node (name, ty, defs, backs, finals)) = getnode graph noderef
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
   349
	    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   350
		case Symtab.lookup (backs, pname) of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   351
		    NONE => 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   352
		    let 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   353
			val defnames = Symtab.update ((pointingdefname, ()), Symtab.empty)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   354
			val backs = Symtab.update ((pname, Backref (pointingnoderef, defnames)), backs)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   355
		    in
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   356
			Symtab.update ((name, Node (name, ty, defs, backs, finals)), graph) 			
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
   357
		    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
   358
		  | SOME (Backref (pointingnoderef, defnames)) =>
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   359
		    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   360
			val defnames = Symtab.update_new ((pointingdefname, ()), defnames)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   361
			val backs = Symtab.update ((pname, Backref (pointingnoderef, defnames)), backs)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   362
		    in
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   363
			Symtab.update ((name, Node (name, ty, defs, backs, finals)), graph)
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
   364
		    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
   365
		    handle Symtab.DUP _ => graph
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   366
	    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
   367
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   368
	fun install_backrefs (graph, (_, (noderef, labelled_edges))) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   369
	    if hasNONElink labelled_edges then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   370
		install_backref graph noderef mainref axname
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   371
	    else 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   372
		graph
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   373
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   374
        val graph = Symtab.foldl install_backrefs (graph, edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   375
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   376
        val (Node (_, _, _, backs, _)) = getnode graph mainref
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   377
	val graph = Symtab.update ((mainref, Node (n, gty, Symtab.update_new 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   378
          ((axname, thisDefnode), defs), backs, finals)), graph)
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
   379
		    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   380
	(* Now we have to check all backreferences to this node and inform them about the new defnode. 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   381
	   In this section we also check for circularity. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   382
        fun update_backrefs ((backs, newedges), (nodename, Backref (noderef, defnames))) =	    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   383
	    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   384
		val node = getnode graph noderef
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   385
		fun update_defs ((defnames, newedges),(defname, _)) =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   386
		    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   387
			val (Defnode (_, defnode_edges)) = the (get_defnode node defname)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   388
			val (_, labelled_edges) = the (Symtab.lookup (defnode_edges, n))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   389
						      
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   390
			(* the type of thisDefnode is ty *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   391
			fun update (e as (max, alpha, beta, history), (none_edges, this_edges)) = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   392
			    case unify_r max beta ty of
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   393
				NONE => (e::none_edges, this_edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   394
			      | SOME (max', s_beta, s_ty) =>
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   395
				let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   396
				    val alpha' = subst s_beta alpha
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   397
				    val ty' = subst s_ty ty				      
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   398
				    val _ = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   399
					if noderef = mainref andalso defname = axname then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   400
					    (case unify alpha' ty' of
16113
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
   401
						 NONE => 
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
   402
						   if (is_instance_r ty' alpha') then
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
   403
						       raise (INFINITE_CHAIN (
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
   404
							      (alpha', mainref, axname)::
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
   405
							      (subst_history s_beta history)@
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
   406
							      [(ty', mainref, axname)]))
692fe6595755 Infinite chains in definitions are now detected, too.
obua
parents: 16108
diff changeset
   407
						   else ()
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
   408
					       | SOME s => raise (CIRCULAR (
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   409
								  (subst s alpha', mainref, axname)::
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   410
								  (subst_history s (subst_history s_beta history))@
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   411
								  [(subst s ty', mainref, axname)])))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   412
					else ()
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   413
				    val edge = (max', alpha', ty', subst_history s_beta history)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   414
				in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   415
				    if is_instance_r beta ty then 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   416
					(none_edges, edge::this_edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   417
				    else
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   418
					(e::none_edges, edge::this_edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   419
				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
   420
		    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   421
			case labelled_edges of 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   422
			    ((NONE, edges)::_) => 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   423
			    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   424
				val (none_edges, this_edges) = foldl update ([], []) edges
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   425
				val defnames = if none_edges = [] then defnames else Symtab.update_new ((defname, ()), defnames) 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   426
			    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   427
				(defnames, (defname, none_edges, this_edges)::newedges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   428
			    end			    
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   429
			  | _ => sys_error "define: update_defs, internal error, corrupt backrefs"
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
   430
		    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
   431
		    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   432
		val (defnames, newedges') = Symtab.foldl update_defs ((Symtab.empty, []), defnames)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   433
	    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   434
		if Symtab.is_empty defnames then 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   435
		    (backs, (noderef, newedges')::newedges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   436
		else
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   437
		    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   438
			val backs = Symtab.update_new ((nodename, Backref (noderef, defnames)), backs)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   439
		    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   440
			(backs, newedges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   441
		    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
   442
	    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
   443
	    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   444
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   445
	val (backs, newedges) = Symtab.foldl update_backrefs ((Symtab.empty, []), backs)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   446
						 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   447
	(* If a Circular exception is thrown then we never reach this point. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   448
        (* Ok, the definition is consistent, let's update this node. *)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   449
	val graph = Symtab.update ((mainref, Node (n, gty, Symtab.update 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   450
	  ((axname, thisDefnode), defs), backs, finals)), graph)
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
   451
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   452
        (* Furthermore, update all the other nodes that backreference this node. *)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   453
        fun final_update_backrefs graph noderef defname none_edges this_edges =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   454
	    let
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   455
		val node = getnode graph noderef
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   456
		val (Node (nodename, nodety, defs, backs, finals)) = node
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
   457
		val (Defnode (defnode_ty, defnode_edges)) = the (get_defnode node defname)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   458
		val (_, defnode_links) = the (Symtab.lookup (defnode_edges, n))
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   459
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   460
		fun update edges none_edges this_edges =
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   461
		    let 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   462
			val u = merge_labelled_edges edges [(SOME axname, pack_edges this_edges)]
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   463
		    in
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   464
			if none_edges = [] then
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   465
			    u
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   466
			else
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   467
			    (NONE, pack_edges none_edges)::u
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   468
		    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
   469
		    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   470
		val defnode_links' = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   471
		    case defnode_links of 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   472
			((NONE, _) :: edges) => update edges none_edges this_edges
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   473
		      | edges => update edges none_edges this_edges
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   474
		val defnode_edges' = Symtab.update ((n, (mainref, defnode_links')), defnode_edges)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   475
		val defs' = Symtab.update ((defname, Defnode (defnode_ty, defnode_edges')), defs)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   476
	    in
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   477
		Symtab.update ((nodename, Node (nodename, nodety, defs', backs, finals)), graph)
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
   478
	    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
   479
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   480
	val graph = foldl (fn ((noderef, newedges),graph) => foldl (fn ((defname, none_edges, this_edges), graph) =>
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   481
           final_update_backrefs graph noderef defname none_edges this_edges) graph newedges) graph newedges		    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   482
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   483
    in	    
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   484
	((Define (name, ty, axname, body))::actions, graph)	   
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   485
    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
   486
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   487
    fun finalize' ((c, ty), graph) = 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   488
      case Symtab.lookup (graph, c) of 
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   489
	  NONE => def_err ("cannot finalize constant "^(quote c)^"; it is not declared")
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   490
	| SOME (Node (noderef, nodety, defs, backs, finals)) =>
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   491
	  let 
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   492
	      val ty = checkT ty
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   493
	      val _ = if (not (is_instance_r ty nodety)) then
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   494
			  def_err ("only type instances of the declared constant "^(quote c)^" can be finalized")
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   495
		      else ()
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   496
	      val _ = Symtab.exists (fn (def_name, Defnode (def_ty, _)) =>  
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   497
					if can_be_unified_r ty def_ty then 
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   498
					    def_err ("cannot finalize constant "^(quote c)^"; clash with definition "^(quote def_name))
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   499
					else 
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   500
					    false)
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   501
				    defs
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   502
	  in				    
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   503
	      if exists (is_instance_r ty) finals then
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   504
		  graph
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   505
	      else 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   506
	      let
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   507
	          val finals = ty :: finals
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   508
		  val graph = Symtab.update ((noderef, Node(noderef, nodety, defs, backs, finals)), graph)
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   509
	      in
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   510
		  graph
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   511
	      end
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   512
(*		  fun update_backref ((graph, backs), (backrefname, Backref (_, backdefnames))) =
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   513
		  let
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   514
		      fun update_backdef ((graph, defnames), (backdefname, _)) = 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   515
	              let 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   516
			  val (backnode as Node (_, backty, backdefs, backbacks, backfinals)) = getnode graph backrefname
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   517
			  val (Defnode (def_ty, all_edges)) = the (get_defnode backnode backdefname)						      
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   518
			  val (defnames', all_edges') = 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   519
			      case Symtab.lookup (all_edges, noderef) of
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   520
				  NONE => sys_error "finalize: corrupt backref"
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   521
				| SOME (_, (NONE, edges)::rest) =>
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   522
				  let
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   523
				      val edges' = List.filter (fn (_, _, beta, _) => not (is_instance_r beta ty)) edges
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   524
				  in
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   525
				      if edges' = [] then 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   526
					  (defnames, Symtab.update ((noderef, (noderef, rest)), all_edges))
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   527
				      else
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   528
					  (Symtab.update ((backdefname, ()), defnames), 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   529
					   Symtab.update ((noderef, (noderef, (NONE, edges')::rest)), all_edges))
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   530
				  end
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   531
			  val defnode' = Defnode (def_ty, all_edges')
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   532
			  val backnode' = Node (backrefname, backty, Symtab.update ((backdefname, defnode'), backdefs), 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   533
					   backbacks, backfinals)
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   534
		      in
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   535
			  (Symtab.update ((backrefname, backnode'), graph), defnames')			  			  
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   536
		      end
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   537
	  
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   538
		      val (graph', defnames') = Symtab.foldl update_backdef ((graph, Symtab.empty), backdefnames)
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   539
		  in
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   540
		      (graph', if Symtab.is_empty defnames' then backs 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   541
			       else Symtab.update ((backrefname, Backref (backrefname, defnames')), backs))
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   542
		  end
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   543
		  val (graph', backs') = Symtab.foldl update_backref ((graph, Symtab.empty), backs)
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   544
		  val Node (_, _, defs, _, _) = getnode graph' noderef
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   545
	      in
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   546
		  Symtab.update ((noderef, Node (noderef, nodety, defs, backs', finals)), graph')
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   547
	      end*)
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   548
	  end
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   549
	   
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   550
    fun finalize (history, graph) c_ty = (history, graph)
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   551
	(*((Finalize c_ty)::history, finalize' (c_ty, graph))*)
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
   552
    
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   553
    fun merge' (Declare cty, g) = (declare g cty handle _ => g)
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
   554
      | merge' (Define (name, ty, axname, body), g as (_, graph)) = 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   555
	(case Symtab.lookup (graph, name) of
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   556
	     NONE => define g (name, ty) axname body
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   557
	   | SOME (Node (_, _, defs, _, _)) => 
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
   558
	     (case Symtab.lookup (defs, axname) of
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   559
		  NONE => define g (name, ty) axname body
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
   560
		| SOME _ => g))
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   561
      | merge' (Finalize finals, g) = (finalize g finals handle _ => g)
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
   562
	
16177
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   563
    fun myrev [] ys = ys
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   564
      | myrev (x::xs) ys = myrev xs (x::ys)
1af9f5c69745 Preliminary version of defs.ML that does not check final consts.
obua
parents: 16158
diff changeset
   565
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
   566
    fun merge (actions, _) g = foldr merge' g actions
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   567
16158
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   568
    fun finals (history, graph) = 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   569
	Symtab.foldl 
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   570
	    (fn (finals, (_, Node(name, _, _, _, ftys))) => Symtab.update_new ((name, ftys), finals))  
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   571
	    (Symtab.empty, graph)
2c3565b74b7a Removed final_consts from theory data. Now const_deps deals with final
obua
parents: 16113
diff changeset
   572
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
   573
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
   574
		
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   575
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   576
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   577
(*fun tvar name = TVar ((name, 0), [])
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   578
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   579
val bool = Type ("bool", [])
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   580
val int = Type ("int", [])
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   581
val alpha = tvar "'a"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   582
val beta = tvar "'b"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   583
val gamma = tvar "'c"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   584
fun pair a b = Type ("pair", [a,b])
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   585
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   586
val _ = print "make empty"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   587
val g = Defs.empty 
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   588
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   589
val _ = print "declare"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   590
val g = Defs.declare g "M" (alpha --> bool)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   591
val g = Defs.declare g "N" (beta --> bool)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   592
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   593
val _ = print "define"
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   594
val g = Defs.define g "N" (alpha --> bool) "defN" [("M", alpha --> bool)]
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   595
val g = Defs.define g "M" (alpha --> bool) "defM" [("N", int --> alpha)]
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   596
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   597
val g = Defs.declare g "0" alpha
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   598
val g = Defs.define g "0" (pair alpha beta) "zp" [("0", alpha), ("0", beta)]*)
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   599
cf468b93a02e Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff changeset
   600