src/Tools/IsaPlanner/zipper.ML
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (2 months ago)
changeset 69981 3dced198b9ec
parent 30161 c26e515f1c29
permissions -rw-r--r--
more strict AFP properties;
wenzelm@23175
     1
(*  Title:      Tools/IsaPlanner/zipper.ML
wenzelm@23171
     2
    Author:     Lucas Dixon, University of Edinburgh
wenzelm@23175
     3
wenzelm@23175
     4
A notion roughly based on Huet's Zippers for Isabelle terms.
wenzelm@23171
     5
*)   
wenzelm@23171
     6
wenzelm@23171
     7
(* abstract term for no more than pattern matching *)
wenzelm@23171
     8
signature ABSTRACT_TRM = 
wenzelm@23171
     9
sig
wenzelm@23171
    10
type typ   (* types *)
wenzelm@23171
    11
type aname (* abstraction names *)
wenzelm@23171
    12
type fname (* parameter/free variable names *)
wenzelm@23171
    13
type cname (* constant names *)
wenzelm@23171
    14
type vname (* meta variable names *)
wenzelm@23171
    15
type bname (* bound var name *)
wenzelm@23171
    16
datatype term = Const of cname * typ
wenzelm@23171
    17
           | Abs of aname * typ * term
wenzelm@23171
    18
           | Free of fname * typ
wenzelm@23171
    19
           | Var of vname * typ
wenzelm@23171
    20
           | Bound of bname
wenzelm@23171
    21
           | $ of term * term;
wenzelm@23171
    22
type T = term;
wenzelm@23171
    23
end;
wenzelm@23171
    24
wenzelm@23171
    25
structure IsabelleTrmWrap : ABSTRACT_TRM= 
wenzelm@23171
    26
struct 
wenzelm@23171
    27
open Term;
wenzelm@23171
    28
type typ   = Term.typ; (* types *)
wenzelm@23171
    29
type aname = string; (* abstraction names *)
wenzelm@23171
    30
type fname = string; (* parameter/free variable names *)
wenzelm@23171
    31
type cname = string; (* constant names *)
wenzelm@23171
    32
type vname = string * int; (* meta variable names *)
wenzelm@23171
    33
type bname = int; (* bound var name *)
wenzelm@23171
    34
type T = term;
wenzelm@23171
    35
end;
wenzelm@23171
    36
wenzelm@23171
    37
(* Concrete version for the Trm structure *)
wenzelm@23171
    38
signature TRM_CTXT_DATA = 
wenzelm@23171
    39
sig
wenzelm@23171
    40
wenzelm@23171
    41
  structure Trm : ABSTRACT_TRM
wenzelm@23171
    42
  datatype dtrm = Abs of Trm.aname * Trm.typ
wenzelm@23171
    43
                | AppL of Trm.T
wenzelm@23171
    44
                | AppR of Trm.T;
wenzelm@23171
    45
  val apply : dtrm -> Trm.T -> Trm.T
wenzelm@23171
    46
  val eq_pos : dtrm * dtrm -> bool
wenzelm@23171
    47
end;
wenzelm@23171
    48
wenzelm@23171
    49
(* A trm context = list of derivatives *)
wenzelm@23171
    50
signature TRM_CTXT =
wenzelm@23171
    51
sig
wenzelm@23171
    52
  structure D : TRM_CTXT_DATA
wenzelm@23171
    53
  type T = D.dtrm list;
wenzelm@23171
    54
wenzelm@23171
    55
  val empty : T;
wenzelm@23171
    56
  val is_empty : T -> bool;
wenzelm@23171
    57
wenzelm@23171
    58
  val add_abs : D.Trm.aname * D.Trm.typ -> T -> T;
wenzelm@23171
    59
  val add_appl : D.Trm.T -> T -> T;
wenzelm@23171
    60
  val add_appr : D.Trm.T -> T -> T;
wenzelm@23171
    61
wenzelm@23171
    62
  val add_dtrm : D.dtrm -> T -> T;
wenzelm@23171
    63
wenzelm@23171
    64
  val eq_path : T * T -> bool
wenzelm@23171
    65
wenzelm@23171
    66
  val add_outerctxt : T -> T -> T
wenzelm@23171
    67
wenzelm@23171
    68
  val apply : T -> D.Trm.T -> D.Trm.T
