src/Pure/System/scala.scala
changeset 77027 ac7af931189f
parent 76537 cdbe20024038
child 77028 f5896dea6fce
equal deleted inserted replaced
77026:808412ec2e13 77027:ac7af931189f
    32     override def toString: String = name
    32     override def toString: String = name
    33     def single: Boolean = false
    33     def single: Boolean = false
    34     def bytes: Boolean = false
    34     def bytes: Boolean = false
    35     def position: Properties.T = here.position
    35     def position: Properties.T = here.position
    36     def here: Scala_Project.Here
    36     def here: Scala_Project.Here
    37     def invoke(args: List[Bytes]): List[Bytes]
    37     def invoke(session: Session, args: List[Bytes]): List[Bytes]
    38   }
    38   }
    39 
    39 
    40   trait Single_Fun extends Fun { override def single: Boolean = true }
    40   trait Single_Fun extends Fun { override def single: Boolean = true }
    41   trait Bytes_Fun extends Fun { override def bytes: Boolean = true }
    41   trait Bytes_Fun extends Fun { override def bytes: Boolean = true }
    42 
    42 
    43   abstract class Fun_Strings(name: String, thread: Boolean = false)
    43   abstract class Fun_Strings(name: String, thread: Boolean = false)
    44   extends Fun(name, thread = thread) {
    44   extends Fun(name, thread = thread) {
    45     override def invoke(args: List[Bytes]): List[Bytes] =
    45     override def invoke(session: Session, args: List[Bytes]): List[Bytes] =
    46       apply(args.map(_.text)).map(Bytes.apply)
    46       apply(args.map(_.text)).map(Bytes.apply)
    47     def apply(args: List[String]): List[String]
    47     def apply(args: List[String]): List[String]
    48   }
    48   }
    49 
    49 
    50   abstract class Fun_String(name: String, thread: Boolean = false)
    50   abstract class Fun_String(name: String, thread: Boolean = false)
    54     def apply(arg: String): String
    54     def apply(arg: String): String
    55   }
    55   }
    56 
    56 
    57   abstract class Fun_Bytes(name: String, thread: Boolean = false)
    57   abstract class Fun_Bytes(name: String, thread: Boolean = false)
    58     extends Fun(name, thread = thread) with Single_Fun with Bytes_Fun {
    58     extends Fun(name, thread = thread) with Single_Fun with Bytes_Fun {
    59     override def invoke(args: List[Bytes]): List[Bytes] =
    59     override def invoke(session: Session, args: List[Bytes]): List[Bytes] =
    60       List(apply(Library.the_single(args)))
    60       List(apply(Library.the_single(args)))
    61     def apply(arg: Bytes): Bytes
    61     def apply(arg: Bytes): Bytes
    62   }
    62   }
    63 
    63 
    64   val encode_fun: XML.Encode.T[Fun] = { fun =>
    64   val encode_fun: XML.Encode.T[Fun] = { fun =>
    91         }
    91         }
    92       val t0 = Time.now()
    92       val t0 = Time.now()
    93       t.sleep()
    93       t.sleep()
    94       val t1 = Time.now()
    94       val t1 = Time.now()
    95       (t1 - t0).toString
    95       (t1 - t0).toString
       
    96     }
       
    97   }
       
    98 
       
    99   object Theory_Name extends Fun("command_theory_name") with Single_Fun {
       
   100     val here = Scala_Project.here
       
   101     override def invoke(session: Session, args: List[Bytes]): List[Bytes] = {
       
   102       val id = Value.Long.parse(Library.the_single(args).text)
       
   103       val name =
       
   104         session.get_state().lookup_id(id) match {
       
   105           case None => ""
       
   106           case Some(st) => st.command.node_name.theory
       
   107         }
       
   108       List(Bytes(name))
    96     }
   109     }
    97   }
   110   }
    98 
   111 
    99 
   112 
   100 
   113 
   283     functions.find(fun => fun.name == name) match {
   296     functions.find(fun => fun.name == name) match {
   284       case Some(fun) => fun.thread
   297       case Some(fun) => fun.thread
   285       case None => false
   298       case None => false
   286     }
   299     }
   287 
   300 
   288   def function_body(name: String, args: List[Bytes]): (Tag.Value, List[Bytes]) =
   301   def function_body(session: Session, name: String, args: List[Bytes]): (Tag.Value, List[Bytes]) =
   289     functions.find(fun => fun.name == name) match {
   302     functions.find(fun => fun.name == name) match {
   290       case Some(fun) =>
   303       case Some(fun) =>
   291         Exn.capture { fun.invoke(args) } match {
   304         Exn.capture { fun.invoke(session, args) } match {
   292           case Exn.Res(null) => (Tag.NULL, Nil)
   305           case Exn.Res(null) => (Tag.NULL, Nil)
   293           case Exn.Res(res) => (Tag.OK, res)
   306           case Exn.Res(res) => (Tag.OK, res)
   294           case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, Nil)
   307           case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, Nil)
   295           case Exn.Exn(e) => (Tag.ERROR, List(Bytes(Exn.message(e))))
   308           case Exn.Exn(e) => (Tag.ERROR, List(Bytes(Exn.message(e))))
   296         }
   309         }
   327 
   340 
   328     private def invoke_scala(msg: Prover.Protocol_Output): Boolean = synchronized {
   341     private def invoke_scala(msg: Prover.Protocol_Output): Boolean = synchronized {
   329       msg.properties match {
   342       msg.properties match {
   330         case Markup.Invoke_Scala(name, id) =>
   343         case Markup.Invoke_Scala(name, id) =>
   331           def body(): Unit = {
   344           def body(): Unit = {
   332             val (tag, res) = Scala.function_body(name, msg.chunks)
   345             val (tag, res) = Scala.function_body(session, name, msg.chunks)
   333             result(id, tag, res)
   346             result(id, tag, res)
   334           }
   347           }
   335           val future =
   348           val future =
   336             if (Scala.function_thread(name)) {
   349             if (Scala.function_thread(name)) {
   337               Future.thread(name = Isabelle_Thread.make_name(base = "invoke_scala"))(body())
   350               Future.thread(name = Isabelle_Thread.make_name(base = "invoke_scala"))(body())
   363 }
   376 }
   364 
   377 
   365 class Scala_Functions extends Scala.Functions(
   378 class Scala_Functions extends Scala.Functions(
   366   Scala.Echo,
   379   Scala.Echo,
   367   Scala.Sleep,
   380   Scala.Sleep,
       
   381   Scala.Theory_Name,
   368   Scala.Toplevel,
   382   Scala.Toplevel,
   369   Scala_Build.Scala_Fun,
   383   Scala_Build.Scala_Fun,
   370   Base64.Decode,
   384   Base64.Decode,
   371   Base64.Encode,
   385   Base64.Encode,
   372   Compress.XZ_Compress,
   386   Compress.XZ_Compress,