src/Pure/General/exn.scala
author wenzelm
Tue, 29 May 2018 14:25:39 +0200
changeset 68315 d088799fd278
parent 68131 62a3294edda3
child 68806 4597812d5182
permissions -rw-r--r--
more operations (as in ML);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62508
d0b68218ea55 discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents: 62492
diff changeset
     1
/*  Title:      Pure/General/exn.scala
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     3
43761
e72ba84ae58f tuned signature -- corresponding to Scala version;
wenzelm
parents: 34313
diff changeset
     4
Support for exceptions (arbitrary throwables).
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     5
*/
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     6
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     7
package isabelle
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     8
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     9
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    10
object Exn
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    11
{
62492
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    12
  /* user errors */
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    13
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    14
  class User_Error(message: String) extends RuntimeException(message)
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    15
  {
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    16
    override def equals(that: Any): Boolean =
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    17
      that match {
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    18
        case other: User_Error => message == other.getMessage
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    19
        case _ => false
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    20
      }
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    21
    override def hashCode: Int = message.hashCode
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    22
65828
02dd430d80c5 tuned signature;
wenzelm
parents: 65716
diff changeset
    23
    override def toString: String = "\n" + Output.error_message_text(message)
62492
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    24
  }
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    25
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    26
  object ERROR
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    27
  {
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    28
    def apply(message: String): User_Error = new User_Error(message)
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    29
    def unapply(exn: Throwable): Option[String] = user_message(exn)
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    30
  }
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    31
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    32
  def error(message: String): Nothing = throw ERROR(message)
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    33
68131
62a3294edda3 tuned signature;
wenzelm
parents: 65828
diff changeset
    34
  def cat_message(msgs: String*): String =
62a3294edda3 tuned signature;
wenzelm
parents: 65828
diff changeset
    35
    cat_lines(msgs.iterator.filterNot(_ == ""))
62492
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    36
68131
62a3294edda3 tuned signature;
wenzelm
parents: 65828
diff changeset
    37
  def cat_error(msgs: String*): Nothing =
62a3294edda3 tuned signature;
wenzelm
parents: 65828
diff changeset
    38
    error(cat_message(msgs:_*))
62492
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    39
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
    40
44158
fe6d1ae7a065 clarified Exn.message;
wenzelm
parents: 43762
diff changeset
    41
  /* exceptions as values */
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    42
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    43
  sealed abstract class Result[A]
59698
d4ce901f20c5 value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents: 59697
diff changeset
    44
  {
d4ce901f20c5 value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents: 59697
diff changeset
    45
    def user_error: Result[A] =
d4ce901f20c5 value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents: 59697
diff changeset
    46
      this match {
d4ce901f20c5 value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents: 59697
diff changeset
    47
        case Exn(ERROR(msg)) => Exn(ERROR(msg))
d4ce901f20c5 value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents: 59697
diff changeset
    48
        case _ => this
d4ce901f20c5 value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents: 59697
diff changeset
    49
      }
d4ce901f20c5 value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents: 59697
diff changeset
    50
  }
43762
wenzelm
parents: 43761
diff changeset
    51
  case class Res[A](res: A) extends Result[A]
wenzelm
parents: 43761
diff changeset
    52
  case class Exn[A](exn: Throwable) extends Result[A]
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    53
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    54
  def capture[A](e: => A): Result[A] =
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    55
    try { Res(e) }
34313
2f890016afab treat *all* JVM throwables as "exceptions", cf. ML version;
wenzelm
parents: 34300
diff changeset
    56
    catch { case exn: Throwable => Exn[A](exn) }
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    57
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    58
  def release[A](result: Result[A]): A =
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    59
    result match {
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    60
      case Res(x) => x
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    61
      case Exn(exn) => throw exn
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    62
    }
44158
fe6d1ae7a065 clarified Exn.message;
wenzelm
parents: 43762
diff changeset
    63
59136
c2b23cb8a677 added Par_List in Scala, in accordance to ML version;
wenzelm
parents: 57831
diff changeset
    64
  def release_first[A](results: List[Result[A]]): List[A] =
59138
wenzelm
parents: 59136
diff changeset
    65
    results.find({ case Exn(exn) => !is_interrupt(exn) case _ => false }) match {
wenzelm
parents: 59136
diff changeset
    66
      case Some(Exn(exn)) => throw exn
wenzelm
parents: 59136
diff changeset
    67
      case _ => results.map(release(_))
wenzelm
parents: 59136
diff changeset
    68
    }
59136
c2b23cb8a677 added Par_List in Scala, in accordance to ML version;
wenzelm
parents: 57831
diff changeset
    69
44158
fe6d1ae7a065 clarified Exn.message;
wenzelm
parents: 43762
diff changeset
    70
56667
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    71
  /* interrupts */
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    72
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    73
  def is_interrupt(exn: Throwable): Boolean =
56670
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    74
  {
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    75
    var found_interrupt = false
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    76
    var e = exn
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    77
    while (!found_interrupt && e != null) {
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    78
      found_interrupt |= e.isInstanceOf[InterruptedException]
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    79
      e = e.getCause
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    80
    }
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    81
    found_interrupt
3f23441453d0 detect nested interrupts;
wenzelm
parents: 56667
diff changeset
    82
  }
56667
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    83
68315
d088799fd278 more operations (as in ML);
wenzelm
parents: 68131
diff changeset
    84
  def interruptible_capture[A](e: => A): Result[A] =
d088799fd278 more operations (as in ML);
wenzelm
parents: 68131
diff changeset
    85
    try { Res(e) }
d088799fd278 more operations (as in ML);
wenzelm
parents: 68131
diff changeset
    86
    catch { case exn: Throwable if !is_interrupt(exn) => Exn[A](exn) }
d088799fd278 more operations (as in ML);
wenzelm
parents: 68131
diff changeset
    87
56667
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    88
  object Interrupt
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    89
  {
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    90
    def apply(): Throwable = new InterruptedException
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    91
    def unapply(exn: Throwable): Boolean = is_interrupt(exn)
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
    92
61558
wenzelm
parents: 59698
diff changeset
    93
    def expose() { if (Thread.interrupted) throw apply() }
56861
5f827142d89a tuned signature;
wenzelm
parents: 56860
diff changeset
    94
    def impose() { Thread.currentThread.interrupt }
56860
dc71c3d0e909 tuned signature;
wenzelm
parents: 56782
diff changeset
    95
56862
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
    96
    def postpone[A](body: => A): Option[A] =
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
    97
    {
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
    98
      val interrupted = Thread.interrupted
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
    99
      val result = capture { body }
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   100
      if (interrupted) impose()
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   101
      result match {
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   102
        case Res(x) => Some(x)
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   103
        case Exn(e) =>
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   104
          if (is_interrupt(e)) { impose(); None }
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   105
          else throw e
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   106
      }
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   107
    }
e6f7ed54d64e more robust process kill -- postpone interrupts on current thread;
wenzelm
parents: 56861
diff changeset
   108
56667
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   109
    val return_code = 130
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   110
  }
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   111
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   112
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   113
  /* POSIX return code */
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   114
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   115
  def return_code(exn: Throwable, rc: Int): Int =
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   116
    if (is_interrupt(exn)) Interrupt.return_code else rc
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   117
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   118
44158
fe6d1ae7a065 clarified Exn.message;
wenzelm
parents: 43762
diff changeset
   119
  /* message */
fe6d1ae7a065 clarified Exn.message;
wenzelm
parents: 43762
diff changeset
   120
48479
819f7a5f3e7f more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents: 48476
diff changeset
   121
  def user_message(exn: Throwable): Option[String] =
59697
43e14b0e2ef8 more explicit exception User_Error, with value-oriented equality;
wenzelm
parents: 59138
diff changeset
   122
    if (exn.getClass == classOf[RuntimeException] ||
62492
0e53fade87fe clarified modules;
wenzelm
parents: 61925
diff changeset
   123
        exn.getClass == classOf[User_Error])
59697
43e14b0e2ef8 more explicit exception User_Error, with value-oriented equality;
wenzelm
parents: 59138
diff changeset
   124
    {
65716
wenzelm
parents: 65275
diff changeset
   125
      Some(proper_string(exn.getMessage) getOrElse "Error")
44158
fe6d1ae7a065 clarified Exn.message;
wenzelm
parents: 43762
diff changeset
   126
    }
63782
aced4f0d1ad4 clarified exceptions;
wenzelm
parents: 62508
diff changeset
   127
    else if (exn.isInstanceOf[java.sql.SQLException])
aced4f0d1ad4 clarified exceptions;
wenzelm
parents: 62508
diff changeset
   128
    {
65716
wenzelm
parents: 65275
diff changeset
   129
      Some(proper_string(exn.getMessage) getOrElse "SQL error")
63782
aced4f0d1ad4 clarified exceptions;
wenzelm
parents: 62508
diff changeset
   130
    }
59697
43e14b0e2ef8 more explicit exception User_Error, with value-oriented equality;
wenzelm
parents: 59138
diff changeset
   131
    else if (exn.isInstanceOf[java.io.IOException])
43e14b0e2ef8 more explicit exception User_Error, with value-oriented equality;
wenzelm
parents: 59138
diff changeset
   132
    {
43e14b0e2ef8 more explicit exception User_Error, with value-oriented equality;
wenzelm
parents: 59138
diff changeset
   133
      val msg = exn.getMessage
43e14b0e2ef8 more explicit exception User_Error, with value-oriented equality;
wenzelm
parents: 59138
diff changeset
   134
      Some(if (msg == null || msg == "") "I/O error" else "I/O error: " + msg)
43e14b0e2ef8 more explicit exception User_Error, with value-oriented equality;
wenzelm
parents: 59138
diff changeset
   135
    }
48479
819f7a5f3e7f more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents: 48476
diff changeset
   136
    else if (exn.isInstanceOf[RuntimeException]) Some(exn.toString)
819f7a5f3e7f more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents: 48476
diff changeset
   137
    else None
819f7a5f3e7f more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents: 48476
diff changeset
   138
819f7a5f3e7f more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents: 48476
diff changeset
   139
  def message(exn: Throwable): String =
56667
65e84b0ef974 more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents: 52066
diff changeset
   140
    user_message(exn) getOrElse (if (is_interrupt(exn)) "Interrupt" else exn.toString)
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
   141
}