src/Provers/trancl.ML
author wenzelm
Wed Apr 04 23:29:33 2007 +0200 (2007-04-04)
changeset 22596 d0d2af4db18f
parent 22257 159bfab776e2
child 26834 87a5b9ec3863
permissions -rw-r--r--
rep_thm/cterm/ctyp: removed obsolete sign field;
ballarin@15076
     1
(*
ballarin@15098
     2
  Title:	Transitivity reasoner for transitive closures of relations
ballarin@15076
     3
  Id:		$Id$
ballarin@15076
     4
  Author:	Oliver Kutter
ballarin@15076
     5
  Copyright:	TU Muenchen
ballarin@15076
     6
*)
ballarin@15076
     7
ballarin@15098
     8
(*
ballarin@15098
     9
ballarin@15098
    10
The packages provides tactics trancl_tac and rtrancl_tac that prove
ballarin@15098
    11
goals of the form
ballarin@15098
    12
ballarin@15098
    13
   (x,y) : r^+     and     (x,y) : r^* (rtrancl_tac only)
ballarin@15098
    14
ballarin@15098
    15
from premises of the form
ballarin@15098
    16
ballarin@15098
    17
   (x,y) : r,     (x,y) : r^+     and     (x,y) : r^* (rtrancl_tac only)
ballarin@15098
    18
ballarin@15098
    19
by reflexivity and transitivity.  The relation r is determined by inspecting
ballarin@15098
    20
the conclusion.
ballarin@15098
    21
ballarin@15098
    22
The package is implemented as an ML functor and thus not limited to
ballarin@15098
    23
particular constructs for transitive and reflexive-transitive
ballarin@15098
    24
closures, neither need relations be represented as sets of pairs.  In
ballarin@15098
    25
order to instantiate the package for transitive closure only, supply
ballarin@15098
    26
dummy theorems to the additional rules for reflexive-transitive
ballarin@15098
    27
closures, and don't use rtrancl_tac!
ballarin@15098
    28
ballarin@15098
    29
*)
ballarin@15098
    30
ballarin@15076
    31
signature TRANCL_ARITH = 
ballarin@15076
    32
sig
ballarin@15076
    33
ballarin@15076
    34
  (* theorems for transitive closure *)
ballarin@15076
    35
ballarin@15076
    36
  val r_into_trancl : thm
ballarin@15076
    37
      (* (a,b) : r ==> (a,b) : r^+ *)
ballarin@15076
    38
  val trancl_trans : thm
ballarin@15076
    39
      (* [| (a,b) : r^+ ; (b,c) : r^+ |] ==> (a,c) : r^+ *)
ballarin@15076
    40
ballarin@15076
    41
  (* additional theorems for reflexive-transitive closure *)
ballarin@15076
    42
ballarin@15076
    43
  val rtrancl_refl : thm
ballarin@15076
    44
      (* (a,a): r^* *)
ballarin@15076
    45
  val r_into_rtrancl : thm
ballarin@15076
    46
      (* (a,b) : r ==> (a,b) : r^* *)
ballarin@15076
    47
  val trancl_into_rtrancl : thm
ballarin@15076
    48
      (* (a,b) : r^+ ==> (a,b) : r^* *)
ballarin@15076
    49
  val rtrancl_trancl_trancl : thm
ballarin@15076
    50
      (* [| (a,b) : r^* ; (b,c) : r^+ |] ==> (a,c) : r^+ *)
ballarin@15076
    51
  val trancl_rtrancl_trancl : thm
ballarin@15076
    52
      (* [| (a,b) : r^+ ; (b,c) : r^* |] ==> (a,c) : r^+ *)
ballarin@15076
    53
  val rtrancl_trans : thm
ballarin@15076
    54
      (* [| (a,b) : r^* ; (b,c) : r^* |] ==> (a,c) : r^* *)
ballarin@15076
    55
ballarin@15098
    56
  (* decomp: decompose a premise or conclusion
ballarin@15098
    57
ballarin@15098
    58
     Returns one of the following:
ballarin@15098
    59
skalberg@15531
    60
     NONE if not an instance of a relation,
skalberg@15531
    61
     SOME (x, y, r, s) if instance of a relation, where
ballarin@15098
    62
       x: left hand side argument, y: right hand side argument,
ballarin@15098
    63
       r: the relation,
ballarin@15098
    64
       s: the kind of closure, one of
ballarin@15098
    65
            "r":   the relation itself,
ballarin@15098
    66
            "r^+": transitive closure of the relation,
ballarin@15098
    67
            "r^*": reflexive-transitive closure of the relation
ballarin@15098
    68
  *)  
