src/Tools/isac/BaseDefinitions/store.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 20 Apr 2020 15:54:19 +0200
changeset 59892 b8cfae027755
parent 59891 68396aa5821f
child 59897 8cba439d0454
permissions -rw-r--r--
separate Check_Unique, an exercise in higher order funs
walther@59891
     1
(* Title:  BaseDefinitions/store.sml
walther@59882
     2
   Author: Walther Neuper
walther@59882
     3
   (c) due to copyright terms
walther@59882
     4
walther@59891
     5
A tree for storing collections of Problem, Method and Thy_Html.
walther@59891
     6
*)
walther@59890
     7
signature STORE_TREE =
walther@59882
     8
sig
walther@59892
     9
  type key
walther@59884
    10
  datatype 'a ptyp = Ptyp of string * 'a list * 'a ptyp list
walther@59892
    11
(*vvv merge *)
walther@59884
    12
  val merge_ptyps: 'a ptyp list * 'a ptyp list -> 'a ptyp list
walther@59892
    13
(*vvv insert *)
walther@59892
    14
  val insrt: key -> 'a -> string list -> 'a ptyp list -> 'a ptyp list
walther@59892
    15
(*vvv get *)
walther@59892
    16
  val get_py: 'a ptyp list -> key -> (*rev*)key -> 'a
walther@59892
    17
(*vvv update *)
walther@59892
    18
  val update_ptyps: key -> string list -> 'a -> 'a ptyp list -> 'a ptyp list
walther@59892
    19
(*vvv apply *)
walther@59892
    20
  val app_py: 'a ptyp list -> ('a ptyp -> 'b) -> key -> (*rev*)key -> 'b
walther@59882
    21
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59882
    22
  (*NONE*)
walther@59886
    23
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59882
    24
  (*NONE*)
walther@59886
    25
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59882
    26
end
walther@59882
    27
walther@59882
    28
(**)
walther@59890
    29
structure Store(**): STORE_TREE(**) =
walther@59882
    30
struct
walther@59882
    31
(**)
walther@59882
    32
walther@59892
    33
type key = string list; (*will become pblID, methID, theID*)
walther@59890
    34
(*/------- to Store -------\*)
walther@59884
    35
(* A tree for storing data defined in different theories 
walther@59884
    36
  for access from the Interpreter and from dialogue authoring 
walther@59884
    37
  using a string list as key.
walther@59884
    38
  'a is for pbt | met | thydata; after WN030424 naming "pbt" became inappropriate *)
walther@59884
    39
datatype 'a ptyp =
walther@59884
    40
	Ptyp of string *  (* element of the key                                                        *)
walther@59884
    41
		'a list *       (* several pbts with different domIDs/thy TODO: select by subthy (isaref.p.69)
walther@59884
    42
			                 presently only _ONE_ elem FOR ALL KINDS OF CONTENT pbt | met | thydata    *)
walther@59884
    43
		('a ptyp) list; (* the children nodes                                                        *)
walther@59884
    44
walther@59884
    45
(* this function only works wrt. the way Isabelle evaluates Theories and is not a general merge
walther@59884
    46
  function for trees / ptyps *)
walther@59884
    47
fun merge_ptyps ([], pt) = pt
walther@59884
    48
  | merge_ptyps (pt, []) = pt
walther@59884
    49
  | merge_ptyps ((x' as Ptyp (k, _, ps)) :: xs, (xs' as Ptyp (k', y, ps') :: ys)) =
walther@59884
    50
      if k = k'
walther@59884
    51
      then Ptyp (k, y, merge_ptyps (ps, ps')) :: merge_ptyps (xs, ys)
walther@59884
    52
      else x' :: merge_ptyps (xs, xs');
walther@59884
    53
walther@59884
    54
fun insrt _ pbt [k] [] = [Ptyp (k, [pbt], [])]
walther@59884
    55
  | insrt d pbt [k] ((Ptyp (k', [p], ps)) :: pys) =
walther@59884
    56
    ((*writeln ("### insert 1: ks = " ^ strs2str [k] ^ "    k'= " ^ k');*)
walther@59884
    57
    if k = k'
walther@59884
    58
    then ((Ptyp (k', [pbt], ps)) :: pys)
walther@59884
    59
    else  ((Ptyp (k', [p], ps)) :: (insrt d pbt [k] pys))
walther@59884
    60
    )			 
walther@59884
    61
  | insrt d pbt (k::ks) ((Ptyp (k', [p], ps)) :: pys) =
walther@59884
    62
    ((*writeln ("### insert 2: ks = "^(strs2str (k::ks))^"    k'= "^k');*)
walther@59884
    63
    if k = k'
walther@59884
    64
    then ((Ptyp (k', [p], insrt d pbt ks ps)) :: pys)
walther@59884
    65
    else 
walther@59884
    66
      if length pys = 0
walther@59884
    67
      then error ("insert: not found " ^ (strs2str (d(* : pblID*))))
walther@59884
    68
      else ((Ptyp (k', [p], ps)) :: (insrt d pbt (k :: ks) pys))
walther@59884
    69
    )
walther@59884
    70
  | insrt _ _ _ _ = raise ERROR "";
walther@59884
    71
walther@59884
    72
fun app_py p f (d(*:pblID*)) (k(*:pblRD*)) =
walther@59884
    73
  let
walther@59884
    74
    fun py_err _ = raise ERROR ("app_py: not found: " ^ strs2str d);
walther@59884
    75
    fun app_py' _ [] = py_err ()
walther@59884
    76
      | app_py' [] _ = py_err ()
walther@59884
    77
      | app_py' [k0] ((p' as Ptyp (k', _, _  )) :: ps) =
walther@59884
    78
        if k0 = k' then f p' else app_py' [k0] ps
walther@59884
    79
      | app_py' (k' as (k0 :: ks)) (Ptyp (k'', _, ps) :: ps') =
walther@59884
    80
        if k0 = k'' then app_py' ks ps else app_py' k' ps';
walther@59884
    81
  in app_py' k p end;
walther@59884
    82
fun get_py p =
walther@59884
    83
  let
walther@59884
    84
    fun extract_py (Ptyp (_, [py], _)) = py
walther@59884
    85
      | extract_py _ = raise ERROR ("extract_py: Ptyp has wrong format.");
walther@59884
    86
  in app_py p extract_py end;
walther@59884
    87
walther@59884
    88
fun update_ptyps ID _ _ [] =
walther@59884
    89
    error ("update_ptyps: " ^ strs2str' ID ^ " does not exist")
walther@59884
    90
  | update_ptyps ID [i] data ((py as Ptyp (key, _, pys)) :: pyss) =
walther@59884
    91
    if i = key
walther@59884
    92
    then 
walther@59884
    93
      if length pys = 0
walther@59884
    94
      then ((Ptyp (key, [data], [])) :: pyss)
walther@59884
    95
      else error ("update_ptyps: " ^ strs2str' ID ^ " has descendants")
walther@59884
    96
    else py :: update_ptyps ID [i] data pyss
walther@59884
    97
  | update_ptyps ID (i :: is) data ((py as Ptyp (key, d, pys)) :: pyss) =
walther@59884
    98
    if i = key
walther@59884
    99
    then ((Ptyp (key,  d, update_ptyps ID is data pys)) :: pyss)
walther@59884
   100
    else (py :: (update_ptyps ID (i :: is) data pyss))
walther@59884
   101
  | update_ptyps _ _ _ _ = raise ERROR "update_ptyps called with undef pattern.";
walther@59890
   102
(*\------- to Store -------/*)
walther@59882
   103
walther@59882
   104
(**)end(**)