src/Tools/isac/MathEngBasic/rewrite.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 13 Apr 2020 13:27:55 +0200
changeset 59870 0042fe0bc764
parent 59868 d77aa0992e0f
child 59871 82428ca0d23e
permissions -rw-r--r--
improve renaming
neuper@37906
     1
(* isac's rewriter
neuper@37906
     2
   (c) Walther Neuper 2000
neuper@37906
     3
*)
neuper@37906
     4
wneuper@59380
     5
signature REWRITE =
wneuper@59380
     6
  sig
walther@59865
     7
(*/------- to ThmC_Def -------\*)
walther@59865
     8
    val assoc_thm': theory -> ThmC_Def.thm' -> thm
walther@59865
     9
    val assoc_thm'': theory -> ThmC_Def.thmID -> thm
walther@59865
    10
(*\------- to ThmC_Def -------/*)
walther@59854
    11
    val calculate_: theory -> string * Exec_Def.eval_fn -> term -> (term * (string * thm)) option
walther@59851
    12
    val eval__true: theory -> int -> term list -> (term * term) list -> Rule_Set.T -> term list * bool
walther@59851
    13
    val eval_prog_expr: theory -> Rule_Set.T -> term -> term
walther@59851
    14
    val eval_true_: theory -> Rule_Set.T -> term -> bool
walther@59851
    15
    val eval_true: theory -> term list -> Rule_Set.T -> bool
wneuper@59405
    16
    val rew_sub: theory -> int -> (term * term) list -> ((term * term) list -> term * term -> bool)
walther@59851
    17
      -> Rule_Set.T -> bool -> TermC.path -> term -> term -> term * term list * TermC.path * bool
walther@59851
    18
    val rewrite_: theory -> ((term * term) list -> term * term -> bool) -> Rule_Set.T -> bool -> thm ->
wneuper@59405
    19
      term -> (term * term list) option
walther@59851
    20
    val rewrite_inst_: theory -> ((term * term) list -> term * term -> bool) -> Rule_Set.T -> bool
wneuper@59405
    21
      -> (term * term) list -> thm -> term -> (term * term list) option
walther@59851
    22
    val rewrite_set_: theory -> bool -> Rule_Set.T -> term -> (term * term list) option
walther@59851
    23
    val rewrite_set_inst_: theory -> bool -> (term * term) list -> Rule_Set.T -> term -> (term * term list) option
walther@59851
    24
    val rewrite_terms_: theory -> ((term * term) list -> term * term -> bool) -> Rule_Set.T -> term list
wneuper@59405
    25
      -> term -> (term * term list) option
wneuper@59380
    26
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59380
    27
  (* NONE *)
walther@59785
    28
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59409
    29
    val rewrite__: theory -> int -> (term * term) list -> ((term * term) list -> term * term -> bool) ->
walther@59851
    30
      Rule_Set.T -> bool -> thm -> term -> (term * term list) option
walther@59851
    31
    val rewrite__set_: theory -> int -> bool -> (term * term) list -> Rule_Set.T -> term -> (term * term list) option
walther@59851
    32
    val app_rev: theory -> int -> Rule_Set.T -> term -> term * term list * bool
walther@59851
    33
    val app_sub: theory -> int -> Rule_Set.T -> term -> term * term list * bool
wneuper@59380
    34
    val mk_thm: theory -> string -> thm
wneuper@59398
    35
    val trace1: int -> string -> unit
walther@59785
    36
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59380
    37
  end
wneuper@59380
    38
wneuper@59380
    39
(**)
wneuper@59380
    40
structure Rewrite(**): REWRITE(**) =
wneuper@59380
    41
struct
wneuper@59380
    42
(**)
neuper@37906
    43
neuper@37906
    44
exception NO_REWRITE;
neuper@37906
    45
exception STOP_REW_SUB; (*WN050820 quick and dirty*)
neuper@37906
    46
neuper@52101
    47
fun trace i str = 
wneuper@59405
    48
  if ! Celem.trace_rewrite andalso i < ! Celem.depth then tracing (idt "#" i ^ str) else ()
neuper@52101
    49
fun trace1 i str = 
wneuper@59405
    50
  if ! Celem.trace_rewrite andalso i < ! Celem.depth then tracing (idt "#" (i + 1) ^ str) else ()
neuper@52101
    51
neuper@37906
    52
fun rewrite__ thy i bdv tless rls put_asm thm ct =
wneuper@59381
    53
  let
walther@59767
    54
    val (t', asms, _ (*lrd*), rew) = rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
walther@59773
    55
		  (((TermC.inst_bdv bdv) o Num_Calc.norm o #prop o Thm.rep_thm) thm) ct
wneuper@59381
    56
  in if rew then SOME (t', distinct asms) else NONE end
wneuper@59383
    57
  (* one rewrite (possibly conditional, ordered) EXOR exn EXOR go into subterms *)
wneuper@59381
    58
and rew_sub thy i bdv tless rls put_asm lrd r t = 
neuper@38022
    59
  (let
wneuper@59381
    60
    val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
wneuper@59381
    61
    (*?alternative Unify.matchers:
wneuper@59381
    62
      http://isabelle.in.tum.de/repos/isabelle/file/Isabelle2017/src/Pure/more_unify.ML*)
wneuper@59381
    63
    val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r
wneuper@59381
    64
    val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
wneuper@59381
    65
    val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
wneuper@59405
    66
    val _ = if ! Celem.trace_rewrite andalso i < ! Celem.depth andalso p' <> []
walther@59870
    67
	    then tracing (idt "#" (i + 1) ^ " eval asms: " ^ UnparseC.term_in_thy thy r') else ()
wneuper@59381
    68
    val (t'', p'') =                                                     (*conditional rewriting*)
wneuper@59381
    69
      let
wneuper@59381
    70
        val (simpl_p', nofalse) = eval__true thy (i + 1) p' bdv rls 	     
wneuper@59381
    71
	    in
wneuper@59381
    72
	      if nofalse
wneuper@59381
    73
        then
wneuper@59405
    74
          (if ! Celem.trace_rewrite andalso i < ! Celem.depth andalso p' <> []
walther@59870
    75
          then tracing (idt "#" (i + 1) ^ " asms accepted: " ^ UnparseC.terms_in_thy thy p' ^
walther@59870
    76
  	        "   stored: " ^ UnparseC.terms_in_thy thy simpl_p')
wneuper@59381
    77
  	      else();
wneuper@59381
    78
          (t',simpl_p'))                                               (* uncond.rew. from above*)
wneuper@59381
    79
        else 
wneuper@59405
    80
          (if ! Celem.trace_rewrite andalso i < ! Celem.depth 
walther@59870
    81
          then tracing (idt "#" (i + 1) ^ " asms false: " ^ UnparseC.terms_in_thy thy p')
wneuper@59381
    82
          else();
wneuper@59381
    83
          raise STOP_REW_SUB (* don't go into subterms of cond *))
wneuper@59381
    84
	    end
wneuper@59381
    85
    in
wneuper@59389
    86
      if TermC.perm lhs rhs andalso not (tless bdv (t', t))                        (*ordered rewriting*)
wneuper@59405
    87
      then (if ! Celem.trace_rewrite andalso i < ! Celem.depth 
walther@59870
    88
  	    then tracing (idt"#"i ^ " not: \"" ^ UnparseC.term_in_thy thy t ^ "\" > \"" ^ UnparseC.term_in_thy thy t' ^ "\"")
wneuper@59381
    89
  	    else (); 
wneuper@59381
    90
  	    raise NO_REWRITE)
wneuper@59381
    91
  	  else (t'', p'', [], true)
wneuper@59381
    92
    end
wneuper@59383
    93
    ) handle _ (*TODO Pattern.MATCH when tests are ready: ERROR 364ce4699452 *) => 
wneuper@59381
    94
      (case t of
wneuper@59381
    95
        Const(s,T) => (Const(s,T),[],lrd,false)
neuper@37906
    96
      | Free(s,T) => (Free(s,T),[],lrd,false)
neuper@37906
    97
      | Var(n,T) => (Var(n,T),[],lrd,false)
neuper@37906
    98
      | Bound i => (Bound i,[],lrd,false)
neuper@37906
    99
      | Abs(s,T,body) => 
walther@59767
   100
        let val (t', asms, _ (*lrd*), rew) =  rew_sub thy i bdv tless rls put_asm (lrd @ [TermC.D]) r body
wneuper@59381
   101
    	   in (Abs(s, T, t'), asms, [], rew) end
neuper@37906
   102
      | t1 $ t2 => 
walther@59767
   103
    	   let val (t2', asm2, lrd, rew2) = rew_sub thy i bdv tless rls put_asm (lrd @ [TermC.R]) r t2
wneuper@59381
   104
    	   in
wneuper@59381
   105
    	    if rew2 then (t1 $ t2', asm2, lrd, true)
wneuper@59381
   106
    	    else
walther@59767
   107
    	      let val (t1', asm1, lrd, rew1) = rew_sub thy i bdv tless rls put_asm (lrd @ [TermC.L]) r t1
wneuper@59381
   108
    	      in if rew1 then (t1' $ t2, asm1, lrd, true) else (t1 $ t2,[], lrd, false) end
wneuper@59381
   109
  end)
wneuper@59381
   110
and eval__true thy i asms bdv rls =         (* simplify asumptions until one evaluates to false *)
neuper@48760
   111
  if asms = [@{term True}] orelse asms = [] then ([], true)
wneuper@59381
   112
  else (* this allows to check Rrls with prepat = ([@{term True}], pat) *)
wneuper@59381
   113
    if asms = [@{term False}] then ([], false)
wneuper@59381
   114
    else
wneuper@59381
   115
      let                            
wneuper@59381
   116
        fun chk indets [] = (indets, true) (*return asms<>True until false*)
wneuper@59381
   117
          | chk indets (a :: asms) =
wneuper@59381
   118
            (case rewrite__set_ thy (i + 1) false bdv rls a of
wneuper@59381
   119
              NONE => (chk (indets @ [a]) asms)
wneuper@59381
   120
            | SOME (t, a') =>
wneuper@59381
   121
              if t = @{term True} then (chk (indets @ a') asms) 
wneuper@59381
   122
              else if t = @{term False} then ([], false)
wneuper@59381
   123
            (*asm false .. thm not applied ^^^; continue until False vvv*)
wneuper@59381
   124
            else chk (indets @ [t] @ a') asms);
wneuper@59381
   125
      in chk [] asms end
walther@59851
   126
and rewrite__set_ thy _ __ Rule_Set.Empty t =                             (* rewrite with a rule set *)
walther@59870
   127
    error ("rewrite__set_ called with 'Erls' for '" ^ UnparseC.term_in_thy thy t ^ "'")
walther@59850
   128
  | rewrite__set_ thy i _ _ (rrls as Rule_Set.Rrls _) t =      (* rewrite with a 'reverse rule set' *)
neuper@52101
   129
    let
walther@59870
   130
      val _= trace i (" rls: " ^ Rule_Set.id rrls ^ " on: " ^ UnparseC.term_in_thy thy t)
wneuper@59382
   131
	    val (t', asm, rew) = app_rev thy (i + 1) rrls t                   
neuper@52101
   132
    in if rew then SOME (t', distinct asm) else NONE end
walther@59773
   133
  | rewrite__set_ thy i put_asm bdv rls ct =          (* Rls, Seq containing Thms or Num_Calc, Cal1 *)
neuper@52101
   134
    let
walther@59841
   135
      (* attention with cp to test/..: unbound thy, i, bdv, rls; TODO1803? pull out to rewrite__*)
neuper@52101
   136
      datatype switch = Appl | Noap;
walther@59841
   137
      fun rew_once _ asm ct Noap [] = (ct, asm)         (* ?TODO unify with Prog_Expr.rew_once? *)
neuper@52101
   138
        | rew_once ruls asm ct Appl [] = 
walther@59851
   139
          (case rls of Rule_Def.Repeat _ => rew_once ruls asm ct Noap ruls
walther@59851
   140
          | Rule_Set.Seqence _ => (ct, asm)
walther@59867
   141
          | rls => raise ERROR ("rew_once not appl. to \"" ^ Rule_Set.id rls ^ "\""))
wneuper@59381
   142
        | rew_once ruls asm ct apno (rul :: thms) =
neuper@52101
   143
          case rul of
wneuper@59416
   144
            Rule.Thm (thmid, thm) =>
walther@59841
   145
              (trace1 i (" try thm: \"" ^ thmid ^ "\"");
walther@59852
   146
              case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule_Set.rep) rls)
walther@59852
   147
                  ((#erls o Rule_Set.rep) rls) put_asm thm ct of
neuper@52101
   148
                NONE => rew_once ruls asm ct apno thms
wneuper@59381
   149
              | SOME (ct', asm') => 
walther@59870
   150
                (trace1 i (" rewrites to: \"" ^ UnparseC.term_in_thy thy ct' ^ "\"");
wneuper@59399
   151
                rew_once ruls (union (op =) asm asm') ct' Appl (rul :: thms)))
wneuper@59399
   152
                (* once again try the same rule, e.g. associativity against "()"*)
walther@59773
   153
          | Rule.Num_Calc (cc as (op_, _)) => 
walther@59841
   154
            let val _= trace1 i (" try calc: \"" ^ op_ ^ "\"")
wneuper@59521
   155
              val ct = TermC.uminus_to_string ct (*WN190312: superfluous?*)
walther@59773
   156
            in case Num_Calc.adhoc_thm thy cc ct of
neuper@52101
   157
                NONE => rew_once ruls asm ct apno thms
wneuper@59381
   158
              | SOME (_, thm') => 
neuper@52101
   159
                let 
walther@59852
   160
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule_Set.rep) rls)
walther@59852
   161
                    ((#erls o Rule_Set.rep) rls) put_asm thm' ct;
neuper@52101
   162
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
walther@59870
   163
                    ThmC_Def.string_of_thmI thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
walther@59870
   164
                  val _ = trace1 i (" calc. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
neuper@52101
   165
                in rew_once ruls asm ((fst o the) pairopt) Appl (rul :: thms) end
neuper@52101
   166
            end
wneuper@59416
   167
          | Rule.Cal1 (cc as (op_, _)) => 
neuper@52101
   168
            let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
wneuper@59389
   169
              val ct = TermC.uminus_to_string ct
walther@59773
   170
            in case Num_Calc.adhoc_thm1_ thy cc ct of
neuper@52101
   171
                NONE => (ct, asm)
wneuper@59381
   172
              | SOME (_, thm') =>
neuper@52101
   173
                let 
walther@59852
   174
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule_Set.rep) rls)
walther@59852
   175
                    ((#erls o Rule_Set.rep) rls) put_asm thm' ct;
neuper@52101
   176
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
walther@59870
   177
                     ThmC_Def.string_of_thmI thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
walther@59870
   178
                  val _ = trace1 i (" cal1. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
neuper@52101
   179
                in the pairopt end
neuper@52101
   180
            end
wneuper@59416
   181
          | Rule.Rls_ rls' => 
neuper@52101
   182
            (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
neuper@52101
   183
              SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
wneuper@59381
   184
            | NONE => rew_once ruls asm ct apno thms)
walther@59867
   185
          | r => raise ERROR ("rew_once not appl. to \"" ^ Rule.to_string r ^ "\"");
walther@59852
   186
      val ruls = (#rules o Rule_Set.rep) rls;
walther@59870
   187
      val _ = trace i (" rls: " ^ Rule_Set.id rls ^ " on: " ^ UnparseC.term_in_thy thy ct)
neuper@52101
   188
      val (ct', asm') = rew_once ruls [] ct Noap ruls;
neuper@52101
   189
	  in if ct = ct' then NONE else SOME (ct', distinct asm') end
walther@59865
   190
(*-------------------------------------------------------------*)
wneuper@59381
   191
and app_rev thy i rrls t =            (* apply an Rrls; if not applicable proceed with subterms *)
neuper@52085
   192
  let (* check a (precond, pattern) of a rev-set; stops with 1st true *)
wneuper@59381
   193
    fun chk_prepat _ _ [] _ = true
neuper@52085
   194
      | chk_prepat thy erls prepat t =
neuper@52085
   195
        let
neuper@52085
   196
          fun chk (pres, pat) =
neuper@52085
   197
            (let 
neuper@52085
   198
              val subst: Type.tyenv * Envir.tenv =
neuper@52085
   199
                Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
neuper@52085
   200
             in
neuper@52085
   201
              snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
wneuper@59381
   202
             end) handle _ (*TODO Pattern.MATCH*) => false
wneuper@59381
   203
           fun scan_ _ [] = false
wneuper@59398
   204
             | scan_ f (pp :: pps) =
neuper@52085
   205
               if f pp then true else scan_ f pps;
neuper@52085
   206
        in scan_ chk prepat end;
neuper@52085
   207
    (* apply the normal_form of a rev-set *)
walther@59850
   208
    fun app_rev' thy (Rule_Set.Rrls {erls, prepat, scr = Rule.Rfuns {normal_form, ...}, ...}) t =
wneuper@59381
   209
      if chk_prepat thy erls prepat t then normal_form t else NONE
walther@59867
   210
      | app_rev' _ r _ = raise ERROR ("app_rev' not appl. to \"" ^ Rule_Set.id r ^ "\"");
neuper@52085
   211
    val opt = app_rev' thy rrls t
neuper@52085
   212
  in
neuper@52085
   213
    case opt of
neuper@52085
   214
      SOME (t', asm) => (t', asm, true)
neuper@52085
   215
    | NONE => app_sub thy i rrls t
neuper@52085
   216
  end
wneuper@59381
   217
and app_sub thy i rrls t =                                         (* apply an Rrls to subterms *)
neuper@52085
   218
  case t of
neuper@52085
   219
    Const (s, T) => (Const(s, T), [], false)
neuper@52085
   220
  | Free (s, T) => (Free(s, T), [], false)
neuper@52085
   221
  | Var (n, T) => (Var(n, T), [], false)
neuper@52085
   222
  | Bound i => (Bound i, [], false)
neuper@52085
   223
  | Abs (s, T, body) => 
neuper@37906
   224
	  let val (t', asm, rew) = app_rev thy i rrls body
neuper@37906
   225
	  in (Abs(s, T, t'), asm, rew) end
neuper@52085
   226
  | t1 $ t2 => 
neuper@52085
   227
    let val (t2', asm2, rew2) = app_rev thy i rrls t2
neuper@52085
   228
    in
neuper@52085
   229
      if rew2 then (t1 $ t2', asm2, true)
neuper@52085
   230
      else
neuper@52085
   231
        let val (t1', asm1, rew1) = app_rev thy i rrls t1
neuper@52085
   232
        in if rew1 then (t1' $ t2, asm1, true)
neuper@52085
   233
           else (t1 $ t2, [], false)
neuper@52085
   234
        end
wneuper@59381
   235
    end;
neuper@37906
   236
wneuper@59381
   237
(* rewriting without argument [] for rew_ord;  WN110603: shouldnt asm<>[] lead to false? *)
neuper@37906
   238
fun eval_true thy terms rls = (snd o (eval__true thy 1 terms [])) rls;
neuper@37906
   239
neuper@52101
   240
(* rewriting without internal argument [] *)
neuper@52101
   241
fun rewrite_ thy rew_ord erls bool thm term = rewrite__ thy 1 [] rew_ord erls bool thm term;
neuper@52101
   242
fun rewrite_set_ thy bool rls term = rewrite__set_ thy 1 bool [] rls term;
neuper@37906
   243
wneuper@59381
   244
(* variants of rewrite; TODO del. put_asm *)
wneuper@59381
   245
fun rewrite_inst_  thy rew_ord rls put_asm subst thm ct =
wneuper@59381
   246
  rewrite__ thy 1 subst rew_ord rls put_asm thm ct;
wneuper@59381
   247
fun rewrite_set_inst_ thy put_asm subst rls ct = rewrite__set_ thy 1 put_asm subst rls ct;
neuper@37906
   248
neuper@38025
   249
(* given a list of equalities (lhs = rhs) and a term, 
neuper@38025
   250
   replace all occurrences of lhs in the term with rhs;
neuper@38025
   251
   thus the order or equalities matters: put variables in lhs first. *)
neuper@38025
   252
fun rewrite_terms_ thy ord erls equs t =
neuper@42359
   253
  let
neuper@42359
   254
	  fun rew_ (t', asm') [] _ = (t', asm')
neuper@42359
   255
	    | rew_ (t', asm') (rules as r::rs) t =
neuper@42359
   256
	        let
wneuper@59390
   257
	          val (t'', asm'', _(*lrd*), rew) = rew_sub thy 1 [] ord erls false [] (HOLogic.Trueprop $ r) t
neuper@42359
   258
	        in 
neuper@42359
   259
	          if rew 
neuper@42359
   260
	          then rew_ (t'', asm' @ asm'') rules t''
neuper@42359
   261
	          else rew_ (t', asm') rs t'
neuper@42359
   262
	        end
walther@59861
   263
	  val (t'', asm'') = rew_ (TermC.empty, []) equs t
walther@59861
   264
    in if t'' = TermC.empty then NONE else SOME (t'', asm'')
neuper@42359
   265
    end;
neuper@37906
   266
wneuper@59381
   267
(* search ct for adjacent numerals and calculate them by operator isa_fn *)
neuper@37906
   268
fun calculate_ thy isa_fn ct =
wneuper@59389
   269
  let val ct = TermC.uminus_to_string ct
walther@59773
   270
    in case Num_Calc.adhoc_thm thy isa_fn ct of
neuper@37906
   271
	   NONE => NONE
wneuper@59381
   272
	 | SOME (thmID, thm) =>
walther@59857
   273
	   (let val rew = case rewrite_ thy Rewrite_Ord.dummy_ord Rule_Set.empty false thm ct of
wneuper@59381
   274
         SOME (rew, _) => rew
wneuper@59381
   275
       | NONE => raise ERROR ""
wneuper@59381
   276
     in SOME (rew, (thmID, thm)) end)
wneuper@59381
   277
	   handle _ (*TODO Pattern.MATCH ?del?*)=> error ("calculate_: " ^ thmID ^ " does not rewrite")
neuper@37906
   278
  end;
neuper@37906
   279
wneuper@59381
   280
(* Thm.make_thm added to Pure/thm.ML *)
neuper@37906
   281
fun mk_thm thy str = 
wneuper@59385
   282
  let
wneuper@59389
   283
    val t = (Thm.term_of o the o (TermC.parse thy)) str
wneuper@59385
   284
    val t' = case t of
wneuper@59385
   285
        Const ("Pure.imp", _) $ _ $ _ => t
wneuper@59390
   286
      | _ => HOLogic.Trueprop $ t
wneuper@59385
   287
  in Thm.make_thm (Thm.global_cterm_of thy t') end;
neuper@37906
   288
walther@59865
   289
(*/------- to ThmC_Def -------\*)
walther@59865
   290
