src/HOL/Imperative_HOL/Heap_Monad.thy
changeset 73137 ca450d902198
parent 69690 1fb204399d8d
child 75653 ea4f5b0ef497
equal deleted inserted replaced
73136:ca17e9ebfdf1 73137:ca450d902198
   577 
   577 
   578 subsubsection \<open>Scala\<close>
   578 subsubsection \<open>Scala\<close>
   579 
   579 
   580 code_printing code_module "Heap" \<rightharpoonup> (Scala)
   580 code_printing code_module "Heap" \<rightharpoonup> (Scala)
   581 \<open>object Heap {
   581 \<open>object Heap {
   582   def bind[A, B](f: Unit => A, g: A => Unit => B): Unit => B = (_: Unit) => g (f ()) ()
   582   def bind[A, B](f: Unit => A, g: A => Unit => B): Unit => B = (_: Unit) => g(f(()))(())
   583 }
   583 }
   584 
   584 
   585 class Ref[A](x: A) {
   585 class Ref[A](x: A) {
   586   var value = x
   586   var value = x
   587 }
   587 }
   594   def update[A](r: Ref[A], x: A): Unit =
   594   def update[A](r: Ref[A], x: A): Unit =
   595     { r.value = x }
   595     { r.value = x }
   596 }
   596 }
   597 
   597 
   598 object Array {
   598 object Array {
   599   import collection.mutable.ArraySeq
   599   class T[A](n: Int)
   600   def alloc[A](n: BigInt)(x: A): ArraySeq[A] =
   600   {
   601     ArraySeq.fill(n.toInt)(x)
   601     val array: Array[AnyRef] = new Array[AnyRef](n)
   602   def make[A](n: BigInt)(f: BigInt => A): ArraySeq[A] =
   602     def apply(i: Int): A = array(i).asInstanceOf[A]
   603     ArraySeq.tabulate(n.toInt)((k: Int) => f(BigInt(k)))
   603     def update(i: Int, x: A): Unit = array(i) = x.asInstanceOf[AnyRef]
   604   def len[A](a: ArraySeq[A]): BigInt =
   604     def length: Int = array.length
   605     BigInt(a.length)
   605     def toList: List[A] = array.toList.asInstanceOf[List[A]]
   606   def nth[A](a: ArraySeq[A], n: BigInt): A =
   606     override def toString: String = array.mkString("Array.T(", ",", ")")
   607     a(n.toInt)
   607   }
   608   def upd[A](a: ArraySeq[A], n: BigInt, x: A): Unit =
   608   def make[A](n: BigInt)(f: BigInt => A): T[A] =
   609     a.update(n.toInt, x)
   609   {
   610   def freeze[A](a: ArraySeq[A]): List[A] =
   610     val m = n.toInt
   611     a.toList
   611     val a = new T[A](m)
       
   612     for (i <- 0 until m) a(i) = f(i)
       
   613     a
       
   614   }
       
   615   def alloc[A](n: BigInt)(x: A): T[A] = make(n)(_ => x)
       
   616   def len[A](a: T[A]): BigInt = BigInt(a.length)
       
   617   def nth[A](a: T[A], n: BigInt): A = a(n.toInt)
       
   618   def upd[A](a: T[A], n: BigInt, x: A): Unit = a.update(n.toInt, x)
       
   619   def freeze[A](a: T[A]): List[A] = a.toList
   612 }
   620 }
       
   621 
   613 \<close>
   622 \<close>
   614 
   623 
   615 code_reserved Scala Heap Ref Array
   624 code_reserved Scala Heap Ref Array
   616 
   625 
   617 code_printing type_constructor Heap \<rightharpoonup> (Scala) "(Unit/ =>/ _)"
   626 code_printing type_constructor Heap \<rightharpoonup> (Scala) "(Unit/ =>/ _)"