src/Tools/isac/BaseDefinitions/contextC.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 25 Jan 2023 15:52:33 +0100
changeset 60651 b7a2ad3b3d45
parent 60609 5967b6e610b5
child 60654 c2db35151fba
permissions -rw-r--r--
ContextC.build_while_parsing, improves O_Model.init_PIDE
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
walther@59846
     8
  val empty : Proof.context
walther@59846
     9
  val is_empty : Proof.context -> bool
wneuper@59597
    10
  val isac_ctxt : 'a -> Proof.context
walther@59820
    11
  val declare_constraints: term -> Proof.context -> Proof.context
walther@59998
    12
  val add_constraints: term list -> Proof.context -> Proof.context
Walther@60651
    13
  val build_while_parsing: Formalise.model -> theory -> term list * Proof.context
Walther@60609
    14
  val initialise : Proof.context -> term list -> Proof.context
walther@60111
    15
  val initialise' : theory -> TermC.as_string list -> Proof.context
wneuper@59577
    16
  val get_assumptions : Proof.context -> term list
wneuper@59577
    17
  val insert_assumptions : term list -> Proof.context -> Proof.context
walther@59842
    18
  val avoid_contradict: term -> term list -> term * term list
walther@59842
    19
  val subpbl_to_caller : Proof.context -> term -> Proof.context -> term * Proof.context
Walther@60609
    20
wenzelm@60223
    21
\<^isac_test>\<open>
wneuper@59577
    22
  val transfer_asms_from_to : Proof.context -> Proof.context -> Proof.context
walther@59842
    23
  val contradict : term list -> term -> bool
walther@59842
    24
  val insert_assumptions_cao : Proof.context -> term list -> Proof.context
wenzelm@60223
    25
\<close>
wneuper@59577
    26
end
wneuper@59577
    27
wneuper@59577
    28
structure ContextC(**) : CONTEXT_C(**) =
wneuper@59577
    29
struct
wneuper@59577
    30
walther@59846
    31
val empty = Proof_Context.init_global @{theory "Pure"};
walther@59719
    32
(* ATTENTION: does _not_ recognise Variable.declare_constraints, etc...*)
walther@60114
    33
walther@59846
    34
fun is_empty ctxt = Context.eq_thy (Proof_Context.theory_of ctxt, @{theory "Pure"});
walther@59854
    35
fun isac_ctxt xxx = Proof_Context.init_global (ThyC.Isac xxx)
wneuper@59581
    36
walther@59820
    37
val declare_constraints = Variable.declare_constraints
walther@59998
    38
