src/Tools/isac/Specify/Input_Descript.thy
author wneuper <walther.neuper@jku.at>
Wed, 28 Apr 2021 12:38:13 +0200
changeset 60265 9c9d61fed195
parent 60223 740ebee5948b
child 60309 70a1d102660d
permissions -rw-r--r--
eliminate "handle _ => ..." by \<^try>CARTOUCHE in src/*
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"
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@60125
    70
(*type T          ..TODO rename*)
walther@59953
    71
  type descriptor = Model_Pattern.descriptor
walther@59953
    72
wenzelm@60223
    73
  val for_real_list: term -> bool
walther@59953
    74
  val for_list: term -> bool
wenzelm@60223
    75
\<^isac_test>\<open>
wenzelm@60223
    76
  val for_bool_list: term -> bool
wenzelm@60223
    77
  val for_fun: term -> bool
wenzelm@60223
    78
  val is_a: term -> bool 
wenzelm@60223
    79
\<close>
walther@59953
    80
  val split: term -> descriptor * term list
walther@59953
    81
  val split': term * term -> term list
walther@59953
    82
  val join: descriptor * term list -> term
walther@59953
    83
  val join': descriptor * term list -> term
walther@59953
    84
  val join''': descriptor * term list -> string
walther@59953
    85
  val join'''': descriptor * term list -> term
wneuper@59595
    86
end
wneuper@59595
    87
\<close> ML \<open>
walther@59954
    88
walther@59954
    89
wneuper@59595
    90
(**)
wneuper@59595
    91
structure Input_Descript(**): INPUT_DESCRIPT =(**)
wneuper@59595
    92
struct
wneuper@59595
    93
(**)
walther@59953
    94
walther@59953
    95
type descriptor = Model_Pattern.descriptor;
walther@59953
    96
wneuper@59595
    97
(* distinguish input to Model (deep embedding of terms as Isac's object language) *)
walther@59953
    98
fun for_real_list (Const (_, Type("fun", [Type ("List.list", [Type ("Real.real", [])]), _]))) = true
walther@59953
    99
  | for_real_list (Const (_, Type("fun", [Type ("List.list", [Type ("Real.real", [])]),_])) $ _) = true
walther@59953
   100
  | for_real_list _ = false;
walther@59953
   101
fun for_bool_list (Const (_, Type("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _]))) = true
walther@59953
   102
  | for_bool_list (Const (_, Type("fun", [Type ("List.list", [Type ("HOL.bool", [])]),_])) $ _) = true
walther@59953
   103
  | for_bool_list _ = false;
walther@59953
   104
fun for_list (Const (_, Type("fun", [Type("List.list", _), _]))) = true
walther@59953
   105
  | for_list (Const (_, Type("fun", [Type("List.list", _), _])) $ _) = true
wneuper@59595
   106
  (*WN:8.5.03: ???   TODO test/../scrtools.sml                ~~~~ ???*)
walther@59953
   107
  | for_list _ = false;
walther@59953
   108
fun for_fun (Const (_, Type("fun", [_, Type("Input_Descript.unl", _)]))) = true
walther@59953
   109
  | for_fun _ = false;
walther@59953
   110
fun is_a (Const(_, Type("fun", [_, Type ("Input_Descript.nam",_)]))) = true
walther@59953
   111
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.una",_)]))) = true
walther@59953
   112
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.unl",_)]))) = true
walther@59953
   113
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.str",_)]))) = true
walther@59953
   114
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.toreal",_)]))) = true
walther@59953
   115
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.toreall",_)])))= true
walther@59953
   116
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.tobooll",_)])))= true
walther@59953
   117
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.unknow",_)])))= true
walther@59953
   118
  | is_a (Const(_, Type("fun", [_, Type ("Input_Descript.cpy",_)])))= true
walther@59953
   119
  | is_a _ = false;
wneuper@59595
   120
walther@59953
   121
(* special handling for lists. ?WN:14.5.03 ??!? *)
walther@59953
   122
fun dest_list (d, ts) = 
walther@59953
   123
  let fun dest t = 
walther@59953
   124
    if for_list d andalso not (for_fun d) andalso not (is_Free t) (*..for pbt*)
walther@59953
   125
    then TermC.isalist2list t
walther@59953
   126
    else [t]
walther@59953
   127
  in (flat o (map dest)) ts end;
wneuper@59599
   128
walther@59997
   129
(* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]", "[b]"] *)
walther@59953
   130
fun take_apart t =
walther@59953
   131
  let val elems = TermC.isalist2list t
walther@59953
   132
  in map ((TermC.list2isalist (type_of (hd elems))) o single) elems end;
walther@59953
   133
walther@59953
   134
(* decompose an input into description, terms (ev. elems of lists),
walther@59953
   135
    and the value for the problem-environment; inv to join   *)
walther@59953
   136
fun split (t as d $ arg) =
walther@59953
   137
    if is_a d
walther@59953
   138
    then if for_list d andalso TermC.is_list arg andalso for_fun d |> not
walther@59953
   139
      then (d, take_apart arg)
walther@59953
   140
      else (d, [arg])
walther@59953
   141
    else (TermC.empty, TermC.dest_list' t)
walther@59953
   142
  | split t =
walther@59953
   143
    let val t' as (h, _) = strip_comb t;
walther@59953
   144
    in
walther@59953
   145
      if is_a h
walther@59953
   146
      then (h, dest_list t')
walther@59953
   147
      else (TermC.empty, TermC.dest_list' t)
walther@59953
   148
    end;
walther@59953
   149
walther@59953
   150
(* version returning ts only *)
walther@59953
   151
fun split' (d, arg) = 
walther@59953
   152
    if is_a d
walther@59953
   153
    then if for_list d
walther@59953
   154
      then if TermC.is_list arg
walther@59953
   155
	      then if for_fun d
walther@59953
   156
	        then ([arg])           (* e.g. someList [1,3,2]                 *)
walther@59953
   157
		      else (take_apart arg)  (* [a,b] --> SML[ [a], [b] ]SML          *)
walther@59953
   158
	      else ([arg])             (* a variable or metavariable for a list *)
walther@59953
   159
	     else ([arg])
walther@59953
   160
    else (TermC.dest_list' arg)
walther@59953
   161
walther@59997
   162
(* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]", "[b]"] *)
walther@59953
   163
fun take_apart_inv ts = (* t = (take_apart_inv o take_apart) t *)
walther@59953
   164
  let val elems = (flat o (map TermC.isalist2list)) ts;
walther@59953
   165
  in TermC.list2isalist (type_of (hd elems)) elems end;
walther@59953
   166
walther@59953
   167
val script_parse = the o (@{theory BaseDefinitions} |> ThyC.to_ctxt |> TermC.parseNEW);
walther@59953
   168
val e_listReal = script_parse "[]::(real list)";
walther@59953
   169
val e_listBool = script_parse "[]::(bool list)";
walther@59953
   170
walther@59953
   171
(* revert split only for ts; jcompare join *)
walther@59953
   172
fun join'''' (d, ts) = 
walther@59953
   173
  if for_list d
walther@59953
   174
  then if TermC.is_list (hd ts)
walther@59953
   175
	  then if for_fun d
walther@59953
   176
	    then (hd ts)             (* e.g. someList [1,3,2] *)
walther@59953
   177
	    else (take_apart_inv ts) (* [ [a], [b] ] -> [a,b] *)
walther@59953
   178
	  else (hd ts)               (* a variable or metavariable for a list *)
walther@59953
   179
  else (hd ts);
walther@59953
   180
fun join (d, []) = 
walther@59953
   181
  	if for_real_list d
walther@59953
   182
  	then (d $ e_listReal)
walther@59953
   183
  	else if for_bool_list d then (d $ e_listBool) else d
walther@60265
   184
  | join (d, ts) = 
walther@60265
   185
    case \<^try>\<open> d $ (join'''' (d, ts))\<close> of
walther@60265
   186
      SOME x => x
walther@60265
   187
    | NONE => 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@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 UnparseC.term (d $ e_listReal)
walther@59953
   199
    else if for_bool_list d
walther@59953
   200
      then UnparseC.term (d $ e_listBool)
walther@59953
   201
      else UnparseC.term d
walther@60265
   202
  | join''' (d, ts) = 
walther@60265
   203
    case \<^try>\<open> UnparseC.term (d $ (join'''' (d, ts)))\<close> of
walther@60265
   204
      SOME x => x
walther@60265
   205
    | NONE => raise ERROR ("join''': " ^ UnparseC.term d ^ " $ " ^ UnparseC.term (hd ts)); 
wneuper@59599
   206
wneuper@59595
   207
(**)end(**)
wneuper@59595
   208
\<close> ML \<open>
wneuper@59595
   209
\<close> ML \<open>
wneuper@59595
   210
\<close> 
wneuper@59595
   211
wneuper@59599
   212
end