src/Provers/quasi.ML
author wenzelm
Sun Mar 07 12:19:47 2010 +0100 (2010-03-07)
changeset 35625 9c818cab0dd0
parent 33063 4d462963a7db
child 37744 3daaf23b9ab4
permissions -rw-r--r--
modernized structure Object_Logic;
wenzelm@29276
     1
(*  Author:     Oliver Kutter, TU Muenchen
wenzelm@29276
     2
wenzelm@29276
     3
Reasoner for simple transitivity and quasi orders.
wenzelm@29276
     4
*)
ballarin@15103
     5
wenzelm@32215
     6
(*
wenzelm@32215
     7
ballarin@15103
     8
The package provides tactics trans_tac and quasi_tac that use
wenzelm@32215
     9
premises of the form
ballarin@15103
    10
ballarin@15103
    11
  t = u, t ~= u, t < u and t <= u
ballarin@15103
    12
ballarin@15103
    13
to
ballarin@15103
    14
- either derive a contradiction, in which case the conclusion can be
ballarin@15103
    15
  any term,
ballarin@15103
    16
- or prove the concluson, which must be of the form t ~= u, t < u or
ballarin@15103
    17
  t <= u.
ballarin@15103
    18
ballarin@15103
    19
Details:
ballarin@15103
    20
ballarin@15103
    21
1. trans_tac:
ballarin@15103
    22
   Only premises of form t <= u are used and the conclusion must be of
ballarin@15103
    23
   the same form.  The conclusion is proved, if possible, by a chain of
ballarin@15103
    24
   transitivity from the assumptions.
ballarin@15103
    25
ballarin@15103
    26
2. quasi_tac:
ballarin@15103
    27
   <= is assumed to be a quasi order and < its strict relative, defined
ballarin@15103
    28
   as t < u == t <= u & t ~= u.  Again, the conclusion is proved from
ballarin@15103
    29
   the assumptions.
ballarin@15103
    30
   Note that the presence of a strict relation is not necessary for
ballarin@15103
    31
   quasi_tac.  Configure decomp_quasi to ignore < and ~=.  A list of
wenzelm@32215
    32
   required theorems for both situations is given below.
ballarin@15103
    33
*)
ballarin@15103
    34
ballarin@15103
    35
signature LESS_ARITH =
ballarin@15103
    36
sig
ballarin@15103
    37
  (* Transitivity of <=
wenzelm@32215
    38
     Note that transitivities for < hold for partial orders only. *)
ballarin@15103
    39
  val le_trans: thm  (* [| x <= y; y <= z |] ==> x <= z *)
wenzelm@32215
    40
ballarin@15103
    41
  (* Additional theorem for quasi orders *)
ballarin@15103
    42
  val le_refl: thm  (* x <= x *)
ballarin@15103
    43
  val eqD1: thm (* x = y ==> x <= y *)
ballarin@15103
    44
  val eqD2: thm (* x = y ==> y <= x *)
ballarin@15103
    45
ballarin@15103
    46
  (* Additional theorems for premises of the form x < y *)
ballarin@15103
    47
  val less_reflE: thm  (* x < x ==> P *)
ballarin@15103
    48
  val less_imp_le : thm (* x < y ==> x <= y *)
ballarin@15103
    49
ballarin@15103
    50
  (* Additional theorems for premises of the form x ~= y *)
ballarin@15103
    51
  val le_neq_trans : thm (* [| x <= y ; x ~= y |] ==> x < y *)
ballarin@15103
    52
  val neq_le_trans : thm (* [| x ~= y ; x <= y |] ==> x < y *)
ballarin@15103
    53
ballarin@15103
    54
  (* Additional theorem for goals of form x ~= y *)
ballarin@15103
    55
  val less_imp_neq : thm (* x < y ==> x ~= y *)
ballarin@15103
    56
ballarin@15103
    57
  (* Analysis of premises and conclusion *)
