src/Pure/IsaPlanner/isaplib.ML
author wenzelm
Fri, 12 May 2006 01:01:08 +0200
changeset 19620 ccd6de95f4a6
parent 19507 b386fcdc9945
permissions -rw-r--r--
improved propagate_deps; removed structs_less, which is actually unsound in conjunction with interdependent overloaded consts;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     1
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
16179
fa7e70be26b0 header;
wenzelm
parents: 16004
diff changeset
     2
(*  Title:      Pure/IsaPlanner/isaplib.ML
fa7e70be26b0 header;
wenzelm
parents: 16004
diff changeset
     3
    ID:		$Id$
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     4
    Author:     Lucas Dixon, University of Edinburgh
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     5
                lucasd@dai.ed.ac.uk
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     6
*)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     7
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     8
(*  DESCRIPTION:
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     9
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    10
    A few useful system-independent utilities.
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    11
*)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    12
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    13
signature ISAP_LIB =
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    14
sig
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    15
  (* seq operations *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    16
  val ALL_BUT_LAST : ('a -> 'b Seq.seq) -> 'a -> 'b Seq.seq
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    17
  val FST : ('a -> 'b Seq.seq) -> 'a -> 'b Seq.seq
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    18
  val NTH : int -> ('a -> 'b Seq.seq) -> 'a -> 'b Seq.seq
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    19
  val all_but_last_of_seq : 'a Seq.seq -> 'a Seq.seq
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    20
  val nat_seq : int Seq.seq
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
    21
  val nth_of_seq : int -> 'a Seq.seq -> 'a option
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    22
  val pair_seq : 'a Seq.seq -> 'b Seq.seq -> ('a * 'b) Seq.seq
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    23
  val number_seq : 'a Seq.seq -> (int * 'a) Seq.seq
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    24
16004
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
    25
  datatype 'a skipseqT = 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
    26
           skipmore of int
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
    27
         | skipseq of 'a Seq.seq Seq.seq;
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
    28
  val skipto_seqseq : int -> 'a Seq.seq Seq.seq -> 'a skipseqT
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
    29
  val seqflat_seq : 'a Seq.seq Seq.seq -> 'a Seq.seq 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
    30
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    31
  (* lists *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    32
  val mk_num_list : int -> int list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    33
  val number_list : int -> 'a list -> (int * 'a) list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    34
  val repeated_list : int -> 'a -> 'a list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    35
  val all_pairs : 'a list -> 'b list -> ('a * 'b) list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    36
	val get_ends_of : ('a * 'a -> bool) ->
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    37
										('a * 'a) -> 'a list -> ('a * 'a)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    38
  val flatmap : ('a -> 'b list) -> 'a list -> 'b list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    39
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    40
	val lrem : ('a * 'b -> bool) -> 'a list -> 'b list -> 'b list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    41
  val forall_list : ('a -> bool) -> 'a list -> bool
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    42
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    43
  (* the list of lists with one of each of the original sublists *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    44
  val one_of_each : 'a list list -> 'a list list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    45
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    46
  (* type changing *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    47
  exception NOT_A_DIGIT of string
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    48
  val int_of_string : string -> int
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    49
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    50
  (* string manipulations *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    51
  val remove_end_spaces : string -> string
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    52
  val str_indent : string -> string
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    53
  val string_of_intlist : int list -> string
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    54
  val string_of_list : ('a -> string) -> 'a list -> string
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    55
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    56
  (* options *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
    57
  val aptosome : 'a option -> ('a -> 'b) -> 'b option
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
    58
  val seq_mapfilter : ('a -> 'b option) -> 'a Seq.seq -> 'b Seq.seq
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
    59
  val seq_map_to_some_filter : ('a -> 'b) -> 'a option Seq.seq 
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    60
                               -> 'b Seq.seq
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    61
end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    62
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    63
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    64
17796
86daafee72d6 minor tweaks for Poplog/PML;
wenzelm
parents: 16179
diff changeset
    65
structure IsaPLib : ISAP_LIB = 
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    66
struct
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    67
 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    68
(* Seq *)
19507
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
    69
fun seq_map_to_some_filter f = Seq.map_filter (Option.map f);
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
    70
fun seq_mapfilter f = Seq.map_filter f;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    71
19507
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
    72
(* the sequence of natural numbers *)
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
    73
val nat_seq = 
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
    74
    let fun nseq i () = SOME (i, Seq.make (nseq (i+1)))
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
    75
    in Seq.make (nseq 1)
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
    76
    end;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    77
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    78
(* a simple function to pair with each element of a list, a number *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    79
fun number_list i [] = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    80
	| number_list i (h::t) = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    81
		(i,h)::(number_list (i+1) t)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    82
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    83
(* create a sequence of pairs of the elements of the two sequences
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    84
   If one sequence becomes empty, then so does the pairing of them. 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    85
   
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    86
   This is particularly useful if you wish to perform counting or
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    87
   other repeated operations on a sequence and you want to combvine
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    88
   this infinite sequence with a possibly finite one.
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    89
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    90
   behaviour: 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    91
   s1: a1, a2, ... an
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    92
   s2: b1, b2, ... bn ...
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    93
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    94
   pair_seq s1 s2: (a1,b1), (a2,b2), ... (an,bn)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    95
*)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    96
fun pair_seq seq1 seq2 = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    97
    let
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    98
      fun pseq s1 s2 () = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    99
	        case Seq.pull s1 of 
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   100
	          NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   101
	        | SOME (s1h, s1t) => 
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   102
	          case Seq.pull s2 of 
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   103
		          NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   104
	          | SOME (s2h, s2t) =>
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   105
		          SOME ((s1h, s2h), Seq.make (pseq s1t s2t))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   106
    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   107
      Seq.make (pseq seq1 seq2)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   108
    end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   109
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   110
(* number a sequence *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   111
fun number_seq s = pair_seq nat_seq s;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   112
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   113
(* cuts off the last element of a sequence *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   114
fun all_but_last_of_seq s = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   115
    let 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   116
      fun f () = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   117
	  case Seq.pull s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   118
	    NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   119
	  | SOME (a, s2) => 
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   120
	    (case Seq.pull s2 of 
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   121
	       NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   122
	     | SOME (a2,s3) => 
19475
8aa2b380614a curried Seq.cons;
wenzelm
parents: 17796
diff changeset
   123
	       SOME (a, all_but_last_of_seq (Seq.cons a2 s3)))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   124
    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   125
      Seq.make f
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   126
    end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   127
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   128
 fun ALL_BUT_LAST r st = all_but_last_of_seq (r st);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   129
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   130
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   131
  (* nth elem for sequenes, return none if out of bounds *)
19507
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   132
  fun nth_of_seq i l =
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   133
    (case Seq.pull l of
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   134
      NONE => NONE
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   135
    | SOME (x, xq) =>
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   136
        if i <= 1 then SOME x
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   137
        else nth_of_seq (i - 1) xq);
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   138
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   139
  (* for use with tactics... gives no_tac if element isn't there *)
19507
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   140
  fun NTH n f st =
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   141
    (case nth_of_seq n (f st) of
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   142
      NONE => Seq.empty
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   143
    | SOME r => Seq.single r);
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   144
 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   145
  (* First result of a tactic... uses NTH, so if there is no elements,
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   146
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   147
     then no_tac is returned. *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   148
  fun FST f = NTH 1 f;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   149
16004
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   150
datatype 'a skipseqT = skipmore of int
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   151
  | skipseq of 'a Seq.seq Seq.seq;
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   152
(* given a seqseq, skip the first m non-empty seq's *)
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   153
fun skipto_seqseq m s = 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   154
    let 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   155
      fun skip_occs n sq = 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   156
          case Seq.pull sq of 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   157
            NONE => skipmore n
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   158
          | SOME (h,t) => 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   159
            (case Seq.pull h of NONE => skip_occs n t
19475
8aa2b380614a curried Seq.cons;
wenzelm
parents: 17796
diff changeset
   160
             | SOME _ => if n <= 1 then skipseq (Seq.cons h t)
16004
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   161
                         else skip_occs (n - 1) t)
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   162
    in (skip_occs m s) end;
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   163
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   164
(* handy function for re-arranging Sequence operations *)
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   165
(* [[a,b,c],[d,e],...] => flatten [[a,d,...], [b,e,...], [c, ...]] *)
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   166
fun seqflat_seq ss = 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   167
    let 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   168
      fun pulltl LL () = 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   169
          (case Seq.pull LL of 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   170
             NONE => NONE
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   171
           | SOME (hL,tLL) => 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   172
             (case Seq.pull hL of 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   173
                NONE => pulltl tLL ()
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   174
              | SOME (h,tL) => 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   175
                SOME (h, Seq.make (recf (tLL, (Seq.single tL))))))
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   176
      and recf (fstLL,sndLL) () = 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   177
          (case Seq.pull fstLL of 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   178
             NONE => pulltl sndLL ()
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   179
           | SOME (hL, tLL) => 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   180
             (case Seq.pull hL of 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   181
                NONE => recf (tLL, sndLL) ()
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   182
              | SOME (h,tL) => 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   183
                SOME (h, Seq.make (recf (tLL, Seq.append (sndLL, Seq.single tL))))))
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   184
    in Seq.make (pulltl ss) end;
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   185
(* tested with: 
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   186
val ss = Seq.of_list [Seq.of_list [1,2,3], Seq.of_list [4,5], Seq.of_list [7,8,9,10]];
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   187
Seq.list_of (seqflat_seq ss);
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   188
val it = [1, 4, 7, 2, 5, 8, 3, 9, 10] : int list
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   189
*)
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   190
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   191
031f56012483 lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents: 15570
diff changeset
   192
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   193
  (* create a list opf the form (n, n-1, n-2, ... ) *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   194
  fun mk_num_list n =  
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   195
      if n < 1 then [] else (n :: (mk_num_list (n-1))); 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   196
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   197
  fun repeated_list n a =  
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   198
      if n < 1 then [] else (a :: (repeated_list (n-1) a)); 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   199
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   200
  (* create all possible pairs with fst element from the first list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   201
     and snd element from teh second list *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   202
  fun all_pairs xs ys = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   203
      let 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   204
        fun all_pairs_aux yss [] _ acc = acc
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   205
          | all_pairs_aux yss (x::xs) [] acc = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   206
            all_pairs_aux yss xs yss acc
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   207
          | all_pairs_aux yss (xs as (x1::x1s)) (y::ys) acc = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   208
                               all_pairs_aux yss xs ys ((x1,y)::acc)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   209
      in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   210
        all_pairs_aux ys xs ys []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   211
      end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   212
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   213
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   214
  (* create all possible pairs with fst element from the first list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   215
     and snd element from teh second list *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   216
  (* FIXME: make tail recursive and quick *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   217
  fun one_of_each [] = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   218
    | one_of_each [[]] = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   219
    | one_of_each [(h::t)] = [h] :: (one_of_each [t])
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   220
    | one_of_each ([] :: xs) = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   221
    | one_of_each ((h :: t) :: xs) = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   222
      (map (fn z => h :: z) (one_of_each xs)) 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   223
      @ (one_of_each (t :: xs));
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   224
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   225
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   226
(* function to get the upper/lower bounds of a list 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   227
given: 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   228
gr : 'a * 'a -> bool  = greater than check
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   229
e as (u,l) : ('a * 'a) = upper and lower bits
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   230
l : 'a list = the list to get the upper and lower bounds of
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   231
returns a pair ('a * 'a) of the biggest and lowest value w.r.t "gr"
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   232
*)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   233
fun get_ends_of gr (e as (u,l)) [] = e
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   234
  | get_ends_of gr (e as (u,l)) (h :: t) = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   235
    if gr(h,u) then get_ends_of gr (h,l) t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   236
    else if gr(l,h) then get_ends_of gr (u,h) t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   237
    else get_ends_of gr (u,l) t;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   238
19507
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   239
fun flatmap f = maps f;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   240
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   241
  (* quick removal of "rs" elements from "ls" when (eqf (r,l)) is true 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   242
		 Ignores ordering. *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   243
  fun lrem eqf rs ls = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   244
			let fun list_remove rs ([],[]) = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   245
						| list_remove [] (xs,_) = xs
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   246
						| list_remove (r::rs) ([],leftovers) = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   247
							list_remove rs (leftovers,[])
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   248
						| list_remove (r1::rs) ((x::xs),leftovers) = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   249
							if eqf (r1, x) then list_remove (r1::rs) (xs,leftovers)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   250
							else list_remove (r1::rs) (xs, x::leftovers)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   251
			in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   252
				list_remove rs (ls,[])
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   253
			end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   254
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   255
fun forall_list f [] = true
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   256
  | forall_list f (a::t) = f a orelse forall_list f t;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   257
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   258
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   259
  (* crappy string indeter *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   260
  fun str_indent s = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   261
    implode (map (fn s => if s = "\n" then "\n  " else s) (explode s));
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   262
      
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   263
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   264
  fun remove_end_spaces s = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   265
      let 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   266
				fun rem_starts [] = [] 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   267
					| rem_starts (" " :: t) = rem_starts t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   268
					| rem_starts ("\t" :: t) = rem_starts t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   269
					| rem_starts ("\n" :: t) = rem_starts t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   270
					| rem_starts l = l
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   271
				fun rem_ends l = rev (rem_starts (rev l))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   272
      in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   273
				implode (rem_ends (rem_starts (explode s)))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   274
      end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   275
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   276
(* convert a string to an integer *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   277
  exception NOT_A_DIGIT of string;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   278
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   279
  fun int_of_string s = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   280
      let 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   281
				fun digits_to_int [] x = x
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   282
					| digits_to_int (h :: t) x = digits_to_int t (x * 10 + h);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   283
	
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   284
				fun char_to_digit c = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   285
						case c of 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   286
							"0" => 0
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   287
						| "1" => 1
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   288
						| "2" => 2
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   289
						| "3" => 3
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   290
						| "4" => 4
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   291
						| "5" => 5
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   292
						| "6" => 6
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   293
						| "7" => 7
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   294
						| "8" => 8
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   295
						| "9" => 9
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   296
						| _ => raise NOT_A_DIGIT c
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   297
      in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   298
				digits_to_int (map char_to_digit (explode (remove_end_spaces s))) 0
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   299
      end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   300
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   301
  (* Debugging/printing code for this datatype *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   302
  fun string_of_list f l = 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   303
      let 
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   304
				fun auxf [] = ""
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   305
					| auxf [a] = (f a)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   306
					| auxf (h :: (t as (h2 :: t2))) = (f h) ^ ", " ^ (auxf t)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   307
      in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   308
				"[" ^ (auxf l) ^ "]"
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   309
      end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   310
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   311
   val string_of_intlist = string_of_list string_of_int;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   312
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   313
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   314
  (* options *)
19507
b386fcdc9945 reduced code duplication;
wenzelm
parents: 19475
diff changeset
   315
  fun aptosome opt f = Option.map f opt;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   316
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   317
end;