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