wenzelm@23171
    69
wenzelm@23171
    70
  val nty_ctxt : T -> (D.Trm.aname * D.Trm.typ) list;
wenzelm@23171
    71
  val ty_ctxt : T -> D.Trm.typ list;
wenzelm@23171
    72
wenzelm@23171
    73
  val depth : T -> int;
wenzelm@23171
    74
  val map : (D.dtrm -> D.dtrm) -> T -> T
wenzelm@23171
    75
  val fold_up : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
wenzelm@23171
    76
  val fold_down : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
wenzelm@23171
    77
wenzelm@23171
    78
end;
wenzelm@23171
    79
wenzelm@23171
    80
(* A zipper = a term looked at, at a particular point in the term *)
wenzelm@23171
    81
signature ZIPPER =
wenzelm@23171
    82
sig
wenzelm@23171
    83
  structure C : TRM_CTXT
wenzelm@23171
    84
  type T
wenzelm@23171
    85
wenzelm@23171
    86
  val mktop : C.D.Trm.T -> T
wenzelm@23171
    87
  val mk : (C.D.Trm.T * C.T) -> T
wenzelm@23171
    88
wenzelm@23171
    89
  val goto_top : T -> T 
wenzelm@23171
    90
  val at_top : T -> bool
wenzelm@23171
    91
wenzelm@23171
    92
  val split : T -> T * C.T
wenzelm@23171
    93
  val add_outerctxt : C.T -> T -> T
wenzelm@23171
    94
wenzelm@23171
    95
  val set_trm : C.D.Trm.T -> T -> T
wenzelm@23171
    96
  val set_ctxt : C.T -> T -> T
wenzelm@23171
    97
wenzelm@23171
    98
  val ctxt : T -> C.T
wenzelm@23171
    99
  val trm : T -> C.D.Trm.T
wenzelm@23171
   100
  val top_trm : T -> C.D.Trm.T
wenzelm@23171
   101
wenzelm@23171
   102
  val zipto : C.T -> T -> T (* follow context down *)
wenzelm@23171
   103
wenzelm@23171
   104
  val nty_ctxt : T -> (C.D.Trm.aname * C.D.Trm.typ) list;
wenzelm@23171
   105
  val ty_ctxt : T -> C.D.Trm.typ list;
wenzelm@23171
   106
wenzelm@23171
   107
  val depth_of_ctxt : T -> int;
wenzelm@23171
   108
  val map_on_ctxt : (C.D.dtrm -> C.D.dtrm) -> T -> T
wenzelm@23171
   109
  val fold_up_ctxt : (C.D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
wenzelm@23171
   110
  val fold_down_ctxt : (C.D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
wenzelm@23171
   111
wenzelm@23171
   112
  (* searching through a zipper *)
wenzelm@23171
   113
  datatype zsearch = Here of T | LookIn of T;
wenzelm@23171
   114
  (* lazily search through the zipper *)
wenzelm@23171
   115
  val lzy_search : (T -> zsearch list) -> T -> T Seq.seq
wenzelm@23171
   116
  (* lazy search with folded data *)
wenzelm@23171
   117
  val pf_lzy_search : ('a -> T -> ('a * zsearch list)) 
wenzelm@23171
   118
                      -> 'a -> T -> T Seq.seq
wenzelm@23171
   119
  (* zsearch list is or-choices *)
wenzelm@23171
   120
  val searchfold : ('a -> T -> (('a * zsearch) list)) 
wenzelm@23171
   121
                      -> 'a -> T -> ('a * T) Seq.seq