ballarin@15098
    69
ballarin@15076
    70
  val decomp: term ->  (term * term * term * string) option 
ballarin@15076
    71
ballarin@15076
    72
end;
ballarin@15076
    73
ballarin@15076
    74
signature TRANCL_TAC = 
ballarin@15076
    75
sig
ballarin@15076
    76
  val trancl_tac: int -> tactic;
ballarin@15076
    77
  val rtrancl_tac: int -> tactic;
ballarin@15076
    78
end;
ballarin@15076
    79
ballarin@15076
    80
functor Trancl_Tac_Fun (Cls : TRANCL_ARITH): TRANCL_TAC = 
ballarin@15076
    81
struct
ballarin@15076
    82
ballarin@15076
    83
 
berghofe@22257
    84
datatype proof
ballarin@15076
    85
  = Asm of int 
ballarin@15076
    86
  | Thm of proof list * thm; 
ballarin@15076
    87
berghofe@22257
    88
exception Cannot; (* internal exception: raised if no proof can be found *)
ballarin@15076
    89
berghofe@22257
    90
fun prove thy r asms = 
berghofe@22257
    91
  let
berghofe@22257
    92
    fun inst thm =
berghofe@22257
    93
      let val SOME (_, _, r', _) = Cls.decomp (concl_of thm)
berghofe@22257
    94
      in Drule.cterm_instantiate [(cterm_of thy r', cterm_of thy r)] thm end;
berghofe@22257
    95
    fun pr (Asm i) = List.nth (asms, i)
berghofe@22257
    96
      | pr (Thm (prfs, thm)) = map pr prfs MRS inst thm
ballarin@15076
    97
  in pr end;
ballarin@15076
    98
ballarin@15076
    99
  
ballarin@15076
   100
(* Internal datatype for inequalities *)
ballarin@15076
   101
datatype rel 
ballarin@15098
   102
   = Trans  of term * term * proof  (* R^+ *)
ballarin@15076
   103
   | RTrans of term * term * proof; (* R^* *)
ballarin@15076
   104
   
ballarin@15076
   105
 (* Misc functions for datatype rel *)
ballarin@15098
   106
fun lower (Trans (x, _, _)) = x
ballarin@15076
   107
  | lower (RTrans (x,_,_)) = x;
ballarin@15076
   108
ballarin@15098
   109
fun upper (Trans (_, y, _)) = y
ballarin@15076
   110
  | upper (RTrans (_,y,_)) = y;
ballarin@15076
   111
ballarin@15098
   112
fun getprf   (Trans   (_, _, p)) = p
ballarin@15076
   113
|   getprf   (RTrans (_,_, p)) = p; 
ballarin@15076
   114
 
ballarin@15098
   115
(* ************************************************************************ *)
ballarin@15098
   116
(*                                                                          *)
ballarin@15098
   117
(*  mkasm_trancl Rel (t,n): term -> (term , int) -> rel list                *)
ballarin@15098
   118
(*                                                                          *)
ballarin@15098
   119
(*  Analyse assumption t with index n with respect to relation Rel:         *)
ballarin@15098
   120
(*  If t is of the form "(x, y) : Rel" (or Rel^+), translate to             *)
ballarin@15098
   121
(*  an object (singleton list) of internal datatype rel.                    *)
ballarin@15098
   122
(*  Otherwise return empty list.                                            *)
ballarin@15098
   123
(*                                                                          *)
ballarin@15098
   124
(* ************************************************************************ *)
ballarin@15098
   125
ballarin@15076
   126
fun mkasm_trancl  Rel  (t, n) =
ballarin@15076
   127
  case Cls.decomp t of
skalberg@15531
   128
    SOME (x, y, rel,r) => if rel aconv Rel then  
ballarin@15076
   129
    
ballarin@15076
   130
    (case r of
ballarin@15076
   131
      "r"   => [Trans (x,y, Thm([Asm n], Cls.r_into_trancl))]
ballarin@15076
   132
    | "r+"  => [Trans (x,y, Asm n)]
ballarin@15076
   133
    | "r*"  => []
ballarin@15076
   134
    | _     => error ("trancl_tac: unknown relation symbol"))
ballarin@15076
   135
    else [] 
skalberg@15531
   136
  | NONE => [];
ballarin@15076
   137
  
ballarin@15098
   138
(* ************************************************************************ *)
ballarin@15098
   139
(*                                                                          *)
ballarin@15098
   140
(*  mkasm_rtrancl Rel (t,n): term -> (term , int) -> rel list               *)
ballarin@15098
   141
(*                                                                          *)
ballarin@15098
   142
(*  Analyse assumption t with index n with respect to relation Rel:         *)
ballarin@15098
   143
(*  If t is of the form "(x, y) : Rel" (or Rel^+ or Rel^* ), translate to   *)
ballarin@15098
   144
(*  an object (singleton list) of internal datatype rel.                    *)
ballarin@15098
   145
(*  Otherwise return empty list.                                            *)
ballarin@15098
   146
(*                                                                          *)
ballarin@15098
   147
(* ************************************************************************ *)
ballarin@15098
   148
ballarin@15076
   149
fun mkasm_rtrancl Rel (t, n) =
ballarin@15076
   150
  case Cls.decomp t of
skalberg@15531
   151
   SOME (x, y, rel, r) => if rel aconv Rel then
ballarin@15076
   152
    (case r of
ballarin@15076
   153
      "r"   => [ Trans (x,y, Thm([Asm n], Cls.r_into_trancl))]
ballarin@15076
   154
    | "r+"  => [ Trans (x,y, Asm n)]
ballarin@15076
   155
    | "r*"  => [ RTrans(x,y, Asm n)]
ballarin@15076
   156
    | _     => error ("rtrancl_tac: unknown relation symbol" ))
ballarin@15076
   157
   else [] 
skalberg@15531
   158
  | NONE => [];
ballarin@15076
   159
ballarin@15098
   160
(* ************************************************************************ *)
ballarin@15098
   161
(*                                                                          *)
ballarin@15098
   162
(*  mkconcl_trancl t: term -> (term, rel, proof)                            *)
ballarin@15098
   163
(*  mkconcl_rtrancl t: term -> (term, rel, proof)                           *)
ballarin@15098
   164
(*                                                                          *)
ballarin@15098
   165
(*  Analyse conclusion t:                                                   *)
ballarin@15098
   166
(*    - must be of form "(x, y) : r^+ (or r^* for rtrancl)                  *)
ballarin@15098
   167
(*    - returns r                                                           *)
ballarin@15098
   168
(*    - conclusion in internal form                                         *)
ballarin@15098
   169
(*    - proof object                                                        *)
ballarin@15098
   170
(*                                                                          *)
ballarin@15098
   171
(* ************************************************************************ *)
ballarin@15098
   172
ballarin@15076
   173
fun mkconcl_trancl  t =
ballarin@15076
   174
  case Cls.decomp t of
skalberg@15531
   175
    SOME (x, y, rel, r) => (case r of
ballarin@15076
   176
      "r+"  => (rel, Trans (x,y, Asm ~1), Asm 0)
ballarin@15076
   177
    | _     => raise Cannot)
skalberg@15531
   178
  | NONE => raise Cannot;
ballarin@15076
   179
ballarin@15076
   180
fun mkconcl_rtrancl  t =
ballarin@15076
   181
  case Cls.decomp t of
skalberg@15531
   182
    SOME (x,  y, rel,r ) => (case r of
ballarin@15076
   183
      "r+"  => (rel, Trans (x,y, Asm ~1),  Asm 0)
ballarin@15076
   184
    | "r*"  => (rel, RTrans (x,y, Asm ~1), Asm 0)
ballarin@15076
   185
    | _     => raise Cannot)
skalberg@15531
   186
  | NONE => raise Cannot;
ballarin@15076
   187
ballarin@15098
   188
(* ************************************************************************ *)
ballarin@15098
   189
(*                                                                          *)
ballarin@15098
   190
(*  makeStep (r1, r2): rel * rel -> rel                                     *)
ballarin@15098
   191
(*                                                                          *)
ballarin@15098
   192
(*  Apply transitivity to r1 and r2, obtaining a new element of r^+ or r^*, *)
ballarin@15098
   193
(*  according the following rules:                                          *)
ballarin@15098
   194
(*                                                                          *)
ballarin@15098
   195
(* ( (a, b) : r^+ , (b,c) : r^+ ) --> (a,c) : r^+                           *)
ballarin@15098
   196
(* ( (a, b) : r^* , (b,c) : r^+ ) --> (a,c) : r^+                           *)
ballarin@15098
   197
(* ( (a, b) : r^+ , (b,c) : r^* ) --> (a,c) : r^+                           *)
ballarin@15098
   198
(* ( (a, b) : r^* , (b,c) : r^* ) --> (a,c) : r^*                           *)
ballarin@15098
   199
(*                                                                          *)
ballarin@15098
   200
(* ************************************************************************ *)
ballarin@15098
   201
ballarin@15076
   202
fun makeStep (Trans (a,_,p), Trans(_,c,q))  = Trans (a,c, Thm ([p,q], Cls.trancl_trans))
ballarin@15076
   203
(* refl. + trans. cls. rules *)
ballarin@15076
   204
|   makeStep (RTrans (a,_,p), Trans(_,c,q))  = Trans (a,c, Thm ([p,q], Cls.rtrancl_trancl_trancl))
ballarin@15076
   205
|   makeStep (Trans (a,_,p), RTrans(_,c,q))  = Trans (a,c, Thm ([p,q], Cls.trancl_rtrancl_trancl))   
ballarin@15098
   206
|   makeStep (RTrans (a,_,p), RTrans(_,c,q))  = RTrans (a,c, Thm ([p,q], Cls.rtrancl_trans));
ballarin@15076
   207
ballarin@15076
   208
(* ******************************************************************* *)
ballarin@15076
   209
(*                                                                     *)
ballarin@15076
   210
(* transPath (Clslist, Cls): (rel  list * rel) -> rel                  *)
ballarin@15076
   211
(*                                                                     *)
ballarin@15076
   212
(* If a path represented by a list of elements of type rel is found,   *)
ballarin@15076
   213
(* this needs to be contracted to a single element of type rel.        *)
ballarin@15076
   214
(* Prior to each transitivity step it is checked whether the step is   *)
ballarin@15076
   215
(* valid.                                                              *)
ballarin@15076
   216
(*                                                                     *)
ballarin@15076
   217
(* ******************************************************************* *)
ballarin@15076
   218
ballarin@15076
   219
fun transPath ([],acc) = acc
ballarin@15076
   220
|   transPath (x::xs,acc) = transPath (xs, makeStep(acc,x))
ballarin@15076
   221
      
ballarin@15076
   222
(* ********************************************************************* *)
ballarin@15076
   223
(* Graph functions                                                       *)
ballarin@15076
   224
(* ********************************************************************* *)
ballarin@15076
   225
ballarin@15076
   226
(* *********************************************************** *)
ballarin@15076
   227
(* Functions for constructing graphs                           *)
ballarin@15076
   228
(* *********************************************************** *)
ballarin@15076
   229
ballarin@15076
   230
fun addEdge (v,d,[]) = [(v,d)]
ballarin@15076
   231
|   addEdge (v,d,((u,dl)::el)) = if v aconv u then ((v,d@dl)::el)
ballarin@15076
   232
    else (u,dl):: (addEdge(v,d,el));
ballarin@15076
   233
    
ballarin@15076
   234
(* ********************************************************************** *)
ballarin@15076
   235
(*                                                                        *)
ballarin@15076
   236
(* mkGraph constructs from a list of objects of type rel  a graph g       *)
ballarin@15098
   237
(* and a list of all edges with label r+.                                 *)
ballarin@15076
   238
(*                                                                        *)
ballarin@15076
   239
(* ********************************************************************** *)
ballarin@15076
   240
ballarin@15076
   241
fun mkGraph [] = ([],[])
ballarin@15076
   242
|   mkGraph ys =  
ballarin@15076
   243
 let
ballarin@15076
   244
  fun buildGraph ([],g,zs) = (g,zs)
ballarin@15076
   245
  |   buildGraph (x::xs, g, zs) = 
ballarin@15076
   246
        case x of (Trans (_,_,_)) => 
ballarin@15076
   247
	       buildGraph (xs, addEdge((upper x), [],(addEdge ((lower x),[((upper x),x)],g))), x::zs) 
ballarin@15076
   248
	| _ => buildGraph (xs, addEdge((upper x), [],(addEdge ((lower x),[((upper x),x)],g))), zs)
ballarin@15076
   249
in buildGraph (ys, [], []) end;
ballarin@15076
   250
ballarin@15076
   251
(* *********************************************************************** *)
ballarin@15076
   252
(*                                                                         *)
ballarin@15076
   253
(* adjacent g u : (''a * 'b list ) list -> ''a -> 'b list                  *)
ballarin@15076
   254
(*                                                                         *)
ballarin@15076
   255
(* List of successors of u in graph g                                      *)
ballarin@15076
   256
(*                                                                         *)
ballarin@15076
   257
(* *********************************************************************** *)
ballarin@15076
   258
 
ballarin@15076
   259
fun adjacent eq_comp ((v,adj)::el) u = 
ballarin@15076
   260
    if eq_comp (u, v) then adj else adjacent eq_comp el u
ballarin@15076
   261
|   adjacent _  []  _ = []  
ballarin@15076
   262
ballarin@15076
   263
(* *********************************************************************** *)
ballarin@15076
   264
(*                                                                         *)
ballarin@15076
   265
(* dfs eq_comp g u v:                                                      *)
ballarin@15076
   266
(* ('a * 'a -> bool) -> ('a  *( 'a * rel) list) list ->                    *)
ballarin@15076
   267
(* 'a -> 'a -> (bool * ('a * rel) list)                                    *) 
ballarin@15076
   268
(*                                                                         *)
ballarin@15076
   269
(* Depth first search of v from u.                                         *)
ballarin@15076
   270
(* Returns (true, path(u, v)) if successful, otherwise (false, []).        *)
ballarin@15076
   271
(*                                                                         *)
ballarin@15076
   272
(* *********************************************************************** *)
ballarin@15076
   273
ballarin@15076
   274
fun dfs eq_comp g u v = 
ballarin@15076
   275
 let 
ballarin@15076
   276
    val pred = ref nil;
ballarin@15076
   277
    val visited = ref nil;
ballarin@15076
   278
    
ballarin@15076
   279
    fun been_visited v = exists (fn w => eq_comp (w, v)) (!visited)
ballarin@15076
   280
    
ballarin@15076
   281
    fun dfs_visit u' = 
ballarin@15076
   282
    let val _ = visited := u' :: (!visited)
ballarin@15076
   283
    
ballarin@15076
   284
    fun update (x,l) = let val _ = pred := (x,l) ::(!pred) in () end;
ballarin@15076
   285
    
ballarin@15076
   286
    in if been_visited v then () 
ballarin@15076
   287
    else (app (fn (v',l) => if been_visited v' then () else (
ballarin@15076
   288
       update (v',l); 
ballarin@15076
   289
       dfs_visit v'; ()) )) (adjacent eq_comp g u')
ballarin@15076
   290
     end
ballarin@15076
   291
  in 
ballarin@15076
   292
    dfs_visit u; 
ballarin@15076
   293
    if (been_visited v) then (true, (!pred)) else (false , [])   
ballarin@15076
   294
  end;
ballarin@15076
   295
ballarin@15076
   296
(* *********************************************************************** *)
ballarin@15076
   297
(*                                                                         *)
ballarin@15076
   298
(* transpose g:                                                            *)
ballarin@15076
   299
(* (''a * ''a list) list -> (''a * ''a list) list                          *)
ballarin@15076
   300
(*                                                                         *)
ballarin@15076
   301
(* Computes transposed graph g' from g                                     *)
ballarin@15076
   302
(* by reversing all edges u -> v to v -> u                                 *)
ballarin@15076
   303
(*                                                                         *)
ballarin@15076
   304
(* *********************************************************************** *)
ballarin@15076
   305
ballarin@15076
   306
fun transpose eq_comp g =
ballarin@15076
   307
  let
ballarin@15076
   308
   (* Compute list of reversed edges for each adjacency list *)
ballarin@15076
   309
   fun flip (u,(v,l)::el) = (v,(u,l)) :: flip (u,el)
ballarin@15076
   310
     | flip (_,nil) = nil
ballarin@15076
   311
   
ballarin@15076
   312
   (* Compute adjacency list for node u from the list of edges
ballarin@15076
   313
      and return a likewise reduced list of edges.  The list of edges
ballarin@15076
   314
      is searches for edges starting from u, and these edges are removed. *)
ballarin@15076
   315
   fun gather (u,(v,w)::el) =
ballarin@15076
   316
    let
ballarin@15076
   317
     val (adj,edges) = gather (u,el)
ballarin@15076
   318
    in
ballarin@15076
   319
     if eq_comp (u, v) then (w::adj,edges)
ballarin@15076
   320
     else (adj,(v,w)::edges)
ballarin@15076
   321
    end
ballarin@15076
   322
   | gather (_,nil) = (nil,nil)
ballarin@15076
   323
ballarin@15076
   324
   (* For every node in the input graph, call gather to find all reachable
ballarin@15076
   325
      nodes in the list of edges *)
ballarin@15076
   326
   fun assemble ((u,_)::el) edges =
ballarin@15076
   327
       let val (adj,edges) = gather (u,edges)
ballarin@15076
   328
       in (u,adj) :: assemble el edges
ballarin@15076
   329
       end
ballarin@15076
   330
     | assemble nil _ = nil
ballarin@15076
   331
ballarin@15076
   332
   (* Compute, for each adjacency list, the list with reversed edges,
ballarin@15076
   333
      and concatenate these lists. *)
skalberg@15574
   334
   val flipped = foldr (op @) nil (map flip g)
ballarin@15076
   335
 
ballarin@15076
   336
 in assemble g flipped end    
ballarin@15076
   337
 
ballarin@15076
   338
(* *********************************************************************** *)
ballarin@15076
   339
(*                                                                         *)
ballarin@15076
   340
(* dfs_reachable eq_comp g u:                                              *)
ballarin@15076
   341
(* (int * int list) list -> int -> int list                                *) 
ballarin@15076
   342
(*                                                                         *)
ballarin@15076
   343
(* Computes list of all nodes reachable from u in g.                       *)
ballarin@15076
   344
(*                                                                         *)
ballarin@15076
   345
(* *********************************************************************** *)
ballarin@15076
   346
ballarin@15076
   347
fun dfs_reachable eq_comp g u = 
ballarin@15076
   348
 let
ballarin@15076
   349
  (* List of vertices which have been visited. *)
ballarin@15076
   350
  val visited  = ref nil;
ballarin@15076
   351
  
ballarin@15076
   352
  fun been_visited v = exists (fn w => eq_comp (w, v)) (!visited)
ballarin@15076
   353
ballarin@15076
   354
  fun dfs_visit g u  =
ballarin@15076
   355
      let
ballarin@15076
   356
   val _ = visited := u :: !visited
ballarin@15076
   357
   val descendents =
skalberg@15574
   358
       foldr (fn ((v,l),ds) => if been_visited v then ds
ballarin@15076
   359
            else v :: dfs_visit g v @ ds)
skalberg@15574
   360
        nil (adjacent eq_comp g u)
ballarin@15076
   361
   in  descendents end
ballarin@15076
   362
 
ballarin@15076
   363
 in u :: dfs_visit g u end;
ballarin@15076
   364
ballarin@15076
   365
(* *********************************************************************** *)
ballarin@15076
   366
(*                                                                         *)
ballarin@15076
   367
(* dfs_term_reachable g u:                                                  *)
ballarin@15076
   368
(* (term * term list) list -> term -> term list                            *) 
ballarin@15076
   369
(*                                                                         *)
ballarin@15076
   370
(* Computes list of all nodes reachable from u in g.                       *)
ballarin@15076
   371
(*                                                                         *)
ballarin@15076
   372
(* *********************************************************************** *)
ballarin@15076
   373
ballarin@15076
   374
fun dfs_term_reachable g u = dfs_reachable (op aconv) g u;
ballarin@15076
   375
ballarin@15076
   376
(* ************************************************************************ *) 
ballarin@15076
   377
(*                                                                          *)
ballarin@15076
   378
(* findPath x y g: Term.term -> Term.term ->                                *)
ballarin@15076
   379
(*                  (Term.term * (Term.term * rel list) list) ->            *) 
ballarin@15076
   380
(*                  (bool, rel list)                                        *)
ballarin@15076
   381
(*                                                                          *)
ballarin@15076
   382
(*  Searches a path from vertex x to vertex y in Graph g, returns true and  *)
ballarin@15098
   383
(*  the list of edges if path is found, otherwise false and nil.            *)
ballarin@15076
   384
(*                                                                          *)
ballarin@15076
   385
(* ************************************************************************ *) 
ballarin@15076
   386
 
ballarin@15076
   387
fun findPath x y g = 
ballarin@15076
   388
  let 
ballarin@15076
   389
   val (found, tmp) =  dfs (op aconv) g x y ;
ballarin@15076
   390
   val pred = map snd tmp;
ballarin@15076
   391
	 
ballarin@15076
   392
   fun path x y  =
ballarin@15076
   393
    let
ballarin@15076
   394
	 (* find predecessor u of node v and the edge u -> v *)
ballarin@15076
   395
		
ballarin@15076
   396
      fun lookup v [] = raise Cannot
ballarin@15076
   397
      |   lookup v (e::es) = if (upper e) aconv v then e else lookup v es;
ballarin@15076
   398
		
ballarin@15076
   399
      (* traverse path backwards and return list of visited edges *)   
ballarin@15076
   400
      fun rev_path v = 
ballarin@15076
   401
	let val l = lookup v pred
ballarin@15076
   402
	    val u = lower l;
ballarin@15076
   403
	in
ballarin@15076
   404
	  if u aconv x then [l] else (rev_path u) @ [l] 
ballarin@15076
   405
	end
ballarin@15076
   406
       
ballarin@15076
   407
    in rev_path y end;
ballarin@15076
   408
		
ballarin@15076
   409
   in 
ballarin@15076
   410
ballarin@15076
   411
     
ballarin@15076
   412
      if found then ( (found, (path x y) )) else (found,[])
ballarin@15076
   413
   
ballarin@15076
   414
     
ballarin@15076
   415
ballarin@15076
   416
   end;
ballarin@15076
   417
ballarin@15098
   418
(* ************************************************************************ *)
ballarin@15098
   419
(*                                                                          *)
ballarin@15098
   420
(* findRtranclProof g tranclEdges subgoal:                                  *)
ballarin@15098
   421
(* (Term.term * (Term.term * rel list) list) -> rel -> proof list           *)
ballarin@15098
   422
(*                                                                          *)
ballarin@15098
   423
(* Searches in graph g a proof for subgoal.                                 *)
ballarin@15098
   424
(*                                                                          *)
ballarin@15098
   425
(* ************************************************************************ *)
ballarin@15076
   426
ballarin@15076
   427
fun findRtranclProof g tranclEdges subgoal = 
ballarin@15076
   428
   case subgoal of (RTrans (x,y,_)) => if x aconv y then [Thm ([], Cls.rtrancl_refl)] else (
ballarin@15076
   429
     let val (found, path) = findPath (lower subgoal) (upper subgoal) g
ballarin@15076
   430
     in 
ballarin@15076
   431
       if found then (
ballarin@15076
   432
          let val path' = (transPath (tl path, hd path))
ballarin@15076
   433
	  in 
ballarin@15076
   434
	   
ballarin@15076
   435
	    case path' of (Trans (_,_,p)) => [Thm ([p], Cls.trancl_into_rtrancl )] 
ballarin@15076
   436
	    | _ => [getprf path']
ballarin@15076
   437
	   
ballarin@15076
   438
	  end
ballarin@15076
   439
       )
ballarin@15076
   440
       else raise Cannot
ballarin@15076
   441
     end
ballarin@15076
   442
   )
ballarin@15076
   443
   
ballarin@15076
   444
| (Trans (x,y,_)) => (
ballarin@15076
   445
 
ballarin@15076
   446
  let
ballarin@15076
   447
   val Vx = dfs_term_reachable g x;
ballarin@15076
   448
   val g' = transpose (op aconv) g;
ballarin@15076
   449
   val Vy = dfs_term_reachable g' y;
ballarin@15076
   450
   
ballarin@15076
   451
   fun processTranclEdges [] = raise Cannot
ballarin@15076
   452
   |   processTranclEdges (e::es) = 
ballarin@15076
   453
          if (upper e) mem Vx andalso (lower e) mem Vx
ballarin@15076
   454
	  andalso (upper e) mem Vy andalso (lower e) mem Vy
ballarin@15076
   455
	  then (
ballarin@15076
   456
	      
ballarin@15076
   457
	   
ballarin@15076
   458
	    if (lower e) aconv x then (
ballarin@15076
   459
	      if (upper e) aconv y then (
ballarin@15076
   460
	          [(getprf e)] 
ballarin@15076
   461
	      )
ballarin@15076
   462
	      else (
ballarin@15076
   463
	          let 
ballarin@15076
   464
		    val (found,path) = findPath (upper e) y g
ballarin@15076
   465
		  in
ballarin@15076
   466
ballarin@15076
   467
		   if found then ( 
ballarin@15076
   468
		       [getprf (transPath (path, e))]
ballarin@15076
   469
		      ) else processTranclEdges es
ballarin@15076
   470
		  
ballarin@15076
   471
		  end 
ballarin@15076
   472
	      )   
ballarin@15076
   473
	    )
ballarin@15076
   474
	    else if (upper e) aconv y then (
ballarin@15076
   475
	       let val (xufound,xupath) = findPath x (lower e) g
ballarin@15076
   476
	       in 
ballarin@15076
   477
	       
ballarin@15076
   478
	          if xufound then (
ballarin@15076
   479
		  	    
ballarin@15076
   480
		    let val xuRTranclEdge = transPath (tl xupath, hd xupath)
ballarin@15076
   481
			    val xyTranclEdge = makeStep(xuRTranclEdge,e)
ballarin@15076
   482
				
ballarin@15076
   483
				in [getprf xyTranclEdge] end
ballarin@15076
   484
				
ballarin@15076
   485
	         ) else processTranclEdges es
ballarin@15076
   486
	       
ballarin@15076
   487
	       end
ballarin@15076
   488
	    )
ballarin@15076
   489
	    else ( 
ballarin@15076
   490
	   
ballarin@15076
   491
	        let val (xufound,xupath) = findPath x (lower e) g
ballarin@15076
   492
		    val (vyfound,vypath) = findPath (upper e) y g
ballarin@15076
   493
		 in 
ballarin@15076
   494
		    if xufound then (
ballarin@15076
   495
		         if vyfound then ( 
ballarin@15076
   496
			    let val xuRTranclEdge = transPath (tl xupath, hd xupath)
ballarin@15076
   497
			        val vyRTranclEdge = transPath (tl vypath, hd vypath)
ballarin@15076
   498
				val xyTranclEdge = makeStep (makeStep(xuRTranclEdge,e),vyRTranclEdge)
ballarin@15076
   499
				
ballarin@15076
   500
				in [getprf xyTranclEdge] end
ballarin@15076
   501
				
ballarin@15076
   502
			 ) else processTranclEdges es
ballarin@15076
   503
		    ) 
ballarin@15076
   504
		    else processTranclEdges es
ballarin@15076
   505
		 end
ballarin@15076
   506
	    )
ballarin@15076
   507
	  )
ballarin@15076
   508
	  else processTranclEdges es;
ballarin@15076
   509
   in processTranclEdges tranclEdges end )
ballarin@15076
   510
| _ => raise Cannot
ballarin@15076
   511
ballarin@15076
   512
   
ballarin@15076
   513
fun solveTrancl (asms, concl) = 
ballarin@15076
   514
 let val (g,_) = mkGraph asms
ballarin@15076
   515
 in
ballarin@15076
   516
  let val (_, subgoal, _) = mkconcl_trancl concl
ballarin@15076
   517
      val (found, path) = findPath (lower subgoal) (upper subgoal) g
ballarin@15076
   518
  in
ballarin@15076
   519
    if found then  [getprf (transPath (tl path, hd path))]
ballarin@15076
   520
    else raise Cannot 
ballarin@15076
   521
  end
ballarin@15076
   522
 end;
ballarin@15076
   523
  
ballarin@15076
   524
fun solveRtrancl (asms, concl) = 
ballarin@15076
   525
 let val (g,tranclEdges) = mkGraph asms
ballarin@15076
   526
     val (_, subgoal, _) = mkconcl_rtrancl concl
ballarin@15076
   527
in
ballarin@15076
   528
  findRtranclProof g tranclEdges subgoal
ballarin@15076
   529
end;
ballarin@15076
   530
 
ballarin@15076
   531
   
berghofe@22257
   532
val trancl_tac =   SUBGOAL (fn (A, n) => fn st =>
ballarin@15076
   533
 let
berghofe@22257
   534
  val thy = theory_of_thm st;
ballarin@15076
   535
  val Hs = Logic.strip_assums_hyp A;
ballarin@15076
   536
  val C = Logic.strip_assums_concl A;
ballarin@15076
   537
  val (rel,subgoals, prf) = mkconcl_trancl C;
skalberg@15570
   538
  val prems = List.concat (ListPair.map (mkasm_trancl rel) (Hs, 0 upto (length Hs - 1)))
ballarin@15076
   539
  val prfs = solveTrancl (prems, C);
ballarin@15076
   540
ballarin@15076
   541
 in
ballarin@15076
   542
  METAHYPS (fn asms =>
berghofe@22257
   543
    let val thms = map (prove thy rel asms) prfs
berghofe@22257
   544
    in rtac (prove thy rel thms prf) 1 end) n st
ballarin@15076
   545
 end
berghofe@22257
   546
handle  Cannot  => no_tac st);
ballarin@15076
   547
ballarin@15076
   548
 
ballarin@15076
   549
 
berghofe@22257
   550
val rtrancl_tac =   SUBGOAL (fn (A, n) => fn st =>
ballarin@15076
   551
 let
berghofe@22257
   552
  val thy = theory_of_thm st;
ballarin@15076
   553
  val Hs = Logic.strip_assums_hyp A;
ballarin@15076
   554
  val C = Logic.strip_assums_concl A;
ballarin@15076
   555
  val (rel,subgoals, prf) = mkconcl_rtrancl C;
ballarin@15076
   556
skalberg@15570
   557
  val prems = List.concat (ListPair.map (mkasm_rtrancl rel) (Hs, 0 upto (length Hs - 1)))
ballarin@15076
   558
  val prfs = solveRtrancl (prems, C);
ballarin@15076
   559
 in
ballarin@15076
   560
  METAHYPS (fn asms =>
berghofe@22257
   561
    let val thms = map (prove thy rel asms) prfs
berghofe@22257
   562
    in rtac (prove thy rel thms prf) 1 end) n st
ballarin@15076
   563
 end
berghofe@22257
   564
handle  Cannot  => no_tac st);
ballarin@15076
   565
ballarin@15076
   566
end;