test/Tools/isac/BaseDefinitions/substitution.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 19 Oct 2022 10:43:04 +0200
changeset 60567 bb3140a02f3d
parent 60565 f92963a33fe3
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 
     6 "-----------------------------------------------------------------------------";
     7 "-----------------------------------------------------------------------------";
     8 "table of contents -----------------------------------------------------------";
     9 "-----------------------------------------------------------------------------";
    10 "-------- fun program_to_input -----------------------------------------------";
    11 "-------- fun T_from_input ---------------------------------------------------";
    12 "-------- fun T_from_program -------------------------------------------------";
    13 "-------- fun T_to_input -----------------------------------------------------";
    14 "-------- fun T_to_string_eqs ------------------------------------------------";
    15 "-------- fun T_from_string_eqs ----------------------------------------------";
    16 "-------- fun input_to_terms -------------------------------------------------";
    17 "-------- build fun for_bdv --------------------------------------------------";
    18 "-----------------------------------------------------------------------------";
    19 "-----------------------------------------------------------------------------";
    20 
    21 "-------- fun program_to_input -----------------------------------------------";
    22 "-------- fun program_to_input -----------------------------------------------";
    23 "-------- fun program_to_input -----------------------------------------------";
    24 val ctxt = Proof_Context.init_global @{theory}
    25 val subst_prog = @{term "[(''bdv_1'', x::real), (''bdv_2'', y::real), (''bdv_3'', z::real)]"};
    26 if Subst.program_to_input subst_prog = ["(''bdv_1'', x)", "(''bdv_2'', y)", "(''bdv_3'', z)"] then ()
    27 else error "program_to_input changed";
    28 
    29 "-------- fun T_from_input -----------------------------------------------------";
    30 "-------- fun T_from_input -----------------------------------------------------";
    31 "-------- fun T_from_input -----------------------------------------------------";
    32 case Subst.T_from_input ctxt ["(''bdv_1'', x)", "(''bdv_2'', y)", "(''bdv_3'', z)"] of 
    33   [(Free ("bdv_1", _), Free ("x", _)),
    34    (Free ("bdv_2", _), Free ("y", _)),
    35    (Free ("bdv_3", _), Free ("z", _))] => ()
    36 | _ => error "T_from_input changed";
    37 
    38 "-------- fun T_from_program ------------------------------------------------";
    39 "-------- fun T_from_program ------------------------------------------------";
    40 "-------- fun T_from_program ------------------------------------------------";
    41 val t = @{term "[(''bdv_1'', x::real), (''bdv_2'', y::real), (''bdv_3'', z::real)]"};
    42 case Subst.T_from_program t of 
    43   [(Free ("bdv_1", _), Free ("x", _)),
    44    (Free ("bdv_2", _), Free ("y", _)),
    45    (Free ("bdv_3", _), Free ("z", _))] => ()
    46 | _ => error "Subst.T_from_program changed";
    47 
    48 "-------- fun T_to_input -----------------------------------------------------";
    49 "-------- fun T_to_input -----------------------------------------------------";
    50 "-------- fun T_to_input -----------------------------------------------------";
    51 val subst_rew = 
    52   [(@{term "bdv_1 :: real"}, @{term "x :: real"}),
    53    (@{term "bdv_2 :: real"}, @{term "y :: real"}),
    54    (@{term "bdv_3 :: real"}, @{term "z :: real"})];
    55 if Subst.T_to_input subst_rew  = ["(''bdv_1'', x)", "(''bdv_2'', y)", "(''bdv_3'', z)"] then ()
    56 else error "T_to_strings changed";
    57 
    58 "-------- fun T_to_string_eqs ------------------------------------------------";
    59 "-------- fun T_to_string_eqs ------------------------------------------------";
    60 "-------- fun T_to_string_eqs ------------------------------------------------";
    61 val subst = 
    62   [(@{term "bdv_1 :: real"}, @{term "x :: real"}),
    63    (@{term "bdv_2 :: real"}, @{term "y :: real"}),
    64    (@{term "xxx :: real"}, @{term "aaa + (111 :: real)"})];
    65 
    66 if Subst.T_to_string_eqs subst = ["bdv_1 = x", "bdv_2 = y", "xxx = aaa + 111"] then ()
    67 else error "T_to_tyty changed";
    68 
    69 "-------- fun T_from_string_eqs ----------------------------------------------";
    70 "-------- fun T_from_string_eqs ----------------------------------------------";
    71 "-------- fun T_from_string_eqs ----------------------------------------------";
    72 val string_eqs = ["bdv_1 = x", "bdv_2 = y", "xxx = aaa + 111"];
    73 
    74 case Subst.T_from_string_eqs @{theory} string_eqs of
    75   [(Free ("bdv_1", _), Free ("x", _)),
    76    (Free ("bdv_2", _), Free ("y", _)),
    77     (Free ("xxx", _),
    78      Const (\<^const_name>\<open>plus_class.plus\<close>, _) $ Free ("aaa", _) $
    79        (Const (\<^const_name>\<open>numeral\<close>, _) $
    80          (Const (\<^const_name>\<open>num.Bit1\<close>, _) $ _ )))] => ()
    81 | _ => error "fun T_from_string_eqs";
    82 
    83 "-------- fun input_to_terms -------------------------------------------------";
    84 "-------- fun input_to_terms -------------------------------------------------";
    85 "-------- fun input_to_terms -------------------------------------------------";
    86 val input = ["(''bdv_1'', x)", "(''bdv_2'', y)", "(aaa, bbb + 2)"];
    87 
    88 case Subst.input_to_terms @{context} input of
    89   [Const (\<^const_name>\<open>Pair\<close>, _) $
    90     (Const (\<^const_name>\<open>Cons\<close>, _) $ (Const (\<^const_name>\<open>Char\<close>, _) $
    91       Const (\<^const_name>\<open>False\<close>, _) $ _ $ _ $ _ $ _ $ _ $ _ $ _) $ _) $
    92     Free ("x", _),
    93   _,
    94   Const (\<^const_name>\<open>Pair\<close>, _) $ Free ("aaa", _) $ _] => ()
    95 | _ => error "input_to_eqs CHANGED";
    96 
    97 "-------- build fun for_bdv --------------------------------------------------";
    98 "-------- build fun for_bdv --------------------------------------------------";
    99 "-------- build fun for_bdv --------------------------------------------------";
   100 Subst.program_to_input: Subst.program -> string list;
   101 
   102 val {scr = Prog prog, ...} = MethodC.from_store ctxt ["diff", "differentiate_on_R"];
   103 val env = [(TermC.parse_test @{context} "v_v", TermC.parse_test @{context} "x")] : Subst.T;
   104 
   105 "~~~~~ fun for_bdv, args:"; val (prog, env) = (prog, env);
   106     fun scan (Const _) = NONE
   107       | scan (Free _) = NONE
   108       | scan (Var _) = NONE
   109       | scan (Bound _) = NONE
   110       | scan (t as Const (\<^const_name>\<open>Cons\<close>, _) $ (Const (\<^const_name>\<open>Pair\<close>, _) $ _ $ _) $ _) =
   111         if TermC.is_bdv_subst t then SOME t else NONE
   112       | scan (Abs (_, _, body)) = scan body
   113       | scan (t1 $ t2) = case scan t1 of NONE => scan t2 | SOME subst => SOME subst
   114 
   115 val SOME tm = scan prog;
   116 val subst = tm |> subst_atomic env;
   117 if UnparseC.term tm = "[(''bdv'', v_v)]" then () else error "for_bdv changed 1";
   118 
   119 if Subst.program_to_input subst = (["(''bdv'', x)"] : Subst.input) then ()
   120 else error "for_bdv changed 2";