(* 
walther@59865
   291
  "metaview" as seen from programs and from user input;
walther@59865
   292
  both are parsed as terms by the function package.
walther@59865
   293
*)
wneuper@59108
   294
fun convert_metaview_to_thmid thy thmid =
wneuper@59108
   295
  let val thmid' = case thmid of
wneuper@59108
   296
      "add_commute" => "add.commute"
wneuper@59108
   297
    | "mult_commute" => "mult.commute"
wneuper@59108
   298
    | "add_left_commute" => "add.left_commute"
wneuper@59108
   299
    | "mult_left_commute" => "mult.left_commute"
wneuper@59108
   300
    | "add_assoc" => "add.assoc"
wneuper@59108
   301
    | "mult_assoc" => "mult.assoc"
wneuper@59108
   302
    | _ => thmid
wneuper@59108
   303
  in (Global_Theory.get_thm thy) thmid' end;
wneuper@59108
   304
wneuper@59381
   305
(* get the theorem associated with the xstring-identifier;
neuper@37906
   306
   if the identifier starts with "sym_" then swap lhs = rhs around =
walther@59865
   307
   (ATTENTION: "RS sym" attaches a [.] -- remove it with ThmC_Def.string_of_thmI);
walther@59773
   308
   identifiers starting with "#" come from Num_Calc and
wneuper@59381
   309
   get a hand-made theorem (containing numerals only) *)