skalberg@15531
    58
  (* decomp_x (`x Rel y') should yield SOME (x, Rel, y)
ballarin@15103
    59
       where Rel is one of "<", "<=", "=" and "~=",
ballarin@15103
    60
       other relation symbols cause an error message *)
ballarin@15103
    61
  (* decomp_trans is used by trans_tac, it may only return Rel = "<=" *)
wenzelm@19250
    62
  val decomp_trans: theory -> term -> (term * string * term) option
ballarin@15103
    63
  (* decomp_quasi is used by quasi_tac *)
wenzelm@19250
    64
  val decomp_quasi: theory -> term -> (term * string * term) option
ballarin@15103
    65
end;
ballarin@15103
    66
wenzelm@32215
    67
signature QUASI_TAC =
ballarin@15103
    68
sig
wenzelm@32215
    69
  val trans_tac: Proof.context -> int -> tactic
wenzelm@32215
    70
  val quasi_tac: Proof.context -> int -> tactic
ballarin@15103
    71
end;
ballarin@15103
    72
wenzelm@32215
    73
functor Quasi_Tac(Less: LESS_ARITH): QUASI_TAC =
ballarin@15103
    74
struct
ballarin@15103
    75
ballarin@15103
    76
(* Internal datatype for the proof *)
ballarin@15103
    77
datatype proof
wenzelm@32215
    78
  = Asm of int
wenzelm@32215
    79
  | Thm of proof list * thm;
wenzelm@32215
    80
ballarin@15103
    81
exception Cannot;
ballarin@15103
    82
 (* Internal exception, raised if conclusion cannot be derived from
ballarin@15103
    83
     assumptions. *)
ballarin@15103
    84
exception Contr of proof;
ballarin@15103
    85
  (* Internal exception, raised if contradiction ( x < x ) was derived *)
ballarin@15103
    86
wenzelm@32215
    87
fun prove asms =
skalberg@15570
    88
  let fun pr (Asm i) = List.nth (asms, i)
ballarin@15103
    89
  |       pr (Thm (prfs, thm)) = (map pr prfs) MRS thm
ballarin@15103
    90
  in pr end;
ballarin@15103
    91
ballarin@15103
    92
(* Internal datatype for inequalities *)
wenzelm@32215
    93
datatype less
wenzelm@32215
    94
   = Less  of term * term * proof
ballarin@15103
    95
   | Le    of term * term * proof
wenzelm@32215
    96
   | NotEq of term * term * proof;
ballarin@15103
    97
ballarin@15103
    98
 (* Misc functions for datatype less *)
ballarin@15103
    99
fun lower (Less (x, _, _)) = x
ballarin@15103
   100
  | lower (Le (x, _, _)) = x
ballarin@15103
   101
  | lower (NotEq (x,_,_)) = x;
ballarin@15103
   102
ballarin@15103
   103
fun upper (Less (_, y, _)) = y
ballarin@15103
   104
  | upper (Le (_, y, _)) = y
ballarin@15103
   105
  | upper (NotEq (_,y,_)) = y;
ballarin@15103
   106
ballarin@15103
   107
fun getprf   (Less (_, _, p)) = p
ballarin@15103
   108
|   getprf   (Le   (_, _, p)) = p
ballarin@15103
   109
|   getprf   (NotEq (_,_, p)) = p;
ballarin@15103
   110
ballarin@15103
   111
(* ************************************************************************ *)
ballarin@15103
   112
(*                                                                          *)
wenzelm@19250
   113
(* mkasm_trans sign (t, n) :  theory -> (Term.term * int)  -> less          *)
ballarin@15103
   114
(*                                                                          *)
ballarin@15103
   115
(* Tuple (t, n) (t an assumption, n its index in the assumptions) is        *)
ballarin@15103
   116
(* translated to an element of type less.                                   *)
ballarin@15103
   117
(* Only assumptions of form x <= y are used, all others are ignored         *)
ballarin@15103
   118
(*                                                                          *)
ballarin@15103
   119
(* ************************************************************************ *)
ballarin@15103
   120
haftmann@33063
   121
fun mkasm_trans thy (t, n) =
haftmann@33063
   122
  case Less.decomp_trans thy t of
wenzelm@32215
   123
    SOME (x, rel, y) =>
ballarin@15103
   124
    (case rel of
ballarin@15103
   125
     "<="  =>  [Le (x, y, Asm n)]
ballarin@15103
   126
    | _     => error ("trans_tac: unknown relation symbol ``" ^ rel ^
ballarin@15103
   127
                 "''returned by decomp_trans."))
skalberg@15531
   128
  | NONE => [];
wenzelm@32215
   129
ballarin@15103
   130
(* ************************************************************************ *)
ballarin@15103
   131
(*                                                                          *)
wenzelm@19250
   132
(* mkasm_quasi sign (t, n) : theory -> (Term.term * int) -> less            *)
ballarin@15103
   133
(*                                                                          *)
ballarin@15103
   134
(* Tuple (t, n) (t an assumption, n its index in the assumptions) is        *)
ballarin@15103
   135
(* translated to an element of type less.                                   *)
ballarin@15103
   136
(* Quasi orders only.                                                       *)
ballarin@15103
   137
(*                                                                          *)
ballarin@15103
   138
(* ************************************************************************ *)
ballarin@15103
   139
haftmann@33063
   140
fun mkasm_quasi thy (t, n) =
haftmann@33063
   141
  case Less.decomp_quasi thy t of
skalberg@15531
   142
    SOME (x, rel, y) => (case rel of
wenzelm@32215
   143
      "<"   => if (x aconv y) then raise Contr (Thm ([Asm n], Less.less_reflE))
ballarin@15103
   144
               else [Less (x, y, Asm n)]
ballarin@15103
   145
    | "<="  => [Le (x, y, Asm n)]
ballarin@15103
   146
    | "="   => [Le (x, y, Thm ([Asm n], Less.eqD1)),
ballarin@15103
   147
                Le (y, x, Thm ([Asm n], Less.eqD2))]
wenzelm@32215
   148
    | "~="  => if (x aconv y) then
ballarin@15103
   149
                  raise Contr (Thm ([(Thm ([(Thm ([], Less.le_refl)) ,(Asm n)], Less.le_neq_trans))], Less.less_reflE))
ballarin@15103
   150
               else [ NotEq (x, y, Asm n),
wenzelm@32215
   151
                      NotEq (y, x,Thm ( [Asm n], thm "not_sym"))]
ballarin@15103
   152
    | _     => error ("quasi_tac: unknown relation symbol ``" ^ rel ^
ballarin@15103
   153
                 "''returned by decomp_quasi."))
skalberg@15531
   154
  | NONE => [];
ballarin@15103
   155
ballarin@15103
   156
ballarin@15103
   157
(* ************************************************************************ *)
ballarin@15103
   158
(*                                                                          *)
wenzelm@19250
   159
(* mkconcl_trans sign t : theory -> Term.term -> less                       *)
ballarin@15103
   160
(*                                                                          *)
ballarin@15103
   161
(* Translates conclusion t to an element of type less.                      *)
ballarin@15103
   162
(* Only for Conclusions of form x <= y or x < y.                            *)
ballarin@15103
   163
(*                                                                          *)
ballarin@15103
   164
(* ************************************************************************ *)
ballarin@15103
   165
wenzelm@32215
   166
haftmann@33063
   167
fun mkconcl_trans thy t =
haftmann@33063
   168
  case Less.decomp_trans thy t of
skalberg@15531
   169
    SOME (x, rel, y) => (case rel of
wenzelm@32215
   170
     "<="  => (Le (x, y, Asm ~1), Asm 0)
ballarin@15103
   171
    | _  => raise Cannot)
skalberg@15531
   172
  | NONE => raise Cannot;
wenzelm@32215
   173
wenzelm@32215
   174
ballarin@15103
   175
(* ************************************************************************ *)
ballarin@15103
   176
(*                                                                          *)
wenzelm@19250
   177
(* mkconcl_quasi sign t : theory -> Term.term -> less                       *)
ballarin@15103
   178
(*                                                                          *)
ballarin@15103
   179
(* Translates conclusion t to an element of type less.                      *)
ballarin@15103
   180
(* Quasi orders only.                                                       *)
ballarin@15103
   181
(*                                                                          *)
ballarin@15103
   182
(* ************************************************************************ *)
ballarin@15103
   183
haftmann@33063
   184
fun mkconcl_quasi thy t =
haftmann@33063
   185
  case Less.decomp_quasi thy t of
skalberg@15531
   186
    SOME (x, rel, y) => (case rel of
ballarin@15103
   187
      "<"   => ([Less (x, y, Asm ~1)], Asm 0)
ballarin@15103
   188
    | "<="  => ([Le (x, y, Asm ~1)], Asm 0)
ballarin@15103
   189
    | "~="  => ([NotEq (x,y, Asm ~1)], Asm 0)
ballarin@15103
   190
    | _  => raise Cannot)
skalberg@15531
   191
| NONE => raise Cannot;
wenzelm@32215
   192
wenzelm@32215
   193
ballarin@15103
   194
(* ******************************************************************* *)
ballarin@15103
   195
(*                                                                     *)
ballarin@15103
   196
(* mergeLess (less1,less2):  less * less -> less                       *)
ballarin@15103
   197
(*                                                                     *)
ballarin@15103
   198
(* Merge to elements of type less according to the following rules     *)
ballarin@15103
   199
(*                                                                     *)
ballarin@15103
   200
(* x <= y && y <= z ==> x <= z                                         *)
ballarin@15103
   201
(* x <= y && x ~= y ==> x < y                                          *)
ballarin@15103
   202
(* x ~= y && x <= y ==> x < y                                          *)
ballarin@15103
   203
(*                                                                     *)
ballarin@15103
   204
(* ******************************************************************* *)
ballarin@15103
   205
ballarin@15103
   206
fun mergeLess (Le (x, _, p) , Le (_ , z, q)) =
ballarin@15103
   207
      Le (x, z, Thm ([p,q] , Less.le_trans))
ballarin@15103
   208
|   mergeLess (Le (x, z, p) , NotEq (x', z', q)) =
wenzelm@32215
   209
      if (x aconv x' andalso z aconv z' )
ballarin@15103
   210
       then Less (x, z, Thm ([p,q] , Less.le_neq_trans))
ballarin@15103
   211
        else error "quasi_tac: internal error le_neq_trans"
ballarin@15103
   212
|   mergeLess (NotEq (x, z, p) , Le (x' , z', q)) =
wenzelm@32215
   213
      if (x aconv x' andalso z aconv z')
ballarin@15103
   214
      then Less (x, z, Thm ([p,q] , Less.neq_le_trans))
ballarin@15103
   215
      else error "quasi_tac: internal error neq_le_trans"
ballarin@15103
   216
|   mergeLess (_, _) =
ballarin@15103
   217
      error "quasi_tac: internal error: undefined case";
ballarin@15103
   218
ballarin@15103
   219
ballarin@15103
   220
(* ******************************************************************** *)
ballarin@15103
   221
(* tr checks for valid transitivity step                                *)
ballarin@15103
   222
(* ******************************************************************** *)
ballarin@15103
   223
ballarin@15103
   224
infix tr;
ballarin@15103
   225
fun (Le (_, y, _))   tr (Le (x', _, _))   = ( y aconv x' )
ballarin@15103
   226
  | _ tr _ = false;
wenzelm@32215
   227
ballarin@15103
   228
(* ******************************************************************* *)
ballarin@15103
   229
(*                                                                     *)
ballarin@15103
   230
(* transPath (Lesslist, Less): (less list * less) -> less              *)
ballarin@15103
   231
(*                                                                     *)
ballarin@15103
   232
(* If a path represented by a list of elements of type less is found,  *)
ballarin@15103
   233
(* this needs to be contracted to a single element of type less.       *)
ballarin@15103
   234
(* Prior to each transitivity step it is checked whether the step is   *)
ballarin@15103
   235
(* valid.                                                              *)
ballarin@15103
   236
(*                                                                     *)
ballarin@15103
   237
(* ******************************************************************* *)
ballarin@15103
   238
ballarin@15103
   239
fun transPath ([],lesss) = lesss
ballarin@15103
   240
|   transPath (x::xs,lesss) =
ballarin@15103
   241
      if lesss tr x then transPath (xs, mergeLess(lesss,x))
ballarin@15103
   242
      else error "trans/quasi_tac: internal error transpath";
wenzelm@32215
   243
ballarin@15103
   244
(* ******************************************************************* *)
ballarin@15103
   245
(*                                                                     *)
ballarin@15103
   246
(* less1 subsumes less2 : less -> less -> bool                         *)
ballarin@15103
   247
(*                                                                     *)
ballarin@15103
   248
(* subsumes checks whether less1 implies less2                         *)
ballarin@15103
   249
(*                                                                     *)
ballarin@15103
   250
(* ******************************************************************* *)
wenzelm@32215
   251
ballarin@15103
   252
infix subsumes;
ballarin@15103
   253
fun (Le (x, y, _)) subsumes (Le (x', y', _)) =
ballarin@15103
   254
      (x aconv x' andalso y aconv y')
ballarin@15103
   255
  | (Le _) subsumes (Less _) =
ballarin@15103
   256
      error "trans/quasi_tac: internal error: Le cannot subsume Less"
ballarin@15103
   257
  | (NotEq(x,y,_)) subsumes (NotEq(x',y',_)) = x aconv x' andalso y aconv y' orelse x aconv y' andalso y aconv x'
ballarin@15103
   258
  | _ subsumes _ = false;
ballarin@15103
   259
ballarin@15103
   260
(* ******************************************************************* *)
ballarin@15103
   261
(*                                                                     *)
skalberg@15531
   262
(* triv_solv less1 : less ->  proof option                     *)
ballarin@15103
   263
(*                                                                     *)
ballarin@15103
   264
(* Solves trivial goal x <= x.                                         *)
ballarin@15103
   265
(*                                                                     *)
ballarin@15103
   266
(* ******************************************************************* *)
ballarin@15103
   267
ballarin@15103
   268
fun triv_solv (Le (x, x', _)) =
wenzelm@32215
   269
    if x aconv x' then  SOME (Thm ([], Less.le_refl))
skalberg@15531
   270
    else NONE
skalberg@15531
   271
|   triv_solv _ = NONE;
ballarin@15103
   272
ballarin@15103
   273
(* ********************************************************************* *)
ballarin@15103
   274
(* Graph functions                                                       *)
ballarin@15103
   275
(* ********************************************************************* *)
ballarin@15103
   276
ballarin@15103
   277
(* *********************************************************** *)
ballarin@15103
   278
(* Functions for constructing graphs                           *)
ballarin@15103
   279
(* *********************************************************** *)
ballarin@15103
   280
ballarin@15103
   281
fun addEdge (v,d,[]) = [(v,d)]
ballarin@15103
   282
|   addEdge (v,d,((u,dl)::el)) = if v aconv u then ((v,d@dl)::el)
ballarin@15103
   283
    else (u,dl):: (addEdge(v,d,el));
wenzelm@32215
   284
ballarin@15103
   285
(* ********************************************************************** *)
ballarin@15103
   286
(*                                                                        *)
wenzelm@32215
   287
(* mkQuasiGraph constructs from a list of objects of type less a graph g, *)
ballarin@15103
   288
(* by taking all edges that are candidate for a <=, and a list neqE, by   *)
ballarin@15103
   289
(* taking all edges that are candiate for a ~=                            *)
ballarin@15103
   290
(*                                                                        *)
ballarin@15103
   291
(* ********************************************************************** *)
ballarin@15103
   292
ballarin@15103
   293
fun mkQuasiGraph [] = ([],[])
wenzelm@32215
   294
|   mkQuasiGraph lessList =
ballarin@15103
   295
 let
ballarin@15103
   296
 fun buildGraphs ([],leG, neqE) = (leG,  neqE)
wenzelm@32215
   297
  |   buildGraphs (l::ls, leG,  neqE) = case l of
ballarin@15103
   298
       (Less (x,y,p)) =>
wenzelm@32215
   299
         let
wenzelm@32215
   300
          val leEdge  = Le (x,y, Thm ([p], Less.less_imp_le))
wenzelm@32215
   301
          val neqEdges = [ NotEq (x,y, Thm ([p], Less.less_imp_neq)),
wenzelm@32215
   302
                           NotEq (y,x, Thm ( [Thm ([p], Less.less_imp_neq)], thm "not_sym"))]
wenzelm@32215
   303
         in
wenzelm@32215
   304
           buildGraphs (ls, addEdge(y,[],(addEdge (x,[(y,leEdge)],leG))), neqEdges@neqE)
wenzelm@32215
   305
         end
wenzelm@32215
   306
     |  (Le (x,y,p))   => buildGraphs (ls, addEdge(y,[],(addEdge (x,[(y,l)],leG))), neqE)
ballarin@15103
   307
     | _ =>  buildGraphs (ls, leG,  l::neqE) ;
ballarin@15103
   308
ballarin@15103
   309
in buildGraphs (lessList, [],  []) end;
wenzelm@32215
   310
ballarin@15103
   311
(* ********************************************************************** *)
ballarin@15103
   312
(*                                                                        *)
ballarin@15103
   313
(* mkGraph constructs from a list of objects of type less a graph g       *)
ballarin@15103
   314
(* Used for plain transitivity chain reasoning.                           *)
ballarin@15103
   315
(*                                                                        *)
ballarin@15103
   316
(* ********************************************************************** *)
ballarin@15103
   317
ballarin@15103
   318
fun mkGraph [] = []
wenzelm@32215
   319
|   mkGraph lessList =
ballarin@15103
   320
 let
ballarin@15103
   321
  fun buildGraph ([],g) = g
wenzelm@32215
   322
  |   buildGraph (l::ls, g) =  buildGraph (ls, (addEdge ((lower l),[((upper l),l)],g)))
wenzelm@32215
   323
ballarin@15103
   324
in buildGraph (lessList, []) end;
ballarin@15103
   325
ballarin@15103
   326
(* *********************************************************************** *)
ballarin@15103
   327
(*                                                                         *)
ballarin@15103
   328
(* adjacent g u : (''a * 'b list ) list -> ''a -> 'b list                  *)
ballarin@15103
   329
(*                                                                         *)
ballarin@15103
   330
(* List of successors of u in graph g                                      *)
ballarin@15103
   331
(*                                                                         *)
ballarin@15103
   332
(* *********************************************************************** *)
wenzelm@32215
   333
wenzelm@32215
   334
fun adjacent eq_comp ((v,adj)::el) u =
ballarin@15103
   335
    if eq_comp (u, v) then adj else adjacent eq_comp el u
wenzelm@32215
   336
|   adjacent _  []  _ = []
ballarin@15103
   337
ballarin@15103
   338
(* *********************************************************************** *)
ballarin@15103
   339
(*                                                                         *)
ballarin@15103
   340
(* dfs eq_comp g u v:                                                      *)
ballarin@15103
   341
(* ('a * 'a -> bool) -> ('a  *( 'a * less) list) list ->                   *)
wenzelm@32215
   342
(* 'a -> 'a -> (bool * ('a * less) list)                                   *)
ballarin@15103
   343
(*                                                                         *)
ballarin@15103
   344
(* Depth first search of v from u.                                         *)
ballarin@15103
   345
(* Returns (true, path(u, v)) if successful, otherwise (false, []).        *)
ballarin@15103
   346
(*                                                                         *)
ballarin@15103
   347
(* *********************************************************************** *)
ballarin@15103
   348
wenzelm@32215
   349
fun dfs eq_comp g u v =
wenzelm@32215
   350
 let
wenzelm@32740
   351
    val pred = Unsynchronized.ref [];
wenzelm@32740
   352
    val visited = Unsynchronized.ref [];
wenzelm@32215
   353
ballarin@15103
   354
    fun been_visited v = exists (fn w => eq_comp (w, v)) (!visited)
wenzelm@32215
   355
wenzelm@32215
   356
    fun dfs_visit u' =
ballarin@15103
   357
    let val _ = visited := u' :: (!visited)
wenzelm@32215
   358
ballarin@15103
   359
    fun update (x,l) = let val _ = pred := (x,l) ::(!pred) in () end;
wenzelm@32215
   360
wenzelm@32215
   361
    in if been_visited v then ()
ballarin@15103
   362
    else (app (fn (v',l) => if been_visited v' then () else (
wenzelm@32215
   363
       update (v',l);
ballarin@15103
   364
       dfs_visit v'; ()) )) (adjacent eq_comp g u')
ballarin@15103
   365
     end
wenzelm@32215
   366
  in
wenzelm@32215
   367
    dfs_visit u;
wenzelm@32215
   368
    if (been_visited v) then (true, (!pred)) else (false , [])
ballarin@15103
   369
  end;
ballarin@15103
   370
ballarin@15103
   371
(* ************************************************************************ *)
ballarin@15103
   372
(*                                                                          *)
ballarin@15103
   373
(* Begin: Quasi Order relevant functions                                    *)
ballarin@15103
   374
(*                                                                          *)
ballarin@15103
   375
(*                                                                          *)
ballarin@15103
   376
(* ************************************************************************ *)
ballarin@15103
   377
ballarin@15103
   378
(* ************************************************************************ *)
ballarin@15103
   379
(*                                                                          *)
ballarin@15103
   380
(* findPath x y g: Term.term -> Term.term ->                                *)
ballarin@15103
   381
(*                  (Term.term * (Term.term * less list) list) ->           *)
ballarin@15103
   382
(*                  (bool, less list)                                       *)
ballarin@15103
   383
(*                                                                          *)
ballarin@15103
   384
(*  Searches a path from vertex x to vertex y in Graph g, returns true and  *)
ballarin@15103
   385
(*  the list of edges forming the path, if a path is found, otherwise false *)
ballarin@15103
   386
(*  and nil.                                                                *)
ballarin@15103
   387
(*                                                                          *)
ballarin@15103
   388
(* ************************************************************************ *)
ballarin@15103
   389
ballarin@15103
   390
wenzelm@32215
   391
 fun findPath x y g =
wenzelm@32215
   392
  let
ballarin@15103
   393
    val (found, tmp) =  dfs (op aconv) g x y ;
ballarin@15103
   394
    val pred = map snd tmp;
ballarin@15103
   395
ballarin@15103
   396
    fun path x y  =
ballarin@15103
   397
      let
ballarin@15103
   398
       (* find predecessor u of node v and the edge u -> v *)
ballarin@15103
   399
       fun lookup v [] = raise Cannot
ballarin@15103
   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
ballarin@15103
   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
ballarin@15103
   409
      in rev_path y end;
wenzelm@32215
   410
wenzelm@32215
   411
  in
ballarin@15103
   412
   if found then (
ballarin@15103
   413
    if x aconv y then (found,[(Le (x, y, (Thm ([], Less.le_refl))))])
wenzelm@32215
   414
    else (found, (path x y) ))
ballarin@15103
   415
   else (found,[])
wenzelm@32215
   416
  end;
wenzelm@32215
   417
wenzelm@32215
   418
wenzelm@32215
   419
(* ************************************************************************ *)
ballarin@15103
   420
(*                                                                          *)
ballarin@15103
   421
(* findQuasiProof (leqG, neqE) subgoal:                                     *)
ballarin@15103
   422
(* (Term.term * (Term.term * less list) list) * less list  -> less -> proof *)
ballarin@15103
   423
(*                                                                          *)
ballarin@15103
   424
(* Constructs a proof for subgoal by searching a special path in leqG and   *)
wenzelm@32215
   425
(* neqE. Raises Cannot if construction of the proof fails.                  *)
ballarin@15103
   426
(*                                                                          *)
wenzelm@32215
   427
(* ************************************************************************ *)
ballarin@15103
   428
ballarin@15103
   429
ballarin@15103
   430
(* As the conlusion can be either of form x <= y, x < y or x ~= y we have        *)
ballarin@15103
   431
(* three cases to deal with. Finding a transitivity path from x to y with label  *)
wenzelm@32215
   432
(* 1. <=                                                                         *)
ballarin@15103
   433
(*    This is simply done by searching any path from x to y in the graph leG.    *)
ballarin@15103
   434
(*    The graph leG contains only edges with label <=.                           *)
ballarin@15103
   435
(*                                                                               *)
ballarin@15103
   436
(* 2. <                                                                          *)
ballarin@15103
   437
(*    A path from x to y with label < can be found by searching a path with      *)
ballarin@15103
   438
(*    label <= from x to y in the graph leG and merging the path x <= y with     *)
ballarin@15103
   439
(*    a parallel edge x ~= y resp. y ~= x to x < y.                              *)
ballarin@15103
   440
(*                                                                               *)
ballarin@15103
   441
(* 3. ~=                                                                         *)
ballarin@15103
   442
(*   If the conclusion is of form x ~= y, we can find a proof either directly,   *)
ballarin@15103
   443
(*   if x ~= y or y ~= x are among the assumptions, or by constructing x ~= y if *)
ballarin@15103
   444
(*   x < y or y < x follows from the assumptions.                                *)
ballarin@15103
   445
ballarin@15103
   446
fun findQuasiProof (leG, neqE) subgoal =
ballarin@15103
   447
  case subgoal of (Le (x,y, _)) => (
wenzelm@32215
   448
   let
wenzelm@32215
   449
    val (xyLefound,xyLePath) = findPath x y leG
ballarin@15103
   450
   in
ballarin@15103
   451
    if xyLefound then (
wenzelm@32215
   452
     let
ballarin@15103
   453
      val Le_x_y = (transPath (tl xyLePath, hd xyLePath))
ballarin@15103
   454
     in getprf Le_x_y end )
ballarin@15103
   455
    else raise Cannot
ballarin@15103
   456
   end )
ballarin@15103
   457
  | (Less (x,y,_))  => (
wenzelm@32215
   458
   let
skalberg@15531
   459
    fun findParallelNeq []  = NONE
ballarin@15103
   460
    |   findParallelNeq (e::es)  =
skalberg@15531
   461
     if      (x aconv (lower e) andalso y aconv (upper e)) then SOME e
skalberg@15531
   462
     else if (y aconv (lower e) andalso x aconv (upper e)) then SOME (NotEq (x,y, (Thm ([getprf e], thm "not_sym"))))
wenzelm@32215
   463
     else findParallelNeq es ;
ballarin@15103
   464
   in
ballarin@15103
   465
   (* test if there is a edge x ~= y respectivly  y ~= x and     *)
ballarin@15103
   466
   (* if it possible to find a path x <= y in leG, thus we can conclude x < y *)
wenzelm@32215
   467
    (case findParallelNeq neqE of (SOME e) =>
wenzelm@32215
   468
      let
wenzelm@32215
   469
       val (xyLeFound,xyLePath) = findPath x y leG
ballarin@15103
   470
      in
ballarin@15103
   471
       if xyLeFound then (
wenzelm@32215
   472
        let
ballarin@15103
   473
         val Le_x_y = (transPath (tl xyLePath, hd xyLePath))
ballarin@15103
   474
         val Less_x_y = mergeLess (e, Le_x_y)
ballarin@15103
   475
        in getprf Less_x_y end
ballarin@15103
   476
       ) else raise Cannot
wenzelm@32215
   477
      end
wenzelm@32215
   478
    | _ => raise Cannot)
ballarin@15103
   479
   end )
wenzelm@32215
   480
 | (NotEq (x,y,_)) =>
ballarin@15103
   481
  (* First check if a single premiss is sufficient *)
ballarin@15103
   482
  (case (Library.find_first (fn fact => fact subsumes subgoal) neqE, subgoal) of
skalberg@15531
   483
    (SOME (NotEq (x, y, p)), NotEq (x', y', _)) =>
wenzelm@32215
   484
      if  (x aconv x' andalso y aconv y') then p
ballarin@15103
   485
      else Thm ([p], thm "not_sym")
wenzelm@32215
   486
    | _  => raise Cannot
ballarin@15103
   487
  )
ballarin@15103
   488
wenzelm@32215
   489
wenzelm@32215
   490
(* ************************************************************************ *)
wenzelm@32215
   491
(*                                                                          *)
wenzelm@32215
   492
(* End: Quasi Order relevant functions                                      *)
wenzelm@32215
   493
(*                                                                          *)
wenzelm@32215
   494
(*                                                                          *)
wenzelm@32215
   495
(* ************************************************************************ *)
ballarin@15103
   496
ballarin@15103
   497
(* *********************************************************************** *)
ballarin@15103
   498
(*                                                                         *)
ballarin@15103
   499
(* solveLeTrans sign (asms,concl) :                                        *)
wenzelm@19250
   500
(* theory -> less list * Term.term -> proof list                           *)
ballarin@15103
   501
(*                                                                         *)
ballarin@15103
   502
(* Solves                                                                  *)
ballarin@15103
   503
(*                                                                         *)
ballarin@15103
   504
(* *********************************************************************** *)
ballarin@15103
   505
haftmann@33063
   506
fun solveLeTrans thy (asms, concl) =
wenzelm@32215
   507
 let
ballarin@15103
   508
  val g = mkGraph asms
ballarin@15103
   509
 in
wenzelm@32215
   510
   let
haftmann@33063
   511
    val (subgoal, prf) = mkconcl_trans thy concl
wenzelm@32215
   512
    val (found, path) = findPath (lower subgoal) (upper subgoal) g
ballarin@15103
   513
   in
wenzelm@32215
   514
    if found then [getprf (transPath (tl path, hd path))]
ballarin@15103
   515
    else raise Cannot
ballarin@15103
   516
  end
ballarin@15103
   517
 end;
ballarin@15103
   518
ballarin@15103
   519
ballarin@15103
   520
(* *********************************************************************** *)
ballarin@15103
   521
(*                                                                         *)
ballarin@15103
   522
(* solveQuasiOrder sign (asms,concl) :                                     *)
wenzelm@19250
   523
(* theory -> less list * Term.term -> proof list                           *)
ballarin@15103
   524
(*                                                                         *)
ballarin@15103
   525
(* Find proof if possible for quasi order.                                 *)
ballarin@15103
   526
(*                                                                         *)
ballarin@15103
   527
(* *********************************************************************** *)
ballarin@15103
   528
haftmann@33063
   529
fun solveQuasiOrder thy (asms, concl) =
wenzelm@32215
   530
 let
ballarin@15103
   531
  val (leG, neqE) = mkQuasiGraph asms
ballarin@15103
   532
 in
wenzelm@32215
   533
   let
haftmann@33063
   534
   val (subgoals, prf) = mkconcl_quasi thy concl
ballarin@15103
   535
   fun solve facts less =
skalberg@15531
   536
       (case triv_solv less of NONE => findQuasiProof (leG, neqE) less
skalberg@15531
   537
       | SOME prf => prf )
ballarin@15103
   538
  in   map (solve asms) subgoals end
ballarin@15103
   539
 end;
ballarin@15103
   540
wenzelm@32215
   541
(* ************************************************************************ *)
wenzelm@32215
   542
(*                                                                          *)
ballarin@15103
   543
(* Tactics                                                                  *)
ballarin@15103
   544
(*                                                                          *)
wenzelm@32215
   545
(*  - trans_tac                                                          *)
wenzelm@32215
   546
(*  - quasi_tac, solves quasi orders                                        *)
wenzelm@32215
   547
(* ************************************************************************ *)
ballarin@15103
   548
ballarin@15103
   549
ballarin@15103
   550
(* trans_tac - solves transitivity chains over <= *)
wenzelm@32215
   551
wenzelm@32277
   552
fun trans_tac ctxt = SUBGOAL (fn (A, n) => fn st =>
ballarin@15103
   553
 let
wenzelm@32285
   554
  val thy = ProofContext.theory_of ctxt;
wenzelm@32215
   555
  val rfrees = map Free (Term.rename_wrt_term A (Logic.strip_params A));
wenzelm@32215
   556
  val Hs = map (fn H => subst_bounds (rfrees, H)) (Logic.strip_assums_hyp A);
wenzelm@32215
   557
  val C = subst_bounds (rfrees, Logic.strip_assums_concl A);
haftmann@33063
   558
  val lesss = flat (map_index (mkasm_trans thy o swap) Hs);
wenzelm@32215
   559
  val prfs = solveLeTrans thy (lesss, C);
wenzelm@32215
   560
wenzelm@32215
   561
  val (subgoal, prf) = mkconcl_trans thy C;
ballarin@15103
   562
 in
wenzelm@32283
   563
  Subgoal.FOCUS (fn {prems, ...} =>
wenzelm@32215
   564
    let val thms = map (prove prems) prfs
wenzelm@32277
   565
    in rtac (prove thms prf) 1 end) ctxt n st
ballarin@15103
   566
 end
wenzelm@32283
   567
 handle Contr p => Subgoal.FOCUS (fn {prems, ...} => rtac (prove prems p) 1) ctxt n st
wenzelm@32277
   568
  | Cannot  => Seq.empty);
wenzelm@32215
   569
ballarin@15103
   570
ballarin@15103
   571
(* quasi_tac - solves quasi orders *)
wenzelm@32215
   572
wenzelm@32215
   573
fun quasi_tac ctxt = SUBGOAL (fn (A, n) => fn st =>
ballarin@15103
   574
 let
wenzelm@32285
   575
  val thy = ProofContext.theory_of ctxt
wenzelm@32215
   576
  val rfrees = map Free (Term.rename_wrt_term A (Logic.strip_params A));
wenzelm@32215
   577
  val Hs = map (fn H => subst_bounds (rfrees, H)) (Logic.strip_assums_hyp A);
wenzelm@32215
   578
  val C = subst_bounds (rfrees, Logic.strip_assums_concl A);
haftmann@33063
   579
  val lesss = flat (map_index (mkasm_quasi thy o swap) Hs);
wenzelm@32215
   580
  val prfs = solveQuasiOrder thy (lesss, C);
wenzelm@32215
   581
  val (subgoals, prf) = mkconcl_quasi thy C;
ballarin@15103
   582
 in
wenzelm@32283
   583
  Subgoal.FOCUS (fn {prems, ...} =>
wenzelm@32215
   584
    let val thms = map (prove prems) prfs
wenzelm@32215
   585
    in rtac (prove thms prf) 1 end) ctxt n st
ballarin@15103
   586
 end
wenzelm@32215
   587
 handle Contr p =>
wenzelm@32283
   588
    (Subgoal.FOCUS (fn {prems, ...} => rtac (prove prems p) 1) ctxt n st
wenzelm@32215
   589
      handle Subscript => Seq.empty)
wenzelm@32215
   590
  | Cannot => Seq.empty
wenzelm@32215
   591
  | Subscript => Seq.empty);
wenzelm@32215
   592
ballarin@15103
   593
end;