src/Tools/isac/MathEngBasic/ctree-basic.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 09 Apr 2020 17:13:17 +0200
changeset 59861 65ec9f679c3f
parent 59854 c20d08e01ad2
child 59868 d77aa0992e0f
permissions -rw-r--r--
separate struct. UnparseC, shift code to ThmC
wneuper@59292
     1
(* Title: the calctree, which holds a calculation
wneuper@59292
     2
   Author: Walther Neuper 1999
wneuper@59292
     3
   (c) due to copyright terms
wneuper@59292
     4
*)
wneuper@59292
     5
wneuper@59292
     6
signature BASIC_CALC_TREE =
walther@59736
     7
sig
walther@59736
     8
(** definitions required for datatype ctree, renamed later appropriately **)
walther@59736
     9
walther@59736
    10
(** the basic datatype **)
wneuper@59292
    11
  type state
walther@59814
    12
  val e_state: state
walther@59787
    13
wneuper@59292
    14
  type con
wneuper@59292
    15
  eqtype cellID
wneuper@59292
    16
wneuper@59292
    17
  datatype branch  = AndB | CollectB | IntersectB | MapB | NoBranch | OrB | SequenceB | TransitiveB
wneuper@59292
    18
  datatype ostate = Complete | Incomplete | Inconsistent
wneuper@59292
    19
  datatype ppobj =
wneuper@59292
    20
    PblObj of
wneuper@59292
    21
     {branch: branch,
walther@59737
    22
      loc: (Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context) option,
wneuper@59292
    23
      ostate: ostate,
wneuper@59299
    24
      result: Selem.result,
wneuper@59292
    25
wneuper@59298
    26
      fmz: Selem.fmz,
wneuper@59405
    27
      origin: Model.ori list * Celem.spec * term,
wneuper@59316
    28
      probl: Model.itm list,
wneuper@59316
    29
      meth: Model.itm list,
wneuper@59405
    30
      spec: Celem.spec,
walther@59839
    31
      ctxt: Proof.context}
wneuper@59292
    32
  | PrfObj of
wneuper@59292
    33
     {branch: branch,
walther@59737
    34
      loc: (Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context) option,
wneuper@59292
    35
      ostate: ostate,
wneuper@59299
    36
      result: Selem.result,
wneuper@59292
    37
wneuper@59292
    38
      form: term,
walther@59741
    39
      tac: Tactic.input}
walther@59736
    40
  datatype ctree = EmptyPtree | Nd of ppobj * ctree list
wneuper@59292
    41
walther@59736
    42
(** basic functions **)
wneuper@59292
    43
  val e_ctree : ctree (* TODO: replace by EmptyPtree*)
walther@59846
    44
  val existpt' : Pos.pos' -> ctree -> bool                                     (* for interface.sml *)
walther@59846
    45
  val is_interpos : Pos.pos' -> bool                                           (* for interface.sml *)
walther@59846
    46
  val lev_pred' : ctree -> Pos.pos' -> Pos.pos'                                    (* for interface.sml *)
walther@59846
    47
  val ins_chn : ctree list -> ctree -> Pos.pos -> ctree                       (* for solve.sml *)
wneuper@59292
    48
  val children : ctree -> ctree list                                           (* for solve.sml *)
walther@59846
    49
  val get_nd : ctree -> Pos.pos -> ctree                                           (* for solve.sml *)
wneuper@59292
    50
  val just_created_ : ppobj -> bool                                       (* for mathengine.sml *)
wneuper@59405
    51
  val just_created : state -> bool                                        (* for mathengine.sml *)
wneuper@59405
    52
  val e_origin : Model.ori list * Celem.spec * term                       (* for mathengine.sml *)
wneuper@59292
    53
wneuper@59292
    54
  val is_pblobj : ppobj -> bool
walther@59846
    55
  val is_pblobj' : ctree -> Pos.pos -> bool
wneuper@59292
    56
  val is_pblnd : ctree -> bool
wneuper@59292
    57
wneuper@59405
    58
  val g_spec : ppobj -> Celem.spec
walther@59737
    59
  val g_loc : ppobj -> (Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context) option
wneuper@59292
    60
  val g_form : ppobj -> term
wneuper@59316
    61
  val g_pbl : ppobj -> Model.itm list
wneuper@59316
    62
  val g_met : ppobj -> Model.itm list
wneuper@59405
    63
  val g_metID : ppobj -> Celem.metID
wneuper@59299
    64
  val g_result : ppobj -> Selem.result
walther@59741
    65
  val g_tac : ppobj -> Tactic.input
walther@59854
    66
  val g_domID : ppobj -> ThyC.domID                     (* for appl.sml TODO: replace by thyID *)
wneuper@59292
    67
wneuper@59405
    68
  val g_origin : ppobj -> Model.ori list * Celem.spec * term                  (* for script.sml *)
walther@59846
    69
  val get_loc : ctree -> Pos.pos' -> Istate_Def.T * Proof.context                 (* for script.sml *)
walther@59846
    70
  val get_istate_LI : ctree -> Pos.pos' -> Istate_Def.T                              (* for script.sml *)
walther@59846
    71
  val get_ctxt_LI: ctree -> Pos.pos' -> Proof.context
walther@59846
    72
  val get_ctxt : ctree -> Pos.pos' -> Proof.context (*DEPRECATED*)
