src/Tools/isac/ProgLang/Prog_Tac.thy
author Walther Neuper <walther.neuper@jku.at>
Thu, 23 Jan 2020 10:48:57 +0100
changeset 59784 9800556c5cfe
parent 59729 ca1d9f75472c
child 59785 b5de2ec15f36
permissions -rw-r--r--
lucin: renaming due to simpler scanning (Istate .. found_accept)
wneuper@59601
     1
(* Title:  tactics, tacticals etc. for scripts
wneuper@59601
     2
   Author: Walther Neuper 000224
wneuper@59601
     3
   (c) due to copyright terms
wneuper@59601
     4
 *)
wneuper@59601
     5
walther@59603
     6
theory Prog_Tac
walther@59717
     7
  imports Program
walther@59603
     8
begin
wneuper@59601
     9
walther@59619
    10
text \<open>Abstract:
walther@59619
    11
  Specific constants for tactics in programs, which each create a step in a calculation
walther@59619
    12
  as a side effect.
walther@59619
    13
  These program-tactics are related to Tactic.input for use in calculations and
walther@59619
    14
  Tactic.T for internal use by lucas-interpretation.
walther@59619
    15
\<close>
walther@59619
    16
wneuper@59601
    17
subsection \<open>arguments of tactic SubProblem\<close>
wneuper@59601
    18
wneuper@59601
    19
typedecl
wneuper@59601
    20
  arg
wneuper@59601
    21
wneuper@59601
    22
consts
wneuper@59601
    23
  REAL        :: "real => arg"
wneuper@59601
    24
  REAL_LIST   :: "(real list) => arg"
wneuper@59601
    25
  REAL_SET    :: "(real set) => arg"
wneuper@59601
    26
  BOOL        :: "bool => arg"
wneuper@59601
    27
  BOOL_LIST   :: "(bool list) => arg"
wneuper@59601
    28
  REAL_REAL   :: "(real => real) => arg"
wneuper@59601
    29
  STRING      :: "(char list) => arg"
wneuper@59601
    30
  STRING_LIST :: "(char list list) => arg"
wneuper@59601
    31
wneuper@59601
    32
subsection \<open>tactics in programs\<close>
wneuper@59601
    33
text \<open>
wneuper@59601
    34
  Note: the items below MUST ALL be recorded in xxx TODO
wneuper@59601
    35
\<close>
wneuper@59601
    36
consts
walther@59634
    37
  Calculate    :: "[char list, 'a] => 'a"
walther@59635
    38
  Rewrite      :: "[char list, 'a] => 'a"
walther@59636
    39
  Rewrite'_Inst:: "[(char list * 'b) list, char list, 'a] => 'a"
walther@59637
    40
			               ("(Rewrite'_Inst (_ _))" 11) (*without last argument ^^ for #>*)
walther@59635
    41
  Rewrite'_Set :: "[char list, 'a] => 'a" ("(Rewrite'_Set (_))" 11)
wneuper@59601
    42
  Rewrite'_Set'_Inst
walther@59636
    43
               :: "[((char list) * 'b) list, char list, 'a] => 'a"
walther@59637
    44
		                 ("(Rewrite'_Set'_Inst (_ _))" 11) (*without last argument ^^ for #>*)
walther@59634
    45
  Or'_to'_List :: "bool => 'a list"  ("Or'_to'_List (_)" 11)
walther@59634
    46
  SubProblem   :: "[char list * char list list * char list list, arg list] => 'a"
walther@59634
    47
  Substitute   :: "[bool list, 'a] => 'a"
walther@59634
    48
  Take         :: "'a => 'a"
walther@59634
    49
walther@59635
    50
  (* legacy.. *)
wneuper@59601
    51
  Check'_elementwise :: (* TODO: is idle, retained for test purposes in Minisubpbl *)
wneuper@59601
    52
		  "['a list, 'b set] => 'a list" ("Check'_elementwise (_ _)" 11)
wneuper@59601
    53
  Assumptions  :: bool  (* TODO: remove with making ^^^ idle *)
walther@59634
    54
wneuper@59601
    55
walther@59716
    56
subsection \<open>ML code for Prog_Tac\<close>
wneuper@59601
    57
ML \<open>
wneuper@59601
    58
signature PROGAM_TACTIC =
wneuper@59601
    59
sig
walther@59659
    60
  val dest_spec : term -> Celem.spec
walther@59659
    61
  val get_stac : 'a -> term -> term option (*rename get_first*)
walther@59729
    62
  val eval_leaf: (term * term) list -> term option -> term -> term -> Program.leaf * term option
walther@59716
    63
  val is: term -> bool
wneuper@59601
    64
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59601
    65
  (*NONE*)
walther@59784
    66
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\*)
wneuper@59601
    67
  (*NONE*)
