src/Tools/isac/BridgeJEdit/Calculation.thy
author Walther Neuper <walther.neuper@jku.at>
Thu, 17 Dec 2020 09:10:30 +0100
changeset 60134 85ce6e27e130
parent 60133 83003c700845
child 60146 aaef037414a1
permissions -rw-r--r--
step 4.2: writeln at calling Output.report uncover some of proof handling
walther@60096
     1
(*  Title:      src/Tools/isac/BridgeJEdit/Calculation.thy
walther@60095
     2
    Author:     Walther Neuper, JKU Linz
walther@60095
     3
    (c) due to copyright terms
walther@60095
     4
walther@60121
     5
Outer syntax for Isabelle/Isac's Calculation.
walther@60095
     6
*)
walther@60095
     7
walther@60095
     8
theory Calculation
walther@60115
     9
imports
walther@60115
    10
  "~~/src/Tools/isac/MathEngine/MathEngine"
walther@60121
    11
(**)"HOL-SPARK.SPARK" (** ) preliminarily for parsing Example files *)
walther@60123
    12
(** )"HOL-Word.Word"( ** ) trial on ERROR in definition sdiv below *)
walther@60095
    13
keywords
walther@60127
    14
    "Example" :: thy_load ("str") (* "spark_vc" :: thy_goal *)
walther@60132
    15
  and "Problem" :: thy_decl (* root-problem + recursively in Solution;
walther@60132
    16
                               "spark_vc" :: thy_goal *)
walther@60127
    17
  and "Specification" "Model" "References" "Solution" (* structure only *)
walther@60127
    18
  and"Given" "Find" "Relate" "Where" (* await input of term *)
walther@60127
    19
  and "RTheory" (* await input of string; "R" distingues "Problem".."RProblem" *)
walther@60127
    20
  and "RProblem" "RMethod" (* await input of string list *)
walther@60127
    21
    "Tactic" (* optional for each step 0..end of calculation *)
walther@60095
    22
begin
walther@60095
    23
walther@60123
    24
(**)ML_file parseC.sml(**)
walther@60095
    25
walther@60123
    26
section \<open>new code for Outer_Syntax Example, Problem, ...\<close>
walther@60117
    27
walther@60123
    28
subsection \<open>code for Example\<close>
walther@60106
    29
ML \<open>
walther@60097
    30
\<close> ML \<open>
walther@60114
    31
structure Refs_Data = Theory_Data
walther@60114
    32
(
walther@60114
    33
  type T = References.T
walther@60114
    34
  val empty : T = References.empty
walther@60114
    35
  val extend = I
walther@60114
    36
  fun merge (_, refs) = refs
walther@60114
    37
);
walther@60116
    38
(*/----------------------------- shift to test/../? ----------------------------------------\*)
walther@60116
    39
(* Pure/context.ML
walther@60116
    40
signature THEORY_DATA =
walther@60116
    41
sig
walther@60116
    42
  type T
walther@60116
    43
  val get: theory -> T
walther@60116
    44
  val put: T -> theory -> theory
walther@60116
    45
  val map: (T -> T) -> theory -> theory
walther@60116
    46
end;*)
walther@60114
    47
(*
walther@60114
    48
Refs_Data.empty;                                    (*in Refs_Data defined workers are hidden*)
walther@60114
    49
Refs_Data.extend;                                   (*in Refs_Data defined workers are hidden*)
walther@60114
    50
Refs_Data.merge;                                    (*in Refs_Data defined workers are hidden*)
walther@60114
    51
ML error\<^here>:
walther@60114
    52
Value or constructor (empty) has not been declared in structure Refs *)
walther@60114
    53
walther@60114
    54
Refs_Data.get: theory -> Refs_Data.T;                            (*from signature THEORY_DATA*)
walther@60114
    55
Refs_Data.put: Refs_Data.T -> theory -> theory;                  (*from signature THEORY_DATA*)
walther@60114
    56
Refs_Data.map: (Refs_Data.T -> Refs_Data.T) -> theory -> theory; (*from signature THEORY_DATA*)
walther@60116
    57
