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