src/Tools/isac/BaseDefinitions/contextC.sml
author wneuper <Walther.Neuper@jku.at>
Thu, 04 Aug 2022 12:48:37 +0200
changeset 60509 2e0b7ca391dc
parent 60223 740ebee5948b
child 60609 5967b6e610b5
permissions -rw-r--r--
polish naming in Rewrite_Order
     1 (* Title: ../contextC.sml
     2    Author: Walther Neuper, Mathias Lehnfeld
     3    (c) due to copyright terms
     4 *)
     5 (* Extension to Isabelle's naming conventions: "C" indicates Isac add-ons to an Isabelle module *)
     6 signature CONTEXT_C =
     7 sig
     8   val empty : Proof.context
     9   val is_empty : Proof.context -> bool
    10   val isac_ctxt : 'a -> Proof.context
    11   val declare_constraints: term -> Proof.context -> Proof.context
    12   val add_constraints: term list -> Proof.context -> Proof.context
    13   val initialise : ThyC.id -> term list -> Proof.context
    14   val initialise' : theory -> TermC.as_string list -> Proof.context
    15   val get_assumptions : Proof.context -> term list
    16   val insert_assumptions : term list -> Proof.context -> Proof.context
    17   val avoid_contradict: term -> term list -> term * term list
    18   val subpbl_to_caller : Proof.context -> term -> Proof.context -> term * Proof.context
    19 (*val subpbl_to_caller  .. rename according to naming convention TODO*)
    20 \<^isac_test>\<open>
    21   val transfer_asms_from_to : Proof.context -> Proof.context -> Proof.context
    22   val contradict : term list -> term -> bool
    23   val insert_assumptions_cao : Proof.context -> term list -> Proof.context
    24 \<close>
    25 end
    26 
    27 structure ContextC(**) : CONTEXT_C(**) =
    28 struct
    29 
    30 val empty = Proof_Context.init_global @{theory "Pure"};
    31 (* ATTENTION: does _not_ recognise Variable.declare_constraints, etc...*)
    32 
    33 fun is_empty ctxt = Context.eq_thy (Proof_Context.theory_of ctxt, @{theory "Pure"});
    34 fun isac_ctxt xxx = Proof_Context.init_global (ThyC.Isac xxx)
    35 
    36 val declare_constraints = Variable.declare_constraints
    37 fun add_constraints ts ctxt =
    38   fold Variable.declare_constraints (TermC.vars' ts) ctxt
    39 
    40 (* in Subproblem take respective actual arguments from program *)
    41 fun initialise thy' ts =
    42   let
    43     val ctxt = ThyC.get_theory thy' |> Proof_Context.init_global
    44     val frees = TermC.vars' ts
    45     val _ = TermC.raise_type_conflicts frees
    46   in
    47     fold Variable.declare_constraints frees ctxt
    48   end
    49 (* in root-problem take respective formalisation *)
    50 fun initialise' thy fmz =
    51   let 
    52     val ctxt = thy |> Proof_Context.init_global
    53     val frees = map (TermC.parseNEW' ctxt) fmz |> TermC.vars'
    54     val _ = TermC.raise_type_conflicts frees
    55   in
    56     fold Variable.declare_constraints frees ctxt
    57   end
    58 
    59 structure Context_Data = Proof_Data (type T = term list fun init _ = []);
    60 fun get_assumptions ctxt = Context_Data.get ctxt
    61 fun insert_assumptions asms = Context_Data.map (fn xs => distinct op = (asms @ xs))
    62 
    63 (* 
    64   transfer assumptions from one to another ctxt.
    65   does NOT respect scope: in a calculation identifiers are unique.
    66   but environments are scoped as usual in Luacs-interpretation.
    67 *)
    68 fun transfer_asms_from_to from_ctxt to_ctxt =
    69   let
    70     val to_vars = get_assumptions to_ctxt |> map TermC.vars |> flat
    71     fun transfer [] to_ctxt = to_ctxt
    72         | transfer (from_asm :: fas) to_ctxt =
    73             if inter op = (TermC.vars from_asm) to_vars = []
    74             then transfer fas to_ctxt
    75             else transfer fas (insert_assumptions [from_asm] to_ctxt)
    76   in
    77     transfer (get_assumptions from_ctxt) to_ctxt
    78   end
    79 
    80 (* there is still much TODO: factorise polynomials, etc *)
    81 fun contradict asm res = fold (curry or_) (map (fn p => TermC.negates res p) asm) false;
    82 
    83 (*
    84   Check a term for contradiction with a predicate list.
    85   This makes only sense for types bool and bool list. In these cases return
    86   the probably updated term together with 
    87   a respective list of non-contradicting predicates
    88 *)
    89 fun avoid_contradict t preds = 
    90   if TermC.is_bool_list t then
    91     let
    92       val no_contra = t |> TermC.dest_list' |> filter_out (contradict preds)
    93     in
    94       (no_contra |> TermC.list2isalist HOLogic.boolT, no_contra)
    95     end
    96   else if Term.type_of t = HOLogic.boolT then
    97     if contradict preds t then (@{term bool_undef}, []) else (t, [t])
    98   else (t (* type_of t \<noteq> bool *), [(* no predicate to add *)])
    99 
   100 (* for Canonical Argument Order used by |> below *)
   101 fun insert_assumptions_cao ctxt tms  = insert_assumptions tms ctxt
   102 
   103 fun subpbl_to_caller sub_ctxt expr_val caller_ctxt = caller_ctxt
   104   |> get_assumptions 
   105   |> avoid_contradict expr_val
   106   |> apsnd (insert_assumptions_cao caller_ctxt)
   107   |> apsnd (transfer_asms_from_to sub_ctxt)
   108 
   109 end