src/Tools/isac/BaseDefinitions/parseC.sml
author wneuper <Walther.Neuper@jku.at>
Mon, 30 Jan 2023 12:11:40 +0100
changeset 60661 91c30b11e5bc
parent 60660 c4b24621077e
child 60697 dd386fd3ec5e
permissions -rw-r--r--
cleanup parse #4: eliminate TermC.parse
Walther@60660
     1
(* Title:  BaseDefinitions/parseC..sml
Walther@60660
     2
   Author: Walther Neuper
Walther@60660
     3
   (c) due to copyright terms
Walther@60660
     4
*)
Walther@60660
     5
signature PARSE_ISAC =
Walther@60660
     6
sig
Walther@60661
     7
  val term_position: Proof.context -> string * Position.T -> term
Walther@60660
     8
  val pattern_position: Proof.context -> string * Position.T -> term
Walther@60660
     9
Walther@60660
    10
  val term_opt: Proof.context -> string -> term option
Walther@60660
    11
  val patt_opt: theory -> string -> term option
Walther@60660
    12
Walther@60660
    13
  val parse_test: Proof.context -> string -> term
Walther@60660
    14
  val parse_patt_test: theory -> string -> term
Walther@60660
    15
Walther@60660
    16
  val adapt_term_to_type: Proof.context -> term -> term
Walther@60660
    17
  val adapt_to_type_real: term -> term
Walther@60660
    18
  val adapt_to_type_int: term -> term
Walther@60660
    19
end
Walther@60660
    20
Walther@60660
    21
(**)
Walther@60660
    22
structure ParseC(**): PARSE_ISAC(**) =
Walther@60660
    23
struct
Walther@60660
    24
(**)
Walther@60660
    25
Walther@60660
    26
(** parse term with feedback by PIDE **)
Walther@60660
    27
Walther@60660
    28
fun term_position ctxt (str, pos) =
Walther@60660
    29
  Syntax.read_term ctxt str
Walther@60660
    30
    handle ERROR msg => error (msg ^ Position.here pos)
Walther@60660
    31
    (*this exception is caught by PIDE to show "msg" at the proper location on screen*)
Walther@60660
    32
fun pattern_position ctxt (str, pos) =
Walther@60660
    33
  Proof_Context.read_term_pattern ctxt str
Walther@60660
    34
    handle ERROR msg => error (msg ^ Position.here pos)
Walther@60660
    35
    (*this exception is caught by PIDE to show "msg" at the proper location on screen*)
Walther@60660
    36
Walther@60660
    37
Walther@60660
    38
(** parse term internally **)
Walther@60660
    39
Walther@60660
    40
fun term_opt ctxt str = \<^try>\<open>Syntax.read_term ctxt str\<close>;
Walther@60660
    41
fun patt_opt thy str = \<^try>\<open>Proof_Context.read_term_pattern (Proof_Context.init_global thy) str\<close>;
Walther@60660
    42
Walther@60660
    43
Walther@60660
    44
(** adapt type of pre-typed terms to current context **)
Walther@60660
    45
(* 
Walther@60660
    46
  adapt type of terms with most general type to a more specific one.
Walther@60660
    47
  TODO: clarify how to decide by use of data from the current context.
Walther@60660
    48
*)
Walther@60660
    49
fun T_a2real (Type (s, [])) = 
Walther@60660
    50
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
Walther@60660
    51
  | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
Walther@60660
    52
  | T_a2real (TFree (s, srt)) = 
Walther@60660
    53
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
Walther@60660
    54
  | T_a2real (TVar (("DUMMY", _), _)) = HOLogic.realT
Walther@60660
    55
  | T_a2real (TVar ((s, i), srt)) = 
Walther@60660
    56
    if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TVar ((s, i), srt)
Walther@60660
    57
(*val adapt_to_type_real: term -> term*)
Walther@60660
    58
fun adapt_to_type_real (Const( s, T)) = (Const( s, T_a2real T)) 
Walther@60660
    59
  | adapt_to_type_real (Free( s, T)) = (Free( s, T_a2real T))
Walther@60660
    60
  | adapt_to_type_real (Var( n, T)) = (Var( n, T_a2real T))
Walther@60660
    61
  | adapt_to_type_real (Bound i) = (Bound i)
Walther@60660
    62
  | adapt_to_type_real (Abs(s,T,t)) = Abs(s, T, adapt_to_type_real t)
Walther@60660
    63
  | adapt_to_type_real (t1 $ t2) = (adapt_to_type_real t1) $ (adapt_to_type_real t2);
Walther@60660
    64
(*val adapt_to_type_int: term -> term*)
Walther@60660
    65
fun adapt_to_type_int _ = raise ERROR "Model_Pattern.adapt_to_type NOT implemented for HOLogic.intT"
Walther@60660
    66
Walther@60660
    67
fun adapt_term_to_type ctxt t =
Walther@60660
    68
  let
Walther@60660
    69
    val choice = ctxt |> K HOLogic.realT (*clarify how ctxt can be used here*)
Walther@60660
    70
  in
Walther@60660
    71
    if choice = HOLogic.realT then adapt_to_type_real t
Walther@60660
    72
    else if choice = HOLogic.intT then adapt_to_type_int t
Walther@60660
    73
    else raise ERROR "TermC.adapt_to_type only implemented for HOLogic.realT"
Walther@60660
    74
  end
Walther@60660
    75
Walther@60660
    76
(** parse term in test/ **)
Walther@60660
    77
(*
Walther@60660
    78
  This bypasses building ctxt at the begin of a calculation
Walther@60660
    79
  and thus borrows adapt_to_type (used for adapting pre-parsed terms from Know_Store).
Walther@60660
    80
  These identifiers have been crudely query\replaced and
Walther@60660
    81
  shall eventually be brought in accordance with src/.
Walther@60660
    82
*)
Walther@60660
    83
fun parse_test ctxt str = str 
Walther@60660
    84
  |> Syntax.read_term_global (Proof_Context.theory_of ctxt)
Walther@60660
    85
  |> adapt_term_to_type ctxt
Walther@60660
    86
fun parse_patt_test thy str = (thy, str)
Walther@60660
    87
  |>> Proof_Context.init_global
Walther@60660
    88
  |-> Proof_Context.read_term_pattern
Walther@60660
    89
  |> adapt_term_to_type (Proof_Context.init_global thy)
Walther@60660
    90
Walther@60660
    91
(**)end(*struct*)