src/Tools/isac/CalcElements/rule-def.sml
author Walther Neuper <walther.neuper@jku.at>
Wed, 01 Apr 2020 19:20:05 +0200
changeset 59849 d82a32869f27
child 59850 f3cac3053e7b
permissions -rw-r--r--
separate Rule_Def from Rule
walther@59849
     1
(* Title:  CalcElements/rule-def.sml
walther@59849
     2
   Author: Walther Neuper
walther@59849
     3
   (c) due to copyright terms
walther@59849
     4
walther@59849
     5
Separate the mutual recursive definition from individual items specified later.
walther@59849
     6
*)
walther@59849
     7
signature RULE_DEFINITION =
walther@59849
     8
  sig
walther@59849
     9
walther@59849
    10
    type calc                                       (*struct:   sig: OK in rule*)
walther@59849
    11
    eqtype calID                                    (*struct:   sig: NOT ok in rule*)
walther@59849
    12
    eqtype errpatID                                 (*struct:   sig: *)
walther@59849
    13
    type rew_ord                                    (*struct:   sig: *)
walther@59849
    14
    type eval_fn                                    (*struct:   sig: OK in rule*)
walther@59849
    15
    val e_evalfn: eval_fn                           (*struct:   sig: (*NOT ok in rule*)*)
walther@59849
    16
    datatype rls                                                                 (*OK*)
walther@59849
    17
      =  Erls
walther@59849
    18
       | Rls of {calc: calc list, erls: rls, errpatts: errpatID list, id: string,
walther@59849
    19
         preconds: term list, rew_ord: rew_ord, rules: rule list, scr: program, srls: rls}
walther@59849
    20
       | Seq of {calc: calc list, erls: rls, errpatts: errpatID list, id: string,
walther@59849
    21
         preconds: term list, rew_ord: rew_ord, rules: rule list, scr: program, srls: rls}
walther@59849
    22
       | Rrls of {calc: calc list, erls: rls, errpatts: errpatID list, id: string,
walther@59849
    23
         prepat: (term list * term) list, rew_ord: rew_ord, scr: program}
walther@59849
    24
    and rule = Cal1 of string * eval_fn | Num_Calc of string * eval_fn | Erule  (*OK*)
walther@59849
    25
       | Rls_ of rls | Thm of string * thm
walther@59849
    26
    and program                                                                 (*OK*)
walther@59849
    27
      =  EmptyScr
walther@59849
    28
       | Prog of term
walther@59849
    29
       | Rfuns of
walther@59849
    30
           {attach_form: rule list list -> term -> term -> (rule * (term * term list)) list,
walther@59849
    31
            init_state: term -> term * term * rule list list * (rule * (term * term list)) list,
walther@59849
    32
            locate_rule: rule list list -> term -> rule -> (rule * (term * term list)) list,
walther@59849
    33
            next_rule: rule list list -> term -> rule option, normal_form: term ->
walther@59849
    34
            (term * term list) option}
walther@59849
    35
walther@59849
    36
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59849
    37
  (*NONE*)
walther@59849
    38
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59849
    39
  (*NONE*)
walther@59849
    40
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59849
    41
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
walther@59849
    42
  end
walther@59849
    43
walther@59849
    44
(**)
walther@59849
    45
structure Rule_Def(**): RULE_DEFINITION(**) =
walther@59849
    46
struct
walther@59849
    47
(**)
walther@59849
    48
walther@59849
    49
type calID = string;
walther@59849
    50
(* eval function calling sml code during rewriting.
walther@59849
    51
Unifying "type cal" and "type calc" would make Lucas-Interpretation more efficient,
walther@59849
    52
  see "fun rule2stac": instead of 
walther@59849
    53
    Num_Calc: calID * eval_fn -> rule
walther@59849
    54
  would be better
walther@59849
    55
    Num_Calc: prog_calcID * (calID * eval_fn)) -> rule*)
walther@59849
    56
type eval_fn = (string -> term -> theory -> (string * term) option);
walther@59849
    57
