1 (* Title: collect all defitions for the Lucas-Interpreter
2 Author: Walther Neuper 110226
3 (c) due to copyright terms
5 Descriptors for input into an I_Model.
6 Since PblObj.ctxt records explicit type constraints in Formalise.model,
7 the types of the descriptors below can be more relaxed.
10 theory Input_Descript imports "$ISABELLE_ISAC/BaseDefinitions/BaseDefinitions"
13 subsection \<open>typedecl for descriptors of input in specify-phase\<close>
15 The typedecl below MUST ALL be registered in "Input_Descript.is_a"
17 typedecl nam (* named variables *)
18 typedecl una (* unnamed variables *)
19 typedecl unl (* unnamed variables of type list, elementwise input prohibited*)
20 typedecl str (* structured variables *)
21 typedecl toreal (* var with undef real value: forces typing *)
22 typedecl toreall (* var with undef real list value: forces typing *)
23 typedecl tobooll (* var with undef bool list value: forces typing *)
24 typedecl unknow (* input without dsc in fmz=[] *)
25 typedecl cpy (* copy-named variables identified by .._0, .._i .._' in pbt *)
27 subsection \<open>consts for general descriptors of input in the specify-phase\<close>
30 (*TODO.180331: review for localisation of model-patterns and of method's guards*)
31 someList :: "'a list => unl" (*not for elementwise input, eg. inssort*)
33 additionalRels :: "bool list => una"
34 boundVariable :: "real => una"
35 FunctionVariable :: "real => una"
36 derivative :: "real => una"
37 equalities :: "bool list => tobooll" (*WN071228 see fixedValues*)
38 equality :: "bool => una"
39 errorBound :: "bool => nam"
40 ErrorBound :: "bool => nam"
42 fixedValues :: "bool list => nam"
43 Constants :: "bool list => nam"
44 functionEq :: "bool => una" (*6.5.03: functionTerm -> functionEq*)
45 antiDerivative :: "bool => una"
46 functionOf :: "real => una"
47 (*functionTerm :: 'a => toreal 28.11.00*)
48 functionTerm :: "real => una" (*6.5.03: functionTerm -> functionEq*)
49 functionName :: "real => una"
50 interval :: "real set => una"
51 Domain :: "real set => una"
52 maxArgument :: "bool => toreal"
53 maximum :: "real => toreal"
54 Maximum :: "real => toreal"
55 Extremum :: "bool => toreal"
57 relations :: "bool list => una"
58 SideConditions :: "bool list => una"
59 solutions :: "bool list => toreall"
60 (*solution :: bool => toreal WN0509 bool list=> toreall --->EqSystem*)
61 solveFor :: "real => una"
62 differentiateFor:: "real => una"
63 unknown :: "'a => unknow"
64 valuesFor :: "real list => toreall"
65 AdditionalValues :: "real list => toreall"
67 intTestGiven :: "int => una"
68 realTestGiven :: "real => una"
69 realTestFind :: "real => una"
70 boolTestGiven :: "bool => una"
71 boolTestFind :: "bool => una"
73 subsection \<open>Functions decking for descriptions of input in specify-phase\<close>
76 signature INPUT_DESCRIPT =
78 (*type T ..TODO rename*)
79 type descriptor = Model_Pattern.descriptor
81 val for_real_list: term -> bool
82 val for_list: term -> bool
84 val for_bool_list: term -> bool
85 val for_fun: term -> bool
86 val is_a: term -> bool
88 val split: term -> descriptor * term list
89 val split': term * term -> term list
90 val join: descriptor * term list -> term
91 val join': descriptor * term list -> term
92 val join''': descriptor * term list -> string
93 val join'''': descriptor * term list -> term
99 structure Input_Descript(**): INPUT_DESCRIPT =(**)
103 type descriptor = Model_Pattern.descriptor;
105 (* distinguish input to Model (deep embedding of terms as Isac's object language) *)
106 fun for_real_list (Const (_, Type(\<^type_name>\<open>fun\<close>, [Type(\<^type_name>\<open>list\<close>, [Type (\<^type_name>\<open>real\<close>, [])]), _]))) = true
107 | for_real_list (Const (_, Type(\<^type_name>\<open>fun\<close>, [Type(\<^type_name>\<open>list\<close>, [Type (\<^type_name>\<open>real\<close>, [])]),_])) $ _) = true
108 | for_real_list _ = false;
109 fun for_bool_list (Const (_, Type(\<^type_name>\<open>fun\<close>, [Type(\<^type_name>\<open>list\<close>, [Type (\<^type_name>\<open>bool\<close>, [])]), _]))) = true
110 | for_bool_list (Const (_, Type(\<^type_name>\<open>fun\<close>, [Type(\<^type_name>\<open>list\<close>, [Type (\<^type_name>\<open>bool\<close>, [])]),_])) $ _) = true
111 | for_bool_list _ = false;
112 fun for_list (Const (_, Type(\<^type_name>\<open>fun\<close>, [Type(\<^type_name>\<open>list\<close>, _), _]))) = true
113 | for_list (Const (_, Type(\<^type_name>\<open>fun\<close>, [Type(\<^type_name>\<open>list\<close>, _), _])) $ _) = true
114 (*WN:8.5.03: ??? TODO test/../scrtools.sml ~~~~ ???*)
115 | for_list _ = false;
116 fun for_fun (Const (_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>unl\<close>, _)]))) = true
118 fun is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>nam\<close>, _)]))) = true
119 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>una\<close>, _)]))) = true
120 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>unl\<close>, _)]))) = true
121 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>str\<close>, _)]))) = true
122 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>toreal\<close>, _)]))) = true
123 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>toreall\<close>, _)])))= true
124 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>tobooll\<close>, _)])))= true
125 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>unknow\<close>, _)])))= true
126 | is_a (Const(_, Type(\<^type_name>\<open>fun\<close>, [_, Type(\<^type_name>\<open>cpy\<close>, _)])))= true
129 (* special handling for lists. ?WN:14.5.03 ??!? *)
130 fun dest_list (d, ts) =
132 if for_list d andalso not (for_fun d) andalso not (is_Free t) (*..for pbt*)
133 then TermC.isalist2list t
135 in (flat o (map dest)) ts end;
137 (* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]", "[b]"] *)
139 let val elems = TermC.isalist2list t
140 in map ((TermC.list2isalist (type_of (hd elems))) o single) elems end;
142 (* decompose an input into description, terms (ev. elems of lists),
143 and the value for the problem-environment; inv to join *)
144 fun split (t as d $ arg) =
146 then if for_list d andalso TermC.is_list arg andalso for_fun d |> not
147 then (d, take_apart arg)
149 else (TermC.empty, TermC.dest_list' t)
151 let val t' as (h, _) = strip_comb t;
154 then (h, dest_list t')
155 else (TermC.empty, TermC.dest_list' t)
158 (* version returning ts only *)
159 fun split' (d, arg) =
162 then if TermC.is_list arg
164 then ([arg]) (* e.g. someList [1,3,2] *)
165 else (take_apart arg) (* [a,b] --> SML[ [a], [b] ]SML *)
166 else ([arg]) (* a variable or metavariable for a list *)
168 else (TermC.dest_list' arg)
170 (* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]", "[b]"] *)
171 fun take_apart_inv ts = (* t = (take_apart_inv o take_apart) t *)
172 let val elems = (flat o (map TermC.isalist2list)) ts;
173 in TermC.list2isalist (type_of (hd elems)) elems end;
175 val script_parse = the o (@{theory BaseDefinitions} |> Proof_Context.init_global |> TermC.parseNEW);
176 val e_listReal = script_parse "[]::(real list)";
177 val e_listBool = script_parse "[]::(bool list)";
179 (* revert split only for ts; jcompare join *)
180 fun join'''' (d, ts) =
182 then if TermC.is_list (hd ts)
184 then (hd ts) (* e.g. someList [1,3,2] *)
185 else (take_apart_inv ts) (* [ [a], [b] ] -> [a,b] *)
186 else (hd ts) (* a variable or metavariable for a list *)
190 then (d $ e_listReal)
191 else if for_bool_list d then (d $ e_listBool) else d
193 case \<^try>\<open> d $ (join'''' (d, ts))\<close> of
195 | NONE => raise ERROR ("join: " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts));
198 then (d $ e_listReal)
199 else if for_bool_list d then (d $ e_listBool) else d
201 case \<^try>\<open> d $ (join'''' (d, ts))\<close> of
203 | NONE => raise ERROR ("join': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts));
204 fun join''' (d, []) =
206 then UnparseC.term (d $ e_listReal)
207 else if for_bool_list d
208 then UnparseC.term (d $ e_listBool)
211 case \<^try>\<open> UnparseC.term (d $ (join'''' (d, ts)))\<close> of
213 | NONE => raise ERROR ("join''': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts));