src/Tools/isac/BaseDefinitions/contextC.sml
author Walther Neuper <walther.neuper@jku.at>
Wed, 03 Jun 2020 11:25:19 +0200
changeset 60017 cdcc5eba067b
parent 59998 5dd825c9e2d5
child 60022 979ecb48e909
permissions -rw-r--r--
follow 2 ancient updates of Library.ML
     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 : string -> term list -> Proof.context
    14   val initialise' : theory -> 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 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    21   (*NONE*)
    22 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    23   val transfer_asms_from_to : Proof.context -> Proof.context -> Proof.context
    24   val contradict : term list -> term -> bool
    25   val insert_assumptions_cao : Proof.context -> term list -> Proof.context
    26 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    27 end
    28 
    29 structure ContextC(**) : CONTEXT_C(**) =
    30 struct
    31 
    32 val empty = Proof_Context.init_global @{theory "Pure"};
    33 (* ATTENTION: does _not_ recognise Variable.declare_constraints, etc...*)
    34 fun is_empty ctxt = Context.eq_thy (Proof_Context.theory_of ctxt, @{theory "Pure"});
    35 fun isac_ctxt xxx = Proof_Context.init_global (ThyC.Isac xxx)
    36 
    37 val declare_constraints = Variable.declare_constraints
    38 fun add_constraints ts ctxt =
    39   fold Variable.declare_constraints (TermC.vars' ts) ctxt
    40 
    41 (* in Subproblem take respective actual arguments from program *)
    42 fun initialise thy' ts =
    43   let
    44     val ctxt = ThyC.get_theory thy' |> Proof_Context.init_global
    45     val frees = TermC.vars' ts
    46     val _ = TermC.raise_type_conflicts frees
    47   in
    48     fold Variable.declare_constraints frees ctxt
    49   end
    50 (* in root-problem take respective formalisation *)
    51 fun initialise' thy fmz =
    52   let 
    53     val ctxt = thy |> Proof_Context.init_global
    54     val frees = map (TermC.parseNEW' ctxt) fmz |> TermC.vars'
    55     val _ = TermC.raise_type_conflicts frees
    56   in
    57     fold Variable.declare_constraints frees ctxt
    58   end
    59 
    60 structure Context_Data = Proof_Data (type T = term list fun init _ = []);
    61 fun get_assumptions ctxt = Context_Data.get ctxt
    62 fun insert_assumptions asms = Context_Data.map (fn xs => distinct op = (asms @ xs))
    63 
    64 (* 
    65   transfer assumptions from one to another ctxt.
    66   does NOT respect scope: in a calculation identifiers are unique.
    67   but environments are scoped as usual in Luacs-interpretation.
    68 *)
    69 fun transfer_asms_from_to from_ctxt to_ctxt =
    70   let
    71     val to_vars = get_assumptions to_ctxt |> map TermC.vars |> flat
    72     fun transfer [] to_ctxt = to_ctxt
    73         | transfer (from_asm :: fas) to_ctxt =
    74             if inter op = (TermC.vars from_asm) to_vars = []
    75             then transfer fas to_ctxt
    76             else transfer fas (insert_assumptions [from_asm] to_ctxt)
    77   in transfer (get_assumptions from_ctxt) to_ctxt end
    78 
    79 (* there is still much TODO: factorise polynomials, etc *)
    80 fun contradict asm res = fold (curry or_) (map (fn p => TermC.negates res p) asm) false;
    81 
    82 (*
    83   Check a term for contradiction with a predicate list.
    84   This makes only sense for types bool and bool list. In these cases return
    85   the probably updated term together with 
    86   a respective list of non-contradicting predicates
    87 *)
    88 fun avoid_contradict t preds = 
    89       if TermC.is_bool_list t then
    90         let
    91           val no_contra = t |> TermC.dest_list' |> filter_out (contradict preds)
    92         in (no_contra |> TermC.list2isalist HOLogic.boolT, no_contra)
    93         end
    94       else if Term.type_of t = HOLogic.boolT then
    95         if contradict preds t then (@{term bool_undef}, []) else (t, [t])
    96       else (t (* type_of t \<noteq> bool *), [(* no predicate to add *)])
    97 
    98 (* for Canonical Argument Order used by |> below *)
    99 fun insert_assumptions_cao ctxt tms  = insert_assumptions tms ctxt
   100 
   101 fun subpbl_to_caller sub_ctxt prog_res caller_ctxt = caller_ctxt
   102   |> get_assumptions 
   103   |> avoid_contradict prog_res
   104   |> apsnd (insert_assumptions_cao caller_ctxt)
   105   |> apsnd (transfer_asms_from_to sub_ctxt)
   106 
   107 end