wenzelm@23171
   122
  (* limit function to the current focus of the zipper, 
wenzelm@23171
   123
     but give function the zipper's context *)
wenzelm@23171
   124
  val limit_pcapply : (C.T -> T -> ('a * T) Seq.seq) 
wenzelm@23171
   125
                      -> T -> ('a * T) Seq.seq
wenzelm@23171
   126
  val limit_apply : (T -> T Seq.seq) -> T -> T Seq.seq
wenzelm@23171
   127
  val limit_capply : (C.T -> T -> T Seq.seq) -> T -> T Seq.seq
wenzelm@23171
   128
wenzelm@23171
   129
  (* moving around zippers with option types *)
wenzelm@23171
   130
  val omove_up : T -> T option
wenzelm@23171
   131
  val omove_up_abs : T -> T option
wenzelm@23171
   132
  val omove_up_app : T -> T option
wenzelm@23171
   133
  val omove_up_left : T -> T option
wenzelm@23171
   134
  val omove_up_right : T -> T option
wenzelm@23171
   135
  val omove_up_left_or_abs : T -> T option
wenzelm@23171
   136
  val omove_up_right_or_abs : T -> T option
wenzelm@23171
   137
  val omove_down_abs : T -> T option
wenzelm@23171
   138
  val omove_down_left : T -> T option
wenzelm@23171
   139
  val omove_down_right : T -> T option
wenzelm@23171
   140
  val omove_down_app : T -> (T * T) option
wenzelm@23171
   141
wenzelm@23171
   142
  (* moving around zippers, raising exceptions *)
wenzelm@23171
   143
  exception move of string * T
wenzelm@23171
   144
  val move_up : T -> T
wenzelm@23171
   145
  val move_up_abs : T -> T
wenzelm@23171
   146
  val move_up_app : T -> T
wenzelm@23171
   147
  val move_up_left : T -> T
wenzelm@23171
   148
  val move_up_right : T -> T
wenzelm@23171
   149
  val move_up_left_or_abs : T -> T
wenzelm@23171
   150
  val move_up_right_or_abs : T -> T
wenzelm@23171
   151
  val move_down_abs : T -> T
wenzelm@23171
   152
  val move_down_left : T -> T
wenzelm@23171
   153
  val move_down_right : T -> T
wenzelm@23171
   154
  val move_down_app : T -> T * T
wenzelm@23171
   155
wenzelm@23171
   156
end;
wenzelm@23171
   157
wenzelm@23171
   158
wenzelm@23171
   159
(* Zipper data for an generic trm *)
wenzelm@23171
   160
functor TrmCtxtDataFUN(Trm : ABSTRACT_TRM) 
wenzelm@23171
   161
: TRM_CTXT_DATA 
wenzelm@23171
   162
= struct
wenzelm@23171
   163
  
wenzelm@23171
   164
  structure Trm = Trm;
wenzelm@23171
   165
wenzelm@23171
   166
  (* a dtrm is, in McBridge-speak, a differentiated term. It represents
wenzelm@23171
   167
  the different ways a term can occur within its datatype constructors *)
wenzelm@23171
   168
  datatype dtrm = Abs of Trm.aname * Trm.typ
wenzelm@23171
   169
                | AppL of Trm.T
wenzelm@23171
   170
                | AppR of Trm.T;
wenzelm@23171
   171
wenzelm@23171
   172
  (* apply a dtrm to a term, ie put the dtrm above it, building context *)
wenzelm@23171
   173
  fun apply (Abs (s,ty)) t = Trm.Abs (s,ty,t)
wenzelm@23171
   174
    | apply (AppL tl) tr = Trm.$ (tl, tr)
wenzelm@23171
   175
    | apply (AppR tr) tl = Trm.$ (tl, tr);
wenzelm@23171
   176
wenzelm@23171
   177
  fun eq_pos (Abs _, Abs _) = true
wenzelm@23171
   178
    | eq_pos (AppL _, AppL _) = true
wenzelm@23171
   179
    | eq_pos (AppR _, AppR _) = true
wenzelm@23171
   180
    | eq_pos _ = false;
wenzelm@23171
   181
wenzelm@23171
   182
end;
wenzelm@23171
   183
wenzelm@23171
   184
wenzelm@23171
   185
(* functor for making term contexts given term data *)
wenzelm@23171
   186
functor TrmCtxtFUN(D : TRM_CTXT_DATA) 
wenzelm@23171
   187
 : TRM_CTXT =
wenzelm@23171
   188
struct 
wenzelm@23171
   189
  structure D = D;
wenzelm@23171
   190
wenzelm@23171
   191
  type T = D.dtrm list;
wenzelm@23171
   192
wenzelm@23171
   193
  val empty = [];
wenzelm@23171
   194
  val is_empty = List.null;
wenzelm@23171
   195
wenzelm@23171
   196
  fun add_abs d l = (D.Abs d) :: l;
wenzelm@23171
   197
  fun add_appl d l = (D.AppL d) :: l;
wenzelm@23171
   198
  fun add_appr d l = (D.AppR d) :: l;
wenzelm@23171
   199
wenzelm@23171
   200
  fun add_dtrm d l = d::l;
wenzelm@23171
   201
wenzelm@23171
   202
  fun eq_path ([], []) = true
wenzelm@23171
   203
    | eq_path ([], _::_) = false
wenzelm@23171
   204
    | eq_path ( _::_, []) = false
wenzelm@23171
   205
    | eq_path (h::t, h2::t2) = 
wenzelm@23171
   206
      D.eq_pos(h,h2) andalso eq_path (t, t2);
wenzelm@23171
   207
wenzelm@23171
   208
  (* add context to outside of existing context *) 
wenzelm@23171
   209
  fun add_outerctxt ctop cbottom = cbottom @ ctop; 
wenzelm@23171
   210
wenzelm@23171
   211
  (* mkterm : zipper -> trm -> trm *)
wenzelm@23171
   212
  val apply = Basics.fold D.apply;
wenzelm@23171
   213
  
wenzelm@23171
   214
  (* named type context *)
wenzelm@23171
   215
  val nty_ctxt = List.foldr (fn (D.Abs nty,ntys) => nty::ntys
wenzelm@23171
   216
                             | (_,ntys) => ntys) [];
wenzelm@23171
   217
  (* type context *)
wenzelm@23171
   218
  val ty_ctxt = List.foldr (fn (D.Abs (_,ty),tys) => ty::tys
wenzelm@23171
   219
                           | (_,tys) => tys) [];
wenzelm@23171
   220
wenzelm@23171
   221
  val depth = length : T -> int;
wenzelm@23171
   222
wenzelm@23171
   223
  val map = List.map : (D.dtrm -> D.dtrm) -> T -> T
wenzelm@23171
   224
wenzelm@23171
   225
  val fold_up = Basics.fold : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a;
wenzelm@23171
   226
  val fold_down = Basics.fold_rev : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a;
wenzelm@23171
   227
wenzelm@23171
   228
end;
wenzelm@23171
   229
wenzelm@23171
   230
(* zippers in terms of term contexts *)
wenzelm@23171
   231
functor ZipperFUN(C : TRM_CTXT) 
wenzelm@23171
   232
 : ZIPPER
wenzelm@23171
   233
= struct 
wenzelm@23171
   234
wenzelm@23171
   235
  structure C = C;
wenzelm@23171
   236
  structure D = C.D;
wenzelm@23171
   237
  structure Trm = D.Trm;
wenzelm@23171
   238
wenzelm@23171
   239
  type T = C.D.Trm.T * C.T;
wenzelm@23171
   240
wenzelm@23171
   241
  fun mktop t = (t, C.empty) : T
wenzelm@23171
   242
wenzelm@23171
   243
  val mk = I;
wenzelm@23171
   244
  fun set_trm x = apfst (K x);
wenzelm@23171
   245
  fun set_ctxt x = apsnd (K x);
wenzelm@23171
   246
wenzelm@23171
   247
  fun goto_top (z as (t,c)) = 
wenzelm@23171
   248
      if C.is_empty c then z else (C.apply c t, C.empty);
wenzelm@23171
   249
wenzelm@23171
   250
  fun at_top (_,c) = C.is_empty c;
wenzelm@23171
   251
wenzelm@23171
   252
  fun split (t,c) = ((t,C.empty) : T, c : C.T) 
wenzelm@23171
   253
  fun add_outerctxt c (t,c2) = (t, C.add_outerctxt c c2) : T
wenzelm@23171
   254
wenzelm@23171
   255
  val ctxt = snd;
wenzelm@23171
   256
  val trm = fst;
wenzelm@23171
   257
  val top_trm = trm o goto_top;
wenzelm@23171
   258
wenzelm@23171
   259
  fun nty_ctxt x = C.nty_ctxt (ctxt x);
wenzelm@23171
   260
  fun ty_ctxt x = C.ty_ctxt (ctxt x);
wenzelm@23171
   261
wenzelm@23171
   262
  fun depth_of_ctxt x = C.depth (ctxt x);
wenzelm@23171
   263
  fun map_on_ctxt x = apsnd (C.map x);
wenzelm@23171
   264
  fun fold_up_ctxt f = C.fold_up f o ctxt;
wenzelm@23171
   265
  fun fold_down_ctxt f = C.fold_down f o ctxt;
wenzelm@23171
   266
wenzelm@23171
   267
  fun omove_up (t,(d::c)) = SOME (D.apply d t, c)
wenzelm@23171
   268
    | omove_up (z as (_,[])) = NONE;
wenzelm@23171
   269
  fun omove_up_abs (t,((D.Abs(n,ty))::c)) = SOME (Trm.Abs(n,ty,t), c)
wenzelm@23171
   270
    | omove_up_abs z = NONE;
wenzelm@23171
   271
  fun omove_up_app (t,(D.AppL tl)::c) = SOME(Trm.$(tl,t), c)
wenzelm@23171
   272
    | omove_up_app (t,(D.AppR tr)::c) = SOME(Trm.$(t,tr), c)
wenzelm@23171
   273
    | omove_up_app z = NONE;
wenzelm@23171
   274
  fun omove_up_left (t,(D.AppL tl)::c) = SOME(Trm.$(tl,t), c)
wenzelm@23171
   275
    | omove_up_left z = NONE;
wenzelm@23171
   276
  fun omove_up_right (t,(D.AppR tr)::c) = SOME(Trm.$(t,tr), c)
wenzelm@23171
   277
    | omove_up_right _ = NONE;
wenzelm@23171
   278
  fun omove_up_left_or_abs (t,(D.AppL tl)::c) = 
wenzelm@23171
   279
      SOME (Trm.$(tl,t), c)
wenzelm@23171
   280
    | omove_up_left_or_abs (t,(D.Abs (n,ty))::c) = 
wenzelm@23171
   281
      SOME (Trm.Abs(n,ty,t), c)
wenzelm@23171
   282
    | omove_up_left_or_abs z = NONE;
wenzelm@23171
   283
  fun omove_up_right_or_abs (t,(D.Abs (n,ty))::c) = 
wenzelm@23171
   284
      SOME (Trm.Abs(n,ty,t), c) 
wenzelm@23171
   285
    | omove_up_right_or_abs (t,(D.AppR tr)::c) = 
wenzelm@23171
   286
      SOME (Trm.$(t,tr), c)
wenzelm@23171
   287
    | omove_up_right_or_abs _ = NONE;
wenzelm@23171
   288
  fun omove_down_abs (Trm.Abs(s,ty,t),c) = SOME (t,(D.Abs(s,ty))::c)
wenzelm@23171
   289
    | omove_down_abs _ = NONE;
wenzelm@23171
   290
  fun omove_down_left (Trm.$(l,r),c) = SOME (l,(D.AppR r)::c)
wenzelm@23171
   291
    | omove_down_left _ = NONE;
wenzelm@23171
   292
  fun omove_down_right (Trm.$(l,r),c) = SOME (r,(D.AppL l)::c)
wenzelm@23171
   293
    | omove_down_right _ = NONE;
wenzelm@23171
   294
  fun omove_down_app (Trm.$(l,r),c) = 
wenzelm@23171
   295
      SOME ((l,(D.AppR r)::c),(r,(D.AppL l)::c))
wenzelm@23171
   296
    | omove_down_app _ = NONE;
wenzelm@23171
   297
wenzelm@23171
   298
  exception move of string * T
wenzelm@23171
   299
  fun move_up (t,(d::c)) = (D.apply d t, c)
wenzelm@23171
   300
    | move_up (z as (_,[])) = raise move ("move_up",z);
wenzelm@23171
   301
  fun move_up_abs (t,((D.Abs(n,ty))::c)) = (Trm.Abs(n,ty,t), c)
wenzelm@23171
   302
    | move_up_abs z = raise move ("move_up_abs",z);
wenzelm@23171
   303
  fun move_up_app (t,(D.AppL tl)::c) = (Trm.$(tl,t), c)
wenzelm@23171
   304
    | move_up_app (t,(D.AppR tr)::c) = (Trm.$(t,tr), c)
wenzelm@23171
   305
    | move_up_app z = raise move ("move_up_app",z);
wenzelm@23171
   306
  fun move_up_left (t,((D.AppL tl)::c)) = (Trm.$(tl,t), c)
wenzelm@23171
   307
    | move_up_left z = raise move ("move_up_left",z);
wenzelm@23171
   308
  fun move_up_right (t,(D.AppR tr)::c) = (Trm.$(t,tr), c)
wenzelm@23171
   309
    | move_up_right z = raise move ("move_up_right",z);
wenzelm@23171
   310
  fun move_up_left_or_abs (t,(D.AppL tl)::c) = (Trm.$(tl,t), c)
wenzelm@23171
   311
    | move_up_left_or_abs (t,(D.Abs (n,ty))::c) = (Trm.Abs(n,ty,t), c)
wenzelm@23171
   312
    | move_up_left_or_abs z = raise move ("move_up_left_or_abs",z);
wenzelm@23171
   313
  fun move_up_right_or_abs (t,(D.Abs (n,ty))::c) = (Trm.Abs(n,ty,t), c) 
wenzelm@23171
   314
    | move_up_right_or_abs (t,(D.AppR tr)::c) = (Trm.$(t,tr), c)
wenzelm@23171
   315
    | move_up_right_or_abs z = raise move ("move_up_right_or_abs",z);
wenzelm@23171
   316
  fun move_down_abs (Trm.Abs(s,ty,t),c) = (t,(D.Abs(s,ty))::c)
wenzelm@23171
   317
    | move_down_abs z = raise move ("move_down_abs",z);
wenzelm@23171
   318
  fun move_down_left (Trm.$(l,r),c) = (l,(D.AppR r)::c)
wenzelm@23171
   319
    | move_down_left z = raise move ("move_down_left",z);
wenzelm@23171
   320
  fun move_down_right (Trm.$(l,r),c) = (r,(D.AppL l)::c)
wenzelm@23171
   321
    | move_down_right z = raise move ("move_down_right",z);
wenzelm@23171
   322
  fun move_down_app (Trm.$(l,r),c) = 
wenzelm@23171
   323
      ((l,(D.AppR r)::c),(r,(D.AppL l)::c))
wenzelm@23171
   324
    | move_down_app z = raise move ("move_down_app",z);
wenzelm@23171
   325
wenzelm@23171
   326
  (* follow the given path down the given zipper *)
wenzelm@23171
   327
  (* implicit arguments: C.D.dtrm list, then T *)
wenzelm@23171
   328
  val zipto = C.fold_down 
wenzelm@23171
   329
                (fn C.D.Abs _ => move_down_abs 
wenzelm@23171
   330
                  | C.D.AppL _ => move_down_right
wenzelm@23171
   331
                  | C.D.AppR _ => move_down_left); 
wenzelm@23171
   332
wenzelm@23171
   333
  (* Note: interpretted as being examined depth first *)
wenzelm@23171
   334
  datatype zsearch = Here of T | LookIn of T;
wenzelm@23171
   335
wenzelm@23171
   336
  (* lazy search *)
wenzelm@23171
   337
  fun lzy_search fsearch = 
wenzelm@23171
   338
      let 
wenzelm@23171
   339
        fun lzyl [] () = NONE
wenzelm@23171
   340
          | lzyl ((Here z) :: more) () = SOME(z, Seq.make (lzyl more))
wenzelm@23171
   341
          | lzyl ((LookIn z) :: more) () =
wenzelm@23171
   342
            (case lzy z
wenzelm@23171
   343
              of NONE => NONE
wenzelm@23171
   344
               | SOME (hz,mz) => 
wenzelm@23171
   345
                 SOME (hz, Seq.append mz (Seq.make (lzyl more))))
wenzelm@23171
   346
        and lzy z = lzyl (fsearch z) ()
wenzelm@23171
   347
      in Seq.make o lzyl o fsearch end;
wenzelm@23171
   348
wenzelm@23171
   349
  (* path folded lazy search - the search list is defined in terms of
wenzelm@23171
   350
  the path passed through: the data a is updated with every zipper
wenzelm@23171
   351
  considered *)
wenzelm@23171
   352
  fun pf_lzy_search fsearch a0 z = 
wenzelm@23171
   353
      let 
wenzelm@23171
   354
        fun lzyl a [] () = NONE
wenzelm@23171
   355
          | lzyl a ((Here z) :: more) () = SOME(z, Seq.make (lzyl a more))
wenzelm@23171
   356
          | lzyl a ((LookIn z) :: more) () =
wenzelm@23171
   357
            (case lzy a z
wenzelm@23171
   358
              of NONE => lzyl a more ()
wenzelm@23171
   359
               | SOME(hz,mz) => SOME(hz,Seq.append mz (Seq.make(lzyl a more))))
wenzelm@23171
   360
        and lzy a z = 
wenzelm@23171
   361
            let val (a2, slist) = (fsearch a z) in lzyl a2 slist () end
wenzelm@23171
   362
wenzelm@23171
   363
        val (a,slist) = fsearch a0 z
wenzelm@23171
   364
      in Seq.make (lzyl a slist) end;
wenzelm@23171
   365
wenzelm@23171
   366
  (* Note: depth first over zsearch results *)
wenzelm@23171
   367
  fun searchfold fsearch a0 z = 
wenzelm@23171
   368
      let 
wenzelm@23171
   369
        fun lzyl [] () = NONE
wenzelm@23171
   370
          | lzyl ((a, Here z) :: more) () = 
wenzelm@23171
   371
            SOME((a,z), Seq.make (lzyl more))
wenzelm@23171
   372
          | lzyl ((a, LookIn z) :: more) () =
wenzelm@23171
   373
            (case lzyl (fsearch a z) () of 
wenzelm@23171
   374
               NONE => lzyl more ()
wenzelm@23171
   375
             | SOME (z,mz) => SOME (z,Seq.append mz (Seq.make (lzyl more))))
wenzelm@23171
   376
      in Seq.make (lzyl (fsearch a0 z)) end;
wenzelm@23171
   377
wenzelm@23171
   378
wenzelm@23171
   379
  fun limit_pcapply f z = 
wenzelm@23171
   380
      let val (z2,c) = split z
wenzelm@23171
   381
      in Seq.map (apsnd (add_outerctxt c)) (f c z2) end;
wenzelm@23171
   382
  fun limit_capply (f : C.T -> T -> T Seq.seq) (z : T) = 
wenzelm@23171
   383
      let val ((z2 : T),(c : C.T)) = split z
wenzelm@23171
   384
      in Seq.map (add_outerctxt c) (f c z2) end
wenzelm@23171
   385
wenzelm@23171
   386
  val limit_apply = limit_capply o K;
wenzelm@23171
   387
wenzelm@23171
   388
end;
wenzelm@23171
   389
wenzelm@23171
   390
(* now build these for Isabelle terms *)
wenzelm@23171
   391
structure TrmCtxtData = TrmCtxtDataFUN(IsabelleTrmWrap);
wenzelm@23171
   392
structure TrmCtxt = TrmCtxtFUN(TrmCtxtData);
wenzelm@23171
   393
structure Zipper = ZipperFUN(TrmCtxt);
wenzelm@23171
   394
wenzelm@23171
   395
wenzelm@23171
   396
wenzelm@23171
   397
(* For searching through Zippers below the current focus...
wenzelm@23171
   398
   KEY for naming scheme:    
wenzelm@23171
   399
wenzelm@23171
   400
   td = starting at the top down
wenzelm@23171
   401
   lr = going from left to right
wenzelm@23171
   402
   rl = going from right to left
wenzelm@23171
   403
wenzelm@23171
   404
   bl = starting at the bottom left
wenzelm@23171
   405
   br = starting at the bottom right
wenzelm@23171
   406
   ul = going up then left
wenzelm@23171
   407
   ur = going up then right
wenzelm@23171
   408
   ru = going right then up
wenzelm@23171
   409
   lu = going left then up
wenzelm@23171
   410
*)
wenzelm@23171
   411
signature ZIPPER_SEARCH =
wenzelm@23171
   412
sig
wenzelm@23171
   413
  structure Z : ZIPPER;
wenzelm@23171
   414
  
wenzelm@23171
   415
  val leaves_lr : Z.T -> Z.T Seq.seq
wenzelm@23171
   416
  val leaves_rl : Z.T -> Z.T Seq.seq
wenzelm@23171
   417
wenzelm@23171
   418
  val all_bl_ru : Z.T -> Z.T Seq.seq
wenzelm@23171
   419
  val all_bl_ur : Z.T -> Z.T Seq.seq
wenzelm@23171
   420
  val all_td_lr : Z.T -> Z.T Seq.seq
wenzelm@23171
   421
  val all_td_rl : Z.T -> Z.T Seq.seq
wenzelm@23171
   422
  
wenzelm@23171
   423
end;
wenzelm@23171
   424
wenzelm@23171
   425
functor ZipperSearchFUN(Zipper : ZIPPER) : ZIPPER_SEARCH
wenzelm@23171
   426
= struct
wenzelm@23171
   427
wenzelm@23171
   428
structure Z = Zipper;
wenzelm@23171
   429
structure C = Z.C;
wenzelm@23171
   430
structure D = C.D; 
wenzelm@23171
   431
structure Trm = D.Trm; 
wenzelm@23171
   432
wenzelm@23171
   433
fun sf_leaves_lr z = 
wenzelm@23171
   434
    case Z.trm z 
wenzelm@23171
   435
     of Trm.$ _ => [Z.LookIn (Z.move_down_left z),
wenzelm@23171
   436
                    Z.LookIn (Z.move_down_right z)]
wenzelm@23171
   437
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z)]
wenzelm@23171
   438
      | _ => [Z.Here z];
wenzelm@23171
   439
fun sf_leaves_rl z = 
wenzelm@23171
   440
    case Z.trm z 
wenzelm@23171
   441
     of Trm.$ _ => [Z.LookIn (Z.move_down_right z),
wenzelm@23171
   442
                    Z.LookIn (Z.move_down_left z)]
wenzelm@23171
   443
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z)]
wenzelm@23171
   444
      | _ => [Z.Here z];
