src/Tools/isac/Interpret/mstools.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 15 Mar 2018 10:17:44 +0100
changeset 59405 49d7d410b83c
parent 59389 627d25067f2f
child 59416 229e5c9cf78b
permissions -rw-r--r--
separate structure Celem: CALC_ELEMENT, all but Knowledge/
wneuper@59316
     1
(* Title: tools for 'modeling' und 'specifying' to be used in
wneuper@59316
     2
          modspec.sml. The types are separated into this file,
wneuper@59308
     3
          because some of them are stored in the calc-tree, and thus are required
wneuper@59308
     4
          _before_ ctree.sml. 
wneuper@59308
     5
           TODO: allocate elements of Selem and of Stool appropriately
wneuper@59308
     6
   Author: Walther Neuper, Mathias Lehnfeld
neuper@37906
     7
   (c) due to copyright terms
neuper@37906
     8
*)
neuper@37906
     9
wneuper@59308
    10
signature SPECIFY_TOOL =
wneuper@59308
    11
sig
wneuper@59405
    12
  val check_preconds : 'a -> Celem.rls -> term list -> Model.itm list -> (bool * term) list
wneuper@59405
    13
  val check_preconds' : Celem.rls -> term list -> Model.itm list -> 'a -> (bool * term) list
wneuper@59308
    14
wneuper@59308
    15
  val get_assumptions : Proof.context -> term list
wneuper@59308
    16
  val insert_assumptions : term list -> Proof.context -> Proof.context
wneuper@59308
    17
  val declare_constraints : string -> Proof.context -> Proof.context
wneuper@59308
    18
  val declare_constraints' : term list -> Proof.context -> Proof.context
wneuper@59308
    19
  val from_subpbl_to_caller : Proof.context -> term -> Proof.context -> Proof.context
wneuper@59308
    20
wneuper@59405
    21
  datatype match_ = Match_ of Celem.pblID * (Model.itm list * (bool * term) list) | NoMatch_
wneuper@59308
    22
  val refined_ : match_ list -> match_ option
wneuper@59405
    23
  datatype match = Matches of Celem.pblID * Model.item Model.ppc | NoMatch of Celem.pblID * Model.item Model.ppc
wneuper@59308
    24
  val matchs2str : match list -> string
wneuper@59310
    25
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59316
    26
  val pres2str : (bool * term) list -> string
wneuper@59405
    27
  val refined : match list -> Celem.pblID
wneuper@59310
    28
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59310
    29
  val transfer_asms_from_to : Proof.context -> Proof.context -> Proof.context
wneuper@59310
    30
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
    31
wneuper@59310
    32
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59405
    33
  val pblID_of_match : match -> Celem.pblID
wneuper@59309
    34
  val refined_IDitms : match list -> match option
wneuper@59308
    35
end
wneuper@59308
    36
wneuper@59308
    37
structure Stool(**) : SPECIFY_TOOL(**) =
neuper@37906
    38
struct
neuper@37906
    39
neuper@37906
    40
datatype match = 
wneuper@59405
    41
  Matches of Celem.pblID *  Model.item Model.ppc
wneuper@59405
    42
| NoMatch of Celem.pblID *  Model.item  Model.ppc;
wneuper@59316
    43
fun match2str (Matches (pI, ppc)) = "Matches (" ^ strs2str pI ^ ", " ^  Model.itemppc2str ppc ^ ")"
wneuper@59316
    44
  | match2str (NoMatch (pI, ppc)) = "NoMatch (" ^ strs2str pI ^ ", " ^  Model.itemppc2str ppc ^ ")";
neuper@37906
    45
fun matchs2str ms = (strs2str o (map match2str)) ms;
wneuper@59309
    46
fun pblID_of_match (Matches (pI, _)) = pI
wneuper@59309
    47
  | pblID_of_match (NoMatch (pI, _)) = pI;
neuper@37906
    48
wneuper@59309
    49
(* 10.03 for Refine_Problem *)
neuper@37906
    50
datatype match_ = 
wneuper@59405
    51
  Match_ of Celem.pblID * (( Model.itm list) * ((bool * term) list))
neuper@37906
    52
| NoMatch_;
neuper@37906
    53
wneuper@59309
    54
(* the refined pbt is the last_element Matches in the list *)
neuper@37906
    55
fun is_matches (Matches _) = true
neuper@37906
    56
  | is_matches _ = false;
wneuper@59309
    57
fun matches_pblID (Matches (pI, _)) = pI
wneuper@59309
    58
  | matches_pblID _ = error "matches_pblID: uncovered case in fun.def.";
neuper@37906
    59
fun refined ms = ((matches_pblID o the o (find_first is_matches) o rev) ms)
wneuper@59405
    60
    handle _ => [];
neuper@37906
    61
fun refined_IDitms ms = ((find_first is_matches) o rev) ms;
neuper@37906
    62
wneuper@59309
    63
(* the refined pbt is the last_element Matches in the list, for Refine_Problem, tryrefine *)
neuper@37906
    64
fun is_matches_ (Match_ _) = true
neuper@37906
    65
  | is_matches_ _ = false;
neuper@37906
    66
fun refined_ ms = ((find_first is_matches_) o rev) ms;
neuper@37906
    67
wneuper@59309
    68
