src/Tools/isac/CalcElements/contextC.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 14 Nov 2019 12:08:05 +0100
changeset 59702 501a4ae08275
parent 59701 24273e0a6739
child 59719 568ea658e026
permissions -rw-r--r--
tuned
wneuper@59577
     1
(* Title: ../contextC.sml
wneuper@59577
     2
   Author: Walther Neuper, Mathias Lehnfeld
wneuper@59577
     3
   (c) due to copyright terms
wneuper@59577
     4
*)
wneuper@59577
     5
(* Extension to Isabelle's naming conventions: "C" indicates Isac add-ons to an Isabelle module *)
wneuper@59577
     6
signature CONTEXT_C =
wneuper@59577
     7
sig
wneuper@59581
     8
  val e_ctxt : Proof.context
wneuper@59597
     9
  val isac_ctxt : 'a -> Proof.context
wneuper@59580
    10
  val initialise : string -> term list -> Proof.context
wneuper@59580
    11
  val initialise' : theory -> string list -> Proof.context
wneuper@59577
    12
  val get_assumptions : Proof.context -> term list
wneuper@59577
    13
  val insert_assumptions : term list -> Proof.context -> Proof.context
wneuper@59577
    14
  val from_subpbl_to_caller : Proof.context -> term -> Proof.context -> Proof.context
wneuper@59577
    15
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59577
    16
  (*NONE*)
walther@59702
    17
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59577
    18
  val transfer_asms_from_to : Proof.context -> Proof.context -> Proof.context
walther@59702
    19
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59577
    20
end
wneuper@59577
    21
wneuper@59577
    22
(* survey on handling contexts:
wneuper@59577
    23
-------------------------------
wneuper@59577
    24
 theory is required for Pattern.match (and thus for Tactic.Rewrite* ), while
wneuper@59577
    25
 ctxt is required for parsing and for managing pre-conditions and assumptions.
wneuper@59577
    26
 * model-specify-phase:
wneuper@59577
    27
   * Tactic.Model_Problem does declare_constraints for parsing (in Tactic.Add_Given, etc)
wneuper@59577
    28
     ("insert_assumptions pres" has to wait for completing Tactic.Add_Given, etc)
wneuper@59577
    29
     (Tactic.Refine_Problem uses theory NOT ctxt due to Pattern.match)
wneuper@59577
    30
   * 
wneuper@59577
    31
   * 
wneuper@59577
    32
 * solve-phase by Lucas-Interpretation:
wneuper@59577
    33
   * locate_input_tactic: 
wneuper@59577
    34
     * Tactic.Apply_Method
wneuper@59583
    35
       * initialises ctxt (declare_constraints' + insert_assumptions pres) by init_pstate
wneuper@59577
    36
         * in solve for root problem
wneuper@59577
    37
         * in begin_end_prog for subproblem
wneuper@59577
    38
     * Tactic.Rewrite* create assumptions; respective insert_assumptions is done by associate
wneuper@59577
    39
     * associate..Subproblem' returns ctxt ONLY with declare_constraints',
wneuper@59577
    40
       with insert_assumptions wait for Tactic.Apply_Method
wneuper@59577
    41
     * storing ctxt is done after return form locate_input_tactic
wneuper@59577
    42
   * determine_next_tactic: 
wneuper@59577
    43
     * TODO initialises ctxt by TODO
wneuper@59577
    44
     * Tactic.Rewrite* create assumptions; respective insert_assumptions TODO
wneuper@59577
    45
     * 
wneuper@59577
    46
     * 
wneuper@59577
    47
     * 
wneuper@59577
    48
   * locate_input_formula: follows sig. of determine_next_tactic
wneuper@59577
    49
 * changing from one method to another (in determine_next_tactic only):
wneuper@59577
    50
   * from method to sub-program: just add new preconditions of the guard
wneuper@59583
    51
     * locate_input_tactic: init_pstate by begin_end_prog
wneuper@59577
    52
     * determine_next_tactic: 
wneuper@59577
    53
   * from_subpbl_to_caller
wneuper@59577
    54
 * finishing a method:
wneuper@59577
    55
   * Tactic.Check_Postcond' uses ctxt for proving the post-condition (not yet implemented)
wneuper@59577
    56
 *
wneuper@59577
    57
   * 
wneuper@59577
    58
     * 
wneuper@59577
    59
       * 
wneuper@59577
    60
================================================================================================
wneuper@59577
    61
call hierarchy
wneuper@59577
    62
================================================================================================
wneuper@59577
    63
wneuper@59577
    64
  locatetac
wneuper@59577
    65
    applicable_in (p, p_) pt (Tactic.Apply_Method pres
wneuper@59577
    66
      insert_assumptions
wneuper@59577
    67
wneuper@59577
    68
  context_thy
wneuper@59577
    69
    applicable_in (p, p_) pt (Tactic.Apply_Method pres
wneuper@59577
    70
      insert_assumptions
wneuper@59577
    71
wneuper@59577
    72
wneuper@59577
    73
wneuper@59577
    74
wneuper@59577
    75
wneuper@59577
    76
wneuper@59577
    77
    generate1 _ (Tactic.Rewrite***
wneuper@59577
    78
      insert_assumptions
wneuper@59577
    79
wneuper@59577
    80
wneuper@59577
    81
wneuper@59577
    82
wneuper@59577
    83
wneuper@59577
    84
------------------------------------------------------------------------------------------------
wneuper@59577
    85
solve phase before LI
wneuper@59577
    86
------------------------------------------------------------------------------------------------
wneuper@59577
    87
autocalc
wneuper@59577
    88
  all_modspec
wneuper@59577
    89
    declare_constraints'
wneuper@59577
    90
  complete_solve
wneuper@59577
    91
    all_modspec
wneuper@59577
    92
      declare_constraints'
wneuper@59577
    93
wneuper@59577
    94
all_solve
wneuper@59577
    95
  begin_end_prog (Tactic.Apply_Method'
wneuper@59583
    96
    init_pstate
wneuper@59577
    97
      declare_constraints'
wneuper@59577
    98
      insert_assumptions
wneuper@59577
    99
wneuper@59577
   100
nxt_specify_
wneuper@59577
   101
  begin_end_prog (Tactic.Apply_Method'
wneuper@59583
   102
    init_pstate
wneuper@59577
   103
      declare_constraints'
wneuper@59577
   104
      insert_assumptions
wneuper@59577
   105
------------------------------------------------------------------------------------------------
wneuper@59577
   106
LI
wneuper@59577
   107
------------------------------------------------------------------------------------------------
wneuper@59577
   108
solve ("Apply_Method"                                          root-program
wneuper@59583
   109
  init_pstate
wneuper@59577
   110
    declare_constraints'
wneuper@59577
   111
    insert_assumptions
wneuper@59577
   112
  locate_input_tactic
walther@59690
   113
    scan_to_tactic1
walther@59691
   114
      scan_dn1 ..leaf                                              sub-program
wneuper@59577
   115
        associate
wneuper@59577
   116
          declare_constraints'
wneuper@59577
   117
        applicable_in .. Tactic.Apply_Method pres
wneuper@59577
   118
          insert_assumptions
wneuper@59577
   119
  ? generate1 (look in test with "from ... to..))
wneuper@59577
   120
wneuper@59577
   121
determine_next_tactic
walther@59690
   122
  scan_to_tactic2
walther@59691
   123
    scan_dn2 ..leaf
wneuper@59577
   124
      stac2tac_
wneuper@59577
   125
        declare_constraints'
wneuper@59577
   126
      applicable_in (p, p_) pt (Tactic.Apply_Method pres
wneuper@59577
   127
        insert_assumptions
wneuper@59577
   128
  ? generate1 (look in test with "from ... to..))
wneuper@59577
   129
wneuper@59577
   130
locate_input_formula                                          uses determine_next_tactic
wneuper@59577
   131
  compare_step
wneuper@59577
   132
    all_modspec
wneuper@59577
   133
      declare_constraints'
wneuper@59577
   134
    begin_end_prog (Tactic.Apply_Method'
wneuper@59583
   135
      init_pstate
wneuper@59577
   136
        declare_constraints'
wneuper@59577
   137
        insert_assumptions
wneuper@59577
   138
------------------------------------------------------------------------------------------------
wneuper@59577
   139
specification phase
wneuper@59577
   140
------------------------------------------------------------------------------------------------
wneuper@59577
   141
  loc_specify_
wneuper@59577
   142
    specify (Tactic.Init_Proof'
wneuper@59577
   143
      prep_ori
wneuper@59577
   144
        declare_constraints
wneuper@59577
   145
  
wneuper@59577
   146
  CalcTree
wneuper@59577
   147
    nxt_specify_init_calc
wneuper@59577
   148
      prep_ori
wneuper@59577
   149
        declare_constraints
wneuper@59577
   150
  
wneuper@59577
   151
  modifyCalcHead
wneuper@59577
   152
    input_icalhd
wneuper@59577
   153
      prep_ori
wneuper@59577
   154
        declare_constraints
wneuper@59577
   155
  
wneuper@59577
   156
  refine
wneuper@59577
   157
    refin'
wneuper@59577
   158
      prep_ori
wneuper@59577
   159
        declare_constraints
wneuper@59577
   160
------------------------------------------------------------------------------------------------
wneuper@59577
   161
unused ?!
wneuper@59577
   162
------------------------------------------------------------------------------------------------
wneuper@59577
   163
  ??
wneuper@59577
   164
    match_pbl
wneuper@59577
   165
      prep_ori
wneuper@59577
   166
        declare_constraints
wneuper@59577
   167
  ??
wneuper@59577
   168
    from_pblobj'
wneuper@59583
   169
      init_pstate
wneuper@59577
   170
        declare_constraints'
wneuper@59577
   171
        insert_assumptions
wneuper@59577
   172
  ??
wneuper@59577
   173
    tac2tac_
wneuper@59577
   174
      applicable_in (p, p_) pt (Tactic.Apply_Method pres
wneuper@59577
   175
        insert_assumptions
wneuper@59577
   176
wneuper@59577
   177
*)
wneuper@59577
   178
wneuper@59577
   179
structure ContextC(**) : CONTEXT_C(**) =
wneuper@59577
   180
struct
wneuper@59577
   181
wneuper@59581
   182
val e_ctxt = Proof_Context.init_global @{theory "Pure"};
wneuper@59597
   183
fun isac_ctxt xxx = Proof_Context.init_global (Rule.Isac xxx)
wneuper@59581
   184
walther@59677
   185
(* in Subproblem take respective actual arguments from program *)
walther@59677
   186
fun initialise thy' ts =
walther@59677
   187
  let
walther@59677
   188
    val ctxt = Rule.Thy_Info_get_theory thy' |> Proof_Context.init_global
walther@59677
   189
    val frees = map TermC.vars ts |> flat |> distinct
walther@59677
   190
    val _ = TermC.raise_type_conflicts frees
walther@59677
   191
  in
walther@59677
   192
    fold Variable.declare_constraints frees ctxt
walther@59677
   193
  end
wneuper@59582
   194
(* in root-problem take respective formalisation *)
wneuper@59582
   195
fun initialise' thy fmz =
wneuper@59582
   196
  let 
wneuper@59582
   197
    val ctxt = thy |> Proof_Context.init_global
wneuper@59582
   198
    val frees = map (TermC.parseNEW' ctxt) fmz |> map TermC.vars |> flat |> distinct
wneuper@59582
   199
    val _ = TermC.raise_type_conflicts frees
wneuper@59582
   200
  in
wneuper@59582
   201
    fold Variable.declare_constraints frees ctxt
wneuper@59582
   202
  end
wneuper@59580
   203
wneuper@59577
   204
structure Context_Data = Proof_Data (type T = term list fun init _ = []);
wneuper@59577
   205
fun get_assumptions ctxt = Context_Data.get ctxt
wneuper@59577
   206
fun insert_assumptions asms = Context_Data.map (fn xs => distinct (asms @ xs))
wneuper@59577
   207
wneuper@59577
   208
(* transfer assumptions from one to another ctxt.
wneuper@59577
   209
   does NOT respect scope: in a calculation identifiers are unique.
wneuper@59577
   210
   but environments are scoped as usual in Luacs-interpretation.
wneuper@59577
   211
   WN110520 redo (1) take declare_constraints (2) with combinators*)
wneuper@59577
   212
fun transfer_asms_from_to from_ctxt to_ctxt =
wneuper@59577
   213
  let
wneuper@59577
   214
    val to_vars = get_assumptions to_ctxt |> map TermC.vars |> flat
wneuper@59577
   215
    fun transfer [] to_ctxt = to_ctxt
wneuper@59577
   216
        | transfer (from_asm :: fas) to_ctxt =
wneuper@59577
   217
            if inter op = (TermC.vars from_asm) to_vars = []
wneuper@59577
   218
            then transfer fas to_ctxt
wneuper@59577
   219
            else transfer fas (insert_assumptions [from_asm] to_ctxt)
wneuper@59577
   220
  in transfer (get_assumptions from_ctxt) to_ctxt end
wneuper@59577
   221
wneuper@59577
   222
(* exported from a subproblem to the context of the calling method:
wneuper@59577
   223
   # 'scrval': the result of script interpretation and
wneuper@59577
   224
   # those assumptions in the subproblem wich contain a variable known
wneuper@59577
   225
     in the calling method. *)
wneuper@59577
   226
fun from_subpbl_to_caller sub_ctxt scrval caller_ctxt =
wneuper@59577
   227
  let
wneuper@59577
   228
    val caller_ctxt = (scrval |> TermC.dest_list' |> insert_assumptions) caller_ctxt
wneuper@59577
   229
  in transfer_asms_from_to sub_ctxt caller_ctxt end;
wneuper@59577
   230
wneuper@59577
   231
end