src/sml/definitions.sml
author agriesma
Thu, 17 Apr 2003 18:01:03 +0200
branchgriesmayer
changeset 338 90390fecbe74
parent 308 93f297edc4a5
child 340 b0b16b29b497
child 693 37385e57c036
permissions -rw-r--r--
neues cvs-verzeichnis
agriesma@308
     1
(* data-definitions, which should not occur in code-files to be re-evaluated
agriesma@308
     2
   use"definitions.sml";
agriesma@308
     3
   *)
agriesma@308
     4
agriesma@308
     5
type cterm' = string;
agriesma@308
     6
val empty_cterm' = "empty_cterm'";
agriesma@308
     7
type thmID = string;
agriesma@308
     8
type thm' = thmID * cterm';
agriesma@308
     9
type rls' = string; 
agriesma@308
    10
agriesma@308
    11
(*.fun calculate_ fetches the evaluation-function via this list.*)
agriesma@308
    12
(*ie. there is NO rewriting involved in contrary to eval_rls*)
agriesma@308
    13
type calc =
agriesma@308
    14
     (string *     (*op in calculate_ from scripts*)
agriesma@308
    15
      (string *    (*op in isa-term 'Const(op,_)' *)
agriesma@308
    16
       (string -> term -> theory -> (string * term) option))); (*eval_fun*) 
agriesma@308
    17
