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