walther@59784
    68
(*\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59601
    69
end 
wneuper@59601
    70
\<close> ML \<open>
wneuper@59601
    71
(**)
wneuper@59601
    72
structure Prog_Tac(**): PROGAM_TACTIC(**) =
wneuper@59601
    73
struct
wneuper@59601
    74
(**)
wneuper@59601
    75
walther@59618
    76
fun dest_spec (Const ("Product_Type.Pair", _) $ d $ (Const ("Product_Type.Pair", _) $ p $ m)) =
walther@59618
    77
    (d |> HOLogic.dest_string, 
walther@59618
    78
     p |> HOLogic.dest_list |> map HOLogic.dest_string,
walther@59618
    79
     m |> HOLogic.dest_list |> map HOLogic.dest_string) : Celem.spec
walther@59618
    80
  | dest_spec t = raise TERM ("dest_spec: called with ", [t])
walther@59618
    81
walther@59635
    82
(* get argument of first Prog_Tac in a progam for init_form *)
wneuper@59601
    83
fun get_stac thy (_ $ body) =
wneuper@59601
    84
  let
walther@59637
    85
    (* entries occur twice, for form curried by #> or non-curried *)
walther@59688
    86
    fun get_t y (Const ("Tactical.Chain",_) $ e1 $ e2) a = 
wneuper@59601
    87
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
walther@59688
    88
      | get_t y (Const ("Tactical.Chain",_) $ e1 $ e2 $ a) _ = 
wneuper@59601
    89
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
walther@59603
    90
      | get_t y (Const ("Tactical.Try",_) $ e) a = get_t y e a
walther@59603
    91
      | get_t y (Const ("Tactical.Try",_) $ e $ a) _ = get_t y e a
walther@59603
    92
      | get_t y (Const ("Tactical.Repeat",_) $ e) a = get_t y e a
walther@59603
    93
      | get_t y (Const ("Tactical.Repeat",_) $ e $ a) _ = get_t y e a
walther@59603
    94
      | get_t y (Const ("Tactical.Or",_) $e1 $ e2) a =
wneuper@59601
    95
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
walther@59603
    96
      | get_t y (Const ("Tactical.Or",_) $e1 $ e2 $ a) _ =
wneuper@59601
    97
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
walther@59603
    98
      | get_t y (Const ("Tactical.While",_) $ _ $ e) a = get_t y e a
walther@59603
    99
      | get_t y (Const ("Tactical.While",_) $ _ $ e $ a) _ = get_t y e a
walther@59603
   100
      | get_t y (Const ("Tactical.Letpar",_) $ e1 $ Abs (_, _, e2)) a = 
wneuper@59601
   101
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
wneuper@59601
   102
      | get_t y (Const ("HOL.Let",_) $ e1 $ Abs (_, _, _)) a =
walther@59635
   103
    	  get_t y e1 a (* don't go deeper without evaluation *)
wneuper@59601
   104
      | get_t _ (Const ("If", _) $ _ $ _ $ _) _ = NONE
wneuper@59601
   105
    
walther@59635
   106
      | get_t _ (Const ("Prog_Tac.Rewrite",_) $ _ $ a) _ = SOME a
walther@59635
   107
      | get_t _ (Const ("Prog_Tac.Rewrite",_) $ _ ) a = SOME a
walther@59635
   108
      | get_t _ (Const ("Prog_Tac.Rewrite'_Inst",_) $ _ $ _ $ a) _ = SOME a
walther@59635
   109
      | get_t _ (Const ("Prog_Tac.Rewrite'_Inst",_) $ _ $ _ )    a = SOME a
walther@59635
   110
      | get_t _ (Const ("Prog_Tac.Rewrite'_Set",_) $ _ $ a) _ = SOME a
walther@59635
   111
      | get_t _ (Const ("Prog_Tac.Rewrite'_Set",_) $ _ )    a = SOME a
walther@59635
   112
      | get_t _ (Const ("Prog_Tac.Rewrite'_Set'_Inst",_) $ _ $ _ $a)_ =SOME a
walther@59635
   113
      | get_t _ (Const ("Prog_Tac.Rewrite'_Set'_Inst",_) $ _ $ _ )  a =SOME a
wneuper@59601
   114
      | get_t _ (Const ("Prog_Tac.Calculate",_) $ _ $ a) _ = SOME a
wneuper@59601
   115
      | get_t _ (Const ("Prog_Tac.Calculate",_) $ _ )    a = SOME a
wneuper@59601
   116
      | get_t _ (Const ("Prog_Tac.Substitute",_) $ _ $ a) _ = SOME a
wneuper@59601
   117
      | get_t _ (Const ("Prog_Tac.Substitute",_) $ _ )    a = SOME a
wneuper@59601
   118
    
wneuper@59601
   119
      | get_t _ (Const ("Prog_Tac.SubProblem",_) $ _ $ _) _ = NONE
wneuper@59601
   120
wneuper@59601
   121
      | get_t _ _ _ = ((*tracing ("### get_t yac: list-expr "^(term2str x));*) NONE)
wneuper@59601
   122
    in get_t thy body Rule.e_term end
wneuper@59601
   123
  | get_stac _ t = error ("get_stac: no fun-def. for " ^ Rule.term2str t);
wneuper@59601
   124
walther@59635
   125
(* substitute the Istate.T's environment to a leaf of the program
wneuper@59601
   126
   and attach the curried argument of a tactic, if any.
walther@59637
   127
CAUTION: (1) currying with #> requires 2 patterns for each tactic
wneuper@59601
   128
         (2) the non-curried version must return NONE for a 
walther@59717
   129
	       (3) non-matching patterns become an Program.Expr by fall-through.
wneuper@59601
   130
WN060906 quick and dirty fix: due to (2) a is returned, too *)
walther@59718
   131
fun eval_leaf E _ _ (t as (Const ("Prog_Tac.Rewrite"(*1*), _) $ _ $ _)) =
walther@59729
   132
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   133
  | eval_leaf E a v (t as (Const ("Prog_Tac.Rewrite"(*2*), _) $ _)) =
walther@59729
   134
    (Program.Tac (
walther@59717
   135
      case a of SOME a' => (subst_atomic E (t $ a'))
walther@59729
   136
		          | NONE => ((subst_atomic E t) $ v)),
walther@59729
   137
    a)
walther@59718
   138
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Rewrite'_Inst"(*1*), _) $ _ $ _ $ _)) =
walther@59729
   139
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   140
  | eval_leaf E a v (t as (Const ("Prog_Tac.Rewrite'_Inst"(*2*), _) $ _ $ _)) =
walther@59729
   141
    (Program.Tac (
walther@59717
   142
      case a of SOME a' => subst_atomic E (t $ a')
walther@59729
   143
	            | NONE => ((subst_atomic E t) $ v)),
walther@59729
   144
    a)