(*\----------------------------- shift to test/../? ----------------------------------------/*)
walther@60114
    58
\<close> ML \<open>
walther@60114
    59
structure OMod_Data = Theory_Data
walther@60114
    60
(
walther@60114
    61
  type T = O_Model.T
walther@60114
    62
  val empty : T = []
walther@60114
    63
  val extend = I
walther@60114
    64
  fun merge (_, o_model) = o_model
walther@60114
    65
)
walther@60114
    66
\<close> ML \<open>
walther@60114
    67
structure Ctxt_Data = Theory_Data
walther@60114
    68
(
walther@60114
    69
  type T = Proof.context
walther@60114
    70
  val empty : T = ContextC.empty
walther@60114
    71
  val extend = I
walther@60114
    72
  fun merge (_, ctxt) = ctxt
walther@60114
    73
)
walther@60114
    74
\<close> ML \<open>
walther@60132
    75
(*
walther@60132
    76
store_and_show: Formalise.T list -> theory -> theory;
walther@60132
    77
*)
walther@60122
    78
fun store_and_show formalise thy =
walther@60101
    79
  let
walther@60122
    80
(**)val file_read_correct = case formalise of xxx as
walther@60110
    81
      [(["Traegerlaenge L", "Streckenlast q_0", "Biegelinie y", "Randbedingungen [y 0 = (0::real), y L = 0, M_b 0 = 0, M_b L = 0]", "FunktionsVariable x", "GleichungsVariablen [c, c_2, c_3, c_4]", "AbleitungBiegelinie dy"],
walther@60114
    82
        ("Biegelinie", ["Biegelinien"], ["IntegrierenUndKonstanteBestimmen2"]))] => hd xxx
walther@60110
    83
      | _ => error "magic failed: handling file exp_Statics_Biegel_Timischl_7-70.str failed"
walther@60122
    84
(**)
walther@60122
    85
    val formalise = hd formalise (*we expect only one Formalise.T in the list*)
walther@60114
    86
    val (hdlPIDE, _, refs, o_model, var_type_ctxt) = Step_Specify.initialisePIDE formalise
walther@60126
    87
  (*              ^^ TODO remove with cleanup in nxt_specify_init_calc *)
walther@60126
    88
(*
walther@60126
    89
  TODO: present "Problem hdlPIDE" via PIDE
walther@60126
    90
*)
walther@60101
    91
  in
walther@60116
    92
    thy
walther@60114
    93
      |> Refs_Data.put refs
walther@60114
    94
      |> OMod_Data.put o_model
walther@60114
    95
      |> Ctxt_Data.put var_type_ctxt
walther@60101
    96
  end;
walther@60097
    97
\<close> ML \<open>
walther@60132
    98
store_and_show: Formalise.T list -> theory -> theory;
walther@60132
    99
\<close> ML \<open>
walther@60132
   100
\<close> ML \<open>
walther@60122
   101
fun load_formalisation parser (files, _) thy =
walther@60101
   102
  let
walther@60122
   103
    val ([{lines, pos, ...}: Token.file], thy') = files thy;
walther@60101
   104
  in
walther@60122
   105
    store_and_show
walther@60132
   106
      (ParseC.read_out_formalise (fst (parser (fst (ParseC.tokenize_formalise pos (cat_lines lines))))))
walther@60117
   107
        thy'
walther@60101
   108
  end;
walther@60132
   109
fun load_formalisation parse (files, _) thy =
walther@60132
   110
  let
walther@60132
   111
    val ([{lines, pos, ...}: Token.file], thy') = files thy;
walther@60132
   112
    val formalise = lines
walther@60132
   113
      |> cat_lines 
walther@60132
   114
      |> ParseC.tokenize_formalise pos
walther@60132
   115
      |> fst
walther@60132
   116
      |> parse
walther@60132
   117
      |> fst
walther@60132
   118
      |> ParseC.read_out_formalise
walther@60132
   119
      |> store_and_show
walther@60132
   120
    val _ = @{print} {a = "### load_formalisation \<rightarrow>", formalise = formalise}
walther@60132
   121
  in
walther@60132
   122
    formalise thy'
walther@60132
   123
  end;
walther@60097
   124
\<close> ML \<open>
walther@60123
   125
\<close>
walther@60123
   126
walther@60123
   127
subsection \<open>code to write Problem ("Biegelinie", ["Biegelinien"]) to thy\<close>
walther@60123
   128
ML \<open>
walther@60107
   129
\<close> ML \<open>
walther@60110
   130
\<close> ML \<open>
walther@60123
   131
\<close> ML \<open>
walther@60123
   132
\<close>
walther@60123
   133
walther@60123
   134
subsection \<open>code for Problem\<close>
walther@60123
   135
text \<open>
walther@60123
   136
  Again, we copy code from spark_vc into Calculation.thy and
walther@60123
   137
  add functionality for Problem
walther@60123
   138
  such that the code keeps running during adaption from spark_vc to Problem.
walther@60123
   139
\<close> ML \<open>
walther@60123
   140
\<close> ML \<open>
walther@60123
   141
\<close> 
walther@60123
   142
walther@60123
   143
subsubsection \<open>copied from fdl_parser.ML\<close>
walther@60123
   144
ML \<open>
walther@60123
   145
\<close> ML \<open>
walther@60123
   146
datatype expr =
walther@60123
   147
    Ident of string
walther@60123
   148
  | Number of int
walther@60123
   149
  | Quantifier of string * string list * string * expr
walther@60123
   150
  | Funct of string * expr list
walther@60123
   151
  | Element of expr * expr list
walther@60123
   152
  | Update of expr * expr list * expr
walther@60123
   153
  | Record of string * (string * expr) list
walther@60123
   154
  | Array of string * expr option *
walther@60123
   155
      ((expr * expr option) list list * expr) list;
walther@60123
   156
\<close> ML \<open>
walther@60123
   157
  datatype fdl_type =
walther@60123
   158
      Basic_Type of string
walther@60123
   159
    | Enum_Type of string list
walther@60123
   160
    | Array_Type of string list * string
walther@60123
   161
    | Record_Type of (string list * string) list
walther@60123
   162
    | Pending_Type;
walther@60123
   163
\<close> ML \<open>
walther@60123
   164
(* also store items in a list to preserve order *)
walther@60123
   165
type 'a tab = 'a Symtab.table * (string * 'a) list;
walther@60123
   166
\<close> ML \<open>
walther@60123
   167
fun lookup ((tab, _) : 'a tab) = Symtab.lookup tab;
walther@60123
   168
\<close> ML \<open>
walther@60123
   169
\<close>
walther@60123
   170
subsubsection \<open>copied from spark_vcs.ML\<close>
walther@60123
   171
ML \<open>
walther@60123
   172
\<close> ML \<open>
walther@60129
   173
(**)
walther@60123
   174
fun err_unfinished () = error "An unfinished SPARK environment is still open."
walther@60123
   175
\<close> ML \<open>
walther@60123
   176
val strip_number = apply2 implode o chop_suffix Fdl_Lexer.is_digit o raw_explode;
walther@60123
   177
\<close> ML \<open>
walther@60123
   178
val name_ord = prod_ord string_ord (option_ord int_ord) o
walther@60123
   179
  apply2 (strip_number ##> Int.fromString);
walther@60123
   180
\<close> ML \<open>
walther@60123
   181
structure VCtab = Table(type key = string val ord = name_ord);
walther@60123
   182
\<close> ML \<open>
walther@60123
   183
val builtin = Symtab.make (map (rpair ())
walther@60123
   184
  ["->", "<->", "or", "and", "not", "=", "<>", "<", ">", "<=", ">=",
walther@60123
   185
   "+", "-", "*", "/", "div", "mod", "**"]);
walther@60123
   186
\<close> ML \<open>
walther@60123
   187
val is_pfun =
walther@60123
   188
  Symtab.defined builtin orf
walther@60123
   189
  can (unprefix "fld_") orf can (unprefix "upf_") orf
walther@60123
   190
  can (unsuffix "__pos") orf can (unsuffix "__val") orf
walther@60123
   191
  equal "succ" orf equal "pred";
walther@60123
   192
\<close> ML \<open>
walther@60123
   193
fun fold_opt f = the_default I o Option.map f;
walther@60123
   194
fun fold_pair f g (x, y) = f x #> g y;
walther@60123
   195
\<close> ML \<open>
walther@60123
   196
fun fold_expr f g (Funct (s, es)) = f s #> fold (fold_expr f g) es
walther@60123
   197
  | fold_expr f g (Ident s) = g s
walther@60123
   198
  | fold_expr f g (Number _) = I
walther@60123
   199
  | fold_expr f g (Quantifier (_, _, _, e)) = fold_expr f g e
walther@60123
   200
  | fold_expr f g (Element (e, es)) =
walther@60123
   201
      fold_expr f g e #> fold (fold_expr f g) es
walther@60123
   202
  | fold_expr f g (Update (e, es, e')) =
walther@60123
   203
      fold_expr f g e #> fold (fold_expr f g) es #> fold_expr f g e'
walther@60123
   204
  | fold_expr f g (Record (_, flds)) = fold (fold_expr f g o snd) flds
walther@60123
   205
  | fold_expr f g (Array (_, default, assocs)) =
walther@60123
   206
      fold_opt (fold_expr f g) default #>
walther@60123
   207
      fold (fold_pair
walther@60123
   208
        (fold (fold (fold_pair
walther@60123
   209
          (fold_expr f g) (fold_opt (fold_expr f g)))))
walther@60123
   210
        (fold_expr f g)) assocs;
walther@60123
   211
\<close> ML \<open>
walther@60123
   212
fun add_expr_pfuns funs = fold_expr
walther@60123
   213
  (fn s => if is_pfun s then I else insert (op =) s)
walther@60123
   214
  (fn s => if is_none (lookup funs s) then I else insert (op =) s);
walther@60123
   215
\<close> ML \<open>
walther@60123
   216
fun fold_vcs f vcs =
walther@60123
   217
  VCtab.fold (fn (_, (_, _, ps, cs)) => fold f ps #> fold f cs) vcs;
walther@60123
   218
\<close> ML \<open>
walther@60123
   219
fun lookup_prfx "" tab s = Symtab.lookup tab s
walther@60123
   220
  | lookup_prfx prfx tab s = (case Symtab.lookup tab s of
walther@60123
   221
        NONE => Symtab.lookup tab (prfx ^ "__" ^ s)
walther@60123
   222
      | x => x);
walther@60123
   223
\<close> ML \<open>
walther@60123
   224
fun pfuns_of_vcs prfx funs pfuns vcs =
walther@60123
   225
  fold_vcs (add_expr_pfuns funs o snd) vcs [] |>
walther@60123
   226
  filter (is_none o lookup_prfx prfx pfuns);
walther@60123
   227
\<close> ML \<open>
walther@60123
   228
fun pfuns_of_vcs prfx funs pfuns vcs =
walther@60123
   229
  fold_vcs (add_expr_pfuns funs o snd) vcs [] |>
walther@60123
   230
  filter (is_none o lookup_prfx prfx pfuns);
walther@60123
   231
\<close> ML \<open>
walther@60123
   232
structure VCs = Theory_Data
walther@60123
   233
(
walther@60123
   234
  type T =
walther@60123
   235
    {pfuns: ((string list * string) option * term) Symtab.table,
walther@60123
   236
     type_map: (typ * (string * string) list) Symtab.table,
walther@60123
   237
     env:
walther@60123
   238
       {ctxt: Element.context_i list,
walther@60123
   239
        defs: (binding * thm) list,
walther@60123
   240
        types: fdl_type tab,
walther@60123
   241
        funs: (string list * string) tab,
walther@60123
   242
        pfuns: ((string list * string) option * term) Symtab.table,
walther@60123
   243
        ids: (term * string) Symtab.table * Name.context,
walther@60123
   244
        proving: bool,
walther@60123
   245
        vcs: (string * thm list option *
walther@60123
   246
          (string * expr) list * (string * expr) list) VCtab.table,
walther@60123
   247
        path: Path.T,
walther@60123
   248
        prefix: string} option}
walther@60123
   249
  val empty : T = {pfuns = Symtab.empty, type_map = Symtab.empty, env = NONE}
walther@60123
   250
  val extend = I
walther@60123
   251
  fun merge ({pfuns = pfuns1, type_map = type_map1, env = NONE},
walther@60123
   252
        {pfuns = pfuns2, type_map = type_map2, env = NONE}) =
walther@60123
   253
        {pfuns = Symtab.merge (eq_pair (op =) (op aconv)) (pfuns1, pfuns2),
walther@60123
   254
         type_map = Symtab.merge (op =) (type_map1, type_map2),
walther@60123
   255
         env = NONE}
walther@60123
   256
    | merge _ = err_unfinished ()
walther@60123
   257
)
walther@60123
   258
\<close> ML \<open>
walther@60123
   259
fun get_type thy prfx ty =
walther@60123
   260
  let val {type_map, ...} = VCs.get thy
walther@60123
   261
  in lookup_prfx prfx type_map ty end;
walther@60123
   262
\<close> ML \<open>
walther@60123
   263
fun mk_type' _ _ "integer" = (HOLogic.intT, [])
walther@60123
   264
  | mk_type' _ _ "boolean" = (HOLogic.boolT, [])
walther@60123
   265
  | mk_type' thy prfx ty =
walther@60123
   266
      (case get_type thy prfx ty of
walther@60123
   267
         NONE =>
walther@60123
   268
           (Syntax.check_typ (Proof_Context.init_global thy)
walther@60123
   269
              (Type (Sign.full_name thy (Binding.name ty), [])),
walther@60123
   270
            [])
walther@60123
   271
       | SOME p => p);
walther@60123
   272
\<close> ML \<open>
walther@60123
   273
fun mk_type thy prfx ty = fst (mk_type' thy prfx ty);
walther@60123
   274
\<close> ML \<open>
walther@60123
   275
fun pfun_type thy prfx (argtys, resty) =
walther@60123
   276
  map (mk_type thy prfx) argtys ---> mk_type thy prfx resty;
walther@60123
   277
\<close> ML \<open>
walther@60123
   278
fun declare_missing_pfuns thy prfx funs pfuns vcs (tab, ctxt) =
walther@60123
   279
  let
walther@60123
   280
    val (fs, (tys, Ts)) =
walther@60123
   281
      pfuns_of_vcs prfx funs pfuns vcs |>
walther@60123
   282
      map_filter (fn s => lookup funs s |>
walther@60123
   283
        Option.map (fn ty => (s, (SOME ty, pfun_type thy prfx ty)))) |>
walther@60123
   284
      split_list ||> split_list;
walther@60123
   285
    val (fs', ctxt') = fold_map Name.variant fs ctxt
walther@60123
   286
  in
walther@60123
   287
    (fold Symtab.update_new (fs ~~ (tys ~~ map Free (fs' ~~ Ts))) pfuns,
walther@60123
   288
     Element.Fixes (map2 (fn s => fn T =>
walther@60123
   289
       (Binding.name s, SOME T, NoSyn)) fs' Ts),
walther@60123
   290
     (tab, ctxt'))
walther@60123
   291
  end;
walther@60123
   292
\<close> ML \<open>
walther@60123
   293
fun strip_underscores s =
walther@60123
   294
  strip_underscores (unsuffix "_" s) handle Fail _ => s;
walther@60123
   295
walther@60123
   296
fun strip_tilde s =
walther@60123
   297
  unsuffix "~" s ^ "_init" handle Fail _ => s;
walther@60123
   298
\<close> ML \<open>
walther@60123
   299
val mangle_name = strip_underscores #> strip_tilde;
walther@60123
   300
\<close> ML \<open>
walther@60123
   301
fun mk_variables thy prfx xs ty (tab, ctxt) =
walther@60123
   302
  let
walther@60123
   303
    val T = mk_type thy prfx ty;
walther@60123
   304
    val (ys, ctxt') = fold_map Name.variant (map mangle_name xs) ctxt;
walther@60123
   305
    val zs = map (Free o rpair T) ys;
walther@60123
   306
  in (zs, (fold (Symtab.update o apsnd (rpair ty)) (xs ~~ zs) tab, ctxt')) end;
walther@60123
   307
\<close> ML \<open>
walther@60123
   308
fun mk_unop s t =
walther@60123
   309
  let val T = fastype_of t
walther@60123
   310
  in Const (s, T --> T) $ t end;
walther@60123
   311
\<close> ML \<open>
walther@60123
   312
val booleanN = "boolean";
walther@60123
   313
val integerN = "integer";
walther@60123
   314
\<close> ML \<open>
walther@60123
   315
val to_lower = raw_explode #> map Symbol.to_ascii_lower #> implode;
walther@60123
   316
val lcase_eq = (op =) o apply2 (to_lower o Long_Name.base_name);
walther@60123
   317
\<close> ML \<open>
walther@60123
   318
fun find_field [] fname fields =
walther@60123
   319
      find_first (curry lcase_eq fname o fst) fields
walther@60123
   320
  | find_field cmap fname fields =
walther@60123
   321
      (case AList.lookup (op =) cmap fname of
walther@60123
   322
         NONE => NONE
walther@60123
   323
       | SOME fname' => SOME (fname', the (AList.lookup (op =) fields fname')));
walther@60123
   324
\<close> ML \<open>
walther@60123
   325
fun get_record_info thy T = (case Record.dest_recTs T of
walther@60123
   326
    [(tyname, [\<^typ>\<open>unit\<close>])] =>
walther@60123
   327
      Record.get_info thy (Long_Name.qualifier tyname)
walther@60123
   328
  | _ => NONE);
walther@60123
   329
\<close> ML \<open>
walther@60123
   330
fun find_field' fname = get_first (fn (flds, fldty) =>
walther@60123
   331
  if member (op =) flds fname then SOME fldty else NONE);
walther@60123
   332
\<close> ML \<open>
walther@60123
   333
fun invert_map [] = I
walther@60123
   334
  | invert_map cmap =
walther@60123
   335
      map (apfst (the o AList.lookup (op =) (map swap cmap)));
walther@60123
   336
\<close> ML \<open>
walther@60123
   337
fun mk_times (t, u) =
walther@60123
   338
  let
walther@60123
   339
    val setT = fastype_of t;
walther@60123
   340
    val T = HOLogic.dest_setT setT;
walther@60123
   341
    val U = HOLogic.dest_setT (fastype_of u)
walther@60123
   342
  in
walther@60123
   343
    Const (\<^const_name>\<open>Sigma\<close>, setT --> (T --> HOLogic.mk_setT U) -->
walther@60123
   344
      HOLogic.mk_setT (HOLogic.mk_prodT (T, U))) $ t $ Abs ("", T, u)
walther@60123
   345
  end;
walther@60123
   346
\<close> ML \<open>
walther@60123
   347
fun term_of_expr thy prfx types pfuns =
walther@60123
   348
  let
walther@60123
   349
    fun tm_of vs (Funct ("->", [e, e'])) =
walther@60123
   350
          (HOLogic.mk_imp (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
walther@60123
   351
walther@60123
   352
      | tm_of vs (Funct ("<->", [e, e'])) =
walther@60123
   353
          (HOLogic.mk_eq (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
walther@60123
   354
walther@60123
   355
      | tm_of vs (Funct ("or", [e, e'])) =
walther@60123
   356
          (HOLogic.mk_disj (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
walther@60123
   357
walther@60123
   358
      | tm_of vs (Funct ("and", [e, e'])) =
walther@60123
   359
          (HOLogic.mk_conj (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
walther@60123
   360
walther@60123
   361
      | tm_of vs (Funct ("not", [e])) =
walther@60123
   362
          (HOLogic.mk_not (fst (tm_of vs e)), booleanN)
walther@60123
   363
walther@60123
   364
      | tm_of vs (Funct ("=", [e, e'])) =
walther@60123
   365
          (HOLogic.mk_eq (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
walther@60123
   366
walther@60123
   367
      | tm_of vs (Funct ("<>", [e, e'])) = (HOLogic.mk_not
walther@60123
   368
          (HOLogic.mk_eq (fst (tm_of vs e), fst (tm_of vs e'))), booleanN)
walther@60123
   369
walther@60123
   370
      | tm_of vs (Funct ("<", [e, e'])) = (HOLogic.mk_binrel \<^const_name>\<open>less\<close>
walther@60123
   371
          (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
walther@60123
   372
walther@60123
   373
      | tm_of vs (Funct (">", [e, e'])) = (HOLogic.mk_binrel \<^const_name>\<open>less\<close>
walther@60123
   374
          (fst (tm_of vs e'), fst (tm_of vs e)), booleanN)
walther@60123
   375
walther@60123
   376
      | tm_of vs (Funct ("<=", [e, e'])) = (HOLogic.mk_binrel \<^const_name>\<open>less_eq\<close>
walther@60123
   377
          (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
walther@60123
   378
walther@60123
   379
      | tm_of vs (Funct (">=", [e, e'])) = (HOLogic.mk_binrel \<^const_name>\<open>less_eq\<close>
walther@60123
   380
          (fst (tm_of vs e'), fst (tm_of vs e)), booleanN)
walther@60123
   381
walther@60123
   382
      | tm_of vs (Funct ("+", [e, e'])) = (HOLogic.mk_binop \<^const_name>\<open>plus\<close>
walther@60123
   383
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
walther@60123
   384
walther@60123
   385
      | tm_of vs (Funct ("-", [e, e'])) = (HOLogic.mk_binop \<^const_name>\<open>minus\<close>
walther@60123
   386
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
walther@60123
   387
walther@60123
   388
      | tm_of vs (Funct ("*", [e, e'])) = (HOLogic.mk_binop \<^const_name>\<open>times\<close>
walther@60123
   389
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
walther@60123
   390
walther@60123
   391
      | tm_of vs (Funct ("/", [e, e'])) = (HOLogic.mk_binop \<^const_name>\<open>divide\<close>
walther@60123
   392
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
walther@60123
   393
walther@60123
   394
      | tm_of vs (Funct ("div", [e, e'])) = (HOLogic.mk_binop \<^const_name>\<open>sdiv\<close>
walther@60123
   395
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
walther@60123
   396
walther@60123
   397
      | tm_of vs (Funct ("mod", [e, e'])) = (HOLogic.mk_binop \<^const_name>\<open>modulo\<close>
walther@60123
   398
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
walther@60123
   399
walther@60123
   400
      | tm_of vs (Funct ("-", [e])) =
walther@60123
   401
          (mk_unop \<^const_name>\<open>uminus\<close> (fst (tm_of vs e)), integerN)
walther@60123
   402
walther@60123
   403
      | tm_of vs (Funct ("**", [e, e'])) =
walther@60123
   404
          (Const (\<^const_name>\<open>power\<close>, HOLogic.intT --> HOLogic.natT -->
walther@60123
   405
             HOLogic.intT) $ fst (tm_of vs e) $
walther@60123
   406
               (\<^const>\<open>nat\<close> $ fst (tm_of vs e')), integerN)
walther@60123
   407
walther@60123
   408
      | tm_of (tab, _) (Ident s) =
walther@60123
   409
          (case Symtab.lookup tab s of
walther@60123
   410
             SOME t_ty => t_ty
walther@60123
   411
           | NONE => (case lookup_prfx prfx pfuns s of
walther@60123
   412
               SOME (SOME ([], resty), t) => (t, resty)
walther@60123
   413
             | _ => error ("Undeclared identifier " ^ s)))
walther@60123
   414
walther@60123
   415
      | tm_of _ (Number i) = (HOLogic.mk_number HOLogic.intT i, integerN)
walther@60123
   416
walther@60123
   417
      | tm_of vs (Quantifier (s, xs, ty, e)) =
walther@60123
   418
          let
walther@60123
   419
            val (ys, vs') = mk_variables thy prfx xs ty vs;
walther@60123
   420
            val q = (case s of
walther@60123
   421
                "for_all" => HOLogic.mk_all
walther@60123
   422
              | "for_some" => HOLogic.mk_exists)
walther@60123
   423
          in
walther@60123
   424
            (fold_rev (fn Free (x, T) => fn t => q (x, T, t))
walther@60123
   425
               ys (fst (tm_of vs' e)),
walther@60123
   426
             booleanN)
walther@60123
   427
          end
walther@60123
   428
walther@60123
   429
      | tm_of vs (Funct (s, es)) =
walther@60123
   430
walther@60123
   431
          (* record field selection *)
walther@60123
   432
          (case try (unprefix "fld_") s of
walther@60123
   433
             SOME fname => (case es of
walther@60123
   434
               [e] =>
walther@60123
   435
                 let
walther@60123
   436
                   val (t, rcdty) = tm_of vs e;
walther@60123
   437
                   val (rT, cmap) = mk_type' thy prfx rcdty
walther@60123
   438
                 in case (get_record_info thy rT, lookup types rcdty) of
walther@60123
   439
                     (SOME {fields, ...}, SOME (Record_Type fldtys)) =>
walther@60123
   440
                       (case (find_field cmap fname fields,
walther@60123
   441
                            find_field' fname fldtys) of
walther@60123
   442
                          (SOME (fname', fT), SOME fldty) =>
walther@60123
   443
                            (Const (fname', rT --> fT) $ t, fldty)
walther@60123
   444
                        | _ => error ("Record " ^ rcdty ^
walther@60123
   445
                            " has no field named " ^ fname))
walther@60123
   446
                   | _ => error (rcdty ^ " is not a record type")
walther@60123
   447
                 end
walther@60123
   448
             | _ => error ("Function " ^ s ^ " expects one argument"))
walther@60123
   449
           | NONE =>
walther@60123
   450
walther@60123
   451
          (* record field update *)
walther@60123
   452
          (case try (unprefix "upf_") s of
walther@60123
   453
             SOME fname => (case es of
walther@60123
   454
               [e, e'] =>
walther@60123
   455
                 let
walther@60123
   456
                   val (t, rcdty) = tm_of vs e;
walther@60123
   457
                   val (rT, cmap) = mk_type' thy prfx rcdty;
walther@60123
   458
                   val (u, fldty) = tm_of vs e';
walther@60123
   459
                   val fT = mk_type thy prfx fldty
walther@60123
   460
                 in case get_record_info thy rT of
walther@60123
   461
                     SOME {fields, ...} =>
walther@60123
   462
                       (case find_field cmap fname fields of
walther@60123
   463
                          SOME (fname', fU) =>
walther@60123
   464
                            if fT = fU then
walther@60123
   465
                              (Const (fname' ^ "_update",
walther@60123
   466
                                 (fT --> fT) --> rT --> rT) $
walther@60123
   467
                                   Abs ("x", fT, u) $ t,
walther@60123
   468
                               rcdty)
walther@60123
   469
                            else error ("Type\n" ^
walther@60123
   470
                              Syntax.string_of_typ_global thy fT ^
walther@60123
   471
                              "\ndoes not match type\n" ^
walther@60123
   472
                              Syntax.string_of_typ_global thy fU ^
walther@60123
   473
                              "\nof field " ^ fname)
walther@60123
   474
                        | NONE => error ("Record " ^ rcdty ^
walther@60123
   475
                            " has no field named " ^ fname))
walther@60123
   476
                   | _ => error (rcdty ^ " is not a record type")
walther@60123
   477
                 end
walther@60123
   478
             | _ => error ("Function " ^ s ^ " expects two arguments"))
walther@60123
   479
           | NONE =>
walther@60123
   480
walther@60123
   481
          (* enumeration type to integer *)
walther@60123
   482
          (case try (unsuffix "__pos") s of
walther@60123
   483
             SOME tyname => (case es of
walther@60123
   484
               [e] => (Const (\<^const_name>\<open>pos\<close>,
walther@60123
   485
                   mk_type thy prfx tyname --> HOLogic.intT) $ fst (tm_of vs e),
walther@60123
   486
                 integerN)
walther@60123
   487
             | _ => error ("Function " ^ s ^ " expects one argument"))
walther@60123
   488
           | NONE =>
walther@60123
   489
walther@60123
   490
          (* integer to enumeration type *)
walther@60123
   491
          (case try (unsuffix "__val") s of
walther@60123
   492
             SOME tyname => (case es of
walther@60123
   493
               [e] => (Const (\<^const_name>\<open>val\<close>,
walther@60123
   494
                   HOLogic.intT --> mk_type thy prfx tyname) $ fst (tm_of vs e),
walther@60123
   495
                 tyname)
walther@60123
   496
             | _ => error ("Function " ^ s ^ " expects one argument"))
walther@60123
   497
           | NONE =>
walther@60123
   498
walther@60123
   499
          (* successor / predecessor of enumeration type element *)
walther@60123
   500
          if s = "succ" orelse s = "pred" then (case es of
walther@60123
   501
              [e] =>
walther@60123
   502
                let
walther@60123
   503
                  val (t, tyname) = tm_of vs e;
walther@60123
   504
                  val T = mk_type thy prfx tyname
walther@60123
   505
                in (Const
walther@60123
   506
                  (if s = "succ" then \<^const_name>\<open>succ\<close>
walther@60123
   507
                   else \<^const_name>\<open>pred\<close>, T --> T) $ t, tyname)
walther@60123
   508
                end
walther@60123
   509
            | _ => error ("Function " ^ s ^ " expects one argument"))
walther@60123
   510
walther@60123
   511
          (* user-defined proof function *)
walther@60123
   512
          else
walther@60123
   513
            (case lookup_prfx prfx pfuns s of
walther@60123
   514
               SOME (SOME (_, resty), t) =>
walther@60123
   515
                 (list_comb (t, map (fst o tm_of vs) es), resty)
walther@60123
   516
             | _ => error ("Undeclared proof function " ^ s))))))
walther@60123
   517
walther@60123
   518
      | tm_of vs (Element (e, es)) =
walther@60123
   519
          let val (t, ty) = tm_of vs e
walther@60123
   520
          in case lookup types ty of
walther@60123
   521
              SOME (Array_Type (_, elty)) =>
walther@60123
   522
                (t $ foldr1 HOLogic.mk_prod (map (fst o tm_of vs) es), elty)
walther@60123
   523
            | _ => error (ty ^ " is not an array type")
walther@60123
   524
          end
walther@60123
   525
walther@60123
   526
      | tm_of vs (Update (e, es, e')) =
walther@60123
   527
          let val (t, ty) = tm_of vs e
walther@60123
   528
          in case lookup types ty of
walther@60123
   529
              SOME (Array_Type (idxtys, elty)) =>
walther@60123
   530
                let
walther@60123
   531
                  val T = foldr1 HOLogic.mk_prodT
walther@60123
   532
                    (map (mk_type thy prfx) idxtys);
walther@60123
   533
                  val U = mk_type thy prfx elty;
walther@60123
   534
                  val fT = T --> U
walther@60123
   535
                in
walther@60123
   536
                  (Const (\<^const_name>\<open>fun_upd\<close>, fT --> T --> U --> fT) $
walther@60123
   537
                     t $ foldr1 HOLogic.mk_prod (map (fst o tm_of vs) es) $
walther@60123
   538
                       fst (tm_of vs e'),
walther@60123
   539
                   ty)
walther@60123
   540
                end
walther@60123
   541
            | _ => error (ty ^ " is not an array type")
walther@60123
   542
          end
walther@60123
   543
walther@60123
   544
      | tm_of vs (Record (s, flds)) =
walther@60123
   545
          let
walther@60123
   546
            val (T, cmap) = mk_type' thy prfx s;
walther@60123
   547
            val {extension = (ext_name, _), fields, ...} =
walther@60123
   548
              (case get_record_info thy T of
walther@60123
   549
                 NONE => error (s ^ " is not a record type")
walther@60123
   550
               | SOME info => info);
walther@60123
   551
            val flds' = map (apsnd (tm_of vs)) flds;
walther@60123
   552
            val fnames = fields |> invert_map cmap |>
walther@60123
   553
              map (Long_Name.base_name o fst);
walther@60123
   554
            val fnames' = map fst flds;
walther@60123
   555
            val (fvals, ftys) = split_list (map (fn s' =>
walther@60123
   556
              case AList.lookup lcase_eq flds' s' of
walther@60123
   557
                SOME fval_ty => fval_ty
walther@60123
   558
              | NONE => error ("Field " ^ s' ^ " missing in record " ^ s))
walther@60123
   559
                  fnames);
walther@60123
   560
            val _ = (case subtract lcase_eq fnames fnames' of
walther@60123
   561
                [] => ()
walther@60123
   562
              | xs => error ("Extra field(s) " ^ commas xs ^
walther@60123
   563
                  " in record " ^ s));
walther@60123
   564
            val _ = (case duplicates (op =) fnames' of
walther@60123
   565
                [] => ()
walther@60123
   566
              | xs => error ("Duplicate field(s) " ^ commas xs ^
walther@60123
   567
                  " in record " ^ s))
walther@60123
   568
          in
walther@60123
   569
            (list_comb
walther@60123
   570
               (Const (ext_name,
walther@60123
   571
                  map (mk_type thy prfx) ftys @ [HOLogic.unitT] ---> T),
walther@60123
   572
                fvals @ [HOLogic.unit]),
walther@60123
   573
             s)
walther@60123
   574
          end
walther@60123
   575
walther@60123
   576
      | tm_of vs (Array (s, default, assocs)) =
walther@60123
   577
          (case lookup types s of
walther@60123
   578
             SOME (Array_Type (idxtys, elty)) =>
walther@60123
   579
               let
walther@60123
   580
                 val Ts = map (mk_type thy prfx) idxtys;
walther@60123
   581
                 val T = foldr1 HOLogic.mk_prodT Ts;
walther@60123
   582
                 val U = mk_type thy prfx elty;
walther@60123
   583
                 fun mk_idx' T (e, NONE) = HOLogic.mk_set T [fst (tm_of vs e)]
walther@60123
   584
                   | mk_idx' T (e, SOME e') = Const (\<^const_name>\<open>atLeastAtMost\<close>,
walther@60123
   585
                       T --> T --> HOLogic.mk_setT T) $
walther@60123
   586
                         fst (tm_of vs e) $ fst (tm_of vs e');
walther@60123
   587
                 fun mk_idx idx =
walther@60123
   588
                   if length Ts <> length idx then
walther@60123
   589
                     error ("Arity mismatch in construction of array " ^ s)
walther@60123
   590
                   else foldr1 mk_times (map2 mk_idx' Ts idx);
walther@60123
   591
                 fun mk_upd (idxs, e) t =
walther@60123
   592
                   if length idxs = 1 andalso forall (is_none o snd) (hd idxs)
walther@60123
   593
                   then
walther@60123
   594
                     Const (\<^const_name>\<open>fun_upd\<close>, (T --> U) -->
walther@60123
   595
                         T --> U --> T --> U) $ t $
walther@60123
   596
                       foldl1 HOLogic.mk_prod
walther@60123
   597
                         (map (fst o tm_of vs o fst) (hd idxs)) $
walther@60123
   598
                       fst (tm_of vs e)
walther@60123
   599
                   else
walther@60123
   600
                     Const (\<^const_name>\<open>fun_upds\<close>, (T --> U) -->
walther@60123
   601
                         HOLogic.mk_setT T --> U --> T --> U) $ t $
walther@60123
   602
                       foldl1 (HOLogic.mk_binop \<^const_name>\<open>sup\<close>)
walther@60123
   603
                         (map mk_idx idxs) $
walther@60123
   604
                       fst (tm_of vs e)
walther@60123
   605
               in
walther@60123
   606
                 (fold mk_upd assocs
walther@60123
   607
                    (case default of
walther@60123
   608
                       SOME e => Abs ("x", T, fst (tm_of vs e))
walther@60123
   609
                     | NONE => Const (\<^const_name>\<open>undefined\<close>, T --> U)),
walther@60123
   610
                  s)
walther@60123
   611
               end
walther@60123
   612
           | _ => error (s ^ " is not an array type"))
walther@60123
   613
walther@60123
   614
  in tm_of end;
walther@60123
   615
\<close> ML \<open>
walther@60123
   616
fun mk_pat s = (case Int.fromString s of
walther@60123
   617
    SOME i => [HOLogic.mk_Trueprop (Var (("C", i), HOLogic.boolT))]
walther@60123
   618
  | NONE => error ("Bad conclusion identifier: C" ^ s));
walther@60123
   619
\<close> ML \<open>
walther@60123
   620
fun mk_vc thy prfx types pfuns ids name_concl (tr, proved, ps, cs) =
walther@60123
   621
  let val prop_of =
walther@60123
   622
    HOLogic.mk_Trueprop o fst o term_of_expr thy prfx types pfuns ids
walther@60123
   623
  in
walther@60123
   624
    (tr, proved,
walther@60123
   625
     Element.Assumes (map (fn (s', e) =>
walther@60123
   626
       ((Binding.name ("H" ^ s'), []), [(prop_of e, [])])) ps),
walther@60123
   627
     Element.Shows (map (fn (s', e) =>
walther@60123
   628
       (if name_concl then (Binding.name ("C" ^ s'), [])
walther@60123
   629
        else Binding.empty_atts,
walther@60123
   630
        [(prop_of e, mk_pat s')])) cs))
walther@60123
   631
  end;
walther@60123
   632
\<close> ML \<open>
walther@60123
   633
(*val lookup_vc: theory -> bool -> string -> (Element.context_i list *
walther@60123
   634
    (string * thm list option * Element.context_i * Element.statement_i)) option
walther@60123
   635
*)
walther@60123
   636
fun lookup_vc thy name_concl name =
walther@60123
   637
  (case VCs.get thy of
walther@60123
   638
    {env = SOME {vcs, types, funs, pfuns, ids, ctxt, prefix, ...}, ...} =>
walther@60123
   639
      (case VCtab.lookup vcs name of
walther@60123
   640
         SOME vc =>
walther@60123
   641
           let val (pfuns', ctxt', ids') =
walther@60123
   642
             declare_missing_pfuns thy prefix funs pfuns vcs ids
walther@60123
   643
           in
walther@60123
   644
             SOME (ctxt @ [ctxt'],
walther@60123
   645
               mk_vc thy prefix types pfuns' ids' name_concl vc)
walther@60123
   646
           end
walther@60123
   647
       | NONE => NONE)
walther@60123
   648
  | _ => NONE);
walther@60123
   649
\<close> ML \<open>
walther@60123
   650
\<close> 
walther@60123
   651
subsubsection \<open>copied from spark_commands.ML\<close>
walther@60123
   652
ML \<open>
walther@60123
   653
\<close> ML \<open>
walther@60123
   654
fun get_vc thy vc_name =
walther@60123
   655
  (case SPARK_VCs.lookup_vc thy false vc_name of
walther@60123
   656
    SOME (ctxt, (_, proved, ctxt', stmt)) =>
walther@60123
   657
      if is_some proved then
walther@60123
   658
        error ("The verification condition " ^
walther@60123
   659
          quote vc_name ^ " has already been proved.")
walther@60123
   660
      else (ctxt @ [ctxt'], stmt)
walther@60123
   661
  | NONE => error ("There is no verification condition " ^
walther@60123
   662
      quote vc_name ^ "."));
walther@60123
   663
\<close> ML \<open>
walther@60127
   664
(*
walther@60127
   665
val prefer_input: ThyC.id * Problem.id -> References.T -> O_Model.T -> References.T * O_Model.T
walther@60127
   666
*)
walther@60127
   667
fun prefer_input (inthy, inpbl : Problem.id) (thy, pbl, met_id) o_model =
walther@60124
   668
  if inthy = thy andalso inpbl = pbl
walther@60124
   669
  then ((thy, pbl, met_id) : References.T, o_model)
walther@60124
   670
  else ((inthy, inpbl, Method.id_empty), [])
walther@60124
   671
\<close> ML \<open>
walther@60127
   672
(*
walther@60132
   673
val prove_vc: ParseC.problem -> Proof.context -> Proof.state
walther@60127
   674
*)
walther@60132
   675
fun prove_vc (*vc_name*)problem lthy =
walther@60123
   676
  let
walther@60132
   677
    val _ = @{print} {a = "//--- Spark_Commands.prove_vc", headline = problem}
walther@60123
   678
    val thy = Proof_Context.theory_of lthy;
walther@60128
   679
    val vc_name = "procedure_g_c_d_4" (*fixed for spark_open \<open>greatest_common_divisor/g_c_d\<close>*)
walther@60123
   680
    val (ctxt, stmt) = get_vc thy vc_name
walther@60124
   681
(*//--------------------------------- new code --------------------------------\\*)
walther@60132
   682
    val {thy_id, pbl_id, (*givens, wheres, ..*)...} = ParseC.read_out_problem problem
