src/Tools/isac/Specify/Input_Descript.thy
author wneuper <Walther.Neuper@jku.at>
Sun, 19 Jun 2022 12:46:43 +0200
changeset 60460 d282b5f5e5a4
parent 60373 9e906a2b3496
child 60478 feed17a67534
permissions -rw-r--r--
add input-descriptors for Demo_Example
     1 (* Title:  collect all defitions for the Lucas-Interpreter
     2    Author: Walther Neuper 110226
     3    (c) due to copyright terms
     4 
     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. 
     8 *)
     9 
    10 theory Input_Descript imports "$ISABELLE_ISAC/BaseDefinitions/BaseDefinitions"
    11 begin
    12 
    13 subsection \<open>typedecl for descriptors of input in specify-phase\<close>
    14 text \<open>
    15   The typedecl below MUST ALL be registered in "Input_Descript.is_a"
    16 \<close>
    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   *)
    26 
    27 subsection \<open>consts for general descriptors of input in the specify-phase\<close>
    28 
    29 consts
    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*)
    32 
    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"
    41   
    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"
    56   
    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"
    66 
    67   intTestGiven   :: "int => una"
    68   realTestGiven  :: "real => una"
    69   realTestFind   :: "real => una"
    70   boolTestGiven  :: "bool => una"
    71   boolTestFind   :: "bool => una"
    72 
    73 subsection \<open>Functions decking for descriptions of input in specify-phase\<close>
    74 ML \<open>
    75 \<close> ML \<open>
    76 signature INPUT_DESCRIPT =
    77 sig
    78 (*type T          ..TODO rename*)
    79   type descriptor = Model_Pattern.descriptor
    80 
    81   val for_real_list: term -> bool
    82   val for_list: term -> bool
    83 \<^isac_test>\<open>
    84   val for_bool_list: term -> bool
    85   val for_fun: term -> bool
    86   val is_a: term -> bool 
    87 \<close>
    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
    94 end
    95 \<close> ML \<open>
    96 
    97 
    98 (**)
    99 structure Input_Descript(**): INPUT_DESCRIPT =(**)
   100 struct
   101 (**)
   102 
   103 type descriptor = Model_Pattern.descriptor;
   104 
   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
   117   | for_fun _ = false;
   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
   127   | is_a _ = false;
   128 
   129 (* special handling for lists. ?WN:14.5.03 ??!? *)
   130 fun dest_list (d, ts) = 
   131   let fun dest t = 
   132     if for_list d andalso not (for_fun d) andalso not (is_Free t) (*..for pbt*)
   133     then TermC.isalist2list t
   134     else [t]
   135   in (flat o (map dest)) ts end;
   136 
   137 (* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]", "[b]"] *)
   138 fun take_apart t =
   139   let val elems = TermC.isalist2list t
   140   in map ((TermC.list2isalist (type_of (hd elems))) o single) elems end;
   141 
   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) =
   145     if is_a d
   146     then if for_list d andalso TermC.is_list arg andalso for_fun d |> not
   147       then (d, take_apart arg)
   148       else (d, [arg])
   149     else (TermC.empty, TermC.dest_list' t)
   150   | split t =
   151     let val t' as (h, _) = strip_comb t;
   152     in
   153       if is_a h
   154       then (h, dest_list t')
   155       else (TermC.empty, TermC.dest_list' t)
   156     end;
   157 
   158 (* version returning ts only *)
   159 fun split' (d, arg) = 
   160     if is_a d
   161     then if for_list d
   162       then if TermC.is_list arg
   163 	      then if for_fun d
   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 *)
   167 	     else ([arg])
   168     else (TermC.dest_list' arg)
   169 
   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;
   174 
   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)";
   178 
   179 (* revert split only for ts; jcompare join *)
   180 fun join'''' (d, ts) = 
   181   if for_list d
   182   then if TermC.is_list (hd ts)
   183 	  then if for_fun d
   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 *)
   187   else (hd ts);
   188 fun join (d, []) = 
   189   	if for_real_list d
   190   	then (d $ e_listReal)
   191   	else if for_bool_list d then (d $ e_listBool) else d
   192   | join (d, ts) = 
   193     case \<^try>\<open> d $ (join'''' (d, ts))\<close> of
   194       SOME x => x
   195     | NONE => raise ERROR ("join: " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   196 fun join' (d, []) = 
   197     if for_real_list d
   198     then (d $ e_listReal)
   199     else if for_bool_list d then (d $ e_listBool) else d
   200   | join' (d, ts) = 
   201     case \<^try>\<open> d $ (join'''' (d, ts))\<close> of
   202       SOME x => x
   203     | NONE => raise ERROR ("join': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   204 fun join''' (d, []) = 
   205     if for_real_list d
   206     then UnparseC.term (d $ e_listReal)
   207     else if for_bool_list d
   208       then UnparseC.term (d $ e_listBool)
   209       else UnparseC.term d
   210   | join''' (d, ts) = 
   211     case \<^try>\<open> UnparseC.term (d $ (join'''' (d, ts)))\<close> of
   212       SOME x => x
   213     | NONE => raise ERROR ("join''': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   214 
   215 (**)end(**)
   216 \<close> ML \<open>
   217 \<close> ML \<open>
   218 \<close> 
   219 
   220 end