fun e_evalfn (_ : 'a) (_ : term) (_ : theory) = NONE : (string * term) option;
walther@59849
    58
walther@59849
    59
type subst = (term * term) list;
walther@59849
    60
walther@59849
    61
type rew_ord' = string;
walther@59849
    62
type rew_ord_ = subst -> Term.term * Term.term -> bool;
walther@59849
    63
type rew_ord = rew_ord' * rew_ord_;
walther@59849
    64
val e_rew_ord' = "e_rew_ord" : rew_ord';
walther@59849
    65
fun dummy_ord (_: subst) (_: term, _: term) = true;
walther@59849
    66
val e_rew_ord_ = dummy_ord;
walther@59849
    67
val e_rew_ord = dummy_ord; (* TODO.WN071231 clarify identifiers..e_rew_ordX*)
walther@59849
    68
val e_rew_ordX = (e_rew_ord', e_rew_ord_);
walther@59849
    69
walther@59849
    70
(* op in isa-term "Const(op,_)" *)
walther@59849
    71
type cal = calID * eval_fn;
walther@59849
    72
type prog_calcID = string;
walther@59849
    73
type calc = (prog_calcID * cal);
walther@59849
    74
walther@59849
    75
(* error patterns and fill patterns *)
walther@59849
    76
type errpatID = string
walther@59849
    77
walther@59849
    78
(* all these different data justify a step in a calculation *)
walther@59849
    79
datatype rule =
walther@59849
    80
    Erule                (*.the empty rule                     .*)
walther@59849
    81
  | Thm of (string * Basic_Thm.thm) (* see TODO CLEANUP Thm     *)
walther@59849
    82
  | Num_Calc of string *     (*.sml-code manipulating a (sub)term  .*)
walther@59849
    83
  	  eval_fn
walther@59849
    84
  | Cal1 of string *     (*.sml-code applied only to whole term
walther@59849
    85
                            or left/right-hand-side of eqality .*)
walther@59849
    86
  	  eval_fn
walther@59849
    87
  | Rls_ of rls          (*.ie. rule sets may be nested.*)
walther@59849
    88
and program =
walther@59849
    89
    EmptyScr
walther@59849
    90
  | Prog of term (* a leaf is either a tactic or an 'exp' in 'let v = expr' 
walther@59849
    91
                    where 'exp' does not contain a tactic. *)
walther@59849
    92
  | Rfuns of     (* for Rrls, usage see rational.sml ----- reverse rewrite -----      *)
walther@59849
    93
    {init_state : (* initialise for reverse rewriting by the Interpreter              *)
walther@59849
    94
      term ->         (* for this the rrlsstate is initialised:                       *)
walther@59849
    95
      term *          (* the current formula:                                         *)
walther@59849
    96
                      (* goes locate_input_tactic -> find_next_step via istate        *)
walther@59849
    97
      term *          (* the final formula                                            *)
walther@59849
    98
      rule list       (* of reverse rewrite set (#1#)                                 *)
walther@59849
    99
        list *        (*   may be serveral, eg. in norm_rational                      *)
walther@59849
   100
      ( rule *        (* Thm (+ Thm generated from Num_Calc) resulting in ...             *)
walther@59849
   101
        (term *       (*   ... rewrite with ...                                       *)
walther@59849
   102
        term list))   (*   ... assumptions                                            *)
walther@59849
   103
      list,           (* derivation from given term to normalform
walther@59849
   104
                         in reverse order with sym_thm;
walther@59849
   105
                                                (#1#) could be extracted from here #1 *)  
walther@59849
   106
	  normal_form:  (* the function which drives the Rrls ##############################*)
walther@59849
   107
	    term -> (term * term list) option,
walther@59849
   108
	  locate_rule:  (* checks a rule R for being a cancel-rule, and if it is,
walther@59849
   109
                     then return the list of rules (+ the terms they are rewriting to)
walther@59849
   110
                     which need to be applied before R should be applied.
walther@59849
   111
                     precondition: the rule is applicable to the argument-term.       *)
walther@59849
   112
	    rule list list -> (* the reverse rule list                                      *)
walther@59849
   113
	    term ->         (* ... to which the rule shall be applied                       *)
walther@59849
   114
	    rule ->         (* ... to be applied to term                                    *)
walther@59849
   115
	    ( rule *        (* value: a rule rewriting to ...                               *)
walther@59849
   116
	      (term *       (*   ... the resulting term ...                                 *)
walther@59849
   117
	      term list))   (*   ... with the assumptions ( //#0)                           *)
walther@59849
   118
	    list,           (*   there may be several such rules; the list is empty,          
walther@59849
   119
                           if the rule has nothing to do with e.g. cancelation        *)
walther@59849
   120
	  next_rule:    (* for a given term return the next rules to be done for cancelling *)
walther@59849
   121
	    rule list list->(* the reverse rule list                                        *)
walther@59849
   122
	    term ->         (* the term for which ...                                       *)
walther@59849
   123
	    rule option,    (* ... this rule is appropriate for cancellation;
walther@59849
   124
		                     there may be no such rule (if the term is eg.canceled already*)
walther@59849
   125
	  attach_form:  (* checks an input term TI, if it may belong to e.g. a current 
walther@59849
   126
                     cancellation, by trying to derive it from the given term TG.     
walther@59849
   127
                     NOT IMPLEMENTED                                                  *)
walther@59849
   128
	    rule list list->(**)
walther@59849
   129
	    term ->         (* TG, the last one agreed upon by user + math-eng              *)
walther@59849
   130
	    term ->         (* TI, the next one input by the user                           *)
walther@59849
   131
	    ( rule *        (* the rule to be applied in order to reach TI                  *) 
walther@59849
   132
	      (term *       (* ... obtained by applying the rule ...                        *)
walther@59849
   133
	      term list))   (* ... and the respective assumptions                           *) 
walther@59849
   134
	    list}           (* there may be several such rules; the list is empty, if the 
walther@59849
   135
                         users term does not belong to e.g. a cancellation of the term 
walther@59849
   136
                         last agreed upon.                                            *)
walther@59849
   137
and rls =
walther@59849
   138
    Erls                          (*for init e_rls*)
walther@59849
   139
walther@59849
   140
  | Rls of (*a confluent and terminating ruleset, in general                         *)
walther@59849
   141
    {id : string,          (*for trace_rewrite:=true                                 *)
walther@59849
   142
     preconds : term list, (*unused WN020820                                         *)
walther@59849
   143
     (*WN060616 for efficiency...                                                    
walther@59849
   144
      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)                    
walther@59849
   145
     rew_ord  : rew_ord,   (*for rules*)                                             
walther@59849
   146
     erls     : rls,       (*for the conditions in rules                             *)
walther@59849
   147
     srls     : rls,       (*for evaluation of list_fns in script                    *)
walther@59849
   148
     calc     : calc list, (*for Calculate in scr, set by prep_rls'                  *)
walther@59849
   149
     rules    : rule list,                                                           
walther@59849
   150
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy                *)
walther@59849
   151
     scr      : program}   (*Prog term: generating intermed.steps  *)                
walther@59849
   152
  | Seq of (*a sequence of rules to be tried only once                               *)
walther@59849
   153
    {id : string,          (*for trace_rewrite:=true                                 *)
walther@59849
   154
     preconds : term list, (*unused 20.8.02                                          *)
walther@59849
   155
     (*WN060616 for efficiency...                                                    
walther@59849
   156
      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)                    
walther@59849
   157
     rew_ord  : rew_ord,   (*for rules                                               *)
walther@59849
   158
     erls     : rls,       (*for the conditions in rules                             *)
walther@59849
   159
     srls     : rls,       (*for evaluation of list_fns in script                    *)
walther@59849
   160
     calc     : calc list, (*for Calculate in scr, set by prep_rls'                  *)
walther@59849
   161
     rules    : rule list,
walther@59849
   162
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
walther@59849
   163
     scr      : program}   (*Prog term  (how to restrict type ???)*)
walther@59849
   164
walther@59849
   165
  (*Rrls call SML-code and simulate an rls
walther@59849
   166
    difference: there is always _ONE_ redex rewritten in 1 call,
walther@59849
   167
    thus wrap Rrls by: Rls (Rls_ ...)*)
walther@59849
   168
  | Rrls of (* SML-functions within rewriting; step-wise execution provided;   
walther@59849
   169
               Rrls simulate an rls
walther@59849
   170
               difference: there is always _ONE_ redex rewritten in 1 call,
walther@59849
   171
               thus wrap Rrls by: Rls (Rls_ ...)                                     *)
walther@59849
   172
    {id : string,          (* for trace_rewrite := true                              *)
walther@59849
   173
     prepat  : (term list *(* preconds, eval with subst from pattern;                
walther@59849
   174
                              if [@{term True}], match decides alone                 *)
walther@59849
   175
		            term )     (* pattern matched with current (sub)term                 *)
walther@59849
   176
		   list,               (* meta-conjunction is or                                 *)
walther@59849
   177
     rew_ord  : rew_ord,   (* for rules                                              *)
walther@59849
   178
     erls     : rls,       (* for the conditions in rules and preconds               *)
walther@59849
   179
     calc     : calc list, (* for Calculate in scr, set automatic.in prep_rls'       *)
walther@59849
   180
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)              
walther@59849
   181
     scr      : program};      (* Rfuns {...}  (how to restrict type ???)                *)
walther@59849
   182
walther@59849
   183
(**)end(**)
walther@59849
   184