fun e_evalfn (_:'a) (_:term) (_:theory) = None:(string * term) option;
agriesma@308
    18
agriesma@308
    19
type subs' = (cterm' * cterm') list; (*16.11.00 for FE-KE*)
agriesma@308
    20
type subst = (term * term) list; (*here for ets2str*)
agriesma@308
    21
fun subst2str (s:subst) = 
agriesma@308
    22
  (strs2str o 
agriesma@308
    23
   (map (pair2str o
agriesma@308
    24
	 (apsnd (Sign.string_of_term (sign_of thy))) o 
agriesma@308
    25
	 (apfst (Sign.string_of_term (sign_of thy)))))) s;
agriesma@308
    26
agriesma@308
    27
agriesma@308
    28
agriesma@308
    29
datatype rule = 
agriesma@308
    30
  Thm of (string * thm) 
agriesma@308
    31
| Calc of string *      (*check for equality*)
agriesma@308
    32
	  (string -> term -> theory -> (string * term) option)
agriesma@308
    33
| Rls_ of rls         (*.ie. rule sets may be nested.*)
agriesma@308
    34
and scr = 
agriesma@308
    35
    EmptyScr 
agriesma@308
    36
  | Script of term (*for meth'*)
agriesma@308
    37
  | Rfuns of {init_state : term -> 
agriesma@308
    38
     (term *          (*the current formula: 
agriesma@308
    39
                        goes locate_gen -> next_tac via istate*)
agriesma@308
    40
      term *          (*the final formula*)
agriesma@308
    41
      rule list       (*of reverse rewrite set (#1#)*)
agriesma@308
    42
	    list *    (*may be serveral, eg. in norm_rational*)
agriesma@308
    43
      (rule *         (*Thm (+ Thm generated from Calc) resulting in ...*)
agriesma@308
    44
       (term *        (*... rewrite with ...*)
agriesma@308
    45
	term list))   (*... assumptions*)
agriesma@308
    46
	  list),      (*derivation from given term to normalform
agriesma@308
    47
		       in reverse order with sym_thm; 
agriesma@308
    48
                       (#1#) could be extracted from here #1*)
agriesma@308
    49
agriesma@308
    50
	      normal_form: term -> (term * term list) option,
agriesma@308
    51
	      locate_rule: rule list list -> term -> rule 
agriesma@308
    52
			   -> (rule * (term * term list)) list,
agriesma@308
    53
	      next_rule  : rule list list -> term -> rule option,
agriesma@308
    54
	      attach_form: rule list list -> term -> term 
agriesma@308
    55
			   -> (rule * (term * term list)) list}
agriesma@308
    56
and rls =
agriesma@308
    57
    Erls                          (*for init e_rls*)
agriesma@308
    58
  | Rls of {id : string, (*for trace_rewrite:=true*)
agriesma@308
    59
	    preconds : term list, (*unused 20.8.02*)
agriesma@308
    60
	    rew_ord  : string * (subst -> (term * term) -> bool),(*for rules*)
agriesma@308
    61
	    erls     : rls,      (*for the conditions in rules*)
agriesma@308
    62
	    (*            '^ because of rewrite in applicable_in;
agriesma@308
    63
		compare type meth'; FIXXME take name as string into Rls !*)
agriesma@308
    64
	    srls     : rls,       (*for evaluation of list_fns in script*)
agriesma@308
    65
	    calc     : calc list, (*for Calculate in scr*)
agriesma@308
    66
	    asm_thm  : thm' list, (*FIXXME remove with put_asm*)
agriesma@308
    67
	    rules    : rule list,
agriesma@308
    68
	    scr      : scr}  (*Script term  (how to restrict type ???)*)
agriesma@308
    69
  | Seq of {id : string, (*for trace_rewrite:=true*)
agriesma@308
    70
	    preconds : term list, (*unused 20.8.02*)
agriesma@308
    71
	    rew_ord  : string * (subst -> (term * term) -> bool),(*for rules*)
agriesma@308
    72
	    erls     : rls,      (*for the conditions in rules*)
agriesma@308
    73
	    (*            '^ because of rewrite in applicable_in;
agriesma@308
    74
		compare type meth'; FIXXME take name as string into Rls !*)
agriesma@308
    75
	    srls     : rls,       (*for evaluation of list_fns in script*)
agriesma@308
    76
	    calc     : calc list, (*for Calculate in scr*)
agriesma@308
    77
	    asm_thm  : thm' list, (*FIXXME remove with put_asm*)
agriesma@308
    78
	    rules    : rule list,
agriesma@308
    79
	    scr      : scr}  (*Script term  (how to restrict type ???)*)
agriesma@308
    80
  | Rrls of {id : string, (*for trace_rewrite:=true*)
agriesma@308
    81
	     prepat  : (term list *
agriesma@308
    82
			          (*preconds, eval with subst from pattern*)
agriesma@308
    83
			 term )   (*pattern matched in subterms*)
agriesma@308
    84
			    list, (*meta-conjunction is or*)
agriesma@308
    85
	     rew_ord  : string * (subst -> (term * term) -> bool),(*for rules*)
agriesma@308
    86
	     erls     : rls,      (*for the conditions in rules and pat*)
agriesma@308
    87
	     (*            '^ because of rewrite in applicable_in
agriesma@308
    88
						       compare type meth'*)
agriesma@308
    89
	     calc     : calc list, (*for Calculate in scr*)
agriesma@308
    90
	     asm_thm  : thm' list, (*FIXXME remove with put_asm*)
agriesma@308
    91
	     scr      : scr}; (*Rfuns {...}  (how to restrict type ???)*)
agriesma@308
    92
(*1.8.02 ad (how to restrict type ???): scr should be usable indepentently
agriesma@308
    93
  from rls, and then contain both Script _AND_ Rfuns !!!*)
agriesma@308
    94
agriesma@308
    95
agriesma@308
    96
agriesma@308
    97
val e_rule = Thm ("refl",refl) : rule;
agriesma@308
    98
fun id_of_thm (Thm (id, _)) = id
agriesma@308
    99
  | id_of_thm _ = raise error "id_of_thm";
agriesma@308
   100
fun thm_of_thm (Thm (_, thm)) = thm
agriesma@308
   101
  | thm_of_thm _ = raise error "thm_of_thm";
agriesma@308
   102
fun rep_thm_G' (Thm (thmid, thm)) = (thmid, thm);
agriesma@308
   103
fun rule2str (Thm (str, thm)) = "Thm (\""^str^"\","^(string_of_thm thm)^")"
agriesma@308
   104
  | rule2str (Calc (str,f))  = "Calc (\""^str^"\",fn)"
agriesma@308
   105
  | rule2str (Rls_ _) = "Rls_ rls";(*FIXXME*)
agriesma@308
   106
fun eqrule (Thm (id1,_), Thm (id2,_)) = id1 = id2
agriesma@308
   107
  | eqrule (Calc (id1,_), Calc (id2,_)) = id1 = id2
agriesma@308
   108
  | eqrule (Rls_ _, Rls_ _) = false (*{id=id1}{id=id2} = id1 = id2 FIXXME*)
agriesma@308
   109
  | eqrule _ = false;
agriesma@308
   110
fun memrls r (Rls {rules,...}) = gen_mem eqrule (r, rules)
agriesma@308
   111
  | memrls r (Seq {rules,...}) = gen_mem eqrule (r, rules)
agriesma@308
   112
  | memrls r _ = raise error ("memrls: incomplete impl. r= "^(rule2str r));
agriesma@308
   113
agriesma@308
   114
agriesma@308
   115
type rrlsstate =      (*state for reverse rewriting*)
agriesma@308
   116
     (term *          (*the current formula: 
agriesma@308
   117
                        goes locate_gen -> next_tac via istate*)
agriesma@308
   118
      term *          (*the final formula*)
agriesma@308
   119
      rule list       (*of reverse rewrite set (#1#)*)
agriesma@308
   120
	    list *    (*may be serveral, eg. in norm_rational*)
agriesma@308
   121
      (rule *         (*Thm (+ Thm generated from Calc) resulting in ...*)
agriesma@308
   122
       (term *        (*... rewrite with ...*)
agriesma@308
   123
	term list))   (*... assumptions*)
agriesma@308
   124
	  list);      (*derivation from given term to normalform
agriesma@308
   125
		       in reverse order with sym_thm; 
agriesma@308
   126
                       (#1#) could be extracted from here #1*)
agriesma@308
   127
val e_type = Type("empty",[]);
agriesma@308
   128
val a_type = TFree("'a",[]);
agriesma@308
   129
val e_term = Const("empty",e_type);
agriesma@308
   130
val a_term = Free("empty",a_type);
agriesma@308
   131
val e_rrlsstate = (e_term,e_term,[[e_rule]],[(e_rule,(e_term,[]))]):rrlsstate;
agriesma@308
   132
agriesma@308
   133
agriesma@308
   134
agriesma@308
   135
agriesma@308
   136
(*22.2.02: ging auf Linux nicht (Stefan)
agriesma@308
   137
val e_scr = Script ((term_of o the o (parse thy)) "e_script");*)
agriesma@308
   138
val e_term = Const("empty", Type("'a", []));
agriesma@308
   139
val e_scr = Script e_term;
agriesma@308
   140
agriesma@308
   141
agriesma@308
   142
(*ad thm':
agriesma@308
   143
   there are two kinds of theorems ...
agriesma@308
   144
   (1) known by isabelle
agriesma@308
   145
   (2) not known, eg. calc_thm, instantiated rls
agriesma@308
   146
       the latter have a thmid "#..."
agriesma@308
   147
   and thus outside isa we ALWAYS transport both(thmid,string_of_thm)
agriesma@308
   148
   and have a special assoc_thm / assoc_rls in this interface      *)
agriesma@308
   149
type theory' = string; (* = domID ^".thy" *)
agriesma@308
   150
fun string_of_thy thy =  
agriesma@308
   151
((last_elem (Sign.stamp_names_of (sign_of thy)))^".thy"):theory';
agriesma@308
   152
val theory2domID = string_of_thy;
agriesma@308
   153
val theory2theory' = string_of_thy;
agriesma@308
   154
agriesma@308
   155
type rew_ord' = string;
agriesma@308
   156
agriesma@308
   157
val theory'  = ref ([]:(theory' * theory) list);
agriesma@308
   158
val rew_ord' = ref ([]:(rew_ord' * (subst -> (term * term) -> bool)) list);
agriesma@308
   159
val ruleset' = ref ([]:(string * rls) list);
agriesma@308
   160
val theorem' = ref ([]:(string * thm) list);
agriesma@308
   161
(*val xxx = fn: a b => (a,b);   ??? fun-definition ???*)
agriesma@308
   162
local
agriesma@308
   163
    fun ii (_:term) = e_rrlsstate;
agriesma@308
   164
    fun no (_:term) = Some (e_term,[e_term]);
agriesma@308
   165
    fun lo (_:rule list list) (_:term) (_:rule) = [(e_rule,(e_term,[e_term]))];
agriesma@308
   166
    fun ne (_:rule list list) (_:term) = Some e_rule;
agriesma@308
   167
    fun fo (_:rule list list) (_:term) (_:term) = [(e_rule,(e_term,[e_term]))];
agriesma@308
   168
in
agriesma@308
   169
val e_rfuns = Rfuns {init_state=ii,normal_form=no,locate_rule=lo,
agriesma@308
   170
		     next_rule=ne,attach_form=fo};
agriesma@308
   171
end;
agriesma@308
   172
agriesma@308
   173
agriesma@308
   174
agriesma@308
   175
fun dummy_ord (_:subst) (_:term,_:term) = true;
agriesma@308
   176
val e_rls = 
agriesma@308
   177
  Rls{id = "e_rls",
agriesma@308
   178
      preconds = [], 
agriesma@308
   179
      rew_ord = ("dummy_ord", dummy_ord),
agriesma@308
   180
      erls = Erls,srls = Erls,
agriesma@308
   181
      calc = [],
agriesma@308
   182
      asm_thm = [],
agriesma@308
   183
      rules = [], scr = EmptyScr}:rls;
agriesma@308
   184
val e_rrls = Rrls {id = "e_rrls",
agriesma@308
   185
		   prepat = [],
agriesma@308
   186
		   rew_ord = ("dummy_ord", dummy_ord),
agriesma@308
   187
		   erls = Erls,
agriesma@308
   188
		   calc = [],
agriesma@308
   189
		   asm_thm=[],
agriesma@308
   190
		   scr=e_rfuns}:rls;
agriesma@308
   191
ruleset' := overwritel (!ruleset', [("e_rls",e_rls),
agriesma@308
   192
				    ("e_rrls",e_rrls)
agriesma@308
   193
				    ]);
agriesma@308
   194
agriesma@308
   195
fun rep_rls (Rls {id,preconds,rew_ord,erls,srls,calc,asm_thm,rules,scr}) =
agriesma@308
   196
  {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
agriesma@308
   197
   asm_thm=asm_thm,rules=rules,scr=scr}
agriesma@308
   198
  | rep_rls (Seq {id,preconds,rew_ord,erls,srls,calc,asm_thm,rules,scr}) =
agriesma@308
   199
  {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
agriesma@308
   200
   asm_thm=asm_thm,rules=rules,scr=scr}
agriesma@308
   201
  | rep_rls (Rls {id,...})  = 
agriesma@308
   202
    raise error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id)
agriesma@308
   203
  | rep_rls (Seq {id,...})  = 
agriesma@308
   204
    raise error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id);
agriesma@308
   205
fun rep_rrls 
agriesma@308
   206
	(Rrls {id,asm_thm, calc, erls, prepat, rew_ord, 
agriesma@308
   207
	       scr=Rfuns
agriesma@308
   208
		       {attach_form,init_state,locate_rule,
agriesma@308
   209
			next_rule,normal_form}}) =
agriesma@308
   210
    {id=id,asm_thm=asm_thm, calc=calc, erls=erls, prepat=prepat, 
agriesma@308
   211
     rew_ord=rew_ord, attach_form=attach_form, init_state=init_state, 
agriesma@308
   212
     locate_rule=locate_rule, next_rule=next_rule, normal_form=normal_form}
agriesma@308
   213
  | rep_rrls (Rls {id,...}) = 
agriesma@308
   214
    raise error ("rep_rrls doesn't take apart (normal) rule-sets: "^id)
agriesma@308
   215
  | rep_rrls (Seq {id,...}) = 
agriesma@308
   216
    raise error ("rep_rrls doesn't take apart (normal) rule-sets: "^id);
agriesma@308
   217
(*.id requested for all, Rls,Seq,Rrls.*)
agriesma@308
   218
fun id_rls (Rls {id,...}) = id
agriesma@308
   219
  | id_rls (Seq {id,...}) = id
agriesma@308
   220
  | id_rls (Rrls {id,...}) = id;
agriesma@308
   221
agriesma@308
   222
fun append_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   223
			asm_thm=at,rules =rs,scr=sc}) r =
agriesma@308
   224
    (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   225
	 asm_thm=at,rules = rs @ r,scr=sc}:rls)
agriesma@308
   226
  | append_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   227
			asm_thm=at,rules =rs,scr=sc}) r =
agriesma@308
   228
    (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   229
	 asm_thm=at,rules = rs @ r,scr=sc}:rls)