fun add_constraints ts ctxt =
walther@59998
    39
  fold Variable.declare_constraints (TermC.vars' ts) ctxt
walther@59998
    40
Walther@60651
    41
fun build_while_parsing_ [] (ts, ctxt) = (ts, ctxt)
Walther@60651
    42
  | build_while_parsing_ (str :: strs) (ts, ctxt) = 
Walther@60651
    43
    let
Walther@60651
    44
      val t as (_ $ tm) = Syntax.read_term ctxt str
Walther@60651
    45
        handle ERROR msg => raise ERROR ("ContextC.build_while_parsing " ^ quote str ^ " " ^ msg)
Walther@60651
    46
      val vars = TermC.vars tm
Walther@60651
    47
      val ctxt' = add_constraints vars ctxt
Walther@60651
    48
    in build_while_parsing_ strs (ts @ [t], ctxt') end
Walther@60651
    49
(* shouldn't that be done by fold ?
Walther@60651
    50
   fn: string * Proof.context -> term list * Proof.context -> term list * Proof.context *)
Walther@60651
    51
fun build_while_parsing strs thy = build_while_parsing_ strs ([], Proof_Context.init_global thy)
Walther@60651
    52
walther@59677
    53
(* in Subproblem take respective actual arguments from program *)
Walther@60651
    54
(* FIXME: should be replaced by build_while_parsing *)
Walther@60609
    55
fun initialise ctxt ts =
walther@59677
    56
  let
walther@59998
    57
    val frees = TermC.vars' ts
walther@59677
    58
    val _ = TermC.raise_type_conflicts frees
walther@59677
    59
  in
walther@59677
    60
    fold Variable.declare_constraints frees ctxt
walther@59677
    61
  end
wneuper@59582
    62
(* in root-problem take respective formalisation *)
wneuper@59582
    63
fun initialise' thy fmz =
Walther@60609
    64
  let
wneuper@59582
    65
    val ctxt = thy |> Proof_Context.init_global
walther@59998
    66
    val frees = map (TermC.parseNEW' ctxt) fmz |> TermC.vars'
wneuper@59582
    67
    val _ = TermC.raise_type_conflicts frees
wneuper@59582
    68
  in
wneuper@59582
    69
    fold Variable.declare_constraints frees ctxt
wneuper@59582
    70
  end
wneuper@59580
    71
wneuper@59577
    72
structure Context_Data = Proof_Data (type T = term list fun init _ = []);
wneuper@59577
    73
fun get_assumptions ctxt = Context_Data.get ctxt
walther@60017
    74
fun insert_assumptions asms = Context_Data.map (fn xs => distinct op = (asms @ xs))
wneuper@59577
    75
walther@59842
    76
(* 
walther@59842
    77
  transfer assumptions from one to another ctxt.
walther@59842
    78
  does NOT respect scope: in a calculation identifiers are unique.
walther@59842
    79
  but environments are scoped as usual in Luacs-interpretation.
walther@59842
    80
*)
wneuper@59577
    81
fun transfer_asms_from_to from_ctxt to_ctxt =
wneuper@59577
    82
  let
wneuper@59577
    83
    val to_vars = get_assumptions to_ctxt |> map TermC.vars |> flat
wneuper@59577
    84
    fun transfer [] to_ctxt = to_ctxt
Walther@60609
    85
      | transfer (from_asm :: fas) to_ctxt =
Walther@60609
    86
          if inter op = (TermC.vars from_asm) to_vars = []
Walther@60609
    87
          then transfer fas to_ctxt
Walther@60609
    88
          else transfer fas (insert_assumptions [from_asm] to_ctxt)
walther@60022
    89
  in
walther@60022
    90
    transfer (get_assumptions from_ctxt) to_ctxt
walther@60022
    91
  end
wneuper@59577
    92
walther@59842
    93
(* there is still much TODO: factorise polynomials, etc *)
walther@59842
    94
fun contradict asm res = fold (curry or_) (map (fn p => TermC.negates res p) asm) false;
walther@59842
    95
walther@59842
    96
(*
walther@59842
    97
  Check a term for contradiction with a predicate list.
walther@59842
    98
  This makes only sense for types bool and bool list. In these cases return
walther@59842
    99
  the probably updated term together with 
walther@59842
   100
  a respective list of non-contradicting predicates
walther@59842
   101
*)
walther@59842
   102
fun avoid_contradict t preds = 
walther@60022
   103
  if TermC.is_bool_list t then
walther@60022
   104
    let
walther@60022
   105
      val no_contra = t |> TermC.dest_list' |> filter_out (contradict preds)
walther@60022
   106
    in
walther@60022
   107
      (no_contra |> TermC.list2isalist HOLogic.boolT, no_contra)
walther@60022
   108
    end
walther@60022
   109
  else if Term.type_of t = HOLogic.boolT then
walther@60022
   110
    if contradict preds t then (@{term bool_undef}, []) else (t, [t])
walther@60022
   111
  else (t (* type_of t \<noteq> bool *), [(* no predicate to add *)])
walther@59842
   112
walther@59842
   113
(* for Canonical Argument Order used by |> below *)
walther@59842
   114
fun insert_assumptions_cao ctxt tms  = insert_assumptions tms ctxt
walther@59842
   115
walther@60022
   116
fun subpbl_to_caller sub_ctxt expr_val caller_ctxt = caller_ctxt
walther@59842
   117
  |> get_assumptions 
walther@60022
   118
  |> avoid_contradict expr_val
walther@59842
   119
  |> apsnd (insert_assumptions_cao caller_ctxt)
walther@59842
   120
  |> apsnd (transfer_asms_from_to sub_ctxt)
wneuper@59577
   121
wneuper@59577
   122
end