Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutable map TR #1484

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions frontends/benchmarks/dotty-specific/valid/MutableMapTR.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import scala.annotation.tailrec
import stainless.lang.*
import stainless.collection.*
import stainless.annotation.*

object MTailList:
sealed abstract class MutableList[T]
case class MNil[T]() extends MutableList[T]
case class MCons[T](val hd: T, var tail: MutableList[T]) extends MutableList[T]

extension[T] (ml: MutableList[T])
@pure
def toList: List[T] =
ml match
case MNil() => Nil()
case MCons(h, t) => Cons(h, t.toList)

def mapTR[T,U](l: MutableList[T], f: T => U): MutableList[U] = {
l match
case MNil() => MNil[U]()
case MCons(hd, tl) =>
val acc: MCons[U] = MCons[U](f(hd), MNil())
mapTRWorker[T,U](tl, f, acc)
acc
} ensuring(_.toList == l.toList.map(f))

@tailrec
def mapTRWorker[T,U](
l: MutableList[T],
f: T => U,
acc: MCons[U]
): Unit = {
require(acc.tail == MNil[U]())
l match
case MNil() => ()
case MCons(h, t) =>
acc.tail = MCons[U](f(h), MNil())
mapTRWorker(t, f, acc.tail.asInstanceOf[MCons[U]])
assert(acc.tail.asInstanceOf[MCons[U]].toList == f(h) :: t.toList.map(f))
} ensuring(_ => acc.toList == old(acc).hd :: l.toList.map(f))