agriesma@308
   230
  | append_rls id (Rrls _) _ = raise error 
agriesma@308
   231
                   ("append_rls: not for reverse-rewrite-rule-set "^id);
agriesma@308
   232
agriesma@308
   233
fun eq_rule (Thm thm1, Thm thm2) = thm1 = thm2
agriesma@308
   234
  | eq_rule (Calc (id1,_), Calc (id2,_)) = id1 = id2
agriesma@308
   235
  | eq_rule _ = false;
agriesma@308
   236
agriesma@308
   237
fun merge_rls _ Erls rls = rls
agriesma@308
   238
  | merge_rls _ rls Erls = rls
agriesma@308
   239
  | merge_rls id
agriesma@308
   240
	(Rls {id=id1,preconds=pc1,rew_ord=ro1,erls=er1,srls=sr1,calc=ca1,
agriesma@308
   241
	      asm_thm=at1,rules =rs1,scr=sc1}) 
agriesma@308
   242
	r2 =
agriesma@308
   243
	(Rls {id=id,preconds=pc1 @ ((#preconds o rep_rls) r2),
agriesma@308
   244
	      rew_ord=ro1,erls=(*merge_rls er1 er2*)er1,
agriesma@308
   245
	      srls=merge_rls ("merged_"^id1^"_"^((#id o rep_rls) r2)) sr1 
agriesma@308
   246
			     ((#srls o rep_rls) r2),
agriesma@308
   247
	      calc=ca1 @ ((#calc o rep_rls) r2),
agriesma@308
   248
	      asm_thm=at1 @ ((#asm_thm o rep_rls) r2),
agriesma@308
   249
	      rules = gen_union eq_rule rule2str (rs1, (#rules o rep_rls) r2),
agriesma@308
   250
	      scr=sc1}:rls)
agriesma@308
   251
  | merge_rls id 
agriesma@308
   252
	(Seq {id=id1,preconds=pc1,rew_ord=ro1,erls=er1,srls=sr1,calc=ca1,
agriesma@308
   253
	      asm_thm=at1,rules =rs1,scr=sc1}) 
agriesma@308
   254
	r2 =
agriesma@308
   255
	(Seq {id=id,preconds=pc1 @ ((#preconds o rep_rls) r2),
agriesma@308
   256
	      rew_ord=ro1,erls=(*merge_rls er1 er2*)er1,
agriesma@308
   257
	      srls=merge_rls ("merged_"^id1^"_"^((#id o rep_rls) r2)) sr1 
agriesma@308
   258
			     ((#srls o rep_rls) r2),
agriesma@308
   259
	      calc=ca1 @ ((#calc o rep_rls) r2),
agriesma@308
   260
	      asm_thm=at1 @ ((#asm_thm o rep_rls) r2),
agriesma@308
   261
	      rules = gen_union eq_rule rule2str (rs1, (#rules o rep_rls) r2),
agriesma@308
   262
	      scr=sc1}:rls)
agriesma@308
   263
  | merge_rls _ _ _ = raise error 
agriesma@308
   264
				"merge_rls: not for reverse-rewrite-rule-sets";
agriesma@308
   265
fun remove_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   266
		     asm_thm=at,rules =rs,scr=sc}) r =
agriesma@308
   267
    (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   268
	 asm_thm=at,rules = gen_rems eq_rule (rs, r),
agriesma@308
   269
	 scr=sc}:rls)
agriesma@308
   270
  | remove_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   271
		     asm_thm=at,rules =rs,scr=sc}) r =
agriesma@308
   272
    (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
agriesma@308
   273
	 asm_thm=at,rules = gen_rems eq_rule (rs, r),
agriesma@308
   274
	 scr=sc}:rls)
agriesma@308
   275
  | remove_rls id (Rrls _) _ = raise error 
agriesma@308
   276
                   ("remove_rls: not for reverse-rewrite-rule-set "^id);
agriesma@308
   277
agriesma@308
   278
 gen_rems (op=) ([1,2,3], [3,4,5]);
agriesma@308
   279
agriesma@308
   280
agriesma@308
   281
agriesma@308
   282
fun assoc' ([], key) = raise error ("ME_Isa: '"^key^"' not known")
agriesma@308
   283
  | assoc' ((keyi, xi) :: pairs, key) =
agriesma@308
   284
      if key = keyi then Some xi else assoc' (pairs, key);
agriesma@308
   285
agriesma@308
   286
fun assoc_thy (thy:theory') = ((the o assoc')(!theory',thy))
agriesma@308
   287
  handle _ => raise error ("ME_Isa: '"^thy^"' not in system");
agriesma@308
   288
fun assoc_rls (rls:rls') = ((the o assoc')(!ruleset',rls))
agriesma@308
   289
  handle _ => raise error ("ME_Isa: '"^rls^"' not in system");
agriesma@308
   290
agriesma@308
   291
fun termopt2str (Some t) = 
agriesma@308
   292
    "Some " ^ (Sign.string_of_term (sign_of(assoc_thy "Isac.thy")) t)
agriesma@308
   293
  | termopt2str None = "None";
agriesma@308
   294
fun term2str t = Sign.string_of_term (sign_of(assoc_thy "Isac.thy")) t;
agriesma@308
   295
(*for tests only*)
agriesma@308
   296
fun terms2str ts= (strs2str o (map (Sign.string_of_term 
agriesma@308
   297
					(sign_of (assoc_thy "Isac.thy"))))) ts;
agriesma@308
   298
agriesma@308
   299
(*recursive defs:*)
agriesma@308
   300
fun scr2str (Script s) = "Script "^(term2str s)
agriesma@308
   301
  | scr2str (Rfuns _)  = "Rfuns";
agriesma@308
   302
agriesma@308
   303
agriesma@308
   304
type domID = string;   (* domID ^"thy" = theory' ??check 3.00!!*)
agriesma@308
   305
val e_domID = "e_domID":domID;
agriesma@308
   306
type pblID = string list; (* domID::...*)
agriesma@308
   307
val e_pblID = ["e_pblID"]:pblID;
agriesma@308
   308
type metID = (domID * string);
agriesma@308
   309
val e_metID = (e_domID,"e_metID"):metID;
agriesma@308
   310
agriesma@308
   311
fun metID2str ((domID,mI):metID) = pair2str (domID,mI);
agriesma@308
   312