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