wneuper@59381
   310
fun assoc_thm'' thy thmid =
wneuper@59253
   311
  case Symbol.explode thmid of
wneuper@59253
   312
    "s"::"y"::"m"::"_"::"#"::_ => error ("assoc_thm'' not impl.for " ^ thmid)
wneuper@59389
   313
  | "s"::"y"::"m"::"_"::id => ((TermC.num_str o (Global_Theory.get_thm thy)) (implode id)) RS sym
wneuper@59253
   314
  | "#"::_ => error ("assoc_thm'' not impl.for " ^ thmid)
wneuper@59389
   315
  | _ => thmid |> convert_metaview_to_thmid thy |> TermC.num_str
wneuper@59381
   316
fun assoc_thm' thy (thmid, ct') =
neuper@55487
   317
  (case Symbol.explode thmid of
neuper@55487
   318
    "s"::"y"::"m"::"_"::id => 
neuper@55487
   319
      if hd id = "#" 
neuper@55487
   320
      then mk_thm thy ct'
wneuper@59389
   321
      else ((TermC.num_str o (Global_Theory.get_thm thy)) (implode id)) RS sym
wneuper@59109
   322
  | id =>
neuper@55487
   323
    if hd id = "#" 
neuper@55487
   324
    then mk_thm thy ct'
wneuper@59389
   325
    else thmid |> convert_metaview_to_thmid thy |> TermC.num_str
wneuper@59381
   326
  ) handle _ (*TODO: find exn behind ERROR: Undefined fact: "add_commute"*) => 
walther@59865
   327
    raise ERROR
walther@59865
   328
      ("assoc_thm': \"" ^ thmid ^ "\" not in \"" ^ ThyC.theory2domID thy ^ "\" (and parents)")
walther@59865
   329
(*\------- to ThmC_Def -------/*)
walther@59865
   330
neuper@37906
   331
walther@59718
   332
fun eval_prog_expr thy srls t =
neuper@52139
   333
  let val rew = rewrite_set_ thy false srls t;
neuper@52139
   334
  in case rew of SOME (res,_) => res | NONE => t end;
neuper@37906
   335
neuper@41928
   336
fun eval_true_ _ _ (Const ("HOL.True",_)) = true
walther@59722
   337
  | eval_true_ thy rls t =
walther@59722
   338
    case rewrite_set_ thy false rls t of
neuper@41928
   339
	   SOME (Const ("HOL.True",_),_) => true
neuper@37906
   340
	 | _ => false;
neuper@37906
   341
wneuper@59380
   342
end