src/Pure/deriv.ML
author wenzelm
Mon, 09 Nov 1998 15:42:08 +0100
changeset 5840 e2d2b896c717
parent 2672 85d7e800d754
child 6085 3d8dcb09dbfb
permissions -rw-r--r--
Object logic specific operations.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     1
(*  Title:      Pure/deriv.ML
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     2
    ID:         $Id$
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     4
    Copyright   1996  University of Cambridge
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     5
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     6
Derivations (proof objects) and functions for examining them
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     7
*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     8
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
     9
signature DERIV = 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    10
  sig
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    11
  (*Object-level rules*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    12
  datatype orule = Subgoal of cterm
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    13
		 | Asm of int
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    14
		 | Res of deriv
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    15
		 | Equal of deriv
1601
0ef6ea27ab15 Changes required by removal of the theory argument of Theorem
paulson
parents: 1593
diff changeset
    16
		 | Thm   of string
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    17
		 | Other of deriv;
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    18
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    19
  val size : deriv -> int
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    20
  val drop : 'a mtree * int -> 'a mtree
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    21
  val linear : deriv -> deriv list
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    22
  val tree : deriv -> orule mtree
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    23
  end;  
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    24
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    25
structure Deriv : DERIV =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    26
struct
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    27
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    28
fun size (Join(Theorem _, _)) = 1
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    29
  | size (Join(_, ders)) = foldl op+ (1, map size ders);
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    30
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    31
(*Conversion to linear format.  Children of a node are the LIST of inferences
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    32
  justifying ONE of the premises*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    33
fun rev_deriv (Join (rl, [])) 	= [Join(rl,[])]
1601
0ef6ea27ab15 Changes required by removal of the theory argument of Theorem
paulson
parents: 1593
diff changeset
    34
  | rev_deriv (Join (Theorem name, _)) 	= [Join(Theorem name, [])]
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    35
  | rev_deriv (Join (Assumption arg, [der])) = 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    36
              Join(Assumption arg,[]) :: rev_deriv der
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    37
  | rev_deriv (Join (Bicompose arg, [rder, sder])) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    38
	Join (Bicompose arg, linear rder) :: rev_deriv sder
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    39
  | rev_deriv (Join (_, [der]))	= rev_deriv der
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    40
  | rev_deriv (Join (rl, der::ders)) =	(*catch-all case; doubtful?*)
2672
85d7e800d754 Replaced "flat" by the Basis Library function List.concat
paulson
parents: 2042
diff changeset
    41
        Join(rl, List.concat (map linear ders)) :: rev_deriv der
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    42
and linear der 	= rev (rev_deriv der);
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    43
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    44
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    45
(*** Conversion of object-level proof trees ***)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    46
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    47
(*Object-level rules*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    48
datatype orule = Subgoal of cterm
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    49
	       | Asm of int
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    50
               | Res of deriv
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    51
               | Equal of deriv
1601
0ef6ea27ab15 Changes required by removal of the theory argument of Theorem
paulson
parents: 1593
diff changeset
    52
               | Thm   of string
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    53
               | Other of deriv;
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    54
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    55
(*At position i, splice in value x, removing ngoal elements*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    56
fun splice (i,x,ngoal,prfs) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    57
    let val prfs0 = take(i-1,prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    58
        and prfs1 = drop(i-1,prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    59
        val prfs2 = Join (x, take(ngoal, prfs1)) :: drop(ngoal, prfs1)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    60
    in  prfs0 @ prfs2  end;
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    61
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    62
(*Deletes trivial uses of Equal_elim; hides derivations of Theorems*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    63
fun simp_deriv (Join (Equal_elim, [Join (Rewrite_cterm _, []), der])) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    64
      simp_deriv der
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    65
  | simp_deriv (Join (Equal_elim, [Join (Reflexive _, []), der])) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    66
      simp_deriv der
1601
0ef6ea27ab15 Changes required by removal of the theory argument of Theorem
paulson
parents: 1593
diff changeset
    67
  | simp_deriv (Join (rule as Theorem name, [_])) = Join (rule, [])
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    68
  | simp_deriv (Join (rule, ders)) = Join (rule, map simp_deriv ders);
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    69
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    70
(*Proof term is an equality: first premise of equal_elim.
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    71
  Attempt to decode proof terms made by Drule.goals_conv.
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    72
  Subgoal numbers are returned; they are wrong if original subgoal
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    73
	had flexflex pairs!
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    74
  NEGATIVE i means "could affect all subgoals starting from i"*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    75
fun scan_equals (i, Join (Combination, 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    76
			   [Join (Combination, [_, der1]), der2])) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    77
    (case der1 of	(*ignore trivial cases*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    78
         Join (Reflexive _, _)      => scan_equals (i+1, der2)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    79
       | Join (Rewrite_cterm _, []) => scan_equals (i+1, der2)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    80
       | Join (Rewrite_cterm _, _)  => (i,der1) :: scan_equals (i+1, der2)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    81
       | _ (*impossible in gconv*)  => [])
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    82
  | scan_equals (i, Join (Reflexive _, [])) = []
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    83
  | scan_equals (i, Join (Rewrite_cterm _, [])) = []
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    84
	(*Anything else could affect ALL following goals*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    85
  | scan_equals (i, der) = [(~i,der)];
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    86
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    87
(*Record uses of equality reasoning on 1 or more subgoals*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    88
fun update_equals ((i,der), prfs) = 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    89
      if i>0 then splice (i, Equal (simp_deriv der), 1, prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    90
      else take (~i-1, prfs) @
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    91
	   map (fn prf => Join (Equal (simp_deriv der), [prf])) 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    92
	       (drop (~i-1, prfs));
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    93
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    94
fun delift (Join (Lift_rule _, [der])) = der
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    95
  | delift der = der;
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    96
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    97
(*Conversion to an object-level proof tree.
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    98
  Uses embedded Lift_rules to "annotate" the proof tree with subgoals;
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
    99
    -- assumes that Lift_rule never occurs except with resolution
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   100
    -- may contain Vars that, in fact, are instantiated in that step*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   101
fun tree_aux (Join (Trivial ct, []), prfs) = Join(Subgoal ct, prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   102
  | tree_aux (Join (Assumption(i,_), [der]), prfs) = 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   103
      tree_aux (der, splice (i, Asm i, 0, prfs))
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   104
  | tree_aux (Join (Equal_elim, [der1,der2]), prfs) = 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   105
      tree_aux (der2, foldr update_equals (scan_equals (1, der1), prfs))
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   106
  | tree_aux (Join (Bicompose (match,true,i,ngoal,env), ders), prfs) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   107
		(*change eresolve_tac to proof by assumption*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   108
      tree_aux (Join (Assumption(i, Some env), 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   109
			 [Join (Bicompose (match,false,i,ngoal,env), ders)]),
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   110
		   prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   111
  | tree_aux (Join (Lift_rule (ct,i), [der]), prfs) = 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   112
      tree_aux (der, splice (i, Subgoal ct, 1, prfs))
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   113
  | tree_aux (Join (Bicompose arg, 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   114
		       [Join (Instantiate _, [rder]), sder]), prfs) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   115
		(*Ignore Instantiate*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   116
      tree_aux (Join (Bicompose arg, [rder, sder]), prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   117
  | tree_aux (Join (Bicompose arg, 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   118
		       [Join (Lift_rule larg, [rder]), sder]), prfs) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   119
		(*Move Lift_rule: to make a Subgoal on the result*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   120
      tree_aux (Join (Bicompose arg, [rder, 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   121
					 Join(Lift_rule larg, [sder])]), prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   122
  | tree_aux (Join (Bicompose (match,ef,i,ngoal,env), 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   123
		       [Join (Bicompose (match',ef',i',ngoal',env'),
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   124
			      [der1,der2]), 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   125
			der3]), prfs) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   126
		(*associate resolutions to the right*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   127
      tree_aux (Join (Bicompose (match', ef', i'+i-1, ngoal', env'), 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   128
			 [delift der1,	(*This Lift_rule would be wrong!*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   129
			  Join (Bicompose (match, ef, i, ngoal-ngoal'+1, env),
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   130
				[der2, der3])]), prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   131
  | tree_aux (Join (Bicompose (arg as (_,_,i,ngoal,_)), 
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   132
		       [rder, sder]), prfs) =
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   133
		(*resolution with basic rule/assumption -- we hope!*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   134
      tree_aux (sder, splice (i, Res (simp_deriv rder), ngoal, prfs))
1601
0ef6ea27ab15 Changes required by removal of the theory argument of Theorem
paulson
parents: 1593
diff changeset
   135
  | tree_aux (Join (Theorem name, _), prfs)	= Join(Thm name, prfs)
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   136
  | tree_aux (Join (_, [der]), prfs)	= tree_aux (der,prfs)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   137
  | tree_aux (der, prfs) = Join(Other (simp_deriv der), prfs);
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   138
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   139
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   140
fun tree der = tree_aux (der,[]);
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   141
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   142
(*Currently declared at end, to avoid conflicting with library's drop
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   143
  Can put it after "size" once we switch to List.drop*)
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   144
fun drop (der,0) = der
2042
33b4c1624e26 Added catch-all clause to drop, preventing exception Match
paulson
parents: 1601
diff changeset
   145
  | drop (Join (_, der::_), n) = drop (der, n-1)
33b4c1624e26 Added catch-all clause to drop, preventing exception Match
paulson
parents: 1601
diff changeset
   146
  | drop (der,_) = der;
1593
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   147
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   148
end;
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   149
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   150
69ed69a9c32a New module for proof objects (deriviations)
paulson
parents:
diff changeset
   151
(*We do NOT open this structure*)