walther@60127
   683
    val refs' = Refs_Data.get thy
walther@60132
   684
    val (refs as (_, pbl_id, _), o_model) = prefer_input (thy_id, pbl_id) refs' (OMod_Data.get thy)
walther@60126
   685
    val i_model = I_Model.initPIDE pbl_id
walther@60126
   686
(*
walther@60126
   687
  TODO: present Specification = i_model () + refs via PIDE
walther@60126
   688
*)
walther@60124
   689
(*----------------------------------- new code ----------------------------------*)
walther@60126
   690
    val _ = @{print} {a = "prove_vc", o_model = o_model, refs = refs, i_model = i_model}
walther@60124
   691
(*\\--------------------------------- new code --------------------------------//*)
walther@60123
   692
  in
walther@60123
   693
    Specification.theorem true Thm.theoremK NONE
walther@60123
   694
      (fn thmss => (Local_Theory.background_theory
walther@60123
   695
         (SPARK_VCs.mark_proved vc_name (flat thmss))))
walther@60123
   696
      (Binding.name vc_name, []) [] ctxt stmt false lthy
walther@60123
   697
  end;
walther@60123
   698
\<close> ML \<open>
walther@60132
   699
prove_vc: ParseC.problem -> Proof.context -> Proof.state
walther@60132
   700
\<close> ML \<open>
walther@60097
   701
\<close>
walther@60097
   702
walther@60103
   703
section \<open>setup command_keyword \<close>
walther@60103
   704
ML \<open>
walther@60103
   705
\<close> ML \<open>
walther@60103
   706
val _ =                                                  
walther@60117
   707
  Outer_Syntax.command \<^command_keyword>\<open>Example\<close> "start a Calculation from a Formalise-file"
walther@60117
   708
    (Resources.provide_parse_files "Example" -- Parse.parname
walther@60122
   709
      >> (Toplevel.theory o (load_formalisation ParseC.formalisation)));
walther@60110
   710
\<close> ML \<open>
walther@60123
   711
val _ =
walther@60123
   712
  Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>Problem\<close>
walther@60123
   713
    "start a Specification, either from scratch or from preceding 'Example'"
walther@60132
   714
    (ParseC.problem >> prove_vc);
walther@60123
   715
\<close> ML \<open>
walther@60132
   716
\<close>
walther@60103
   717
walther@60116
   718
subsection \<open>test runs with Example\<close>
walther@60129
   719
subsubsection \<open>with new code\<close>
walther@60116
   720
text \<open>
walther@60118
   721
  Starting the Calculation for the Example requires session Isac with Isac.Biegelinie etc.
walther@60118
   722
  So, do ~~$ ./zcoding-to-test.sh and use Test_Some.thy to evaluate
walther@60107
   723
walther@60116
   724
    Example \<open>/usr/local/isabisac/src/Tools/isac/Examples/exp_Statics_Biegel_Timischl_7-70\<close>
walther@60107
   725
walther@60118
   726
  This now gives no error, but also no <Output>. See child of 3ea616c84845.
walther@60102
   727
\<close>
walther@60129
   728
walther@60129
   729
subsubsection \<open>with original SPARK code\<close>
walther@60129
   730
text \<open>
walther@60129
   731
  The sequence of code below is copied from SPARK/Examples/Gcd/Greatest_Common_Divisor.thy.
walther@60129
   732
  But here the code works only partially (see ERROR at end).
walther@60132
   733
  Thus it is advisable to run tests in Test_Some.thy, where "Example" has "Biegelinie".
walther@60129
   734
walther@60129
   735
  spark_proof_functions is required for proof below..
walther@60129
   736
\<close>
walther@60129
   737
(*//------------------- outcomment before creating session Isac ----------------------------\\* )
walther@60129
   738
(**)
walther@60129
   739
  spark_proof_functions gcd = "gcd :: int \<Rightarrow> int \<Rightarrow> int"
walther@60127
   740
(**)
walther@60127
   741
spark_open \<open>~~/src/HOL/SPARK/Examples/Gcd/greatest_common_divisor/g_c_d\<close>
walther@60127
   742
(*Output..* )
walther@60127
   743
The following verification conditions remain to be proved:
walther@60127
   744
  procedure_g_c_d_4
walther@60127
   745
  procedure_g_c_d_11
walther@60127
   746
( **)
walther@60127
   747
spark_vc procedure_g_c_d_4
walther@60127
   748
proof -
walther@60127
   749
  from \<open>0 < d\<close> have "0 \<le> c mod d" by (rule pos_mod_sign)
walther@60127
   750
  with \<open>0 \<le> c\<close> \<open>0 < d\<close> \<open>c - c sdiv d * d \<noteq> 0\<close> show ?C1
walther@60127
   751
    by (simp add: sdiv_pos_pos minus_div_mult_eq_mod [symmetric])
walther@60127
   752
(** )
walther@60127
   753
proof -
walther@60127
   754
  from \<open>0 < d\<close> have "0 \<le> c mod d" by (rule pos_mod_sign)
walther@60127
   755
  with \<open>0 \<le> c\<close> \<open>0 < d\<close> \<open>c - c sdiv d * d \<noteq> 0\<close> show ?C1
walther@60127
   756
    by (simp add: sdiv_pos_pos minus_div_mult_eq_mod [symmetric])
walther@60127
   757
next
walther@60127
   758
  from \<open>0 \<le> c\<close> \<open>0 < d\<close> \<open>gcd c d = gcd m n\<close> show ?C2
walther@60127
   759
    by (simp add: sdiv_pos_pos minus_div_mult_eq_mod [symmetric] gcd_non_0_int)
walther@60127
   760
qed
walther@60127
   761
( **)oops(**)
walther@60127
   762
(** )sorry( **)
walther@60127
   763
walther@60127
   764
spark_vc procedure_g_c_d_11
walther@60127
   765
  sorry
walther@60127
   766
(** )
walther@60127
   767
spark_end
walther@60127
   768
( *Bad path binding: "$ISABELLE_HOME/src/HOL/SPARK/Examples/Gcd/greatest_common_divisor/g_c_d.prv"*)
walther@60127
   769
walther@60129
   770
( *\\------------------- outcomment before creating session Isac ----------------------------//*)
walther@60129
   771
walther@60104
   772
walther@60132
   773
section \<open>adapt spark_vc to Problem using Naproche as model\<close>
walther@60123
   774
walther@60123
   775
subsection \<open>survey on steps of investigation\<close>
walther@60119
   776
text \<open>
walther@60132
   777
  We stopped step 3.4 going down from Outer_Syntax.local_theory_to_proof into proof.
walther@60132
   778
  Now we go the other way: Naproche checks the input via the Isabelle/server
walther@60132
   779
  and outputs highlighting, semantic annotations and errors via PIDE ---
walther@60132
   780
  and we investigate the output.
walther@60133
   781
walther@60133
   782
  Investigation of Naproche showed, that annotations are ONLY sent bY
walther@60133
   783
  Output.report: string list -> unit, where string holds markup.
walther@60134
   784
  For Output.report @ {print} is NOT available, so we trace all respective CALLS.
walther@60134
   785
  However, NO @ {print} available in src/Pure is reached by "spark_vc procedure_g_c_d_4",
walther@60134
   786
  so tracing is done by writeln (which breaks build between Main and Complex_Main
walther@60134
   787
  by writing longer strings, see Pure/General/output.ML).
walther@60134
   788
walther@60134
   789
  Tracing is implemented in (1) Isabelle_Naproche and in (2) isabisac in parallel;
walther@60134
   790
  (1) requires only Pure, thus is built quicker, but does NOT handle proofs within ML
walther@60134
   791
  (2) requires HOL.SPARK, there is full proof handling, but this is complex.
walther@60134
   792
walther@60134
   793
  Tracing the calls of Output.report shows some properties of handling proofs,
walther@60134
   794
  see text in SPARK/Examples/Gcd/Greatest_Common_Divisor.thy.
walther@60119
   795
\<close>
walther@60119
   796
walther@60132
   797
subsection \<open>notes and testbed for Naproche in preliminary spark_vcs\<close>
walther@60134
   798
text \<open>
walther@60134
   799
  tracing "spark_vc procedure_g_c_d_4" see SPARK/Examples/Gcd/Greatest_Common_Divisor.thy
walther@60119
   800
\<close> ML \<open>
walther@60119
   801
\<close> ML \<open>
walther@60123
   802
\<close> ML \<open>
walther@60119
   803
\<close>
walther@60119
   804
walther@60123
   805
subsection \<open>testbed for Problem\<close>
walther@60119
   806
ML \<open>
walther@60119
   807
\<close> ML \<open>
walther@60132
   808
\<close> ML \<open>
walther@60127
   809
\<close> ML \<open>
walther@60119
   810
\<close>
walther@60105
   811
walther@60123
   812
walther@60129
   813
end(* SEE "outcomment before creating session Isac" ABOVE !!! OTHERWISE YOU HAVE..
walther@60127
   814
  ERROR: Found the end of the theory, but the last SPARK environment is still open.
walther@60129
   815
  ..is acceptable for testing spark_vc .. proof - ...
walther@60127
   816
*)