Giter Site home page Giter Site logo

Comments (8)

mario-bucev avatar mario-bucev commented on September 24, 2024 1

Good catch! It's indeed not packaged... We'll fix this for the next release, but in the meantime here is a correctly packaged archive
stainless-algebra_2.13.zip (that should replace the incorrectly packaged one).

from stainless.

mario-bucev avatar mario-bucev commented on September 24, 2024

Hello!
One way to include the Ord typeclass is to have it as a field in BST:

import stainless.*
import stainless.collection.*
import stainless.algebra.*

sealed abstract class BST[V]:
  val ord: TotalOrder[V] // here

  // unbalanced invariant, which should be put before and after each operation method
  // (having it in a require causes an unfortunate crash)
  def valid: Boolean =
    this match
      case Empty(_) => true
      case Node(l, v, r, _, _) =>
        l.values.forall(ord.compare(_, v) < 0)
          && r.values.forall(ord.compare(_, v) > 0)

  def values: List[V] =
    this match
      case Empty(_) => Nil()
      case Node(l, v, r, _, _) => l.values ++ List(v) ++ r.values

  def insert(v: V): BST[V] = {
    require(valid)
    // ...
  }.ensuring(_.valid)

case class Empty[V](override val ord: TotalOrder[V]) extends BST[V]
case class Node[V](left: BST[V], data: V, right: BST[V], h: BigInt, override val ord: TotalOrder[V]) extends BST[V]

In this example, I'm using stainless.algebra.{Eq, PartialOrder, TotalOrder} from here (which should be included explicitly when invoking Stainless).
Hope this helps! If not, please do not hesitate to comment further :)

from stainless.

mingyang91 avatar mingyang91 commented on September 24, 2024

Thank you for you response, I will try later.

By the way, the first idea that came to mind was to use invariant to constrain it is ordered, because it is very simple - just left < value < right. But I was not successful because of the below reason.

Then I tried to add require and ensuring into every operator, as you suggested. But I have a question - if I want to achieve the best form, is it necessary to modify the AVL constructor to private and only expose wrapped operators (e.g. insert/delete/contains)?

from stainless.

mario-bucev avatar mario-bucev commented on September 24, 2024

Then I tried to add require and ensuring into every operator, as you suggested. But I have a question - if I want to achieve the best form, is it necessary to modify the AVL constructor to private and only expose wrapped operators (e.g. insert/delete/contains)?

If I'm understanding correctly, it's not really required for correctness, it's just that the user may construct an AVL tree that is not valid and won't be able to call operations that require validity.
But this idea is nice to prevent "accidental" construction and avoid the user later confusion!

from stainless.

romac avatar romac commented on September 24, 2024

@mario-bucev Does it also crash when adding @invariant to valid and removing the pre/post-conditions?

sealed abstract class BST[V]:
  val ord: TotalOrder[V]

  @invariant
  def valid: Boolean = // ...

  def insert(v: V): BST[V] = {
    // ...
  }
}

from stainless.

mario-bucev avatar mario-bucev commented on September 24, 2024

Alas no, I get the same crash. It seems to be that the invariant FunDef is not expecting a type parameter while it is being fed one (maybe some interaction with TypeEncoding?)

from stainless.

mingyang91 avatar mingyang91 commented on September 24, 2024

How do I use the stainless.algebra? I tried to follow the documentation and added it to sbt as follows:

stainlessExtraDeps ++= Seq(
  "ch.epfl.lara" %% "stainless-algebra" % "0.1.2" cross(CrossVersion.for3Use2_13),
)

Then I ran sbt stainless-algebra/packageSrc to get the sources jar and put it into the stainless folder, but it did not work. I noticed that the source jar is only 253 bytes:

ll stainless/ch/epfl/lara/stainless-algebra_2.13/0.1.2
total 24
-rw-r--r--@ 1 mingyang  staff   253B May 11 23:32 stainless-algebra_2.13-0.1.2-sources.jar
-rw-r--r--@ 1 mingyang  staff   281B May 11 23:25 stainless-algebra_2.13-0.1.2.jar
-rw-r--r--@ 1 mingyang  staff   1.5K May 11 23:26 stainless-algebra_2.13-0.1.2.pom

from stainless.

vkuncak avatar vkuncak commented on September 24, 2024

If you are checking how to do balanced trees in Stainless, maybe you should check the previous working examples
https://github.com/epfl-lara/bolts/tree/master/data-structures/trees

from stainless.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.