wenzelm@23171
   445
val leaves_lr = Z.lzy_search sf_leaves_lr;
wenzelm@23171
   446
val leaves_rl = Z.lzy_search sf_leaves_rl;
wenzelm@23171
   447
wenzelm@23171
   448
wenzelm@23171
   449
fun sf_all_td_lr z = 
wenzelm@23171
   450
    case Z.trm z 
wenzelm@23171
   451
     of Trm.$ _ => [Z.Here z, Z.LookIn (Z.move_down_left z),
wenzelm@23171
   452
                    Z.LookIn (Z.move_down_right z)]
wenzelm@23171
   453
      | Trm.Abs _ => [Z.Here z, Z.LookIn (Z.move_down_abs z)]
wenzelm@23171
   454
      | _ => [Z.Here z];
wenzelm@23171
   455
fun sf_all_td_rl z = 
wenzelm@23171
   456
    case Z.trm z 
wenzelm@23171
   457
     of Trm.$ _ => [Z.Here z, Z.LookIn (Z.move_down_right z),
wenzelm@23171
   458
                    Z.LookIn (Z.move_down_left z)]
wenzelm@23171
   459
      | Trm.Abs _ => [Z.Here z, Z.LookIn (Z.move_down_abs z)]
wenzelm@23171
   460
      | _ => [Z.Here z];
