src/Provers/IsaPlanner/zipper.ML
author haftmann
Wed, 30 May 2007 21:09:16 +0200
changeset 23134 6cd88d27f600
parent 22169 74b61ce6b54d
permissions -rw-r--r--
more example
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 *)
21296
3c245a8a5001 avoid strange typing problem in MosML;
wenzelm
parents: 21145
diff changeset
    24
datatype term = Const of cname * typ
3c245a8a5001 avoid strange typing problem in MosML;
wenzelm
parents: 21145
diff changeset
    25
           | Abs of aname * typ * term
3c245a8a5001 avoid strange typing problem in MosML;
wenzelm
parents: 21145
diff changeset
    26
           | Free of fname * typ
3c245a8a5001 avoid strange typing problem in MosML;
wenzelm
parents: 21145
diff changeset
    27
           | Var of vname * typ
19835
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
22169
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
    83
  val fold_up : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
    84
  val fold_down : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    85
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    86
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    87
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    88
(* 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
    89
signature ZIPPER =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    90
sig
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    91
  structure C : TRM_CTXT
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    92
  type T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    93
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    94
  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
    95
  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
    96
ecd684d62808 fix to subst in order to allow subst when head of a term is a bound variable.
dixon
parents: 19902
diff changeset
    97
  val goto_top : T -> T 
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    98
  val at_top : T -> bool
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    99
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   100
  val split : T -> T * C.T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   101
  val add_outerctxt : C.T -> T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   102
19975
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_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
   104
  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
   105
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   106
  val ctxt : T -> C.T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   107
  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
   108
  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
   109
ecd684d62808 fix to subst in order to allow subst when head of a term is a bound variable.
dixon
parents: 19902
diff changeset
   110
  val zipto : C.T -> T -> T (* follow context down *)
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   111
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   112
  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
   113
  val ty_ctxt : T -> C.D.Trm.typ list;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   114
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   115
  val depth_of_ctxt : T -> int;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   116
  val map_on_ctxt : (C.D.dtrm -> C.D.dtrm) -> T -> T
22169
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   117
  val fold_up_ctxt : (C.D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   118
  val fold_down_ctxt : (C.D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   119
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   120
  (* searching through a zipper *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   121
  datatype zsearch = Here of T | LookIn of T;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   122
  (* lazily search through the zipper *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   123
  val lzy_search : (T -> zsearch list) -> T -> T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   124
  (* lazy search with folded data *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   125
  val pf_lzy_search : ('a -> T -> ('a * zsearch list)) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   126
                      -> 'a -> T -> T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   127
  (* zsearch list is or-choices *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   128
  val searchfold : ('a -> T -> (('a * zsearch) list)) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   129
                      -> 'a -> T -> ('a * T) Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   130
  (* limit function to the current focus of the zipper, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   131
     but give function the zipper's context *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   132
  val limit_pcapply : (C.T -> T -> ('a * T) Seq.seq) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   133
                      -> T -> ('a * T) Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   134
  val limit_apply : (T -> T Seq.seq) -> T -> T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   135
  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
   136
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   137
  (* moving around zippers with option types *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   138
  val omove_up : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   139
  val omove_up_abs : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   140
  val omove_up_app : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   141
  val omove_up_left : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   142
  val omove_up_right : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   143
  val omove_up_left_or_abs : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   144
  val omove_up_right_or_abs : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   145
  val omove_down_abs : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   146
  val omove_down_left : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   147
  val omove_down_right : T -> T option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   148
  val omove_down_app : T -> (T * T) option
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   149
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   150
  (* moving around zippers, raising exceptions *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   151
  exception move of string * T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   152
  val move_up : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   153
  val move_up_abs : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   154
  val move_up_app : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   155
  val move_up_left : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   156
  val move_up_right : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   157
  val move_up_left_or_abs : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   158
  val move_up_right_or_abs : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   159
  val move_down_abs : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   160
  val move_down_left : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   161
  val move_down_right : T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   162
  val move_down_app : T -> T * T
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
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   165
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   166
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   167
(* Zipper data for an generic trm *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   168
functor TrmCtxtDataFUN(Trm : ABSTRACT_TRM) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   169
: TRM_CTXT_DATA 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   170
= struct
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
  structure Trm = Trm;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   173
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   174
  (* a dtrm is, in McBridge-speak, a differentiated term. It represents
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   175
  the different ways a term can occur within its datatype constructors *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   176
  datatype dtrm = Abs of Trm.aname * Trm.typ
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   177
                | AppL of Trm.T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   178
                | AppR of Trm.T;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   179
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   180
  (* 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
   181
  fun apply (Abs (s,ty)) t = Trm.Abs (s,ty,t)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   182
    | apply (AppL tl) tr = Trm.$ (tl, tr)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   183
    | apply (AppR tr) tl = Trm.$ (tl, tr);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   184
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   185
  fun eq_pos (Abs _, Abs _) = true
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   186
    | eq_pos (AppL _, AppL _) = true
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   187
    | eq_pos (AppR _, AppR _) = true
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   188
    | eq_pos _ = false;
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
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   191
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   192
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   193
(* functor for making term contexts given term data *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   194
functor TrmCtxtFUN(D : TRM_CTXT_DATA) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   195
 : TRM_CTXT =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   196
struct 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   197
  structure D = D;
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
  type T = D.dtrm list;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   200
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   201
  val empty = [];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   202
  val is_empty = List.null;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   203
19902
9e5d0df75c98 added interface for making term contexts.
dixon
parents: 19871
diff changeset
   204
  fun add_abs d l = (D.Abs d) :: l;
9e5d0df75c98 added interface for making term contexts.
dixon
parents: 19871
diff changeset
   205
  fun add_appl d l = (D.AppL d) :: l;
9e5d0df75c98 added interface for making term contexts.
dixon
parents: 19871
diff changeset
   206
  fun add_appr d l = (D.AppR d) :: l;
9e5d0df75c98 added interface for making term contexts.
dixon
parents: 19871
diff changeset
   207
9e5d0df75c98 added interface for making term contexts.
dixon
parents: 19871
diff changeset
   208
  fun add_dtrm d l = d::l;
9e5d0df75c98 added interface for making term contexts.
dixon
parents: 19871
diff changeset
   209
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   210
  fun eq_path ([], []) = true
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   211
    | eq_path ([], _::_) = false
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   212
    | eq_path ( _::_, []) = false
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   213
    | eq_path (h::t, h2::t2) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   214
      D.eq_pos(h,h2) andalso eq_path (t, t2);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   215
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   216
  (* add context to outside of existing context *) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   217
  fun add_outerctxt ctop cbottom = cbottom @ ctop; 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   218
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   219
  (* mkterm : zipper -> trm -> trm *)
21395
f34ac19659ae moved some fundamental concepts to General/basics.ML;
wenzelm
parents: 21296
diff changeset
   220
  val apply = Basics.fold D.apply;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   221
  
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   222
  (* named type context *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   223
  val nty_ctxt = List.foldr (fn (D.Abs nty,ntys) => nty::ntys
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   224
                             | (_,ntys) => ntys) [];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   225
  (* type context *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   226
  val ty_ctxt = List.foldr (fn (D.Abs (_,ty),tys) => ty::tys
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   227
                           | (_,tys) => tys) [];
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 depth = length : T -> int;
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 map = List.map : (D.dtrm -> D.dtrm) -> T -> T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   232
22169
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   233
  val fold_up = Basics.fold : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a;
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   234
  val fold_down = Basics.fold_rev : (D.dtrm -> 'a -> 'a) -> T -> 'a -> 'a;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   235
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   236
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   237
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   238
(* zippers in terms of term contexts *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   239
functor ZipperFUN(C : TRM_CTXT) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   240
 : ZIPPER
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   241
= struct 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   242
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   243
  structure C = C;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   244
  structure D = C.D;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   245
  structure Trm = D.Trm;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   246
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   247
  type T = C.D.Trm.T * C.T;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   248
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   249
  fun mktop t = (t, C.empty) : T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   250
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   251
  val mk = I;
19853
cb73c3c367db made smlnj happy;
wenzelm
parents: 19835
diff changeset
   252
  fun set_trm x = apfst (K x);
cb73c3c367db made smlnj happy;
wenzelm
parents: 19835
diff changeset
   253
  fun set_ctxt x = apsnd (K x);
19835
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 goto_top (z as (t,c)) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   256
      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
   257
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   258
  fun at_top (_,c) = C.is_empty c;
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
  fun split (t,c) = ((t,C.empty) : T, c : C.T) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   261
  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
   262
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   263
  val ctxt = snd;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   264
  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
   265
  val top_trm = trm o goto_top;
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 nty_ctxt x = C.nty_ctxt (ctxt x);
cb73c3c367db made smlnj happy;
wenzelm
parents: 19835
diff changeset
   268
  fun ty_ctxt x = C.ty_ctxt (ctxt x);
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   269
19853
cb73c3c367db made smlnj happy;
wenzelm
parents: 19835
diff changeset
   270
  fun depth_of_ctxt x = C.depth (ctxt x);
cb73c3c367db made smlnj happy;
wenzelm
parents: 19835
diff changeset
   271
  fun map_on_ctxt x = apsnd (C.map x);
22169
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   272
  fun fold_up_ctxt f = C.fold_up f o ctxt;
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   273
  fun fold_down_ctxt f = C.fold_down f o ctxt;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   274
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   275
  fun omove_up (t,(d::c)) = SOME (D.apply d t, c)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   276
    | omove_up (z as (_,[])) = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   277
  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
   278
    | omove_up_abs z = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   279
  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
   280
    | 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
   281
    | omove_up_app z = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   282
  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
   283
    | omove_up_left z = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   284
  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
   285
    | omove_up_right _ = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   286
  fun omove_up_left_or_abs (t,(D.AppL tl)::c) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   287
      SOME (Trm.$(tl,t), c)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   288
    | omove_up_left_or_abs (t,(D.Abs (n,ty))::c) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   289
      SOME (Trm.Abs(n,ty,t), c)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   290
    | omove_up_left_or_abs z = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   291
  fun omove_up_right_or_abs (t,(D.Abs (n,ty))::c) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   292
      SOME (Trm.Abs(n,ty,t), c) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   293
    | omove_up_right_or_abs (t,(D.AppR tr)::c) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   294
      SOME (Trm.$(t,tr), c)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   295
    | omove_up_right_or_abs _ = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   296
  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
   297
    | omove_down_abs _ = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   298
  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
   299
    | omove_down_left _ = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   300
  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
   301
    | omove_down_right _ = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   302
  fun omove_down_app (Trm.$(l,r),c) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   303
      SOME ((l,(D.AppR r)::c),(r,(D.AppL l)::c))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   304
    | omove_down_app _ = NONE;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   305
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   306
  exception move of string * T
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   307
  fun move_up (t,(d::c)) = (D.apply d t, c)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   308
    | move_up (z as (_,[])) = raise move ("move_up",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   309
  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
   310
    | move_up_abs z = raise move ("move_up_abs",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   311
  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
   312
    | move_up_app (t,(D.AppR tr)::c) = (Trm.$(t,tr), c)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   313
    | move_up_app z = raise move ("move_up_app",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   314
  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
   315
    | move_up_left z = raise move ("move_up_left",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   316
  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
   317
    | move_up_right z = raise move ("move_up_right",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   318
  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
   319
    | 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
   320
    | 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
   321
  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
   322
    | 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
   323
    | 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
   324
  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
   325
    | move_down_abs z = raise move ("move_down_abs",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   326
  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
   327
    | move_down_left z = raise move ("move_down_left",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   328
  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
   329
    | move_down_right z = raise move ("move_down_right",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   330
  fun move_down_app (Trm.$(l,r),c) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   331
      ((l,(D.AppR r)::c),(r,(D.AppL l)::c))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   332
    | move_down_app z = raise move ("move_down_app",z);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   333
19975
ecd684d62808 fix to subst in order to allow subst when head of a term is a bound variable.
dixon
parents: 19902
diff changeset
   334
  (* 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
   335
  (* implicit arguments: C.D.dtrm list, then T *)
22169
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   336
  val zipto = C.fold_down 
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   337
                (fn C.D.Abs _ => move_down_abs 
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   338
                  | C.D.AppL _ => move_down_right
74b61ce6b54d added a fold up and fold down as separate functions and fixed zipto to
dixon
parents: 22081
diff changeset
   339
                  | C.D.AppR _ => move_down_left); 
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   340
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   341
  (* Note: interpretted as being examined depth first *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   342
  datatype zsearch = Here of T | LookIn of T;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   343
19975
ecd684d62808 fix to subst in order to allow subst when head of a term is a bound variable.
dixon
parents: 19902
diff changeset
   344
  (* lazy search *)
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   345
  fun lzy_search fsearch = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   346
      let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   347
        fun lzyl [] () = NONE
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   348
          | lzyl ((Here z) :: more) () = SOME(z, Seq.make (lzyl more))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   349
          | lzyl ((LookIn z) :: more) () =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   350
            (case lzy z
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   351
              of NONE => NONE
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   352
               | SOME (hz,mz) => 
19861
620d90091788 tuned Seq/Envir/Unify interfaces;
wenzelm
parents: 19853
diff changeset
   353
                 SOME (hz, Seq.append mz (Seq.make (lzyl more))))
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   354
        and lzy z = lzyl (fsearch z) ()
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   355
      in Seq.make o lzyl o fsearch end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   356
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   357
  (* path folded lazy search - the search list is defined in terms of
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   358
  the path passed through: the data a is updated with every zipper
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   359
  considered *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   360
  fun pf_lzy_search fsearch a0 z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   361
      let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   362
        fun lzyl a [] () = NONE
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   363
          | lzyl a ((Here z) :: more) () = SOME(z, Seq.make (lzyl a more))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   364
          | lzyl a ((LookIn z) :: more) () =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   365
            (case lzy a z
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   366
              of NONE => lzyl a more ()
19861
620d90091788 tuned Seq/Envir/Unify interfaces;
wenzelm
parents: 19853
diff changeset
   367
               | 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
   368
        and lzy a z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   369
            let val (a2, slist) = (fsearch a z) in lzyl a2 slist () end
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   370
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   371
        val (a,slist) = fsearch a0 z
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   372
      in Seq.make (lzyl a slist) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   373
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   374
  (* Note: depth first over zsearch results *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   375
  fun searchfold fsearch a0 z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   376
      let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   377
        fun lzyl [] () = NONE
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   378
          | lzyl ((a, Here z) :: more) () = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   379
            SOME((a,z), Seq.make (lzyl more))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   380
          | lzyl ((a, LookIn z) :: more) () =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   381
            (case lzyl (fsearch a z) () of 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   382
               NONE => lzyl more ()
19861
620d90091788 tuned Seq/Envir/Unify interfaces;
wenzelm
parents: 19853
diff changeset
   383
             | 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
   384
      in Seq.make (lzyl (fsearch a0 z)) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   385
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   386
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   387
  fun limit_pcapply f z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   388
      let val (z2,c) = split z
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   389
      in Seq.map (apsnd (add_outerctxt c)) (f c z2) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   390
  fun limit_capply (f : C.T -> T -> T Seq.seq) (z : T) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   391
      let val ((z2 : T),(c : C.T)) = split z
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   392
      in Seq.map (add_outerctxt c) (f c z2) 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
  val limit_apply = limit_capply o K;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   395
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   396
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   397
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   398
(* now build these for Isabelle terms *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   399
structure TrmCtxtData = TrmCtxtDataFUN(IsabelleTrmWrap);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   400
structure TrmCtxt = TrmCtxtFUN(TrmCtxtData);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   401
structure Zipper = ZipperFUN(TrmCtxt);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   402
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
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   405
(* For searching through Zippers below the current focus...
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   406
   KEY for naming scheme:    
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
   td = starting at the top down
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   409
   lr = going from left to right
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   410
   rl = going from right to left
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   411
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   412
   bl = starting at the bottom left
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   413
   br = starting at the bottom right
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   414
   ul = going up then left
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   415
   ur = going up then right
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   416
   ru = going right then up
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   417
   lu = going left then up
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
signature ZIPPER_SEARCH =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   420
sig
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   421
  structure Z : ZIPPER;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   422
  
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   423
  val leaves_lr : Z.T -> Z.T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   424
  val leaves_rl : Z.T -> Z.T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   425
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   426
  val all_bl_ru : Z.T -> Z.T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   427
  val all_bl_ur : Z.T -> Z.T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   428
  val all_td_lr : Z.T -> Z.T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   429
  val all_td_rl : Z.T -> Z.T Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   430
  
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   431
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   432
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   433
functor ZipperSearchFUN(Zipper : ZIPPER) : ZIPPER_SEARCH
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   434
= struct
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   435
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   436
structure Z = Zipper;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   437
structure C = Z.C;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   438
structure D = C.D; 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   439
structure Trm = D.Trm; 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   440
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   441
fun sf_leaves_lr z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   442
    case Z.trm z 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   443
     of Trm.$ _ => [Z.LookIn (Z.move_down_left z),
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   444
                    Z.LookIn (Z.move_down_right z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   445
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   446
      | _ => [Z.Here z];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   447
fun sf_leaves_rl z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   448
    case Z.trm z 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   449
     of Trm.$ _ => [Z.LookIn (Z.move_down_right z),
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   450
                    Z.LookIn (Z.move_down_left z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   451
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   452
      | _ => [Z.Here z];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   453
val leaves_lr = Z.lzy_search sf_leaves_lr;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   454
val leaves_rl = Z.lzy_search sf_leaves_rl;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   455
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   456
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   457
fun sf_all_td_lr z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   458
    case Z.trm z 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   459
     of Trm.$ _ => [Z.Here z, Z.LookIn (Z.move_down_left z),
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   460
                    Z.LookIn (Z.move_down_right z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   461
      | Trm.Abs _ => [Z.Here z, Z.LookIn (Z.move_down_abs z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   462
      | _ => [Z.Here z];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   463
fun sf_all_td_rl z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   464
    case Z.trm z 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   465
     of Trm.$ _ => [Z.Here z, Z.LookIn (Z.move_down_right z),
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   466
                    Z.LookIn (Z.move_down_left z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   467
      | Trm.Abs _ => [Z.Here z, Z.LookIn (Z.move_down_abs z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   468
      | _ => [Z.Here z];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   469
fun sf_all_bl_ur z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   470
    case Z.trm z 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   471
     of Trm.$ _ => [Z.LookIn (Z.move_down_left z), Z.Here z,
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   472
                    Z.LookIn (Z.move_down_right z)]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   473
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z),
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   474
                      Z.Here z]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   475
      | _ => [Z.Here z];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   476
fun sf_all_bl_ru z = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   477
    case Z.trm z 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   478
     of Trm.$ _ => [Z.LookIn (Z.move_down_left z),
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   479
                    Z.LookIn (Z.move_down_right z), Z.Here z]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   480
      | Trm.Abs _ => [Z.LookIn (Z.move_down_abs z), Z.Here z]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   481
      | _ => [Z.Here z];
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   482
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   483
val all_td_lr = Z.lzy_search sf_all_td_lr;
19871
88e8f6173bab Corrected search order for zippers.
dixon
parents: 19861
diff changeset
   484
val all_td_rl = Z.lzy_search sf_all_td_rl;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   485
val all_bl_ur = Z.lzy_search sf_all_bl_ru;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   486
val all_bl_ru = Z.lzy_search sf_all_bl_ur;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   487
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   488
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   489
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   490
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   491
structure ZipperSearch = ZipperSearchFUN(Zipper);