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