src/Tools/isac/Specify/Input_Descript.thy
author Walther Neuper <walther.neuper@jku.at>
Sat, 09 May 2020 12:00:11 +0200
changeset 59954 15ab4a0259fa
parent 59953 933211a252f2
child 59997 46fe5a8c3911
permissions -rw-r--r--
cleanup
     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 "~~/src/Tools/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   derivative     :: "real => una"
    36   equalities     :: "bool list => tobooll" (*WN071228 see fixedValues*)
    37   equality       :: "bool => una"
    38   errorBound     :: "bool => nam"
    39   
    40   fixedValues    :: "bool list => nam"
    41   functionEq     :: "bool => una"     (*6.5.03: functionTerm -> functionEq*)
    42   antiDerivative :: "bool => una"
    43   functionOf     :: "real => una"
    44 (*functionTerm   :: 'a => toreal 28.11.00*)
    45   functionTerm   :: "real => una"     (*6.5.03: functionTerm -> functionEq*)
    46   functionName   :: "real => una"
    47   interval       :: "real set => una"
    48   maxArgument    :: "bool => toreal"
    49   maximum        :: "real => toreal"
    50   
    51   relations      :: "bool list => una"
    52   solutions      :: "bool list => toreall"
    53 (*solution       :: bool => toreal  WN0509 bool list=> toreall --->EqSystem*)
    54   solveFor       :: "real => una"
    55   differentiateFor:: "real => una"
    56   unknown        :: "'a => unknow"
    57   valuesFor      :: "real list => toreall"
    58 
    59   intTestGiven   :: "int => una"
    60   realTestGiven  :: "real => una"
    61   realTestFind   :: "real => una"
    62   boolTestGiven  :: "bool => una"
    63   boolTestFind   :: "bool => una"
    64 
    65 subsection \<open>Functions decking for descriptions of input in specify-phase\<close>
    66 ML \<open>
    67 \<close> ML \<open>
    68 signature INPUT_DESCRIPT =
    69 sig
    70   type descriptor = Model_Pattern.descriptor
    71 
    72   val for_list: term -> bool
    73 
    74   val split: term -> descriptor * term list
    75   val split': term * term -> term list
    76   val join: descriptor * term list -> term
    77   val join': descriptor * term list -> term
    78   val join''': descriptor * term list -> string
    79   val join'''': descriptor * term list -> term
    80 
    81 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    82   val for_real_list: term -> bool
    83 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    84   val is_a: term -> bool 
    85   val for_bool_list: term -> bool
    86   val for_fun: term -> bool
    87 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    88 end
    89 \<close> ML \<open>
    90 
    91 
    92 (**)
    93 structure Input_Descript(**): INPUT_DESCRIPT =(**)
    94 struct
    95 (**)
    96 
    97 type descriptor = Model_Pattern.descriptor;
    98 
    99 (* distinguish input to Model (deep embedding of terms as Isac's object language) *)
   100 fun for_real_list (Const (_, Type("fun", [Type ("List.list", [Type ("Real.real", [])]), _]))) = true
   101   | for_real_list (Const (_, Type("fun", [Type ("List.list", [Type ("Real.real", [])]),_])) $ _) = true
   102   | for_real_list _ = false;
   103 fun for_bool_list (Const (_, Type("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _]))) = true
   104   | for_bool_list (Const (_, Type("fun", [Type ("List.list", [Type ("HOL.bool", [])]),_])) $ _) = true
   105   | for_bool_list _ = false;
   106 fun for_list (Const (_, Type("fun", [Type("List.list", _), _]))) = true
   107   | for_list (Const (_, Type("fun", [Type("List.list", _), _])) $ _) = true
   108   (*WN:8.5.03: ???   TODO test/../scrtools.sml                ~~~~ ???*)
   109   | for_list _ = false;
   110 fun for_fun (Const (_, Type("fun", [_, Type("Input_Descript.unl", _)]))) = true
   111   | for_fun _ = false;
   112 fun is_a (Const(_, Type("fun", [_, Type ("Input_Descript.nam",_)]))) = true
   113   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.una",_)]))) = true
   114   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.unl",_)]))) = true
   115   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.str",_)]))) = true
   116   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.toreal",_)]))) = true
   117   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.toreall",_)])))= true
   118   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.tobooll",_)])))= true
   119   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.unknow",_)])))= true
   120   | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.cpy",_)])))= true
   121   | is_a _ = false;
   122 
   123 (* special handling for lists. ?WN:14.5.03 ??!? *)
   124 fun dest_list (d, ts) = 
   125   let fun dest t = 
   126     if for_list d andalso not (for_fun d) andalso not (is_Free t) (*..for pbt*)
   127     then TermC.isalist2list t
   128     else [t]
   129   in (flat o (map dest)) ts end;
   130 
   131 (* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]","[b]"] *)
   132 fun take_apart t =
   133   let val elems = TermC.isalist2list t
   134   in map ((TermC.list2isalist (type_of (hd elems))) o single) elems end;
   135 
   136 (* decompose an input into description, terms (ev. elems of lists),
   137     and the value for the problem-environment; inv to join   *)
   138 fun split (t as d $ arg) =
   139     if is_a d
   140     then if for_list d andalso TermC.is_list arg andalso for_fun d |> not
   141       then (d, take_apart arg)
   142       else (d, [arg])
   143     else (TermC.empty, TermC.dest_list' t)
   144   | split t =
   145     let val t' as (h, _) = strip_comb t;
   146     in
   147       if is_a h
   148       then (h, dest_list t')
   149       else (TermC.empty, TermC.dest_list' t)
   150     end;
   151 
   152 (* version returning ts only *)
   153 fun split' (d, arg) = 
   154     if is_a d
   155     then if for_list d
   156       then if TermC.is_list arg
   157 	      then if for_fun d
   158 	        then ([arg])           (* e.g. someList [1,3,2]                 *)
   159 		      else (take_apart arg)  (* [a,b] --> SML[ [a], [b] ]SML          *)
   160 	      else ([arg])             (* a variable or metavariable for a list *)
   161 	     else ([arg])
   162     else (TermC.dest_list' arg)
   163 
   164 (* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]","[b]"] *)
   165 fun take_apart_inv ts = (* t = (take_apart_inv o take_apart) t *)
   166   let val elems = (flat o (map TermC.isalist2list)) ts;
   167   in TermC.list2isalist (type_of (hd elems)) elems end;
   168 
   169 val script_parse = the o (@{theory BaseDefinitions} |> ThyC.to_ctxt |> TermC.parseNEW);
   170 val e_listReal = script_parse "[]::(real list)";
   171 val e_listBool = script_parse "[]::(bool list)";
   172 
   173 (* revert split only for ts; jcompare join *)
   174 fun join'''' (d, ts) = 
   175   if for_list d
   176   then if TermC.is_list (hd ts)
   177 	  then if for_fun d
   178 	    then (hd ts)             (* e.g. someList [1,3,2] *)
   179 	    else (take_apart_inv ts) (* [ [a], [b] ] -> [a,b] *)
   180 	  else (hd ts)               (* a variable or metavariable for a list *)
   181   else (hd ts);
   182 fun join (d, []) = 
   183   	if for_real_list d
   184   	then (d $ e_listReal)
   185   	else if for_bool_list d then (d $ e_listBool) else d
   186   | join (d, ts) = (d $ (join'''' (d, ts)))
   187     handle _ => raise ERROR ("join: " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (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) = (d $ (join'''' (d, ts)))
   193     handle _ => raise ERROR ("join': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   194 fun join''' (d, []) = 
   195     if for_real_list d
   196     then UnparseC.term (d $ e_listReal)
   197     else if for_bool_list d
   198       then UnparseC.term (d $ e_listBool)
   199       else UnparseC.term d
   200   | join''' (d, ts) = UnparseC.term (d $ (join'''' (d, ts)))
   201     handle _ => raise ERROR ("join''': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
   202 
   203 (**)end(**)
   204 \<close> ML \<open>
   205 \<close> ML \<open>
   206 \<close> 
   207 
   208 end