src/Tools/isac/BaseDefinitions/rule-set.sml
author Walther Neuper <walther.neuper@jku.at>
Fri, 17 Apr 2020 18:47:29 +0200
changeset 59885 59c5dd27d589
parent 59879 33449c96d99f
child 59886 106e7d8723ca
permissions -rw-r--r--
Test_Isac_Short OK (except the 2 strange errors)
walther@59866
     1
(* Title:  BaseDefinitions/rule-set.sml
walther@59850
     2
   Author: Walther Neuper
walther@59850
     3
   (c) due to copyright terms
walther@59850
     4
walther@59850
     5
In ISAC a Rule_Set serves rewriting. KEStore holds all rule-sets for ubiquitous access.
walther@59850
     6
*)
walther@59850
     7
signature RULE_SET =
walther@59850
     8
sig
walther@59851
     9
  datatype T = datatype Rule_Def.rule_set
walther@59867
    10
  eqtype id
walther@59850
    11
walther@59867
    12
  val id: T -> string
walther@59852
    13
  val rep: T -> {calc: Rule_Def.calc list, erls: T, errpats: Rule_Def.errpatID list, id: string,
walther@59857
    14
    preconds: term list, rew_ord: Rewrite_Ord.rew_ord, rules: Rule_Def.rule list, scr: Rule_Def.program, srls: T}
walther@59852
    15
walther@59852
    16
  val append_rules: string -> T -> Rule_Def.rule list -> T
walther@59852
    17
  val keep_unique_rules: string -> T ->  Rule_Def.rule list -> T
walther@59852
    18
  val merge: string -> T -> T -> T
walther@59852
    19
  val get_rules: T -> Rule_Def.rule list
walther@59852
    20
walther@59852
    21
  type for_kestore
walther@59852
    22
  val equal: (''a * ('b * 'c)) * (''a * ('d * 'e)) -> bool
walther@59852
    23
  val to_kestore: for_kestore list * for_kestore list -> for_kestore list
walther@59852
    24
walther@59869
    25
(*/------- this will disappear eventually ----------------------------------------------------\*)
walther@59852
    26
  val empty: T
walther@59850
    27
  type rrlsstate = term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list
walther@59850
    28
  val e_rrlsstate: rrlsstate
walther@59851
    29
  val e_rrls: T
walther@59869
    30
(*\------- this will disappear eventually ----------------------------------------------------/*)
walther@59850
    31
walther@59850
    32
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59850
    33
  (*NONE*)
walther@59885
    34
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\*)
walther@59852
    35
  val insert_merge: for_kestore -> for_kestore list -> for_kestore list
walther@59885
    36
(*\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59850
    37
end
walther@59852
    38
walther@59850
    39
(**)
walther@59850
    40
structure Rule_Set(**): RULE_SET(**) =
walther@59850
    41
struct
walther@59850
    42
(**)
walther@59867
    43
datatype T = datatype Rule_Def.rule_set;
walther@59867
    44
type id = string;
walther@59850
    45
walther@59852
    46
(*/------- this will disappear eventually ----------------------------------------------------\*)
walther@59852
    47
fun dummy_ord (_: (term * term) list) (_: term, _: term) = true;
walther@59852
    48
val empty =
walther@59852
    49
  Rule_Def.Repeat {id = "empty", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59878
    50
    srls = Rule_Def.Empty, calc = [], rules = [], errpatts = [], scr = Rule_Def.Empty_Prog}
walther@59852
    51
