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