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