src/Tools/isac/ProgLang/Tools.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Fri, 02 Mar 2018 14:19:59 +0100
changeset 59389 627d25067f2f
parent 59186 d9c3e373f8f5
child 59390 f6374c995ac5
permissions -rw-r--r--
separate structure TermC : TERMC
neuper@37906
     1
(* auxiliary functions used in scripts
neuper@37906
     2
   author: Walther Neuper 000301
neuper@37906
     3
   WN0509 shift into Atools ?!? (because used also in where of models !)
neuper@37906
     4
neuper@37906
     5
   (c) copyright due to lincense terms.
neuper@37906
     6
*)
neuper@37906
     7
neuper@37947
     8
theory Tools imports ListC begin
neuper@37906
     9
neuper@37947
    10
(*belongs to theory ListC*)
neuper@37906
    11
ML {*
neuper@37947
    12
val first_isac_thy = @{theory ListC}
neuper@37906
    13
*}
neuper@37906
    14
neuper@37906
    15
(*for Descript.thy*)
neuper@37906
    16
neuper@37906
    17
  (***********************************************************************)
neuper@37947
    18
  (* 'fun is_dsc' in ProgLang/scrtools.smlMUST contain ALL these types !!*)
neuper@37906
    19
  (***********************************************************************)
neuper@37906
    20
typedecl nam     (* named variables                                             *)
neuper@37906
    21
typedecl  una     (* unnamed variables                                           *)
neuper@37906
    22
typedecl  unl     (* unnamed variables of type list, elementwise input prohibited*)
neuper@37906
    23
typedecl  str     (* structured variables                                        *)
neuper@37906
    24
typedecl  toreal  (* var with undef real value: forces typing                    *)
neuper@37906
    25
typedecl  toreall (* var with undef real list value: forces typing               *)
neuper@37906
    26
typedecl  tobooll (* var with undef bool list value: forces typing               *)
neuper@37906
    27
typedecl  unknow  (* input without dsc in fmz=[]                                 *)
neuper@37906
    28