(* check a predicate labelled with indication of incomplete substitution;
wneuper@59309
    69
  rls ->    (* for eval_true                                               *)
wneuper@59309
    70
  bool * 	  (* have _all_ variables(Free) from the model-pattern 
wneuper@59309
    71
               been substituted by a value from the pattern's environment ?*)
wneuper@59309
    72
  term ->   (* the precondition                                            *)
wneuper@59309
    73
  bool * 	  (* has the precondition evaluated to true                      *)
wneuper@59309
    74
  term      (* the precondition (for map)                                  *)
wneuper@59309
    75
*)
wneuper@59309
    76
fun evalprecond _ (false, pre) = 
neuper@37906
    77
  (*NOT ALL Free's have been substituted, eg. because of incomplete model*)
neuper@37906
    78
    (false, pre)
neuper@37906
    79
  | evalprecond prls (true, pre) =
wneuper@59405
    80
    if Rewrite.eval_true (Celem.assoc_thy "Isac") (* for Pattern.match    *)
wneuper@59309
    81
		  [pre] prls                    (* pre parsed, prls.thy *)
neuper@37906
    82
    then (true , pre)
neuper@37906
    83
    else (false , pre);
neuper@37906
    84
wneuper@59405
    85
fun pre2str (b, t) = pair2str(bool2str b, Celem.term2str t);
wneuper@59405
    86
fun pres2str pres = strs2str' (map (Celem.linefeed o pre2str) pres);
neuper@37906
    87
neuper@42009
    88
(* check preconditions, return true if all true *)
wneuper@59309
    89
fun check_preconds' _ [] _ _ = []   (* empty preconditions are true   *)
wneuper@59309
    90
  | check_preconds' prls pres pbl _ (* FIXME.WN0308 mvat re-introduce *) =
wneuper@59309
    91
    let
wneuper@59316
    92
      val env = Model.mk_env pbl;
wneuper@59389
    93
      val pres' = map (TermC.subst_atomic_all env) pres;
neuper@37906
    94
    in map (evalprecond prls) pres' end;
neuper@37906
    95
wneuper@59316
    96
fun check_preconds _(*thy*) prls pres pbl = check_preconds' prls pres pbl (Model.max_vt pbl);
wneuper@59389
    97
fun declare_constraints' ts ctxt = fold Variable.declare_constraints (flat (map TermC.vars ts)) ctxt;
neuper@37906
    98
neuper@41990
    99
(*WN110613 fun declare_constraints' shall replace fun declare_constraints*)
bonzai@41941
   100
fun declare_constraints t ctxt =
wneuper@59309
   101
  let
wneuper@59309
   102
    fun get_vars ((v, T) :: vs) = (case raw_explode v |> Library.read_int of
wneuper@59309
   103
            (_, _ :: _) => (Free (v, T) :: get_vars vs)
wneuper@59309
   104
          | (_, []  ) => get_vars vs) (*filter out nums as long as we have Free ("123",_)*)
wneuper@59309
   105
      | get_vars [] = [];
wneuper@59309
   106
    val ts = Term.add_frees (Syntax.read_term ctxt t) [] |> get_vars;
wneuper@59309
   107
  in fold Variable.declare_constraints ts ctxt end;
bonzai@41941
   108
wneuper@59309
   109
structure Context_Data = Proof_Data (type T = term list fun init _ = []);
neuper@52114
   110
fun get_assumptions ctxt = Context_Data.get ctxt
neuper@52114
   111
fun insert_assumptions asms = Context_Data.map (fn xs => distinct (asms @ xs))
e0726734@41957
   112
neuper@42019
   113
(* transfer assumptions from one to another ctxt.
neuper@42394
   114
   does NOT respect scope: in a calculation identifiers are unique.
neuper@42019
   115
   but environments are scoped as usual in Luacs-interpretation.
neuper@42019
   116
   WN110520 redo (1) take declare_constraints (2) with combinators*)
neuper@42019
   117
fun transfer_asms_from_to from_ctxt to_ctxt =
neuper@42019
   118
  let
wneuper@59389
   119
    val to_vars = get_assumptions to_ctxt |> map TermC.vars |> flat
neuper@42019
   120
    fun transfer [] to_ctxt = to_ctxt
wneuper@59309
   121
        | transfer (from_asm :: fas) to_ctxt =
wneuper@59389
   122
            if inter op = (TermC.vars from_asm) to_vars = []
neuper@42019
   123
            then transfer fas to_ctxt
neuper@42019
   124
            else transfer fas (insert_assumptions [from_asm] to_ctxt)
neuper@42019
   125
  in transfer (get_assumptions from_ctxt) to_ctxt end
neuper@42019
   126
neuper@42023
   127
(* exported from a subproblem to the context of the calling method:
neuper@42023
   128
   # 'scrval': the result of script interpretation and
neuper@42023
   129
   # those assumptions in the subproblem wich contain a variable known
neuper@42023
   130
     in the calling method. *)
neuper@42023
   131
fun from_subpbl_to_caller sub_ctxt scrval caller_ctxt =
wneuper@59309
   132
  let
wneuper@59316
   133
    val caller_ctxt = (scrval |> Model.dest_list' |> insert_assumptions) caller_ctxt
neuper@42023
   134
  in transfer_asms_from_to sub_ctxt caller_ctxt end;
neuper@42023
   135
wneuper@59309
   136
end;