walther@59718
   145
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Rewrite'_Set"(*1*), _) $ _ $ _)) =
walther@59729
   146
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   147
  | eval_leaf E a v (t as (Const ("Prog_Tac.Rewrite'_Set"(*2*), _) $ _)) =
walther@59729
   148
    (Program.Tac (
walther@59717
   149
      case a of SOME a' => subst_atomic E (t $ a')
walther@59729
   150
	            | NONE => ((subst_atomic E t) $ v)),
walther@59729
   151
    a)
walther@59718
   152
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Rewrite'_Set'_Inst"(*1*), _) $ _ $ _ $ _)) =
walther@59729
   153
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   154
  | eval_leaf E a v (t as (Const ("Prog_Tac.Rewrite'_Set'_Inst"(*2*), _) $ _ $ _)) =
walther@59729
   155
    (Program.Tac (
walther@59717
   156
      case a of SOME a' => subst_atomic E (t $ a')
walther@59729
   157
	            | NONE => ((subst_atomic E t) $ v)),
walther@59729
   158
    a)
walther@59718
   159
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Calculate"(*1*), _) $ _ $ _)) =
walther@59729
   160
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   161
  | eval_leaf E a v (t as (Const ("Prog_Tac.Calculate"(*2*), _) $ _)) =
walther@59729
   162
    (Program.Tac (
walther@59717
   163
      case a of SOME a' => subst_atomic E (t $ a')
walther@59729
   164
	            | NONE => ((subst_atomic E t) $ v)),