walther@59846
    73
  val get_obj : (ppobj -> 'a) -> ctree -> Pos.pos -> 'a
wneuper@59292
    74
  val get_curr_formula : state -> term
walther@59846
    75
  val get_assumptions : ctree -> Pos.pos' -> term list                             (* for appl.sml *)
wneuper@59292
    76
walther@59737
    77
  val new_val : term -> Istate_Def.T -> Istate_Def.T
wneuper@59292
    78
  (* for calchead.sml *)
wneuper@59292
    79
  type cid = cellID list
walther@59846
    80
  type ocalhd = bool * Pos.pos_ * term * Model.itm list * (bool * term) list * Celem.spec
wneuper@59292
    81
  datatype ptform = Form of term | ModSpec of ocalhd
wneuper@59405
    82
  val get_somespec' : Celem.spec -> Celem.spec -> Celem.spec
wneuper@59292
    83
  exception PTREE of string;
wneuper@59296
    84
  
walther@59851
    85
  val parent_node : ctree -> Pos.pos -> bool * Pos.pos * Rule_Set.T                      (* for appl.sml *)
wneuper@59405
    86
  val rootthy : ctree -> theory                                               (* for script.sml *)
wneuper@59294
    87
(* ---- made visible ONLY for structure CTaccess : CALC_TREE_ACCESS --------------------------- *)
walther@59846
    88
  val appl_obj : (ppobj -> ppobj) -> ctree -> Pos.pos -> ctree
walther@59846
    89
  val existpt : Pos.pos -> ctree -> bool                                          (* also for tests *)
walther@59846
    90
  val cut_tree : ctree -> Pos.pos * 'a -> ctree * Pos.pos' list                       (* also for tests *)
wneuper@59294
    91
  val insert_pt : ppobj -> ctree -> int list -> ctree
wneuper@59294
    92
(* ---- made visible ONLY for structure CTnavi : CALC_TREE_NAVIGATION ------------------------- *)
wneuper@59296
    93
  val g_branch : ppobj -> branch
wneuper@59296
    94
  val g_form' : ctree -> term
wneuper@59296
    95
  val g_ostate : ppobj -> ostate
wneuper@59296
    96
  val g_ostate' : ctree -> ostate
wneuper@59296
    97
  val g_res : ppobj -> term
wneuper@59296
    98
  val g_res' : ctree -> term 
wneuper@59296
    99
(*/---- duplicates in CTnavi, reconsider structs -----------------------------------------------
walther@59846
   100
  val lev_dn : CTbasic.Pos.pos -> Pos.pos                        (* duplicate in ctree-navi.sml *)
walther@59846
   101
  val par_pblobj : CTbasic.ctree -> Pos.pos -> Pos.pos   (* duplicate in ctree-navi.sml *)
wneuper@59296
   102
   ---- duplicates in CTnavi, reconsider structs ----------------------------------------------/*)
wneuper@59294
   103
walther@59785
   104
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59846
   105
  val pr_ctree : (Pos.pos -> ppobj -> string) -> ctree -> string
walther@59846
   106
  val pr_short : Pos.pos -> ppobj -> string
wneuper@59292
   107
  val g_ctxt : ppobj -> Proof.context
wneuper@59298
   108
  val g_fmz : ppobj -> Selem.fmz
walther@59846
   109
  val get_allp : Pos.pos' list -> Pos.pos * (int list * Pos.pos_) -> ctree -> Pos.pos' list
walther@59846
   110
  val get_allps : (Pos.pos * Pos.pos_) list -> Pos.posel list -> ctree list -> Pos.pos' list
walther@59846
   111
  val get_allpos' : Pos.pos * Pos.posel -> ctree -> Pos.pos' list
walther@59846
   112
  val get_allpos's : Pos.pos * Pos.posel -> ctree list -> (Pos.pos * Pos.pos_) list
walther@59846
   113
  val cut_bottom : Pos.pos * Pos.posel -> ctree -> (ctree * Pos.pos' list) * bool
walther@59846
   114
  val cut_level : Pos.pos' list -> Pos.pos -> ctree -> int list * Pos.pos_ -> ctree * Pos.pos' list
walther@59846
   115
  val cut_level_'_ : Pos.pos' list -> Pos.pos -> ctree -> int list * Pos.pos_ -> ctree * Pos.pos' list
wneuper@59292
   116
  val get_trace : ctree -> int list -> int list -> int list list
wneuper@59292
   117
  val branch2str : branch -> string
walther@59785
   118
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
   119
wneuper@59310
   120
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59310
   121
  (* NONE *)
wneuper@59292
   122
end
wneuper@59292
   123
wneuper@59292
   124
(**)
wneuper@59292
   125
structure CTbasic(**): BASIC_CALC_TREE(**) =
wneuper@59292
   126
struct
wneuper@59292
   127
(**)
walther@59693
   128
open Pos
walther@59693
   129
wneuper@59292
   130
datatype branch = 
wneuper@59292
   131
	NoBranch | AndB | OrB 
wneuper@59292
   132
| TransitiveB  (* FIXXXME.0308: set branch from met in Apply_Method
wneuper@59292
   133
                  FIXXXME.0402: -"- in Begin_Trans'*)
wneuper@59292
   134
| SequenceB | IntersectB | CollectB | MapB;
wneuper@59292
   135
wneuper@59292
   136
fun branch2str NoBranch = "NoBranch" (* for tests only *)
wneuper@59292
   137
  | branch2str AndB = "AndB"
wneuper@59292
   138
  | branch2str OrB = "OrB"
wneuper@59292
   139
  | branch2str TransitiveB = "TransitiveB" 
wneuper@59292
   140
  | branch2str SequenceB = "SequenceB"
wneuper@59292
   141
  | branch2str IntersectB = "IntersectB"
wneuper@59292
   142
  | branch2str CollectB = "CollectB"
wneuper@59292
   143
  | branch2str MapB = "MapB";
wneuper@59292
   144
wneuper@59292
   145
datatype ostate = 
wneuper@59292
   146
    Incomplete | Complete | Inconsistent (* WN041020 latter still unused *);
wneuper@59292
   147
fun ostate2str Incomplete = "Incomplete" (* for tests only *)
wneuper@59292
   148
  | ostate2str Complete = "Complete"
wneuper@59292
   149
  | ostate2str Inconsistent = "Inconsistent";
wneuper@59292
   150
wneuper@59292
   151
type cellID = int;     
wneuper@59292
   152
type cid = cellID list;
wneuper@59292
   153
wneuper@59292
   154
walther@59737
   155
type iist = Istate_Def.T option * Istate_Def.T option;
walther@59846
   156
(*val e_iist = (empty, empty); --- sinnlos f"ur NICHT-equality-type*) 
wneuper@59292
   157
wneuper@59292
   158
walther@59737
   159
fun new_val v (Istate_Def.Pstate pst) =
walther@59737
   160
    (Istate_Def.Pstate (Istate_Def.set_act v pst))
wneuper@59583
   161
  | new_val _ _ = error "new_val: only for Pstate";
wneuper@59292
   162
wneuper@59292
   163
datatype con = land | lor;
wneuper@59292
   164
wneuper@59292
   165
(* executed tactics (tac_s) with local environment etc.;
wneuper@59292
   166
  used for continuing eval script + for generate *)
wneuper@59292
   167
type ets =
walther@59767
   168
  (TermC.path *(* of tactic in scr, tactic (weakly) associated with tac_                   *)
walther@59741
   169
   (Tactic.T * (* (for generate)                                                           *)
walther@59693
   170
    Env.T *      (* with 'tactic=result' as  rule, tactic ev. _not_ ready for 'parallel let' *)
walther@59693
   171
    Env.T *      (* with results of (ready) tacs                                             *)
wneuper@59302
   172
    term *     (* itr_arg of tactic, for upd. env at Repeat, Try                           *)
wneuper@59302
   173
    term *     (* result value of the tac                                                  *)
walther@59675
   174
    Telem.safe))
wneuper@59292
   175
  list;
wneuper@59292
   176
wneuper@59292
   177
fun ets2s (l,(m,eno,env,iar,res,s)) = 
walther@59774
   178
  "\n(" ^ TermC.string_of_path l ^ ",(" ^ Tactic.string_of m ^
walther@59659
   179
  ",\n  ens= " ^ Env.subst2str eno ^
walther@59659
   180
  ",\n  env= " ^ Env.subst2str env ^
walther@59861
   181
  ",\n  iar= " ^ UnparseC.term2str iar ^
walther@59861
   182
  ",\n  res= " ^ UnparseC.term2str res ^
walther@59675
   183
  ",\n  " ^ Telem.safe2str s ^ "))";
wneuper@59292
   184
fun ets2str (ets: ets) = (strs2str o (map ets2s)) ets; (* for tests only *)
wneuper@59292
   185
wneuper@59292
   186
type envp =(*9.5.03: unused, delete with field in ctree.PblObj FIXXXME*)
wneuper@59292
   187
  (int * term list) list * (* assoc-list: args of met*)
walther@59851
   188
  (int * Rule_Set.T) list * (* assoc-list: tacs already done ///15.9.00*)
wneuper@59292
   189
  (int * ets) list *       (* assoc-list: tacs etc. already done*)
wneuper@59292
   190
  (string * pos) list;     (* asms * from where*)
wneuper@59292
   191
wneuper@59292
   192
datatype ppobj = (* TODO: arrange according to signature *)
walther@59809
   193
  PrfObj of                   (* a proof step triggered by a tactic                            *)
walther@59840
   194
   {form  : term,             (* where tactic is applied to                                    *)
walther@59809
   195
	  tac   : Tactic.input,     (* tactic as presented to users                                  *)
walther@59809
   196
	  loc   : (Istate_Def.T *   (* program interpreter state                                     *)
wneuper@59292
   197
	           Proof.context)   (* context for provers, type inference                           *)
wneuper@59292
   198
            option *          (* both for interpreter location on Frm, Pbl, Met                *)
walther@59737
   199
            (Istate_Def.T *   (* script interpreter state                                      *)
wneuper@59292
   200
             Proof.context)   (* context for provers, type inference                           *)
wneuper@59292
   201
            option,           (* both for interpreter location on Res                          *)
walther@59846
   202
                              (*(NONE,NONE) <==> empty ! see update_loc, get_loc            *)
wneuper@59292
   203
	  branch: branch,           (* only rudimentary                                              *)
wneuper@59299
   204
	  result: Selem.result,     (* result and assumptions                                        *)
wneuper@59292
   205
	  ostate: ostate}           (* Complete <=> result is OK                                     *)
walther@59809
   206
| PblObj of                   (* a step serving a whole specification-phase                    *)
walther@59840
   207
   {fmz   : Selem.fmz,        (* from init:FIXME never use this spec;-drop                     *)
walther@59809
   208
    origin: (Model.ori list) *(* from fmz+pbt+met for efficiently adding items to probl, meth  *)
wneuper@59405
   209
	           Celem.spec *     (* updated by Refine_Tacitly                                     *)
wneuper@59292
   210
	           term,            (* headline of calc-head, as calculated initially(!)             *)
wneuper@59405
   211
    spec  : Celem.spec,       (* explicitly input                                              *)
wneuper@59405
   212
    probl : Model.itm list,   (* itms explicitly input                                         *)
wneuper@59405
   213
    meth  : Model.itm list,   (* itms automatically added to copy of probl                     *)
walther@59809
   214
    ctxt  : Proof.context,    (* used while specifying this SubProblem                         *)
walther@59807
   215
    loc   : (Istate_Def.T * Proof.context) option  (* like in PrfObj, calling this SubProblem  *)
walther@59820
   216
          * (Istate_Def.T * Proof.context) option, (* like in PrfObj, finishing the SubProblem *)
wneuper@59292
   217
    branch: branch,           (* like PrfObj                                                   *)
wneuper@59299
   218
    result: Selem.result,     (* like PrfObj                                                   *)
wneuper@59292
   219
    ostate: ostate};          (* like PrfObj                                                   *)
wneuper@59292
   220
wneuper@59292
   221
(* this tree contains isac's calculations;
wneuper@59292
   222
   the tree's structure has been copied from an early version of Theorema(c);
wneuper@59292
   223
   it has the disadvantage, that there is no space 
wneuper@59292
   224
   for the first tactic in a script generating the first formula at (p,Frm);
walther@59845
   225
   this trouble has been covered by 'implicit_take' and 'Take' so far,
wneuper@59292
   226
   but it is crucial if the first tactic in a script is eg. 'Subproblem';
wneuper@59292
   227
   see 'type tac', Apply_Method.
wneuper@59292
   228
*)
wneuper@59292
   229
datatype ctree = 
wneuper@59292
   230
  EmptyPtree
wneuper@59292
   231
| Nd of ppobj * (ctree list);
wneuper@59292
   232
val e_ctree = EmptyPtree;
wneuper@59292
   233
type state = ctree * pos'
walther@59814
   234
val e_state = (EmptyPtree , e_pos')
wneuper@59292
   235
wneuper@59292
   236
fun is_pblobj (PblObj _) = true
wneuper@59292
   237
  | is_pblobj _ = false;
wneuper@59292
   238
wneuper@59292
   239
exception PTREE of string;
wneuper@59292
   240
fun nth _ [] = raise PTREE "nth _ []"
wneuper@59292
   241
  | nth 1 (x :: _) = x
wneuper@59292
   242
  | nth n (_ :: xs) = nth (n - 1) xs;
wneuper@59292
   243
(*> nth 2 [11,22,33]; -->> val it = 22 : int*)
wneuper@59292
   244
wneuper@59292
   245
wneuper@59292
   246
(** convert ctree to a string **)
wneuper@59292
   247
wneuper@59292
   248
(* convert a pos from list to string *)
wneuper@59292
   249
fun pr_pos ps = (space_implode "." (map string_of_int ps))^".   ";
wneuper@59292
   250
(* show hd origin or form only *)
wneuper@59292
   251
fun pr_short p (PblObj _) =  pr_pos p  ^ " ----- pblobj -----\n"               (* for tests only *)
walther@59861
   252
  | pr_short p (PrfObj {form = form, ...}) = pr_pos p ^ UnparseC.term2str form ^ "\n";
wneuper@59292
   253
fun pr_ctree f pt =                                                            (* for tests only *)
wneuper@59292
   254
  let
wneuper@59292
   255
    fun pr_pt _ _  EmptyPtree = ""
wneuper@59292
   256
      | pr_pt pfn ps (Nd (b, [])) = pfn ps b
wneuper@59292
   257
      | pr_pt pfn ps (Nd (b, ts)) = pfn ps b ^ prts pfn ps 1 ts
wneuper@59292
   258
    and prts _ _ _ [] = ""
wneuper@59292
   259
      | prts pfn ps p (t :: ts) = (pr_pt pfn (ps @ [p]) t)^
wneuper@59292
   260
      (prts pfn ps (p + 1) ts)
wneuper@59292
   261
  in pr_pt f [] pt end;
wneuper@59292
   262
wneuper@59292
   263
(** access the branches of ctree **)
wneuper@59292
   264
wneuper@59292
   265
fun repl [] _ _ = raise PTREE "repl [] _ _"
wneuper@59292
   266
  | repl (_ :: ls) 1 e = e :: ls
wneuper@59292
   267
  | repl (l :: ls) n e = l :: (repl ls (n-1) e);
wneuper@59292
   268
fun repl_app ls n e = 
wneuper@59292
   269
  let
wneuper@59292
   270
    val lim = 1 + length ls
wneuper@59292
   271
  in
wneuper@59292
   272
    if n > lim
wneuper@59292
   273
    then raise PTREE "repl_app: n > lim"
wneuper@59292
   274
    else if n = lim
wneuper@59292
   275
      then ls @ [e]
wneuper@59292
   276
      else repl ls n e end;
wneuper@59292
   277
wneuper@59292
   278
(* get from obj at pos by f : ppobj -> 'a *)
wneuper@59292
   279
fun get_obj _ EmptyPtree _ = raise PTREE "get_obj f EmptyPtree"
wneuper@59292
   280
  | get_obj f (Nd (b, _)) [] = f b
wneuper@59292
   281
  | get_obj f (Nd (_, bs)) (p :: ps) =
wneuper@59292
   282
    let
wneuper@59292
   283
      val _ = (nth p bs)
wneuper@59292
   284
      handle _ => raise PTREE ("get_obj: pos = " ^ ints2str' (p::ps) ^ " does not exist");
wneuper@59292
   285
    in
wneuper@59292
   286
      (get_obj f (nth p bs) ps) 
wneuper@59292
   287
      handle _ => raise PTREE ("get_obj: pos = " ^ ints2str' (p::ps) ^ " does not exist")
wneuper@59292
   288
    end;
wneuper@59292
   289
fun get_nd EmptyPtree _ = raise PTREE "get_nd EmptyPtree"
wneuper@59292
   290
  | get_nd n [] = n
wneuper@59292
   291
  | get_nd (Nd (_, nds)) (pos as p :: ps) = (get_nd (nth p nds) ps)
wneuper@59292
   292
    handle _ => raise PTREE ("get_nd: not existent pos = " ^ ints2str' pos);
wneuper@59292
   293
wneuper@59292
   294
(* for use by get_obj *)
wneuper@59292
   295
fun g_form   (PrfObj {form = f,...}) = f
wneuper@59292
   296
  | g_form   (PblObj {origin= (_,_,f),...}) = f;
wneuper@59292
   297
fun g_form' (Nd (PrfObj {form = f, ...}, _)) = f
wneuper@59292
   298
  | g_form' (Nd (PblObj {origin= (_, _, f),...}, _)) = f
wneuper@59292
   299
  | g_form' _ = error "g_form': uncovered fun def.";
wneuper@59292
   300
(*  | g_form   _ = raise PTREE "g_form not for PblObj";*)
wneuper@59292
   301
fun g_origin (PblObj {origin = ori, ...}) = ori
wneuper@59292
   302
  | g_origin _ = raise PTREE "g_origin not for PrfObj";
wneuper@59292
   303
fun g_fmz (PblObj {fmz = f, ...}) = f                                        (* for tests only *)
wneuper@59292
   304
  | g_fmz _ = raise PTREE "g_fmz not for PrfObj";
wneuper@59292
   305
fun g_spec   (PblObj {spec = s, ...}) = s
wneuper@59292
   306
  | g_spec _   = raise PTREE "g_spec not for PrfObj";
wneuper@59292
   307
fun g_pbl    (PblObj {probl = p, ...}) = p
wneuper@59292
   308
  | g_pbl  _   = raise PTREE "g_pbl not for PrfObj";
wneuper@59292
   309
fun g_met    (PblObj {meth = p, ...}) = p
wneuper@59292
   310
  | g_met  _   = raise PTREE "g_met not for PrfObj";
wneuper@59292
   311
fun g_domID  (PblObj {spec = (d, _, _), ...}) = d
wneuper@59292
   312
  | g_domID  _ = raise PTREE "g_metID not for PrfObj";
wneuper@59292
   313
fun g_metID  (PblObj {spec = (_, _, m), ...}) = m
wneuper@59292
   314
  | g_metID  _ = raise PTREE "g_metID not for PrfObj";
wneuper@59292
   315
fun g_ctxt    (PblObj {ctxt, ...}) = ctxt
wneuper@59292
   316
  | g_ctxt    _ = raise PTREE "g_ctxt not for PrfObj"; 
wneuper@59292
   317
fun g_loc    (PblObj {loc = l, ...}) = l
wneuper@59292
   318
  | g_loc    (PrfObj {loc = l, ...}) = l;
wneuper@59292
   319
fun g_branch (PblObj {branch = b, ...}) = b
wneuper@59292
   320
  | g_branch (PrfObj {branch = b, ...}) = b;
walther@59741
   321
fun g_tac  (PblObj {spec = (_, _, m),...}) = Tactic.Apply_Method m
wneuper@59292
   322
  | g_tac  (PrfObj {tac = m, ...}) = m;
wneuper@59292
   323
fun g_result (PblObj {result = r, ...}) = r
wneuper@59292
   324
  | g_result (PrfObj {result = r, ...}) = r;
wneuper@59292
   325
fun g_res (PblObj {result = (r, _) ,...}) = r
wneuper@59299
   326
  | g_res (PrfObj {result = (r, _),...}) = r;
wneuper@59292
   327
fun g_res' (Nd (PblObj {result = (r, _), ...}, _)) = r
wneuper@59292
   328
  | g_res' (Nd (PrfObj {result = (r, _),...}, _)) = r
wneuper@59292
   329
  | g_res' _ = raise PTREE "g_res': uncovered fun def.";
wneuper@59292
   330
fun g_ostate (PblObj {ostate = r, ...}) = r
wneuper@59292
   331
  | g_ostate (PrfObj {ostate = r, ...}) = r;
wneuper@59292
   332
fun g_ostate' (Nd (PblObj {ostate = r, ...}, _)) = r
wneuper@59292
   333
  | g_ostate' (Nd (PrfObj {ostate = r, ...}, _)) = r
wneuper@59292
   334
  | g_ostate' _ = raise PTREE "g_ostate': uncovered fun def.";
wneuper@59292
   335
wneuper@59292
   336
(* get the formula preceeding the current position in a calculation *)
wneuper@59292
   337
fun get_curr_formula (pt, (p, p_)) = 
wneuper@59292
   338
	case p_ of
wneuper@59292
   339
	  Frm => get_obj g_form pt p
wneuper@59292
   340
	| Res => (fst o (get_obj g_result pt)) p
wneuper@59292
   341
	| _ => #3 (get_obj g_origin pt p);
wneuper@59292
   342
  
wneuper@59292
   343
(* in CalcTree/Subproblem an 'just_created_' model is created;
wneuper@59292
   344
   this is filled to 'untouched' by Model/Refine_Problem   *)
wneuper@59292
   345
fun just_created_ (PblObj {meth, probl, spec, ...}) =
wneuper@59405
   346
    null meth andalso null probl andalso spec = Celem.e_spec
wneuper@59292
   347
  | just_created_ _ = raise PTREE "g_ostate': uncovered fun def.";
walther@59861
   348
val e_origin = ([], Celem.e_spec, TermC.empty);
wneuper@59292
   349
wneuper@59292
   350
fun just_created (pt, (p, _)) =
wneuper@59292
   351
    let val ppobj = get_obj I pt p
wneuper@59292
   352
    in is_pblobj ppobj andalso just_created_ ppobj end;
wneuper@59292
   353
wneuper@59292
   354
(* does the pos in the ctree exist ? *)
wneuper@59292
   355
fun existpt pos pt = can (get_obj I pt) pos;
wneuper@59292
   356
(* does the pos' in the ctree exist, ie. extra check for result in the node *)
wneuper@59299
   357
fun existpt' (p, p_) pt = 
wneuper@59292
   358
  if can (get_obj I pt) p 
wneuper@59292
   359
  then case p_ of 
wneuper@59292
   360
	  Res => get_obj g_ostate pt p = Complete
wneuper@59292
   361
	| _ => true
wneuper@59292
   362
  else false;
wneuper@59292
   363
wneuper@59292
   364
(* is this position appropriate for calculating intermediate steps? *)
wneuper@59292
   365
fun is_interpos (_, Res) = true
wneuper@59292
   366
  | is_interpos _ = false;
wneuper@59292
   367
wneuper@59296
   368
(* get the children of a node in ctree *)
wneuper@59296
   369
fun children (Nd (PblObj _, cn)) = cn
wneuper@59296
   370
  | children (Nd (PrfObj _, cn)) = cn
wneuper@59296
   371
  | children _ = error "children: uncovered fun def.";
wneuper@59292
   372
wneuper@59296
   373
(*/--------------- duplicates in ctree-navi.sml: required also here below ---------------\*)
wneuper@59296
   374
fun lev_up [] = raise PTREE "lev_up []"
wneuper@59296
   375
  | lev_up p = (drop_last p):pos;
wneuper@59292
   376
(* find the position of the next parent which is a PblObj in ctree *)
wneuper@59292
   377
fun par_pblobj _ [] = []
wneuper@59292
   378
  | par_pblobj pt p =
wneuper@59292
   379
    let
wneuper@59292
   380
      fun par _ [] = []
wneuper@59292
   381
        | par pt p =
wneuper@59292
   382
          if is_pblobj (get_obj I pt p) 
wneuper@59292
   383
          then p
wneuper@59292
   384
          else par pt (lev_up p)
wneuper@59292
   385
    in par pt (lev_up p) end; 
wneuper@59296
   386
(*\--------------- duplicates in ctree-navi.sml: required also here below ---------------/*)
wneuper@59292
   387
walther@59838
   388
(* find the next parent, which is either a PblObj or a PrfObj *)
walther@59851
   389
fun parent_node _ [] = (true, [], Rule_Set.Empty)
walther@59838
   390
  | parent_node pt p =
wneuper@59292
   391
    let
walther@59851
   392
      fun par _ [] = (true, [], Rule_Set.Empty)
wneuper@59292
   393
        | par pt p =
wneuper@59292
   394
          if is_pblobj (get_obj I pt p)
walther@59851
   395
          then (true, p, Rule_Set.Empty)
wneuper@59292
   396
		      else case get_obj g_tac pt p of
walther@59741
   397
				    Tactic.Rewrite_Set rls' => (false, p, assoc_rls rls')
walther@59741
   398
			    | Tactic.Rewrite_Set_Inst (_, rls') => (false, p, assoc_rls rls')
wneuper@59292
   399
			    | _ => par pt (lev_up p)
wneuper@59292
   400
    in par pt (lev_up p) end; 
wneuper@59292
   401
wneuper@59292
   402
(* insert obj b into ctree at pos, ev.overwriting this pos *)
wneuper@59292
   403
fun insert_pt b EmptyPtree [] = Nd (b, [])
wneuper@59292
   404
  | insert_pt _ EmptyPtree _ = raise PTREE "insert_pt b Empty _"
walther@59820
   405
  | insert_pt b _ [] = Nd (b, [])
wneuper@59292
   406
  | insert_pt b (Nd (b', bs)) (p :: []) = Nd (b', repl_app bs p (Nd (b, []))) 
wneuper@59292
   407
  | insert_pt b (Nd (b', bs)) (p :: ps) = Nd (b', repl_app bs p (insert_pt b (nth p bs) ps));
wneuper@59292
   408
wneuper@59292
   409
(* insert children to a node without children. compare: fun insert_pt *)
wneuper@59292
   410
fun ins_chn _  EmptyPtree _ = raise PTREE "ins_chn: EmptyPtree"
wneuper@59292
   411
  | ins_chn _ (Nd _) [] = raise PTREE "ins_chn: pos = []"
wneuper@59292
   412
  | ins_chn ns (Nd (b, bs)) (p :: []) =
wneuper@59292
   413
    if p > length bs
wneuper@59292
   414
    then raise PTREE "ins_chn: pos not existent"
wneuper@59292
   415
    else
wneuper@59292
   416
      let
wneuper@59292
   417
        val (b', bs') = case nth p bs of
wneuper@59292
   418
          Nd (b', bs') => (b', bs')
wneuper@59292
   419
        | _ => error "ins_chn: uncovered case nth"
wneuper@59292
   420
      in
wneuper@59292
   421
        if null bs'
wneuper@59292
   422
        then Nd (b, repl_app bs p (Nd (b', ns)))
wneuper@59292
   423
        else raise PTREE "ins_chn: pos mustNOT be overwritten"
wneuper@59292
   424
      end
wneuper@59292
   425
  | ins_chn ns (Nd (b, bs)) (p::ps) = Nd (b, repl_app bs p (ins_chn ns (nth p bs) ps));
wneuper@59292
   426
wneuper@59292
   427
(* apply f to obj at pos, f: ppobj -> ppobj *)
wneuper@59292
   428
fun appl_to_node f (Nd (b, bs)) = Nd (f b, bs)
wneuper@59292
   429
  | appl_to_node _ _ = error "appl_to_node: uncovered fun def.";
wneuper@59292
   430
fun appl_obj _ EmptyPtree [] = EmptyPtree
wneuper@59292
   431
  | appl_obj _ EmptyPtree _ = raise PTREE "appl_obj f Empty _"
wneuper@59292
   432
  | appl_obj f (Nd (b, bs)) [] = Nd (f b, bs)
wneuper@59292
   433
  | appl_obj f (Nd (b, bs)) (p :: []) = Nd (b, repl_app bs p (((appl_to_node f) o (nth p)) bs))
wneuper@59292
   434
  | appl_obj f (Nd (b, bs)) (p :: ps) = Nd (b, repl_app bs p (appl_obj f (nth p bs) (ps:pos)));
wneuper@59292
   435
 
wneuper@59292
   436
wneuper@59292
   437
type ocalhd =
wneuper@59292
   438
  bool *                (* ALL itms+preconds true                                              *)
wneuper@59292
   439
  pos_ *                (* model belongs to Problem | Method                                   *)
wneuper@59292
   440
  term *                (* header: Problem... or Cas FIXME.0312: item for marking syntaxerrors *)
wneuper@59405
   441
  Model.itm list *      (* model: given, find, relate                                          *)
wneuper@59292
   442
  ((bool * term) list) *(* model: preconds                                                     *)
wneuper@59405
   443
  Celem.spec;           (* specification                                                       *)
walther@59861
   444
val e_ocalhd = (false, Und, TermC.empty, [Model.e_itm], [(false, TermC.empty)], Celem.e_spec);
wneuper@59292
   445
wneuper@59292
   446
datatype ptform = Form of term | ModSpec of ocalhd;
wneuper@59292
   447
wneuper@59292
   448
(* for cut_level;   (deprecated) *)
wneuper@59292
   449
fun test_trans (PrfObj {branch, ...}) = true andalso branch = TransitiveB
wneuper@59292
   450
  | test_trans (PblObj {branch, ...}) = true andalso branch = TransitiveB;
wneuper@59292
   451
wneuper@59292
   452
fun is_pblobj' pt p =
wneuper@59292
   453
    let val ppobj = get_obj I pt p
wneuper@59292
   454
    in is_pblobj ppobj end;
wneuper@59292
   455
walther@59840
   456
fun del_res (PblObj {fmz, origin, spec, probl, meth, ctxt, loc = (l1, _), branch, ...}) =
walther@59840
   457
    PblObj {fmz = fmz, origin = origin, spec = spec, probl = probl, meth = meth,
walther@59839
   458
	    ctxt = ctxt, loc= (l1, NONE), branch = branch,
walther@59861
   459
	    result = (TermC.empty, []), ostate = Incomplete}
walther@59840
   460
  | del_res (PrfObj {form, tac, loc= (l1, _), branch, ...}) =
walther@59840
   461
    PrfObj {form = form, tac = tac, loc = (l1, NONE), branch = branch, 
walther@59861
   462
	    result = (TermC.empty, []), ostate = Incomplete};
wneuper@59292
   463
wneuper@59292
   464
walther@59846
   465
fun get_loc EmptyPtree _ = (Istate_Def.empty, ContextC.empty)
wneuper@59292
   466
  | get_loc pt (p, Res) =
wneuper@59292
   467
    (case get_obj g_loc pt p of
wneuper@59292
   468
      (SOME i, NONE) => i
walther@59846
   469
    | (NONE  , NONE) => (Istate_Def.empty, ContextC.empty)
wneuper@59292
   470
    | (_     , SOME i) => i)
wneuper@59292
   471
  | get_loc pt (p, _) =
wneuper@59292
   472
    (case get_obj g_loc pt p of
wneuper@59292
   473
      (NONE  , SOME i) => i (*13.8.02 just copied from ^^^: too liberal ?*)
walther@59846
   474
    | (NONE  , NONE) => (Istate_Def.empty, ContextC.empty)
wneuper@59292
   475
    | (SOME i, _) => i);
walther@59807
   476
fun get_istate_LI pt p = get_loc pt p |> #1;
walther@59836
   477
fun get_ctxt_LI pt p = get_loc pt p |> #2;
wneuper@59292
   478
fun get_ctxt pt (pos as (p, p_)) =
wneuper@59292
   479
  if member op = [Frm, Res] p_
wneuper@59292
   480
  then get_loc pt pos |> #2 (*for script interpretation rely on fun get_loc*)
wneuper@59292
   481
  else get_obj g_ctxt pt p (*for specify phase take ctx from PblObj*)
wneuper@59292
   482
walther@59844
   483
fun get_assumptions pt p = get_ctxt pt p |> ContextC.get_assumptions;
wneuper@59292
   484
wneuper@59292
   485
fun get_somespec' (dI, pI, mI) (dI', pI', mI') =
wneuper@59292
   486
  let
walther@59854
   487
    val domID = if dI = ThyC.e_domID then dI' else dI
wneuper@59405
   488
  	val pblID = if pI = Celem.e_pblID then pI' else pI
wneuper@59405
   489
  	val metID = if mI = Celem.e_metID then mI' else mI
wneuper@59292
   490
  in (domID, pblID, metID) end;
wneuper@59292
   491
wneuper@59292
   492
(**.development for extracting an 'interval' from ptree.**)
wneuper@59292
   493
wneuper@59292
   494
(*WN0510 version stopped in favour of get_interval with !!!move_dn, getFormulaeFromTo
wneuper@59292
   495
  actually used (inefficient) version with move_dn: see modspec.sml*)
wneuper@59292
   496
local
wneuper@59292
   497
wneuper@59292
   498
fun hdp [] = 1     | hdp [0] = 1     | hdp x = hd x;(*start with first*)
wneuper@59292
   499
fun hdq	[] = 99999 | hdq [0] = 99999 | hdq x = hd x;(*take until last*)
wneuper@59292
   500
fun tlp [] = [0]     | tlp [_] = [0]     | tlp x = tl x;
wneuper@59292
   501
fun tlq [] = [99999] | tlq [_] = [99999] | tlq x = tl x;
wneuper@59292
   502
wneuper@59292
   503
fun getnd i (b,p) q (Nd (po, nds)) =
wneuper@59292
   504
    (if  i <= 0 then [[b]] else []) @
wneuper@59292
   505
    (getnds (i-1) true (b@[hdp p], tlp p) (tlq q)
wneuper@59292
   506
	   (take_fromto (hdp p) (hdq q) nds))
wneuper@59292
   507
wneuper@59292
   508
and getnds _ _ _ _ [] = []                         (*no children*)
wneuper@59292
   509
  | getnds i _ (b,p) q [nd] = (getnd i (b,p) q nd) (*l+r-margin*)
wneuper@59292
   510
wneuper@59292
   511
  | getnds i true (b,p) q [n1, n2] =               (*l-margin,  r-margin*)
wneuper@59292
   512
    (getnd i      (       b, p ) [99999] n1) @
wneuper@59292
   513
    (getnd ~99999 (lev_on b,[0]) q       n2)
wneuper@59292
   514
wneuper@59292
   515
  | getnds i _    (b,p) q [n1, n2] =               (*intern,  r-margin*)
wneuper@59292
   516
    (getnd i      (       b,[0]) [99999] n1) @
wneuper@59292
   517
    (getnd ~99999 (lev_on b,[0]) q       n2)
wneuper@59292
   518
wneuper@59292
   519
  | getnds i true (b,p) q (nd::(nds as _::_)) =    (*l-margin, intern*)
wneuper@59292
   520
    (getnd i             (       b, p ) [99999] nd) @
wneuper@59292
   521
    (getnds ~99999 false (lev_on b,[0]) q nds)
wneuper@59292
   522
wneuper@59292
   523
  | getnds i _ (b,p) q (nd::(nds as _::_)) =       (*intern, ...*)
wneuper@59292
   524
    (getnd i             (       b,[0]) [99999] nd) @
wneuper@59292
   525
    (getnds ~99999 false (lev_on b,[0]) q nds); 
wneuper@59292
   526
in
wneuper@59292
   527
(*get an 'interval from to' from a ptree as 'intervals f t' of respective nodes
wneuper@59292
   528
  where 'from' are pos, i.e. a key as int list, 'f' an int (to,t analoguous)
wneuper@59292
   529
(1) the 'f' are given 
wneuper@59292
   530
(1a) by 'from' if 'f' = the respective element of 'from' (left margin)
wneuper@59292
   531
(1b) -inifinity, if 'f' > the respective element of 'from' (internal node)
wneuper@59292
   532
(2) the 't' ar given
wneuper@59292
   533
(2a) by 'to' if 't' = the respective element of 'to' (right margin)
wneuper@59292
   534
(2b) inifinity, if 't' < the respective element of 'to (internal node)'
wneuper@59292
   535
the 'f' and 't' are set by hdp,... *)
wneuper@59292
   536
fun get_trace pt p q =
wneuper@59292
   537
    (flat o (getnds ((length p) -1) true ([hdp p], tlp p) (tlq q))) 
wneuper@59292
   538
	(take_fromto (hdp p) (hdq q) (children pt));
wneuper@59292
   539
end;
wneuper@59292
   540
wneuper@59292
   541
(*extract a formula or model from ctree for itms2itemppc or model2xml*)
wneuper@59292
   542
fun preconds2str bts = 
wneuper@59405
   543
  (strs2str o (map (Celem.linefeed o pair2str o
walther@59861
   544
	  (apsnd UnparseC.term2str) o 
wneuper@59292
   545
	  (apfst bool2str)))) bts;
wneuper@59292
   546
fun ocalhd2str (b, p, hdf, itms, prec, spec) =                              (* for tests only *)
walther@59861
   547
    "(" ^ bool2str b ^ ", " ^ pos_2str p ^ ", " ^ UnparseC.term2str hdf ^
walther@59854
   548
    ", " ^ Model.itms2str_ (ThyC.thy2ctxt' "Isac_Knowledge") itms ^
wneuper@59405
   549
    ", " ^ preconds2str prec ^ ", \n" ^ Celem.spec2str spec ^ " )";
wneuper@59292
   550
wneuper@59292
   551
fun is_pblnd (Nd (ppobj, _)) = is_pblobj ppobj
wneuper@59292
   552
  | is_pblnd _ = error "is_pblnd: uncovered fun def.";
wneuper@59292
   553
wneuper@59292
   554
wneuper@59292
   555
(* determine the previous pos' on the same level
wneuper@59292
   556
   WN0502 made for interSteps;  _only_ works for branch TransitiveB WN120517 compare lev_back *)
wneuper@59292
   557
fun lev_pred' _ ([], Res) = ([], Pbl)
wneuper@59292
   558
  | lev_pred' pt (p, Res) =
wneuper@59292
   559
    let val (p', last) = split_last p
wneuper@59292
   560
    in
wneuper@59292
   561
      if last = 1 
wneuper@59292
   562
      then if (is_pblobj o (get_obj I pt)) p then (p, Pbl) else (p, Frm)
wneuper@59292
   563
      else if get_obj g_res pt (p' @ [last - 1]) = get_obj g_form pt p
wneuper@59292
   564
        then (p' @ [last - 1], Res)                                            (* TransitiveB *)
wneuper@59292
   565
        else if (is_pblobj o (get_obj I pt)) p then (p,Pbl) else (p, Frm)
wneuper@59292
   566
    end
wneuper@59292
   567
  | lev_pred' _ _ = error "";
wneuper@59292
   568
wneuper@59292
   569
wneuper@59292
   570
(**.insert into ctree and cut branches accordingly.**)
wneuper@59292
   571
  
wneuper@59292
   572
(* get all positions of certain intervals on the ctree.
walther@59745
   573
   old VERSION without move_dn; kept for occasional redesign
wneuper@59292
   574
   get all pos's to be cut in a ctree
wneuper@59292
   575
   below a pos or from a ctree list after i-th element (NO level_up) *)
wneuper@59292
   576
fun get_allpos' (_, _) EmptyPtree = []
wneuper@59292
   577
  | get_allpos' (p, 1) (Nd (b, bs)) =                                        (* p is pos of Nd *)
wneuper@59292
   578
    if g_ostate b = Incomplete 
wneuper@59292
   579
    then (p, Frm) :: (get_allpos's (p, 1) bs)
wneuper@59292
   580
    else (p, Frm) :: (get_allpos's (p, 1) bs) @ [(p, Res)]
wneuper@59292
   581
  | get_allpos' (p, i) (Nd (b, bs)) =                                        (* p is pos of Nd *)
wneuper@59292
   582
    if length bs > 0 orelse is_pblobj b
wneuper@59292
   583
    then if g_ostate b = Incomplete 
wneuper@59292
   584
      then [(p,Frm)] @ (get_allpos's (p, 1) bs)
wneuper@59292
   585
      else [(p,Frm)] @ (get_allpos's (p, 1) bs) @ [(p, Res)]
wneuper@59292
   586
    else if g_ostate b = Incomplete then [] else [(p, Res)]
wneuper@59292
   587
and get_allpos's _ [] = []
wneuper@59292
   588
  | get_allpos's (p, i) (pt :: pts) =                                 (* p is pos of parent-Nd *)
wneuper@59292
   589
    (get_allpos' (p @ [i], i) pt) @ (get_allpos's (p, i + 1) pts);
wneuper@59292
   590
wneuper@59292
   591
(*WN050106 like cut_level, but deletes exactly 1 node *)
wneuper@59292
   592
fun cut_level_'_  _ _ EmptyPtree _ =raise PTREE "cut_level_'_ Empty _"       (* for tests ONLY *)
wneuper@59292
   593
  | cut_level_'_  _ _ (Nd ( _, _)) ([], _) = raise PTREE "cut_level_'_ _ []"
wneuper@59292
   594
  | cut_level_'_ cuts P (Nd (b, bs)) (p :: [], p_) = 
wneuper@59292
   595
    if test_trans b 
wneuper@59292
   596
    then
walther@59694
   597
      (Nd (b, drop_nth [] (p:Pos.posel, bs)),
wneuper@59292
   598
        cuts @ (if p_ = Frm then [(P @ [p], Res)] else []) @
wneuper@59292
   599
        (get_allpos's (P, p + 1) (drop_nth [] (p, bs))))
wneuper@59292
   600
    else (Nd (b, bs), cuts)
wneuper@59292
   601
  | cut_level_'_ cuts P (Nd (b, bs)) ((p :: ps), p_) =
wneuper@59292
   602
    let
wneuper@59292
   603
      val (bs', cuts') = cut_level_'_ cuts P (nth p bs) (ps, p_)
wneuper@59292
   604
    in (Nd (b, repl_app bs p bs'), cuts @ cuts') end;
wneuper@59292
   605
wneuper@59292
   606
fun cut_level _ _ EmptyPtree _ = raise PTREE "cut_level EmptyPtree _"
wneuper@59292
   607
  | cut_level _ _ (Nd ( _, _)) ([],_) = raise PTREE "cut_level _ []"
wneuper@59292
   608
  | cut_level cuts P (Nd (b, bs)) (p :: [], p_) = 
wneuper@59292
   609
    if test_trans b 
wneuper@59292
   610
    then
walther@59694
   611
      (Nd (b, take (p:Pos.posel, bs)),
wneuper@59292
   612
        cuts @ 
wneuper@59292
   613
        (if p_ = Frm andalso (*#*) g_ostate b = Complete then [(P@[p],Res)] else ([]:pos' list)) @
wneuper@59292
   614
        (get_allpos's (P, p+1) (takerest (p, bs))))
wneuper@59292
   615
    else (Nd (b, bs), cuts)
wneuper@59292
   616
  | cut_level cuts P (Nd (b, bs)) ((p :: ps), p_) =
wneuper@59292
   617
    let
wneuper@59292
   618
      val (bs', cuts') = cut_level cuts P (nth p bs) (ps, p_)
wneuper@59292
   619
    in (Nd (b, repl_app bs p bs'), cuts @ cuts') end;
wneuper@59292
   620
walther@59745
   621
(*old version before WN050219, overwritten below*)
wneuper@59292
   622
fun cut_tree _ ([], _) = raise PTREE "cut_tree _ ([],_)"                      (* for test only *)
wneuper@59292
   623
  | cut_tree pt (pos as ([_], _)) =
wneuper@59292
   624
    let
wneuper@59292
   625
      val (pt', cuts) = cut_level [] [] pt pos
wneuper@59292
   626
    in
wneuper@59292
   627
      (pt', cuts @ (if get_obj g_ostate pt [] = Incomplete  then [] else [([], Res)]))
wneuper@59292
   628
    end
wneuper@59292
   629
  | cut_tree pt (p,p_) =
wneuper@59292
   630
    let	
wneuper@59292
   631
      fun cutfn pt cuts (p, p_) = 
wneuper@59292
   632
	      let
wneuper@59292
   633
	        val (pt', cuts') = cut_level [] (lev_up p) pt (p,p_)
wneuper@59292
   634
	      in
wneuper@59292
   635
	        if length cuts' > 0 andalso length p > 1
wneuper@59292
   636
	        then cutfn pt' (cuts @ cuts') (lev_up p, Frm(*-->(p,Res)*))
wneuper@59292
   637
	        else (pt', cuts @ cuts')
wneuper@59292
   638
	      end
wneuper@59292
   639
	    val (pt', cuts) = cutfn pt [] (p, p_)
wneuper@59292
   640
    in
wneuper@59292
   641
      (pt', cuts @ (if get_obj g_ostate pt [] = Incomplete then [] else [([], Res)]))
wneuper@59292
   642
    end;
wneuper@59292
   643
wneuper@59296
   644
local
wneuper@59296
   645
fun move_dn _ (Nd (_, ns)) ([],p_) =                                            (* root problem *)
wneuper@59296
   646
    (case p_ of 
wneuper@59296
   647
	     Res => raise PTREE "move_dn: end of calculation"
wneuper@59296
   648
	   | _ =>
wneuper@59296
   649
	     if null ns                                                     (* go down from Pbl + Met *)
wneuper@59296
   650
	     then raise PTREE "move_dn: solve problem not started"
wneuper@59296
   651
	     else ([1], Frm))
wneuper@59296
   652
  | move_dn P  (Nd (_, ns)) (p :: (ps as (_ :: _)), p_) =              (* iterate to end of pos *)
wneuper@59296
   653
    if p > length ns
wneuper@59296
   654
    then raise PTREE "move_dn: pos not existent 2"
wneuper@59296
   655
    else move_dn (P @ [p]) (nth p ns) (ps, p_)
wneuper@59296
   656
  | move_dn P (Nd (c, ns)) ([p], p_) =                            (* act on last element of pos *)
wneuper@59296
   657
    if p > length ns
wneuper@59296
   658
    then raise PTREE "move_dn: pos not existent 3"
wneuper@59296
   659
    else
wneuper@59296
   660
      (case p_ of 
wneuper@59296
   661
	      Res => 
wneuper@59296
   662
	      if p = length ns                               (* last Res on this level: go a level up *)
wneuper@59296
   663
	      then if g_ostate c = Complete
wneuper@59296
   664
	        then (P, Res)
wneuper@59296
   665
	        else raise PTREE (ints2str' P ^ " not complete 1")
wneuper@59296
   666
	     else                        (* go to the next Nd on this level, or down into the next Nd *)
wneuper@59296
   667
		     if is_pblnd (nth (p + 1) ns) then (P@[p + 1], Pbl)
wneuper@59296
   668
		     else  if g_res' (nth p ns) = g_form' (nth (p + 1) ns)
wneuper@59296
   669
		       then if (null o children o (nth (p + 1))) ns
wneuper@59296
   670
			       then                                                   (* take the Res if Complete *) 
wneuper@59296
   671
			         if g_ostate' (nth (p + 1) ns) = Complete 
wneuper@59296
   672
			         then (P@[p + 1], Res)
wneuper@59296
   673
			         else raise PTREE (ints2str' (P@[p + 1]) ^ " not complete 2")
wneuper@59296
   674
			       else (P@[p + 1, 1], Frm)                           (* go down into the next PrfObj *)
wneuper@59296
   675
		       else (P@[p + 1], Frm)                           (* take Frm: exists if the Nd exists *)
wneuper@59296
   676
	   | Frm => (*go down or to the Res of this Nd*)
wneuper@59296
   677
	     if (null o children o (nth p)) ns
wneuper@59296
   678
	     then if g_ostate' (nth p ns) = Complete then (P @ [p], Res)
wneuper@59296
   679
		     else raise PTREE (ints2str' (P @ [p])^" not complete 3")
wneuper@59296
   680
	     else (P @ [p, 1], Frm)
wneuper@59296
   681
	   | _ =>                                                                    (* is Pbl or Met *)
wneuper@59296
   682
	     if (null o children o (nth p)) ns
wneuper@59296
   683
	     then raise PTREE "move_dn:solve subproblem not startd"
wneuper@59296
   684
	     else (P @ [p, 1], 
wneuper@59296
   685
		   if (is_pblnd o hd o children o (nth p)) ns
wneuper@59296
   686
		   then Pbl else Frm))
wneuper@59296
   687
  | move_dn _ _ _ = error "";
wneuper@59296
   688
in
wneuper@59292
   689
(* get all positions in a ctree until ([],Res) or ostate=Incomplete
wneuper@59292
   690
val get_allp = fn : 
wneuper@59292
   691
  pos' list -> : accumulated, start with []
wneuper@59292
   692
  pos ->       : the offset for subtrees wrt the root
wneuper@59292
   693
  ctree ->     : (sub)tree
wneuper@59292
   694
  pos'         : initialization (the last pos' before ...)
wneuper@59292
   695
  -> pos' list : of positions in this (sub) tree (relative to the root)
wneuper@59292
   696
*)
wneuper@59292
   697
fun get_allp cuts (P, pos) pt =
wneuper@59292
   698
  (let
wneuper@59292
   699
    val nxt = move_dn [] pt pos (*exn if Incomplete reached*)
wneuper@59292
   700
  in
wneuper@59292
   701
    if nxt <> ([], Res) 
wneuper@59292
   702
    then get_allp (cuts @ [nxt]) (P, nxt) pt
wneuper@59292
   703
    else map (apfst (curry op @ P)) (cuts @ [nxt])
wneuper@59292
   704
  end)
wneuper@59292
   705
  handle PTREE _ => (map (apfst (curry op@ P)) cuts);
wneuper@59296
   706
end
wneuper@59292
   707
wneuper@59292
   708
(* the pts are assumed to be on the same level *)
wneuper@59292
   709
fun get_allps cuts _ [] = cuts
wneuper@59292
   710
  | get_allps cuts P (pt :: pts) =
wneuper@59292
   711
    let
wneuper@59292
   712
      val below = get_allp [] (P, ([], Frm)) pt
wneuper@59292
   713
      val levfrm = 
wneuper@59292
   714
	      if is_pblnd pt 
wneuper@59292
   715
	      then (P, Pbl) :: below
wneuper@59292
   716
	      else if last_elem P = 1 
wneuper@59292
   717
	        then (P, Frm) :: below
wneuper@59292
   718
	        else (*Trans*) below
wneuper@59292
   719
	    val levres = levfrm @ (if null below then [(P, Res)] else [])
wneuper@59292
   720
    in
wneuper@59292
   721
      get_allps (cuts @ levres) (lev_on P) pts
wneuper@59292
   722
    end;
wneuper@59292
   723
wneuper@59292
   724
(** these 2 funs decide on how far cut_tree goes **)
wneuper@59292
   725
(* shall the nodes _after_ the pos to be inserted at be deleted?
wneuper@59292
   726
   shall cutting be continued on the higher level(s)? the Nd regarded will NOT be changed *)
wneuper@59292
   727
fun test_trans (PrfObj {branch, ...}) = (branch = TransitiveB orelse branch = NoBranch)
wneuper@59292
   728
  | test_trans (PblObj {branch, ...}) = (branch = TransitiveB orelse branch = NoBranch);
wneuper@59292
   729
    
wneuper@59292
   730
(* cut_bottom new sml603..608
wneuper@59292
   731
cut the level at the bottom of the pos (used by cappend_...)
wneuper@59292
   732
and handle the parent in order to avoid extra case for root
wneuper@59292
   733
fn: ctree ->         : the _whole_ ctree for cut_levup
walther@59694
   734
    pos * Pos.posel ->   : the pos after split_last
wneuper@59292
   735
    ctree ->         : the parent of the Nd to be cut
wneuper@59292
   736
return
wneuper@59292
   737
    (ctree *         : the updated ctree
wneuper@59292
   738
     pos' list) *    : the pos's cut
wneuper@59292
   739
     bool            : cutting shall be continued on the higher level(s)
wneuper@59292
   740
*)
wneuper@59292
   741
fun cut_bottom _ (pt' as Nd (b, [])) = ((pt', []), test_trans b)
wneuper@59292
   742
  | cut_bottom (P, p) (Nd (b, bs)) =
wneuper@59292
   743
    let (*divide level into 3 parts...*)
wneuper@59292
   744
    	val keep = take (p - 1, bs)
wneuper@59292
   745
    	val pt' = case nth p bs of
wneuper@59292
   746
    	  pt' as Nd _ => pt'
wneuper@59292
   747
    	| _ => error "cut_bottom: uncovered case nth p bs"
wneuper@59292
   748
    	(*^^^^^_here_ will be 'insert_pt'ed by 'append_..'*)
wneuper@59292
   749
    	val (tail, _) = (takerest (p, bs), if null (takerest (p, bs)) then 0 else p + 1)
wneuper@59292
   750
    	val (children, cuts) = 
wneuper@59292
   751
    	  if test_trans b
wneuper@59292
   752
    	  then
wneuper@59292
   753
    	   (keep, (if is_pblnd pt' then [(P @ [p], Pbl)] else [])
wneuper@59292
   754
    	     @ (get_allp  [] (P @ [p], (P, Frm)) pt')
wneuper@59292
   755
    	     @ (get_allps [] (P @ [p + 1]) tail))
wneuper@59292
   756
    	  else (keep @ [(*'insert_pt'ed by 'append_..'*)] @ tail,
wneuper@59292
   757
    		get_allp  [] (P @ [p], (P, Frm)) pt')
wneuper@59292
   758
    	val (pt'', cuts) = 
wneuper@59292
   759
    	  if test_trans b
wneuper@59292
   760
    	  then (Nd (del_res b, children), cuts @ (if g_ostate b = Incomplete then [] else [(P, Res)]))
wneuper@59292
   761
    	  else (Nd (b, children), cuts)
wneuper@59292
   762
    in ((pt'', cuts), test_trans b) end
wneuper@59292
   763
  | cut_bottom _ _ = error "cut_bottom: uncovered fun def.";
wneuper@59292
   764
wneuper@59292
   765
wneuper@59292
   766
(* go all levels from the bottom of 'pos' up to the root, 
wneuper@59292
   767
 on each level compose the children of a node and accumulate the cut Nds
wneuper@59292
   768
args
wneuper@59292
   769
   pos' list ->      : for accumulation
wneuper@59292
   770
   bool -> 	     : cutting shall be continued on the higher level(s)
wneuper@59292
   771
   ctree -> 	     : the whole ctree for 'get_nd pt P' on each level
wneuper@59292
   772
   ctree -> 	     : the Nd from the lower level for insertion at path
walther@59694
   773
   pos * Pos.posel ->    : pos=path split for convenience
wneuper@59292
   774
   ctree -> 	     : Nd the children of are under consideration on this call 
wneuper@59292
   775
returns		     :
wneuper@59292
   776
   ctree * pos' list : the updated parent-Nd and the pos's of the Nds cut
wneuper@59292
   777
*)
walther@59694
   778
fun cut_levup (cuts:pos' list) clevup pt pt' (P:pos, p:Pos.posel) (Nd (b, bs)) =
wneuper@59292
   779
    let (*divide level into 3 parts...*)
wneuper@59292
   780
    	val keep = take (p - 1, bs)
wneuper@59292
   781
    	(*val pt' comes as argument from below*)
wneuper@59292
   782
    	val (tail, _) =
wneuper@59292
   783
    	 (takerest (p, bs), if null (takerest (p, bs)) then 0 else p + 1)
wneuper@59292
   784
    	val (children, cuts') = 
wneuper@59292
   785
    	  if clevup
wneuper@59292
   786
    	  then (keep @ [pt'], get_allps [] (P @ [p+1]) tail)
wneuper@59292
   787
    	  else (keep @ [pt'] @ tail, [])
wneuper@59292
   788
    	val clevup' = if clevup then test_trans b else false 
wneuper@59292
   789
    	(*the first Nd with false stops cutting on all levels above*)
wneuper@59292
   790
    	val (pt'', cuts') = 
wneuper@59292
   791
    	  if clevup'
wneuper@59292
   792
    	  then (Nd (del_res b, children), cuts' @ (if g_ostate b = Incomplete then [] else [(P, Res)]))
wneuper@59292
   793
    	  else (Nd (b, children), cuts')
wneuper@59292
   794
    in
wneuper@59292
   795
      if null P
wneuper@59292
   796
      then (pt'', cuts @ cuts')
wneuper@59292
   797
      else
wneuper@59292
   798
        let val (P, p) = split_last P
wneuper@59292
   799
        in cut_levup (cuts @ cuts') clevup' pt pt'' (P, p) (get_nd pt P) end
wneuper@59292
   800
    end
wneuper@59292
   801
  | cut_levup _ _ _ _ _ _ = error "cut_levup: uncovered fun def.";
wneuper@59292
   802
 
wneuper@59292
   803
(* cut nodes after and below an inserted node in the ctree;
wneuper@59292
   804
   the cuts range is limited by the predicate 'fun cutlevup' *)
wneuper@59292
   805
fun cut_tree pt (pos, _) =
wneuper@59292
   806
  if not (existpt pos pt) 
walther@59820
   807
  then (pt,  []) (*appending a formula never cuts anything*)
wneuper@59292
   808
  else
wneuper@59292
   809
    let
wneuper@59292
   810
      val (P, p) = split_last pos
wneuper@59292
   811
      val ((pt', cuts), clevup) = cut_bottom (P, p) (get_nd pt P)
walther@59820
   812
         (* pt' is the updated parent of the Nd to cappend_..*)
wneuper@59292
   813
    in
wneuper@59292
   814
      if null P
wneuper@59292
   815
      then (pt', cuts)
wneuper@59292
   816
      else
wneuper@59292
   817
        let val (P, p) = split_last P
wneuper@59292
   818
        in cut_levup cuts clevup pt pt' (P, p) (get_nd pt P) end
wneuper@59292
   819
	  end;
wneuper@59292
   820
wneuper@59292
   821
(* get the theory explicitly specified for the rootpbl;
wneuper@59292
   822
   thus use this function _after_ finishing specification *)
wneuper@59405
   823
fun rootthy (Nd (PblObj {spec = (thyID, _, _), ...}, _)) = Celem.assoc_thy thyID
wneuper@59292
   824
  | rootthy _ = error "rootthy: uncovered fun def.";
wneuper@59292
   825
wneuper@59292
   826
(**)
wneuper@59292
   827
end;
wneuper@59292
   828
(**)