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