(*\------- this will disappear eventually ----------------------------------------------------/*)
walther@59850
    52
walther@59867
    53
val id = Rule.set_id;
walther@59852
    54
fun rep Rule_Def.Empty = rep empty
walther@59852
    55
  | rep (Rule_Def.Repeat {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    56
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    57
      calc = calc, rules = rules, scr = scr}
walther@59878
    58
  | rep (Rule_Def.Sequence {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    59
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    60
      calc = calc, rules = rules, scr = scr}
walther@59852
    61
  | rep (Rule_Def.Rrls _)  = rep empty
walther@59850
    62
walther@59852
    63
fun append_rules id Rule_Def.Empty _ = raise ERROR ("append_rules: with \"" ^ id ^ "\" not for Rule_Def.Empty")
walther@59852
    64
  | append_rules id (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    65
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    66
    Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    67
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59878
    68
  | append_rules id (Rule_Def.Sequence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    69
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59878
    70
    Rule_Def.Sequence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    71
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    72
  | append_rules id (Rule_Def.Rrls _) _ = raise ERROR ("append_rules: not for reverse-rewrite-rule-set " ^ id);
walther@59852
    73
walther@59852
    74
fun merge_ids rls1 rls2 =
walther@59852
    75
  let 
walther@59852
    76
    val id1 = (#id o rep) rls1
walther@59852
    77
    val id2 = (#id o rep) rls2
walther@59852
    78
  in
walther@59852
    79
    if id1 = id2 then id1 else "merged_" ^ id1 ^ "_" ^ id2
walther@59852
    80
  end
walther@59852
    81
walther@59852
    82
fun merge _ Rule_Def.Empty rls = rls
walther@59852
    83
  | merge _ rls Rule_Def.Empty = rls
walther@59852
    84
  | merge _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
walther@59852
    85
  | merge _ _ (Rrls x) = Rrls x
walther@59852
    86
  | merge id
walther@59852
    87
	  (Rule_Def.Repeat {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
    88
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
    89
	  (Rule_Def.Repeat {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
    90
	    rules = rs2, errpatts = eps2, ...})
walther@59852
    91
    =
walther@59852
    92
	  Rule_Def.Repeat {id = id, rew_ord = ro1, scr = sc1,
walther@59852
    93
	    preconds = union (op =) pc1 pc2,	    
walther@59852
    94
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
    95
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
    96
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59867
    97
		  rules = union Rule.equal rs1 rs2,
walther@59852
    98
      errpatts = union (op =) eps1 eps2}
walther@59852
    99
  | merge id
walther@59878
   100
	  (Rule_Def.Sequence {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
   101
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59878
   102
	  (Rule_Def.Sequence {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
   103
	    rules = rs2, errpatts = eps2, ...})
walther@59852
   104
    =
walther@59878
   105
	  Rule_Def.Sequence {id = id, rew_ord = ro1, scr = sc1,
walther@59852
   106
	    preconds = union (op =) pc1 pc2,	    
walther@59852
   107
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
   108
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
   109
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59867
   110
		  rules = union Rule.equal rs1 rs2,
walther@59852
   111
      errpatts = union (op =) eps1 eps2}
walther@59852
   112
  | merge id _ _ = error ("merge: \"" ^ id ^ 
walther@59878
   113
    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Rule_Def.Sequence");
walther@59852
   114
walther@59850
   115
(* datastructure for KEStore_Elems, intermediate for thehier *)
walther@59852
   116
type for_kestore = 
walther@59867
   117
  (id *     (* id unique within Isac *)
walther@59879
   118
  (ThyC.id *  (* just for assignment in thehier, not appropriate for parsing etc *)
walther@59867
   119
    T))     (* ((#id o rep) T) = id   by coding discipline *)
walther@59852
   120
fun equal ((id1, (_, _)), (id2, (_, _))) = id1 = id2
walther@59850
   121
walther@59852
   122
fun insert_merge (re as (id, (thyID, r1))) ys = 
walther@59852
   123
    case get_index (fn y => if curry equal re y then SOME y else NONE) ys of
walther@59850
   124
      NONE => re :: ys
walther@59850
   125
    | SOME (i, (_, (_, r2))) => 
walther@59850
   126
      let
walther@59852
   127
        val r12 = merge id r1 r2
walther@59850
   128
      in list_update ys i (id, (thyID, r12)) end
walther@59852
   129
fun to_kestore (s1, s2) = fold insert_merge s1 s2;
walther@59850
   130
walther@59852
   131
(* used only for one hack *)
walther@59852
   132
fun keep_unique_rules id 
walther@59852
   133
     (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   134
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   135
      Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59867
   136
  	    rules = gen_rems Rule.equal (rs, r),
walther@59852
   137
  	    errpatts = eps,
walther@59852
   138
  	    scr = sc}
walther@59852
   139
  | keep_unique_rules id
walther@59878
   140
     (Rule_Def.Sequence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   141
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59878
   142
      Rule_Def.Sequence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59867
   143
  	    rules = gen_rems Rule.equal (rs, r),
walther@59852
   144
  	    errpatts = eps,
walther@59852
   145
  	    scr = sc}
walther@59852
   146
  | keep_unique_rules id (Rule_Def.Rrls _) _ = raise ERROR ("keep_unique_rules: not for reverse-rewrite-rule-set "^id)
walther@59867
   147
  | keep_unique_rules _ rls _ = raise ERROR ("remove_rls called with " ^ id rls);
walther@59850
   148
walther@59851
   149
fun get_rules Rule_Def.Empty = []
walther@59851
   150
  | get_rules (Rule_Def.Repeat {rules, ...}) = rules
walther@59878
   151
  | get_rules (Rule_Def.Sequence {rules, ...}) = rules
walther@59851
   152
  | get_rules (Rule_Def.Rrls _) = [];
walther@59850
   153
walther@59863
   154
(*/------- this will disappear eventually -----------\*)
walther@59863
   155
type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
walther@59863
   156
  (term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list);
walther@59868
   157
val e_rrlsstate = (UnparseC.term_empty, UnparseC.term_empty, [[Rule_Def.Erule]], [(Rule_Def.Erule, (UnparseC.term_empty, []))]) : rrlsstate;
walther@59863
   158
local
walther@59863
   159
    fun ii (_: term) = e_rrlsstate;
walther@59868
   160
    fun no (_: term) = SOME (UnparseC.term_empty, [UnparseC.term_empty]);
walther@59868
   161
    fun lo (_: Rule_Def.rule list list) (_: term) (_: Rule_Def.rule) = [(Rule_Def.Erule, (UnparseC.term_empty, [UnparseC.term_empty]))];
walther@59863
   162
    fun ne (_: Rule_Def.rule list list) (_: term) = SOME Rule_Def.Erule;
walther@59868
   163
    fun fo (_: Rule_Def.rule list list) (_: term) (_: term) = [(Rule_Def.Erule, (UnparseC.term_empty, [UnparseC.term_empty]))];
walther@59863
   164
in
walther@59863
   165
val e_rfuns = Rule_Def.Rfuns {init_state = ii, normal_form = no, locate_rule = lo,
walther@59863
   166
		  next_rule = ne, attach_form = fo};
walther@59863
   167
end;
walther@59863
   168
val e_rrls =
walther@59863
   169
  Rule_Def.Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59863
   170
    calc = [], errpatts = [], scr = e_rfuns}
walther@59863
   171
(*\------- this will disappear eventually -----------/*)
walther@59863
   172
walther@59850
   173
walther@59850
   174
(**)end(**)