src/Tools/isac/BaseDefinitions/substitution.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 19 Oct 2022 10:43:04 +0200
changeset 60567 bb3140a02f3d
parent 60500 59a3af532717
child 60570 44f83099227d
permissions -rw-r--r--
eliminate term2str in src, Prog_Tac.*_adapt_to_type
     1 (* Title:  BaseDefinitions/substitution.sml
     2    Author: Walther Neuper
     3    (c) due to copyright terms
     4 
     5 The variety of substitutions is required by various presentations to the front end;
     6 this shall be simplified during the shift to Isabelle/PIDE.
     7 
     8 Two different formats shall remain, probably:
     9 # type input: is what the user sees (pairs of terms)
    10 # type as_eqs: a format useful for handling results of equation solving
    11 *)
    12 signature SUBSTITUTION =
    13 sig
    14   type T = LibraryC.subst;
    15   val to_string: T -> string
    16 
    17   type as_eqs = term list                   (* for Tactic.Substitute: [@{term "bdv = x"}, ...] *)
    18   type program = term                      (* in programs: @{term "[(''bdv_1'', x::real), ..]} *)
    19   type input = TermC.as_string list                        (* by student: ["(''bdv'', x)", ..] *)
    20   val input_empty: input
    21 
    22   type as_string_eqs                               (* for Tactic.Substitute ["bdv_1 = x", ...] *)
    23   val string_eqs_to_string: as_string_eqs -> string
    24 
    25   val T_to_string_eqs: T -> as_string_eqs
    26   val T_to_input: T -> input
    27   val T_from_string_eqs: theory -> as_string_eqs -> T
    28   val T_from_input: Proof.context -> input -> T
    29 
    30   val input_to_terms: Proof.context -> input -> as_eqs
    31   val eqs_to_input: as_eqs -> as_string_eqs
    32   val program_to_input: program -> input
    33   val for_bdv: program -> T -> input option * T
    34 \<^isac_test>\<open>
    35   val string_eqs_empty: as_string_eqs
    36   val T_from_program: program -> T
    37 \<close>
    38 end
    39 
    40 (**)
    41 structure Subst(**): SUBSTITUTION(**) =
    42 struct
    43 (**)
    44 
    45 type T = LibraryC.subst;
    46 fun to_string s =
    47     (strs2str o
    48       (map (
    49         linefeed o pair2str o (apsnd UnparseC.term) o (apfst UnparseC.term)))) s;
    50 
    51 type input = TermC.as_string list;
    52 val input_empty = [];
    53 
    54 type as_string_eqs = TermC.as_string list;
    55 \<^isac_test>\<open>
    56 val string_eqs_empty = []: TermC.as_string list;
    57 \<close>
    58 fun string_eqs_to_string s = strs2str s;
    59 
    60 type as_eqs = term list;
    61 type program = term;
    62 
    63 fun T_to_string_eqs subst = map UnparseC.term (map HOLogic.mk_eq subst)
    64 fun T_to_input subst_rew = (subst_rew
    65   |> map (apsnd UnparseC.term)
    66   |> map (apfst UnparseC.term)
    67   |> map (apfst (enclose "''" "''")))
    68   |> map pair2str
    69   handle TERM _ => raise TERM ("T_to_input: wrong argument " ^ to_string subst_rew, [])
    70 
    71 fun T_from_string_eqs thy s = map (TermC.dest_equals o (TermC.parse_patt thy)) s;
    72 (*TODO: input requires parse _: _ -> _ option*)
    73 fun T_from_input ctxt input = (input
    74   |> map (TermC.parse_patt (Proof_Context.theory_of ctxt))
    75   |> map TermC.isapair2pair
    76   |> map (apfst HOLogic.dest_string)
    77   |> map (apfst (fn str => (TermC.mk_Free (str, HOLogic.realT)))))
    78   handle TERM _ => raise TERM ("T_from_input: wrong argument " ^ strs2str' input, [])
    79 
    80 val eqs_to_input = map UnparseC.term;
    81 
    82 (* TermC.parse ctxt fails with "c_2 = 0" \<longrightarrow> \<open>c_2 = (0 :: 'a)\<close> and thus requires adapt_to_type *)
    83 fun input_to_terms ctxt strs = strs
    84   |> map (TermC.parse ctxt)
    85   |> map (Model_Pattern.adapt_term_to_type ctxt)
    86 
    87 fun program_to_input sub = (sub 
    88   |> HOLogic.dest_list 
    89   |> map HOLogic.dest_prod 
    90   |> map (fn (e1, e2) => (HOLogic.dest_string e1, UnparseC.term e2))
    91   |> map (fn (e1, e2) => "(''" ^ e1 ^ "'', " ^ e2 ^ ")"): TermC.as_string list)
    92   handle TERM _ => raise TERM ("program_to_input: wrong argument ", [sub])
    93 
    94 fun T_from_program t = (t 
    95   |> HOLogic.dest_list 
    96   |> map HOLogic.dest_prod 
    97   |> map (apfst HOLogic.dest_string))
    98   |> map (apfst (fn e1 => (TermC.mk_Free (e1, HOLogic.realT))))
    99   handle TERM _ => raise TERM ("T_from_program: wrong argument ", [t])
   100 
   101 (* get a substitution for "bdv*" from the current program and environment.
   102     returns a substitution: as_string_eqs for tac, subst for tac_, i.e. for rewriting *)
   103 fun for_bdv prog env =
   104   let
   105     fun scan (Const _) = NONE
   106       | scan (Free _) = NONE
   107       | scan (Var _) = NONE
   108       | scan (Bound _) = NONE
   109       | scan (t as Const (\<^const_name>\<open>Cons\<close>, _) $ (Const (\<^const_name>\<open>Pair\<close>, _) $ _ $ _) $ _) =
   110         if TermC.is_bdv_subst t then SOME t else NONE
   111       | scan (Abs (_, _, body)) = scan body
   112       | scan (t1 $ t2) = case scan t1 of NONE => scan t2 | SOME subst => SOME subst
   113   in
   114     case scan prog of
   115       NONE => (NONE: input option, []: subst)
   116     | SOME tm =>
   117         let val subst = subst_atomic env tm
   118         in (SOME (program_to_input subst), T_from_program subst) end
   119   end
   120 
   121 (**)end(**)