wenzelm@23171
   461
fun sf_all_bl_ur z = 
wenzelm@23171
   462
    case Z.trm z 
wenzelm@23171
   463
     of Trm.$ _ => [Z.LookIn (Z.move_down_left z), Z.Here z,
wenzelm@23171
   464
                    Z.LookIn (Z.move_down_right z)]
wenzelm@23171
   465
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z),
wenzelm@23171
   466
                      Z.Here z]
wenzelm@23171
   467
      | _ => [Z.Here z];
wenzelm@23171
   468
fun sf_all_bl_ru z = 
wenzelm@23171
   469
    case Z.trm z 
wenzelm@23171
   470
     of Trm.$ _ => [Z.LookIn (Z.move_down_left z),
wenzelm@23171
   471
                    Z.LookIn (Z.move_down_right z), Z.Here z]
wenzelm@23171
   472
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z), Z.Here z]
wenzelm@23171
   473
      | _ => [Z.Here z];
wenzelm@23171
   474
wenzelm@23171
   475
val all_td_lr = Z.lzy_search sf_all_td_lr;
wenzelm@23171
   476
val all_td_rl = Z.lzy_search sf_all_td_rl;
wenzelm@23171
   477
val all_bl_ur = Z.lzy_search sf_all_bl_ru;
wenzelm@23171
   478
val all_bl_ru = Z.lzy_search sf_all_bl_ur;
wenzelm@23171
   479
wenzelm@23171
   480
end;
wenzelm@23171
   481
wenzelm@23171
   482
wenzelm@23171
   483
structure ZipperSearch = ZipperSearchFUN(Zipper);