typedecl  cpy     (* UNUSED: copy-named variables
neuper@37906
    29
             identified by .._0, .._i .._' in pbt                        *)
neuper@37906
    30
  (***********************************************************************)
neuper@37947
    31
  (* 'fun is_dsc' in ProgLang/scrtools.smlMUST contain ALL these types !!*)
neuper@37906
    32
  (***********************************************************************)
neuper@37906
    33
  
neuper@37906
    34
consts
neuper@37906
    35
neuper@37906
    36
  UniversalList   :: "bool list"
neuper@37906
    37
neuper@37906
    38
  lhs             :: "bool => real"           (*of an equality*)
neuper@37906
    39
  rhs             :: "bool => real"           (*of an equality*)
neuper@37906
    40
  Vars            :: "'a => real list"        (*get the variables of a term *)
neuper@37906
    41
  matches         :: "['a, 'a] => bool"
neuper@37906
    42
  matchsub        :: "['a, 'a] => bool"
neuper@37906
    43
neuper@41899
    44
(*
neuper@37906
    45
constdefs
neuper@37906
    46
  
neuper@37906
    47
  Testvar   :: "[real, 'a] => bool"  (*is a variable in a term: unused 6.5.03*)
neuper@37906
    48
 "Testvar v t == v mem (Vars t)"     (*by rewriting only,no Calcunused 6.5.03*)
neuper@41899
    49
*)
neuper@37906
    50
neuper@37906
    51
ML {* (*the former Tools.ML*)
neuper@37906
    52
(* auxiliary functions for scripts  WN.9.00*)
neuper@37906
    53
(*11.02: for equation solving only*)
wneuper@59389
    54
val UniversalList = (Thm.term_of o the o (TermC.parse @{theory})) "UniversalList";
wneuper@59389
    55
val EmptyList = (Thm.term_of o the o (TermC.parse @{theory}))  "[]::bool list";     
neuper@37906
    56
neuper@37906
    57
(*+ for Or_to_List +*)
neuper@41928
    58
fun or2list (Const ("HOL.True",_)) = (tracing"### or2list True";UniversalList)
neuper@41928
    59
  | or2list (Const ("HOL.False",_)) = (tracing"### or2list False";EmptyList)
neuper@41922
    60
  | or2list (t as Const ("HOL.eq",_) $ _ $ _) = 
wneuper@59389
    61
    (tracing"### or2list _ = _"; TermC.list2isalist TermC.bool [t])
neuper@37906
    62
  | or2list ors =
neuper@38015
    63
    (tracing"### or2list _ | _";
t@42225
    64
    let fun get ls (Const ("HOL.disj",_) $ o1 $ o2) =
neuper@37906
    65
	    case o2 of
t@42225
    66
		Const ("HOL.disj",_) $ _ $ _ => get (ls @ [o1]) o2
neuper@37906
    67
	      | _ => ls @ [o1, o2] 
wneuper@59389
    68
    in (((TermC.list2isalist TermC.bool) o (get [])) ors)
neuper@38031
    69
       handle _ => error ("or2list: no ORs= "^(term2str ors)) end
neuper@37906
    70
	);
neuper@48760
    71
(*>val t = @{term True};
neuper@37906
    72
> val t' = or2list t;
neuper@37906
    73
> term2str t';
neuper@37906
    74
"Atools.UniversalList"
neuper@48760
    75
> val t = @{term False};
neuper@37906
    76
> val t' = or2list t;
neuper@37906
    77
> term2str t';
neuper@37906
    78
"[]"
wneuper@59186
    79
> val t=(Thm.term_of o the o (parse thy)) "x=3";
neuper@37906
    80
> val t' = or2list t;
neuper@37906
    81
> term2str t';
neuper@37906
    82
"[x = 3]"
wneuper@59186
    83
> val t=(Thm.term_of o the o (parse thy))"(x=3) | (x=-3) | (x=0)";
neuper@37906
    84
> val t' = or2list t;
neuper@37906
    85
> term2str t';
neuper@37906
    86
"[x = #3, x = #-3, x = #0]" : string *)
neuper@37906
    87
neuper@37906
    88
neuper@37906
    89
(** evaluation on the meta-level **)
neuper@37906
    90
neuper@37906
    91
(*. evaluate the predicate matches (match on whole term only) .*)
neuper@37906
    92
(*("matches",("Tools.matches",eval_matches "#matches_")):calc*)
neuper@37906
    93
fun eval_matches (thmid:string) "Tools.matches"
neuper@52070
    94
		  (t as Const ("Tools.matches",_) $ pat $ tst) thy = 
wneuper@59389
    95
    if TermC.matches thy tst pat
neuper@52070
    96
    then 
neuper@52070
    97
      let
wneuper@59389
    98
        val prop = TermC.Trueprop $ (TermC.mk_equality (t, @{term True}))
neuper@52070
    99
	    in SOME (term_to_string''' thy prop, prop) end
neuper@52070
   100
    else 
neuper@52070
   101
      let 
wneuper@59389
   102
        val prop = TermC.Trueprop $ (TermC.mk_equality (t, @{term False}))
neuper@52070
   103
	    in SOME (term_to_string''' thy prop, prop) end
neuper@37906
   104
  | eval_matches _ _ _ _ = NONE; 
neuper@37906
   105
(*
wneuper@59186
   106
> val t  = (Thm.term_of o the o (parse thy)) 
neuper@37906
   107
	      "matches (?x = 0) (1 * x ^^^ 2 = 0)";
neuper@37906
   108
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37906
   109
val it =
neuper@37906
   110
  SOME
neuper@37906
   111
    ("matches (x = 0) (1 * x ^^^ 2 = 0) = False",
neuper@37906
   112
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option
neuper@37906
   113
wneuper@59186
   114
> val t  = (Thm.term_of o the o (parse thy)) 
neuper@37906
   115
	      "matches (?a = #0) (#1 * x ^^^ #2 = #0)";
neuper@37906
   116
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37906
   117
val it =
neuper@37906
   118
  SOME
neuper@37906
   119
    ("matches (?a = #0) (#1 * x ^^^ #2 = #0) = True",
neuper@37906
   120
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option
neuper@37906
   121
wneuper@59186
   122
> val t  = (Thm.term_of o the o (parse thy)) 
neuper@37906
   123
	      "matches (?a * x = #0) (#1 * x ^^^ #2 = #0)";
neuper@37906
   124
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37906
   125
val it =
neuper@37906
   126
  SOME
neuper@37906
   127
    ("matches (?a * x = #0) (#1 * x ^^^ #2 = #0) = False",
neuper@37906
   128
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option
neuper@37906
   129
wneuper@59186
   130
> val t  = (Thm.term_of o the o (parse thy)) 
neuper@37906
   131
	      "matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0)";
neuper@37906
   132
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37906
   133
val it =
neuper@37906
   134
  SOME
neuper@37906
   135
    ("matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0) = True",
neuper@37906
   136
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option                  
neuper@37906
   137
----- before ?patterns ---:
wneuper@59186
   138
> val t  = (Thm.term_of o the o (parse thy)) 
neuper@37906
   139
	      "matches (a * b^^^#2 = c) (#3 * x^^^#2 = #1)";
neuper@37906
   140
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37906
   141
SOME
neuper@37906
   142
    ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2 = #1) = True",
neuper@41924
   143
     Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
neuper@37906
   144
  : (string * term) option 
neuper@37906
   145
wneuper@59186
   146
> val t = (Thm.term_of o the o (parse thy)) 
neuper@37906
   147
	      "matches (a * b^^^#2 = c) (#3 * x^^^#2222 = #1)";
neuper@37906
   148
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37906
   149
SOME ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2222 = #1) = False",
neuper@41924
   150
     Const ("HOL.Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
neuper@37906
   151
wneuper@59186
   152
> val t = (Thm.term_of o the o (parse thy)) 
neuper@37906
   153
               "matches (a = b) (x + #1 + #-1 * #2 = #0)";
neuper@37906
   154
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37906
   155
SOME ("matches (a = b) (x + #1 + #-1 * #2 = #0) = True",Const # $ (# $ #))
neuper@37906
   156
*)
neuper@37906
   157
neuper@37906
   158
(*.does a pattern match some subterm ?.*)
neuper@37906
   159
fun matchsub thy t pat =  
neuper@42423
   160
  let
wneuper@59389
   161
    fun matchs (t as Const _) = TermC.matches thy t pat
wneuper@59389
   162
	      | matchs (t as Free _) = TermC.matches thy t pat
wneuper@59389
   163
	      | matchs (t as Var _) = TermC.matches thy t pat
neuper@42423
   164
	      | matchs (Bound _) = false
neuper@42423
   165
	      | matchs (t as Abs (_, _, body)) = 
wneuper@59389
   166
	          if TermC.matches thy t pat then true else TermC.matches thy body pat
neuper@42423
   167
	      | matchs (t as f1 $ f2) =
wneuper@59389
   168
	          if TermC.matches thy t pat then true 
neuper@42423
   169
	            else if matchs f1 then true else matchs f2
neuper@42423
   170
  in matchs t end;
neuper@37906
   171
neuper@37906
   172
(*("matchsub",("Tools.matchsub",eval_matchsub "#matchsub_")):calc*)
neuper@37906
   173
fun eval_matchsub (thmid:string) "Tools.matchsub"
neuper@52070
   174
		  (t as Const ("Tools.matchsub",_) $ pat $ tst) thy = 
neuper@37906
   175
    if matchsub thy tst pat
neuper@52070
   176
    then 
wneuper@59389
   177
      let val prop = TermC.Trueprop $ (TermC.mk_equality (t, @{term True}))
neuper@52070
   178
      in SOME (term_to_string''' thy prop, prop) end
neuper@52070
   179
    else 
wneuper@59389
   180
      let val prop = TermC.Trueprop $ (TermC.mk_equality (t, @{term False}))
neuper@52070
   181
      in SOME (term_to_string''' thy prop, prop) end
neuper@37906
   182
  | eval_matchsub _ _ _ _ = NONE; 
neuper@37906
   183
neuper@37906
   184
(*get the variables in an isabelle-term*)
neuper@37906
   185
(*("Vars"    ,("Tools.Vars"    ,eval_var "#Vars_")):calc*)
neuper@52070
   186
fun eval_var (thmid:string) "Tools.Vars" (t as (Const(op0,t0) $ arg)) thy = 
neuper@52070
   187
    let 
wneuper@59389
   188
      val t' = ((TermC.list2isalist HOLogic.realT) o TermC.vars) t;
neuper@52070
   189
      val thmId = thmid ^ term_to_string''' thy arg;
wneuper@59389
   190
    in SOME (thmId, TermC.Trueprop $ (TermC.mk_equality (t, t'))) end
neuper@37906
   191
  | eval_var _ _ _ _ = NONE;
neuper@37906
   192
neuper@41922
   193
fun lhs (Const ("HOL.eq",_) $ l $ _) = l
neuper@37906
   194
  | lhs t = error("lhs called with (" ^ term2str t ^ ")");
neuper@37906
   195
(*("lhs"    ,("Tools.lhs"    ,eval_lhs "")):calc*)
neuper@37906
   196
fun eval_lhs _ "Tools.lhs"
neuper@41922
   197
	     (t as (Const ("Tools.lhs",_) $ (Const ("HOL.eq",_) $ l $ _))) _ = 
neuper@37906
   198
    SOME ((term2str t) ^ " = " ^ (term2str l),
wneuper@59389
   199
	  TermC.Trueprop $ (TermC.mk_equality (t, l)))
neuper@37906
   200
  | eval_lhs _ _ _ _ = NONE;
neuper@37906
   201
(*
wneuper@59186
   202
> val t = (Thm.term_of o the o (parse thy)) "lhs (1 * x ^^^ 2 = 0)";
neuper@37906
   203
> val SOME (id,t') = eval_lhs 0 0 t 0;
neuper@37906
   204
val id = "Tools.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
neuper@37906
   205
> term2str t';
neuper@37906
   206
val it = "Tools.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
neuper@37906
   207
*)
neuper@37906
   208
neuper@41922
   209
fun rhs (Const ("HOL.eq",_) $ _ $ r) = r
neuper@37906
   210
  | rhs t = error("rhs called with (" ^ term2str t ^ ")");
neuper@37906
   211
(*("rhs"    ,("Tools.rhs"    ,eval_rhs "")):calc*)
neuper@37906
   212
fun eval_rhs _ "Tools.rhs"
neuper@41922
   213
	     (t as (Const ("Tools.rhs",_) $ (Const ("HOL.eq",_) $ _ $ r))) _ = 
neuper@37906
   214
    SOME ((term2str t) ^ " = " ^ (term2str r),
wneuper@59389
   215
	  TermC.Trueprop $ (TermC.mk_equality (t, r)))
neuper@37906
   216
  | eval_rhs _ _ _ _ = NONE;
neuper@37906
   217
neuper@37906
   218
neuper@37906
   219
(*for evaluating scripts*) 
neuper@37906
   220
neuper@52139
   221
val list_rls = append_rls "list_rls" list_rls [Calc ("Tools.rhs", eval_rhs "")];
neuper@52125
   222
*}
neuper@52125
   223
setup {* KEStore_Elems.add_rlss [("list_rls", (Context.theory_name @{theory}, list_rls))] *}
s1210629013@52145
   224
setup {* KEStore_Elems.add_calcs
s1210629013@52145
   225
  [("matches", ("Tools.matches", eval_matches "#matches_")),
s1210629013@52145
   226
    ("matchsub", ("Tools.matchsub", eval_matchsub "#matchsub_")),
s1210629013@52145
   227
    ("Vars", ("Tools.Vars", eval_var "#Vars_")),
s1210629013@52145
   228
    ("lhs", ("Tools.lhs", eval_lhs "")),
s1210629013@52145
   229
    ("rhs", ("Tools.rhs", eval_rhs ""))] *}
neuper@37906
   230
neuper@37906
   231
end