src/Tools/isac/Specify/Input_Descript.thy
author wneuper <Walther.Neuper@jku.at>
Wed, 11 Jan 2023 11:38:01 +0100
changeset 60650 06ec8abfd3bc
parent 60556 486223010ea8
child 60658 1c089105f581
permissions -rw-r--r--
eliminate use of Thy_Info 12: TermC partially
     1 (* Title:  collect all defitions for the Lucas-Interpreter
     2    Author: Walther Neuper 110226
     3    (c) due to copyright terms
     4 
     5 Descriptors for interactive input into a Specification of an Example.
     6 
     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. 
    10 *)
    11 
    12 theory Input_Descript imports "$ISABELLE_ISAC/BaseDefinitions/BaseDefinitions"
    13 begin
    14 
    15 subsection \<open>typedecl for descriptors of input in specify-phase\<close>
    16 text \<open>
    17   The typedecl below MUST ALL be registered in "Input_Descript.is_a"
    18 \<close>
    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   *)
    28 
    29 subsection \<open>consts for general descriptors of input in the specify-phase\<close>
    30 
    31 consts
    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*)
    34 
    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"
    43   
    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"
    58   
    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"
    68 
    69   intTestGiven   :: "int => una"
    70   realTestGiven  :: "real => una"
    71   realTestFind   :: "real => una"
    72   boolTestGiven  :: "bool => una"
    73   boolTestFind   :: "bool => una"
    74 
    75 subsection \<open>Functions decking for descriptions of input in specify-phase\<close>
    76 ML \<open>
    77 \<close> ML \<open>
    78 signature INPUT_DESCRIPT =
    79 sig
    80 (*type T          ..TODO rename*)
    81   type descriptor = Model_Pattern.descriptor
    82 
    83   val for_real_list: term -> bool
    84   val for_list: term -> bool
    85 \<^isac_test>\<open>
    86   val for_bool_list: term -> bool
    87   val for_fun: term -> bool
    88   val is_a: term -> bool 
    89 \<close>
    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
    96 end
    97 \<close> ML \<open>
    98 
    99 
   100 (**)
   101 structure Input_Descript(**): INPUT_DESCRIPT =(**)
   102 struct
   103 (**)
   104 
   105 type descriptor = Model_Pattern.descriptor;
   106 
   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
   119   | for_fun _ = false;
   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
   129   | is_a _ = false;
   130 
   131 (* special handling for lists. ?WN:14.5.03 ??!? *)
   132 fun dest_list (d, ts) = 
   133   let fun dest t = 
   134     if for_list d andalso not (for_fun d) andalso not (is_Free t) (*..for pbt*)
   135     then TermC.isalist2list t
   136     else [t]
   137   in (flat o (map dest)) ts end;
   138 
   139 (* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]", "[b]"] *)
   140 fun take_apart t =
   141   let val elems = TermC.isalist2list t
   142   in map ((TermC.list2isalist (type_of (hd elems))) o single) elems end;
   143 
   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) =
   147     if is_a d
   148     then if for_list d andalso TermC.is_list arg andalso for_fun d |> not
   149       then (d, take_apart arg)
   150       else (d, [arg])
   151     else (TermC.empty, TermC.dest_list' t)
   152   | split t =
   153     let val t' as (h, _) = strip_comb t;
   154     in
   155       if is_a h
   156       then (h, dest_list t')
   157       else (TermC.empty, TermC.dest_list' t)
   158     end;
   159 
   160 (* version returning ts only *)
   161 fun split' (d, arg) = 
   162     if is_a d
   163     then if for_list d
   164       then if TermC.is_list arg
   165 	      then if for_fun d
   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 *)
   169 	     else ([arg])
   170     else (TermC.dest_list' arg)
   171 
   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;
   176 
   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)";
   180 
   181 (* revert split only for ts *)
   182 fun join'''' (d, ts) = 
   183   if for_list d
   184   then if TermC.is_list (hd ts)
   185 	  then if for_fun d
   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 *)
   189   else (hd ts);
   190 (* *)
   191 fun join (d, []) = 
   192   	if for_real_list d
   193   	then (d $ e_listReal)
   194   	else if for_bool_list d then (d $ e_listBool) else d
   195   | join (d, ts) = 
   196     case \<^try>\<open> d $ (join'''' (d, ts))\<close> of
   197       SOME x => x
   198     | NONE => raise ERROR ("join: " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   199 fun join' (d, []) = 
   200     if for_real_list d
   201     then (d $ e_listReal)
   202     else if for_bool_list d then (d $ e_listBool) else d
   203   | join' (d, ts) = 
   204     case \<^try>\<open> d $ (join'''' (d, ts))\<close> of
   205       SOME x => x
   206     | NONE => raise ERROR ("join': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   207 fun join''' (d, []) = 
   208     if for_real_list d
   209     then UnparseC.term (d $ e_listReal)
   210     else if for_bool_list d
   211       then UnparseC.term (d $ e_listBool)
   212       else UnparseC.term d
   213   | join''' (d, ts) = 
   214     case \<^try>\<open> UnparseC.term (d $ (join'''' (d, ts)))\<close> of
   215       SOME x => x
   216     | NONE => raise ERROR ("join''': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   217 
   218 (**)end(**)
   219 \<close> ML \<open>
   220 \<close> ML \<open>
   221 \<close> 
   222 
   223 end