walther@59729
   165
    a)
walther@59718
   166
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Check'_elementwise"(*1*),_) $ _ $ _)) = 
walther@59729
   167
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   168
  | eval_leaf E a v (t as (Const ("Prog_Tac.Check'_elementwise"(*2*), _) $ _)) = 
walther@59729
   169
    (Program.Tac (
walther@59717
   170
      case a of SOME a' => subst_atomic E (t $ a')
walther@59729
   171
		          | NONE => ((subst_atomic E t) $ v)),
walther@59729
   172
    a)
walther@59718
   173
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Or'_to'_List"(*1*), _) $ _)) = 
walther@59729
   174
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   175
  | eval_leaf E a v (t as (Const ("Prog_Tac.Or'_to'_List"(*2*), _))) = (*t $ v*)
walther@59729
   176
    (Program.Tac (
walther@59717
   177
      case a of SOME a' => subst_atomic E (t $ a')
walther@59729
   178
		          | NONE => ((subst_atomic E t) $ v)),
walther@59729
   179
    a)
walther@59718
   180
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.SubProblem", _) $ _ $ _)) =
walther@59729
   181
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   182
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Take", _) $ _)) =
walther@59729
   183
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   184
  | eval_leaf E _ _ (t as (Const ("Prog_Tac.Substitute"(*1*), _) $ _ $ _)) =
walther@59729
   185
    (Program.Tac (subst_atomic E t), NONE)
walther@59718
   186
  | eval_leaf E a v (t as (Const ("Prog_Tac.Substitute"(*2*), _) $ _)) =
walther@59729
   187
    (Program.Tac (
walther@59717
   188
      case a of SOME a' => subst_atomic E (t $ a')
walther@59729
   189
		          | NONE => ((subst_atomic E t) $ v)),
walther@59729
   190
    a)
wneuper@59601
   191
  (*now all tactics are matched out and this leaf must be without a tactic*)
walther@59718
   192
  | eval_leaf E a v t = 
walther@59729
   193
    (Program.Expr (subst_atomic (case a of SOME a => Env.update E (a,v) | NONE => E) t), a);
wneuper@59601
   194
walther@59716
   195
walther@59716
   196
(* check if a term is a Prog_Tac *)
walther@59716
   197
walther@59716
   198
val SOME Calculate = TermC.parseNEW @{context} "Calculate";
walther@59716
   199
val SOME Rewrite = TermC.parseNEW @{context} "Rewrite";
walther@59716
   200
val SOME Rewrite_Inst = TermC.parseNEW @{context} "Rewrite'_Inst";
walther@59716
   201
val SOME Rewrite_Set = TermC.parseNEW @{context} "Rewrite'_Set";
walther@59716
   202
val SOME Rewrite_Set_Inst = TermC.parseNEW @{context} "Rewrite'_Set'_Inst";
walther@59716
   203
val SOME Or_to_List = TermC.parseNEW @{context} "Or'_to'_List";
walther@59716
   204
val SOME SubProblem = TermC.parseNEW @{context} "SubProblem";
walther@59716
   205
val SOME Substitute = TermC.parseNEW @{context} "Substitute";
walther@59716
   206
val SOME Take = TermC.parseNEW @{context} "Take";
walther@59716
   207
val SOME Check_elementwise = TermC.parseNEW @{context} "Check'_elementwise";
walther@59716
   208
val SOME Assumptions = TermC.parseNEW @{context} "Assumptions";
walther@59716
   209
walther@59716
   210
val all_Consts = [Calculate, Rewrite, Rewrite_Inst, Rewrite_Set, Rewrite_Set_Inst, Or_to_List,
walther@59716
   211
  SubProblem, Substitute, Take, Check_elementwise, Assumptions]
walther@59716
   212
walther@59716
   213
fun is t = TermC.contains_Const_typeless all_Consts t
wneuper@59601
   214
(**)end(**)
wneuper@59601
   215
\<close> ML \<open>
wneuper@59601
   216
\<close> 
wneuper@59601
   217
subsection \<open>TODO\<close>
wneuper@59601
   218
ML \<open>
wneuper@59601
   219
\<close> ML \<open>
wneuper@59601
   220
\<close> ML \<open>
wneuper@59601
   221
\<close> 
wneuper@59601
   222
end