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 immutable ordered finite 'map' and 'set' to the stdlib #561

Open
wants to merge 29 commits into
base: master
Choose a base branch
from

Conversation

jiribenes
Copy link
Contributor

@jiribenes jiribenes commented Aug 30, 2024

I've been sitting on this code for almost 9 months now (started on Xmas 2023), it's about time we finalise it.
This PR adds immutable ordered maps and sets based on weight balanced trees into the standard library.

The map and set now conceptually work on all backends, but not really.

Progress is blocked on:

  1. very annoying inliner bug(s?) [Optimiser (inliner) forgets a binding, crashing the resulting program #733] resolved 🥳
  2. missing comparisons on strings on LLVM [Add string comparison to LLVM backend #748]

@jiribenes jiribenes changed the title Immutable ordered maps and sets Immutable ordered finite maps and sets Aug 30, 2024
// Check if set `s` is empty.
//
// O(1)
def isEmpty[A](s: Set[A]): Bool = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could use empty? here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
In general, feel free to suggest better names everywhere, I really just took the first idea I had every time :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do this change consistently across the stdlib, if we decide for it

@@ -0,0 +1,714 @@
module map
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #488, we agreed to use constructors like $collection_name for array, ref, and bytes. For example:

// Creates a new Array of size `size` filled with the value `init`
extern global def array[T](size: Int, init: T): Array[T]
 
// Allocates new bytes with the given `capacity`, setting its values to `0`.
extern io def bytes(capacity: Int): Bytes

// Creates a new reference with the initial value `init`.
extern global def ref[T](init: T): Ref[T]

There are two main ways to make a map / set:

  • empty map/set
  • map/set created from a (possibly unordered) list

Does the convention mention above apply anywhere here? Personally, I'd always use these namespaced: map::empty() and map::fromList([...]), but I'm interested in what y'all think.

@jiribenes jiribenes changed the title Immutable ordered finite maps and sets [WIP] Immutable ordered finite maps and sets Aug 30, 2024
@jiribenes jiribenes force-pushed the master branch 3 times, most recently from ee9d209 to 58c8510 Compare October 1, 2024 18:11
@jiribenes jiribenes force-pushed the feature/stdlib/treemap-treeset branch from 3890ddd to 889c0f8 Compare October 2, 2024 16:05
@jiribenes
Copy link
Contributor Author

Rebased on current master.

@jiribenes
Copy link
Contributor Author

Blocked on #629, I'll fix it there and then rebase. :)

jiribenes added a commit that referenced this pull request Oct 2, 2024
Cherry-picked from #561: adds arithmetic right shifts and a comprehensive test suite.
@jiribenes jiribenes force-pushed the feature/stdlib/treemap-treeset branch from ef7d4a2 to 498b95f Compare October 4, 2024 09:48
@jiribenes jiribenes force-pushed the feature/stdlib/treemap-treeset branch from 5103d0d to 2d23750 Compare November 25, 2024 16:04
@jiribenes
Copy link
Contributor Author

Rebased on current master (b034213)

@jiribenes
Copy link
Contributor Author

jiribenes commented Nov 25, 2024

There's something that looks like a miscompilation on the JS backend in the examples/stream/map.effekt test 👀 (cc @b-studios @marvinborner when you get the time: I think it's related to some optimizer changes)

It's reproducible locally.
EDIT: And it goes away if I turn off the optimisations!

Here it is on the CI:
https://github.com/effekt-lang/effekt/actions/runs/12014381198/job/33490034109#step:11:249
Screenshot 2024-11-25 at 17 31 00

The cause is that l_6 is not defined here (look for the shl: <<)
Screenshot 2024-11-25 at 17 30 51

But the corresponding l9890516147 is also missing in Core (look for bitwiseShl)
Screenshot 2024-11-25 at 17 36 40

@b-studios
Copy link
Collaborator

b-studios commented Nov 25, 2024

Hey, I am currently away from my computer. Where is the l coming from? What is the core before optimizations?

btw. I typically use println(util.show(...)) in random positions to understand the trees during transformation.

@jiribenes
Copy link
Contributor Author

jiribenes commented Nov 25, 2024

I don't have the time to minimise further, so here's all of the data:

... by the way it would be nice to have a --ir-show=unopt-core or something for these purposes ...

Source (somewhat minimised from ~2000 lines to ~400
namespace map {

/// Ordered finite immutable map, backed by balanced binary trees of logarithmic depth.
/// Currently use `genericCompare` as a comparison primitive, which means the map is now working only in the JS backend.
///
/// Please don't use the internal constructors `Bin` & `Tip` directly,
/// they might change down the line and are not considered stable.
type Map[K, V] {
  Bin(size: Int, k: K, v: V, left: Map[K, V], right: Map[K, V]);
  Tip()
}

/// Create a new empty map.
///
/// O(1)
def empty[K, V](): Map[K, V] = {
  Tip()
}

/// Check if map `m` is empty.
///
/// O(1)
def isEmpty[K, V](m: Map[K, V]): Bool = {
  m match {
    case Tip() => true
    case _     => false
  }
}

/// Check if map `m` is nonempty.
///
/// O(1)
def nonEmpty[K, V](m: Map[K, V]): Bool = {
  m match {
    case Tip() => false
    case _     => true
  }
}

/// Create a new map containing only the mapping from `k` to `v`.
///
/// O(1)
def singleton[K, V](k: K, v: V): Map[K, V] = {
  Bin(1, k, v, Tip(), Tip())
}

/// Get the size of the map (the number of keys/values).
///
/// O(1)
def size[K, V](m: Map[K, V]): Int = {
  m match {
    case Tip() => 0
    case Bin(size, _, _, _, _) => size
  }
}

/// Insert a new key `k` and value `v` into the map `m`.
/// If the key `k` is already present in `m`, its associated value is replaced with `v`.
///
/// O(log N)
def put[K, V](m: Map[K, V], k: K, v: V): Map[K, V] = m match {
  case Tip() => singleton(k, v)
  case Bin(size, k2, v2, l, r) =>
    genericCompare(k, k2) match {
      case Less() => balance(k2, v2, put(l, k, v), r)
      case Greater() => balance(k2, v2, l, put(r, k, v))
      case Equal() => Bin(size, k, v, l, r)
    }
}

/// Traverse all keys and their associated values in map `m` in order,
/// running the function `action` on a key and its associated value.
///
/// Law: `m.foreach { action } === m.toList.foreach { action }`
///
/// O(N)
///
/// TODO: Support {Control} for early exits.
def foreach[K, V](m: Map[K, V]) { action: (K, V) => Unit }: Unit = {
  def go(m: Map[K, V]): Unit = {
    m match {
      case Tip() => ()
      case Bin(_, k, v, l, r) =>
        go(l)
        action(k, v)
        go(r)
    }
  }
  go(m)
}

/// Convert a map `m` into a list of (key, value) pairs.
///
/// O(N)
def toList[K, V](m: Map[K, V]): List[(K, V)] = {
  var acc = Nil()
  m.foreach { (k, v) =>
    acc = Cons((k, v), acc)
  }
  acc.reverse
}

/// Get a list of keys of the map `m`.
///
/// O(N)
def keys[K, V](m: Map[K, V]): List[K] = {
  var acc = Nil()
  m.foreach { (k, _v) =>
    acc = Cons(k, acc)
  }
  acc.reverse  
}

/// Get a list of values of the map `m`.
///
/// O(N)
def values[K, V](m: Map[K, V]): List[V] = {
  var acc = Nil()
  m.foreach { (_k, v) =>
    acc = Cons(v, acc)
  }
  acc.reverse   
}

/// Create a map from a list of (key, value) pairs.
/// If the list contains more than one value for the same key,
/// only the last value is used in the map.
///
/// O(N) if the list is sorted by key,
/// O(N log N) otherwise
def fromList[K, V](pairs: List[(K, V)]): Map[K, V] = {
  pairs match {
    case Nil() => Tip()
    case Cons((k, v), Nil()) => singleton(k, v)
    case Cons((k, v), rest) => 
      // TODO: this function should really, **really** get inlined!
      def notOrdered(k: K, pairs: List[(K, V)]) = {
        pairs match {
          case Nil() => false
          case Cons((k2, _), _) =>  // k >= k2
            genericCompare(k, k2) match {
              case Less() => false
              case Greater() => true
              case Equal() => true
            }
        }
      }

      // Naive insertion, used for the worst-case scenario when the list is not sorted by key
      def insertMany(m: Map[K, V], pairs: List[(K, V)]) = {
        var mapSoFar = m
        pairs.foreach { case (k, v) => 
          mapSoFar = mapSoFar.put(k, v)
        }
        mapSoFar
      }

      // Returns a triple `(map, xs, ys)`
      // 
      // Invariant: At least one of `xs`, `ys` is empty.
      // Moreover, if `ys` is nonempty, its keys are **not** ordered!
      // Otherwise, all of the seen keys have been ordered so far.
      //
      // TODO: Possibly use a better type to encode the invariant?
      def create(level: Int, pairs: List[(K, V)]): (Map[K, V], List[(K, V)], List[(K, V)]) = {
        pairs match {
          case Nil() => (Tip(), [], [])
          case Cons((k, v), rest) => 
            if (level == 1) {
              val singleton = Bin(1, k, v, Tip(), Tip())
              if (notOrdered(k, rest)) {
                (singleton, [], rest)
              } else {
                (singleton, rest, [])
              }
            } else {
              val res = create(level.bitwiseShr(1), pairs)
              res match {
                case (_, Nil(), _) => res
                case (l, Cons((k2, v2), Nil()), zs) => (l.putMax(k2, v2), [], zs)
                case (l, Cons((k2, v2), rest2), _) =>
                  val xs = Cons((k2, v2), rest2) // @-pattern

                  if (notOrdered(k2, rest2)) { (l, [], xs) }
                  else {
                    val (r, zs, ws) = create(level.bitwiseShr(1), rest2);
                    (link(k2, v2, l, r), zs, ws)
                  }
              }
            }
        }
      }

      def go(level: Int, m: Map[K, V], pairs: List[(K, V)]): Map[K, V] = {
        pairs match {
          case Nil() => m
          case Cons((k, v), Nil()) => m.putMax(k, v)
          case Cons((k, v), rest) =>
            if (notOrdered(k, rest)) { insertMany(m, pairs) }
            else {
              val l = m; // m is the left subtree here
              val cr = create(level, rest)
              cr match {
                case (r, xs, Nil()) => go(level.bitwiseShl(1), link(k, v, l, r), xs)
                case (r, Nil(), ys) => insertMany(link(k, v, l, r), ys)
                case _ => panic("create: go: cannot happen, invariant broken!")
              }
            }
        }
      }

      if (notOrdered(k, rest)) { insertMany(singleton(k, v), rest) }
      else { go(1, singleton(k, v), rest) }
  }
}

// -------------
// Internal

val ratio = 2
val delta = 3

def bin[K, V](k: K, v: V, l: Map[K, V], r: Map[K, V]): Map[K, V] = {
  Bin(l.size() + r.size() + 1, k, v, l, r)
}

def balance[K, V](k: K, v: V, l: Map[K, V], r: Map[K, V]): Map[K, V] = {
  /*
     k1->v1
     /    \
    t1     m                    k2->v2
           =                   /      \
         k2->v2        ~>    k1->v1    t3
         /   \               /    \
        t2    t3            t1    t2
  */
  def singleL[A, B](k1: A, v1: B, t1: Map[A, B], m: Map[A, B]): Map[A, B] = {
    m match {
      case Bin(_, k2, v2, t2, t3) => bin(k2, v2, bin(k1, v1, t1, t2), t3)
      case _ => panic("impossible: singleL: Tip")
    }
  }

  /*
        k1->v1
        /     \
       m      t3            k2->v2
       =                   /      \
     k2->v2        ~>    t1       k1->v1
      /   \               /       /   \
    t1    t2            t1      t2    t3
  */
  def singleR[A, B](k1: A, v1: B, m: Map[A, B], t3: Map[A, B]): Map[A, B] = {
    m match {
      case Bin(_, k2, v2, t1, t2) => bin(k2, v2, t1, bin(k1, v1, t2, t3))
      case _ => panic("impossible: singleR: Tip")
    }
  }

  /*
        k1->v1                     k3->v3
       /      \                   /      \
     t1        m                k1->v1    k2->v2
               =                /  \      /   \
             k2->v2     ~>     t1  t2    t3   t4
            /     \
          k3->v3   t4
          /  \
        t2    t3
  */
  def doubleL[A, B](k1: A, v1: B, t1: Map[A, B], m: Map[A, B]): Map[A, B] = {
    m match {
      case Bin(_, k2, v2, Bin(_, k3, v3, t2, t3), t4) =>
        bin(k3, v3, bin(k1, v1, t1, t2), bin(k2, v2, t3, t4))
      case _ => panic("impossible: doubleL: Tip")
    }
  }

   /*
             k1->v1                  k3->v3
            /      \                /      \
            m      t4            k2->v2    k1->v1
            =                    /  \      /   \
          k2->v2         ~>     t1  t2    t3   t4
         /     \
       t1      k3->v3
                /  \
              t2    t3
  */
  def doubleR[A, B](k1: A, v1: B, m: Map[A, B], t4: Map[A, B]): Map[A, B] = {
    m match {
      case Bin(_, k2, v2, t1, Bin(_, k3, v3, t2, t3)) =>
        bin(k3, v3, bin(k2, v2, t1, t2), bin(k1, v1, t3, t4))
      case _ =>
        panic("impossible: doubleR: Tip")
    }
  }

  def rotateL[A, B](k: A, v: B, l: Map[A, B], r: Map[A, B]): Map[A, B] = {
    r match {
      case Bin(_, _, _, rl, rr) and (rl.size() < ratio * rr.size()) => singleL(k, v, l, r)
      case _ => doubleL(k, v, l, r)
    }
  }
  def rotateR[A, B](k: A, v: B, l: Map[A, B], r: Map[A, B]): Map[A, B] = {
    l match {
      case Bin(_, _, _, ll, lr) and (lr.size() < ratio * ll.size()) => singleR(k, v, l, r)
      case _ => doubleR(k, v, l, r)
    }
  }

  val sizeL = l.size()
  val sizeR = r.size()
  val sizeCombined = sizeL + sizeR + 1

  if ((sizeL + sizeR) <= 1) { Bin(sizeCombined, k, v, l, r) }
  else if (sizeR > (delta * sizeL)) { rotateL(k, v, l, r) }
  else if (sizeL > (delta * sizeR)) { rotateR(k, v, l, r) }
  else { Bin(sizeCombined, k, v, l, r)}
}

record MaxView[K, V](k: K, v: V, m: Map[K, V])
record MinView[K, V](k: K, v: V, m: Map[K, V])

def maxViewSure[K, V](k: K, v: V, l: Map[K, V], r: Map[K, V]): MaxView[K, V] = {
  (l, r) match {
    case (l, Tip()) => MaxView(k, v, l)
    case (l, Bin(_, kr, vr, rl, rr)) =>
      val MaxView(km, vm, r2) = maxViewSure(kr, vr, rl, rr)
      MaxView(km, vm, balance(k, v, l, r2))
  }
}

def minViewSure[K, V](k: K, v: V, l: Map[K, V], r: Map[K, V]): MinView[K, V] = {
  (l, r) match {
    case (Tip(), r) => MinView(k, v, r)
    case (Bin(_, kl, vl, ll, lr), r) =>
      val MinView(km, vm, l2) = minViewSure(kl, vl, ll, lr)
      MinView(km, vm, balance(k, v, l2, r))
  }
}

/// Internal: Glues two balanced trees (with respect to each other) together.
def glue[K, V](l: Map[K, V], r: Map[K, V]): Map[K, V] = {
  (l, r) match {
    case (Tip(), r) => r
    case (l, Tip()) => l
    case (Bin(sizeL, kl, vl, ll, lr), Bin(sizeR, kr, vr, rl, rr)) =>
      if (sizeL > sizeR) { 
        val MaxView(km, m, l2) = maxViewSure(kl, vl, ll, lr)
        balance(km, m, l2, r)
      } else {
        val MinView(km, m, r2) = minViewSure(kr, vr, rl, rr)
        balance(km, m, l, r2)
      }
  }
}

def link[K, V](k: K, v: V, l: Map[K, V], r: Map[K, V]): Map[K, V] = {
  (l, r) match {
    case (Tip(), r) => r.putMin(k, v)
    case (l, Tip()) => l.putMax(k, v)
    case (Bin(sizeL, kl, vl, ll, lr), Bin(sizeR, kr, vr, rl, rr)) =>
      if ((delta * sizeL) < sizeR) { balance(kr, vr, link(k, v, l, rl), rr) }
      else if ((delta * sizeR) < sizeL) { balance(kl, vl, ll, link(k, v, lr, r)) }
      else { bin(k, v, l, r) }
  }
}

def putMin[K, V](m: Map[K, V], k: K, v: V): Map[K, V] = {
  m match {
    case Tip() => singleton(k, v)
    case Bin(_, k2, v2, l, r) =>
      balance(k2, v2, l.putMin(k, v), r)
  }
}

def putMax[K, V](m: Map[K, V], k: K, v: V): Map[K, V] = {
  m match {
    case Tip() => singleton(k, v)
    case Bin(_, k2, v2, l, r) =>
      balance(k2, v2, l, r.putMax(k, v))
  }
}

}

namespace stream {
// Effects
// -------

/// Describes push streams by emitting values of type `A`.
effect emit[A](value: A): Unit

/// Describes pull streams by reading values of type `A`.
///
/// The producer can decide to `stop` emitting values.
effect read[A](): A / stop

/// Signal from a producer to a consumer that there are no further values.
effect stop(): Nothing

/// Canonical handler of push streams that performs `action` for every
/// value emitted by `stream`.
def for[A, R] { stream: () => R / emit[A] } { action: A => Unit }: R =
  try {
    stream()
  } with emit[A] { value =>
    resume(action(value))
  }

/// Like `for[A, R]`, but ignores the result of the stream, and consequently
/// works for any type. Use this to annotate the type of stream elements
/// `A` without having to also annotate `R`.
///
/// e.g. for[Int] { prog() } { el => println(el) }
def for[A] { stream: () => Any / emit[A] } { action: A => Unit }: Unit = {
  for[A, Any]{stream}{action}
  ()
}

/// Turns a `map` into a producer of a push stream
/// of `(key, value)` pairs by emitting each contained *in order*.
def each[K, V](map: map::Map[K, V]): Unit / emit[(K, V)] =
  map.map::foreach { (k, v) => do emit((k, v)) }

def collectMap[K, V, R] { stream: () => R / emit[(K, V)] }: (R, map::Map[K, V]) =
  try {
    (stream(), map::empty())
  } with emit[(K, V)] { case (k, v) =>
    val (r, map) = resume(());
    (r, map.map::put(k, v))
  }

def collectMap[K, V] { stream: () => Any / emit[(K, V)] }: map::Map[K, V] =
  collectMap[K, V, Any]{stream}.second

}

def main() = {
  val m = map::fromList([(1, 'H'), (2, 'e'), (3, 'l'), (4, 'l'), (5, 'o')])

  stream::for[(Int, Char)] { stream::each(m) } { case (k, v) =>
    println(show(k) ++ ": " ++ show(v) ++ " (" ++ show(v.toInt) ++ ")")
  }
}

Unopt Core

    def go2822(level2819: Int398, m2820: Map2616[K2660, V2661], pairs2821: List910[Tuple2249[K2660, V2661]]) =
      def b_k_38535593() =
        m2820;
      def b_k_38565594(k2823: K2660, v2824: V2661) =
        putMax2721[K2660, V2661](m2820, k2823, v2824);
      def b_k_38825595(k2825: K2660, v2826: V2661, rest2827: List910[Tuple2249[K2660, V2661]]) =
        let v_r_38575596 = run {notOrdered2790(k2825, rest2827)}
        
        if (v_r_38575596) {
          insertMany2794(m2820, pairs2821)
        } else {
          let l2828 = run {m2820}; // `l2828` bound here
          let cr2829 = run {create2801(level2819, rest2827)};
          def b_k_38655597(r2830: Map2616[K2660, V2661], xs2831: List910[Tuple2249[K2660, V2661]]) =
            let v_r_38625598 = run {link2709[K2660, V2661](k2825, v2826, l2828, r2830)} // `l2828` passed here
            
            go2822(bitwiseShl225(level2819, 1), v_r_38625598, xs2831);
          def b_k_38695599(r2832: Map2616[K2660, V2661], ys2833: List910[Tuple2249[K2660, V2661]]) =
            let v_r_38665600 = run {link2709[K2660, V2661](k2825, v2826, l2828, r2832)} // and `l2828` passed here too
            
            insertMany2794(v_r_38665600, ys2833);
 
...
Full Unoptimized Core
module mapinlined

import effekt
import option
import list
import result
import exception
import array
import string
import ref



type Map2616[K2614, V2615] {
  Bin2750(size2756: Int398, k2757: K2614, v2758: V2615, left2759: Map2616[K2614, V2615], right2760: Map2616[K2614, V2615])
  Tip2761()
}
type MaxView2680[K2678, V2679] {
  MaxView2907(k2911: K2678, v2912: V2679, m2913: Map2616[K2678, V2679])
}
type MinView2683[K2681, V2682] {
  MinView2914(k2918: K2681, v2919: V2682, m2920: Map2616[K2681, V2682])
}
interface emit2723[A2722] {
  emit2978: (A2722) => Unit394
}
interface read2725[A2724] {
  read2979: (){stop5879: stop2726} => A2724
}
interface stop2726 {
  stop2980: () => Nothing397
}

def empty2619[K2617, V2618]() =
  make Map2616[K2617, V2618] Tip2761();
def isEmpty2623[K2620, V2621](m2622: Map2616[K2620, V2621]) =
  def b_k_36625880() =
    true;
  def b_k_36635881() =
    false
  
  m2622 match {
    case Tip2761 { () => 
      b_k_36625880()
    }
  } else {
    b_k_36635881()};
def nonEmpty2627[K2624, V2625](m2626: Map2616[K2624, V2625]) =
  def b_k_36665882() =
    false;
  def b_k_36675883() =
    true
  
  m2626 match {
    case Tip2761 { () => 
      b_k_36665882()
    }
  } else {
    b_k_36675883()};
def singleton2632[K2628, V2629](k2630: K2628, v2631: V2629) =
  make Map2616[K2628, V2629] Bin2750(1, k2630, v2631, make Map2616[K2628, V2629] Tip2761(), make Map2616[K2628, V2629] Tip2761());
def size2636[K2633, V2634](m2635: Map2616[K2633, V2634]) =
  def b_k_36705543() =
    0;
  def b_k_36715544(size2762: Int398) =
    size2762
  
  m2635 match {
    case Tip2761 { () => 
      b_k_36705543()
    }
    case Bin2750 { (v_y_36723678: Int398, v_y_36733677: K2633, v_y_36743679: V2634, v_y_36753680: Map2616[K2633, V2634], v_y_36763681: Map2616[K2633, V2634]) => 
      b_k_36715544(v_y_36723678)
    }
  };
def put2642[K2637, V2638](m2639: Map2616[K2637, V2638], k2640: K2637, v2641: V2638) =
  def b_k_36865535() =
    singleton2632[K2637, V2638](k2640, v2641);
  def b_k_37005536(size2763: Int398, k22764: K2637, v22765: V2638, l2766: Map2616[K2637, V2638], r2767: Map2616[K2637, V2638]) =
    let v_r_36875537 = run {genericCompare51[K2637](k2640, k22764)};
    def b_k_36925538() =
      let v_r_36895539 = run {put2642[K2637, V2638](l2766, k2640, v2641)}
      
      balance2677[K2637, V2638](k22764, v22765, v_r_36895539, r2767);
    def b_k_36965566() =
      let v_r_36935567 = run {put2642[K2637, V2638](r2767, k2640, v2641)}
      
      balance2677[K2637, V2638](k22764, v22765, l2766, v_r_36935567);
    def b_k_36975568() =
      make Map2616[K2637, V2638] Bin2750(size2763, k2640, v2641, l2766, r2767)
    
    v_r_36875537 match {
      case Less321 { () => 
        b_k_36925538()
      }
      case Greater323 { () => 
        b_k_36965566()
      }
      case Equal322 { () => 
        b_k_36975568()
      }
    }
  
  m2639 match {
    case Tip2761 { () => 
      b_k_36865535()
    }
    case Bin2750 { (v_y_37013707: Int398, v_y_37023706: K2637, v_y_37033708: V2638, v_y_37043709: Map2616[K2637, V2638], v_y_37053710: Map2616[K2637, V2638]) => 
      b_k_37005536(v_y_37013707, v_y_37023706, v_y_37033708, v_y_37043709, v_y_37053710)
    }
  };
def foreach2647[K2643, V2644](m2645: Map2616[K2643, V2644]){action2646} =
  def go2769(m2768: Map2616[K2643, V2644]) =
    def b_k_37135611() =
      ();
    def b_k_37205612(k2770: K2643, v2771: V2644, l2772: Map2616[K2643, V2644], r2773: Map2616[K2643, V2644]) =
      go2769(l2772);
      action2646(k2770, v2771);
      go2769(r2773)
    
    m2768 match {
      case Tip2761 { () => 
        b_k_37135611()
      }
      case Bin2750 { (v_y_37213727: Int398, v_y_37223726: K2643, v_y_37233728: V2644, v_y_37243729: Map2616[K2643, V2644], v_y_37253730: Map2616[K2643, V2644]) => 
        b_k_37205612(v_y_37223726, v_y_37233728, v_y_37243729, v_y_37253730)
      }
    }
  
  go2769(m2645);
def toList2651[K2648, V2649](m2650: Map2616[K2648, V2649]) =
  val v_r_37355884: List910[Tuple2249[K2648, V2649]] = make List910[Tuple2249[K2648, V2649]] Nil1114();
  var acc2774 = v_r_37355884 ;
  foreach2647[K2648, V2649](m2650, { (k2775: K2648, v2776: V2649) => 
    val v_r_37365885: List910[Tuple2249[K2648, V2649]] = !acc2774;
    acc2774 := make List910[Tuple2249[K2648, V2649]] Cons1115(make Tuple2249[K2648, V2649] Tuple2330(k2775, v2776), v_r_37365885)
  });
  val v_r_37415886: List910[Tuple2249[K2648, V2649]] = !acc2774;
  reverse1005[Tuple2249[K2648, V2649]](v_r_37415886);
def keys2655[K2652, V2653](m2654: Map2616[K2652, V2653]) =
  val v_r_37445887: List910[K2652] = make List910[K2652] Nil1114();
  var acc2777 = v_r_37445887 ;
  foreach2647[K2652, V2653](m2654, { (k2778: K2652, _v2779: V2653) => 
    val v_r_37455888: List910[K2652] = !acc2777;
    acc2777 := make List910[K2652] Cons1115(k2778, v_r_37455888)
  });
  val v_r_37505889: List910[K2652] = !acc2777;
  reverse1005[K2652](v_r_37505889);
def values2659[K2656, V2657](m2658: Map2616[K2656, V2657]) =
  val v_r_37535890: List910[V2657] = make List910[V2657] Nil1114();
  var acc2780 = v_r_37535890 ;
  foreach2647[K2656, V2657](m2658, { (_k2781: K2656, v2782: V2657) => 
    val v_r_37545891: List910[V2657] = !acc2780;
    acc2780 := make List910[V2657] Cons1115(v2782, v_r_37545891)
  });
  val v_r_37595892: List910[V2657] = !acc2780;
  reverse1005[V2657](v_r_37595892);
def fromList2663[K2660, V2661](pairs2662: List910[Tuple2249[K2660, V2661]]) =
  def b_k_37625516() =
    make Map2616[K2660, V2661] Tip2761();
  def b_k_37655517(k2783: K2660, v2784: V2661) =
    singleton2632[K2660, V2661](k2783, v2784);
  def b_k_39025518(k2785: K2660, v2786: V2661, rest2787: List910[Tuple2249[K2660, V2661]]) =
    def notOrdered2790(k2788: K2660, pairs2789: List910[Tuple2249[K2660, V2661]]) =
      def b_k_37665519() =
        false;
      def b_k_37735520(k22791: K2660) =
        let v_r_37675521 = run {genericCompare51[K2660](k2788, k22791)};
        def b_k_37685527() =
          false;
        def b_k_37695528() =
          true;
        def b_k_37705529() =
          true
        
        v_r_37675521 match {
          case Less321 { () => 
            b_k_37685527()
          }
          case Greater323 { () => 
            b_k_37695528()
          }
          case Equal322 { () => 
            b_k_37705529()
          }
        }
      
      pairs2789 match {
        case Nil1114 { () => 
          b_k_37665519()
        }
        case Cons1115 { (v_y_37743777: Tuple2249[K2660, V2661], v_y_37753776: List910[Tuple2249[K2660, V2661]]) => 
          v_y_37743777 match {
            case Tuple2330 { (v_y_37783781: K2660, v_y_37793780: V2661) => 
              b_k_37735520(v_y_37783781)
            }
          }
        }
      };
    def insertMany2794(m2792: Map2616[K2660, V2661], pairs2793: List910[Tuple2249[K2660, V2661]]) =
      val v_r_37845530: Map2616[K2660, V2661] = m2792;
      var mapSoFar2795 = v_r_37845530 ;
      foreach950[Tuple2249[K2660, V2661]](pairs2793, { (__tmpRes2796: Tuple2249[K2660, V2661]) => 
        def b_k_37895533(k2797: K2660, v2798: V2661) =
          val v_r_37855569: Map2616[K2660, V2661] = !mapSoFar2795;
          let v_r_37865534 = run {put2642[K2660, V2661](v_r_37855569, k2797, v2798)}
          
          mapSoFar2795 := v_r_37865534
        
        __tmpRes2796 match {
          case Tuple2330 { (v_y_37903793: K2660, v_y_37913792: V2661) => 
            b_k_37895533(v_y_37903793, v_y_37913792)
          }
        }
      });
      !mapSoFar2795;
    def create2801(level2799: Int398, pairs2800: List910[Tuple2249[K2660, V2661]]) =
      def b_k_38005570() =
        make Tuple3253[Map2616[K2660, V2661], List910[Tuple2249[K2660, V2661]], List910[Tuple2249[K2660, V2661]]] Tuple3335(make Map2616[K2660, V2661] Tip2761(), make List910[Tuple2249[K2660, V2661]] Nil1114(), make List910[Tuple2249[K2660, V2661]] Nil1114());
      def b_k_38425571(k2802: K2660, v2803: V2661, rest2804: List910[Tuple2249[K2660, V2661]]) =
        if (infixEq69(level2799, 1)) {
          let singleton2805 = run {make Map2616[K2660, V2661] Bin2750(1, k2802, v2803, make Map2616[K2660, V2661] Tip2761(), make Map2616[K2660, V2661] Tip2761())};
          let v_r_38015572 = run {notOrdered2790(k2802, rest2804)}
          
          if (v_r_38015572) {
            make Tuple3253[Map2616[K2660, V2661], List910[Tuple2249[K2660, V2661]], List910[Tuple2249[K2660, V2661]]] Tuple3335(singleton2805, make List910[Tuple2249[K2660, V2661]] Nil1114(), rest2804)
          } else {
            make Tuple3253[Map2616[K2660, V2661], List910[Tuple2249[K2660, V2661]], List910[Tuple2249[K2660, V2661]]] Tuple3335(singleton2805, rest2804, make List910[Tuple2249[K2660, V2661]] Nil1114())
          }
        } else {
          let res2806 = run {create2801(bitwiseShr228(level2799, 1), pairs2800)};
          def b_k_38065573() =
            res2806;
          def b_k_38085574(l2807: Map2616[K2660, V2661], k22808: K2660, v22809: V2661, zs2810: List910[Tuple2249[K2660, V2661]]) =
            let v_r_38075575 = run {putMax2721[K2660, V2661](l2807, k22808, v22809)}
            
            make Tuple3253[Map2616[K2660, V2661], List910[Tuple2249[K2660, V2661]], List910[Tuple2249[K2660, V2661]]] Tuple3335(v_r_38075575, make List910[Tuple2249[K2660, V2661]] Nil1114(), zs2810);
          def b_k_38235579(l2811: Map2616[K2660, V2661], k22812: K2660, v22813: V2661, rest22814: List910[Tuple2249[K2660, V2661]]) =
            let xs2815 = run {make List910[Tuple2249[K2660, V2661]] Cons1115(make Tuple2249[K2660, V2661] Tuple2330(k22812, v22813), rest22814)};
            let v_r_38095580 = run {notOrdered2790(k22812, rest22814)}
            
            if (v_r_38095580) {
              make Tuple3253[Map2616[K2660, V2661], List910[Tuple2249[K2660, V2661]], List910[Tuple2249[K2660, V2661]]] Tuple3335(l2811, make List910[Tuple2249[K2660, V2661]] Nil1114(), xs2815)
            } else {
              let v_r_38105581 = run {create2801(bitwiseShr228(level2799, 1), rest22814)};
              def b_k_38125582(r2816: Map2616[K2660, V2661], zs2817: List910[Tuple2249[K2660, V2661]], ws2818: List910[Tuple2249[K2660, V2661]]) =
                let v_r_38115583 = run {link2709[K2660, V2661](k22812, v22813, l2811, r2816)}
                
                make Tuple3253[Map2616[K2660, V2661], List910[Tuple2249[K2660, V2661]], List910[Tuple2249[K2660, V2661]]] Tuple3335(v_r_38115583, zs2817, ws2818)
              
              v_r_38105581 match {
                case Tuple3335 { (v_y_38133817: Map2616[K2660, V2661], v_y_38143816: List910[Tuple2249[K2660, V2661]], v_y_38153818: List910[Tuple2249[K2660, V2661]]) => 
                  b_k_38125582(v_y_38133817, v_y_38143816, v_y_38153818)
                }
              }
            }
          
          res2806 match {
            case Tuple3335 { (v_y_38243828: Map2616[K2660, V2661], v_y_38253827: List910[Tuple2249[K2660, V2661]], v_y_38263829: List910[Tuple2249[K2660, V2661]]) => 
              v_y_38253827 match {
                case Nil1114 { () => 
                  b_k_38065573()
                }
                case Cons1115 { (v_y_38303833: Tuple2249[K2660, V2661], v_y_38313832: List910[Tuple2249[K2660, V2661]]) => 
                  v_y_38303833 match {
                    case Tuple2330 { (v_y_38343837: K2660, v_y_38353836: V2661) => 
                      v_y_38313832 match {
                        case Nil1114 { () => 
                          b_k_38085574(v_y_38243828, v_y_38343837, v_y_38353836, v_y_38263829)
                        }
                      } else {
                        b_k_38235579(v_y_38243828, v_y_38343837, v_y_38353836, v_y_38313832)}
                    }
                  }
                }
              }
            }
          }
        }
      
      pairs2800 match {
        case Nil1114 { () => 
          b_k_38005570()
        }
        case Cons1115 { (v_y_38433846: Tuple2249[K2660, V2661], v_y_38443845: List910[Tuple2249[K2660, V2661]]) => 
          v_y_38433846 match {
            case Tuple2330 { (v_y_38473850: K2660, v_y_38483849: V2661) => 
              b_k_38425571(v_y_38473850, v_y_38483849, v_y_38443845)
            }
          }
        }
      };
    def go2822(level2819: Int398, m2820: Map2616[K2660, V2661], pairs2821: List910[Tuple2249[K2660, V2661]]) =
      def b_k_38535593() =
        m2820;
      def b_k_38565594(k2823: K2660, v2824: V2661) =
        putMax2721[K2660, V2661](m2820, k2823, v2824);
      def b_k_38825595(k2825: K2660, v2826: V2661, rest2827: List910[Tuple2249[K2660, V2661]]) =
        let v_r_38575596 = run {notOrdered2790(k2825, rest2827)}
        
        if (v_r_38575596) {
          insertMany2794(m2820, pairs2821)
        } else {
          let l2828 = run {m2820};
          let cr2829 = run {create2801(level2819, rest2827)};
          def b_k_38655597(r2830: Map2616[K2660, V2661], xs2831: List910[Tuple2249[K2660, V2661]]) =
            let v_r_38625598 = run {link2709[K2660, V2661](k2825, v2826, l2828, r2830)}
            
            go2822(bitwiseShl225(level2819, 1), v_r_38625598, xs2831);
          def b_k_38695599(r2832: Map2616[K2660, V2661], ys2833: List910[Tuple2249[K2660, V2661]]) =
            let v_r_38665600 = run {link2709[K2660, V2661](k2825, v2826, l2828, r2832)}
            
            insertMany2794(v_r_38665600, ys2833);
          def b_k_38715601() =
            let v_r_38705602 = panic546[Map2616[K2660, V2661]]("create: go: cannot happen, invariant broken!")
            
            v_r_38705602
          
          cr2829 match {
            case Tuple3335 { (v_y_38723876: Map2616[K2660, V2661], v_y_38733875: List910[Tuple2249[K2660, V2661]], v_y_38743877: List910[Tuple2249[K2660, V2661]]) => 
              v_y_38743877 match {
                case Nil1114 { () => 
                  b_k_38655597(v_y_38723876, v_y_38733875)
                }
              } else {
                v_y_38733875 match {
                  case Nil1114 { () => 
                    b_k_38695599(v_y_38723876, v_y_38743877)
                  }
                } else {
                  b_k_38715601()}}
            }
          } else {
            b_k_38715601()}
        }
      
      pairs2821 match {
        case Nil1114 { () => 
          b_k_38535593()
        }
        case Cons1115 { (v_y_38833886: Tuple2249[K2660, V2661], v_y_38843885: List910[Tuple2249[K2660, V2661]]) => 
          v_y_38833886 match {
            case Tuple2330 { (v_y_38873890: K2660, v_y_38883889: V2661) => 
              v_y_38843885 match {
                case Nil1114 { () => 
                  b_k_38565594(v_y_38873890, v_y_38883889)
                }
              } else {
                b_k_38825595(v_y_38873890, v_y_38883889, v_y_38843885)}
            }
          }
        }
      };
    let v_r_38935603 = run {notOrdered2790(k2785, rest2787)}
    
    if (v_r_38935603) {
      let v_r_38945604 = run {singleton2632[K2660, V2661](k2785, v2786)}
      
      insertMany2794(v_r_38945604, rest2787)
    } else {
      let v_r_38975605 = run {singleton2632[K2660, V2661](k2785, v2786)}
      
      go2822(1, v_r_38975605, rest2787)
    }
  
  pairs2662 match {
    case Nil1114 { () => 
      b_k_37625516()
    }
    case Cons1115 { (v_y_39033906: Tuple2249[K2660, V2661], v_y_39043905: List910[Tuple2249[K2660, V2661]]) => 
      v_y_39033906 match {
        case Tuple2330 { (v_y_39073910: K2660, v_y_39083909: V2661) => 
          v_y_39043905 match {
            case Nil1114 { () => 
              b_k_37655517(v_y_39073910, v_y_39083909)
            }
          } else {
            b_k_39025518(v_y_39073910, v_y_39083909, v_y_39043905)}
        }
      }
    }
  };
let ratio2834 = run {2};
let delta2835 = run {3};
def bin2670[K2664, V2665](k2666: K2664, v2667: V2665, l2668: Map2616[K2664, V2665], r2669: Map2616[K2664, V2665]) =
  let v_r_39135542 = run {size2636[K2664, V2665](l2668)};
  let v_r_39145545 = run {size2636[K2664, V2665](r2669)}
  
  make Map2616[K2664, V2665] Bin2750(infixAdd93(infixAdd93(v_r_39135542, v_r_39145545), 1), k2666, v2667, l2668, r2669);
def balance2677[K2671, V2672](k2673: K2671, v2674: V2672, l2675: Map2616[K2671, V2672], r2676: Map2616[K2671, V2672]) =
  def singleL2842[A2836, B2837](k12838: A2836, v12839: B2837, t12840: Map2616[A2836, B2837], m2841: Map2616[A2836, B2837]) =
    def b_k_39185540(k22843: A2836, v22844: B2837, t22845: Map2616[A2836, B2837], t32846: Map2616[A2836, B2837]) =
      let v_r_39155541 = run {bin2670[A2836, B2837](k12838, v12839, t12840, t22845)}
      
      bin2670[A2836, B2837](k22843, v22844, v_r_39155541, t32846);
    def b_k_39205546() =
      let v_r_39195547 = panic546[Map2616[A2836, B2837]]("impossible: singleL: Tip")
      
      v_r_39195547
    
    m2841 match {
      case Bin2750 { (v_y_39213927: Int398, v_y_39223926: A2836, v_y_39233928: B2837, v_y_39243929: Map2616[A2836, B2837], v_y_39253930: Map2616[A2836, B2837]) => 
        b_k_39185540(v_y_39223926, v_y_39233928, v_y_39243929, v_y_39253930)
      }
    } else {
      b_k_39205546()};
  def singleR2853[A2847, B2848](k12849: A2847, v12850: B2848, m2851: Map2616[A2847, B2848], t32852: Map2616[A2847, B2848]) =
    def b_k_39365548(k22854: A2847, v22855: B2848, t12856: Map2616[A2847, B2848], t22857: Map2616[A2847, B2848]) =
      let v_r_39335549 = run {bin2670[A2847, B2848](k12849, v12850, t22857, t32852)}
      
      bin2670[A2847, B2848](k22854, v22855, t12856, v_r_39335549);
    def b_k_39385550() =
      let v_r_39375551 = panic546[Map2616[A2847, B2848]]("impossible: singleR: Tip")
      
      v_r_39375551
    
    m2851 match {
      case Bin2750 { (v_y_39393945: Int398, v_y_39403944: A2847, v_y_39413946: B2848, v_y_39423947: Map2616[A2847, B2848], v_y_39433948: Map2616[A2847, B2848]) => 
        b_k_39365548(v_y_39403944, v_y_39413946, v_y_39423947, v_y_39433948)
      }
    } else {
      b_k_39385550()};
  def doubleL2864[A2858, B2859](k12860: A2858, v12861: B2859, t12862: Map2616[A2858, B2859], m2863: Map2616[A2858, B2859]) =
    def b_k_39555552(k22865: A2858, v22866: B2859, k32867: A2858, v32868: B2859, t22869: Map2616[A2858, B2859], t32870: Map2616[A2858, B2859], t42871: Map2616[A2858, B2859]) =
      let v_r_39515553 = run {bin2670[A2858, B2859](k12860, v12861, t12862, t22869)};
      let v_r_39525554 = run {bin2670[A2858, B2859](k22865, v22866, t32870, t42871)}
      
      bin2670[A2858, B2859](k32867, v32868, v_r_39515553, v_r_39525554);
    def b_k_39575555() =
      let v_r_39565556 = panic546[Map2616[A2858, B2859]]("impossible: doubleL: Tip")
      
      v_r_39565556
    
    m2863 match {
      case Bin2750 { (v_y_39583964: Int398, v_y_39593963: A2858, v_y_39603965: B2859, v_y_39613966: Map2616[A2858, B2859], v_y_39623967: Map2616[A2858, B2859]) => 
        v_y_39613966 match {
          case Bin2750 { (v_y_39683974: Int398, v_y_39693973: A2858, v_y_39703975: B2859, v_y_39713976: Map2616[A2858, B2859], v_y_39723977: Map2616[A2858, B2859]) => 
            b_k_39555552(v_y_39593963, v_y_39603965, v_y_39693973, v_y_39703975, v_y_39713976, v_y_39723977, v_y_39623967)
          }
        } else {
          b_k_39575555()}
      }
    } else {
      b_k_39575555()};
  def doubleR2878[A2872, B2873](k12874: A2872, v12875: B2873, m2876: Map2616[A2872, B2873], t42877: Map2616[A2872, B2873]) =
    def b_k_39845557(k22879: A2872, v22880: B2873, t12881: Map2616[A2872, B2873], k32882: A2872, v32883: B2873, t22884: Map2616[A2872, B2873], t32885: Map2616[A2872, B2873]) =
      let v_r_39805558 = run {bin2670[A2872, B2873](k22879, v22880, t12881, t22884)};
      let v_r_39815559 = run {bin2670[A2872, B2873](k12874, v12875, t32885, t42877)}
      
      bin2670[A2872, B2873](k32882, v32883, v_r_39805558, v_r_39815559);
    def b_k_39865560() =
      let v_r_39855561 = panic546[Map2616[A2872, B2873]]("impossible: doubleR: Tip")
      
      v_r_39855561
    
    m2876 match {
      case Bin2750 { (v_y_39873993: Int398, v_y_39883992: A2872, v_y_39893994: B2873, v_y_39903995: Map2616[A2872, B2873], v_y_39913996: Map2616[A2872, B2873]) => 
        v_y_39913996 match {
          case Bin2750 { (v_y_39974003: Int398, v_y_39984002: A2872, v_y_39994004: B2873, v_y_40004005: Map2616[A2872, B2873], v_y_40014006: Map2616[A2872, B2873]) => 
            b_k_39845557(v_y_39883992, v_y_39893994, v_y_39903995, v_y_39984002, v_y_39994004, v_y_40004005, v_y_40014006)
          }
        } else {
          b_k_39865560()}
      }
    } else {
      b_k_39865560()};
  def rotateL2892[A2886, B2887](k2888: A2886, v2889: B2887, l2890: Map2616[A2886, B2887], r2891: Map2616[A2886, B2887]) =
    def b_k_40115562(rl2893: Map2616[A2886, B2887], rr2894: Map2616[A2886, B2887]) =
      singleL2842[A2886, B2887](k2888, v2889, l2890, r2891);
    def b_k_40185563() =
      doubleL2864[A2886, B2887](k2888, v2889, l2890, r2891)
    
    r2891 match {
      case Bin2750 { (v_y_40194025: Int398, v_y_40204024: A2886, v_y_40214026: B2887, v_y_40224027: Map2616[A2886, B2887], v_y_40234028: Map2616[A2886, B2887]) => 
        let v_r_40144029 = run {size2636[A2886, B2887](v_y_40224027)};
        let v_r_40154030 = run {size2636[A2886, B2887](v_y_40234028)}
        
        if (infixLt175(v_r_40144029, infixMul96(ratio2834, v_r_40154030))) {
          b_k_40115562(v_y_40224027, v_y_40234028)
        } else {
          b_k_40185563()
        }
      }
    } else {
      b_k_40185563()};
  def rotateR2901[A2895, B2896](k2897: A2895, v2898: B2896, l2899: Map2616[A2895, B2896], r2900: Map2616[A2895, B2896]) =
    def b_k_40355564(ll2902: Map2616[A2895, B2896], lr2903: Map2616[A2895, B2896]) =
      singleR2853[A2895, B2896](k2897, v2898, l2899, r2900);
    def b_k_40425565() =
      doubleR2878[A2895, B2896](k2897, v2898, l2899, r2900)
    
    l2899 match {
      case Bin2750 { (v_y_40434049: Int398, v_y_40444048: A2895, v_y_40454050: B2896, v_y_40464051: Map2616[A2895, B2896], v_y_40474052: Map2616[A2895, B2896]) => 
        let v_r_40384053 = run {size2636[A2895, B2896](v_y_40474052)};
        let v_r_40394054 = run {size2636[A2895, B2896](v_y_40464051)}
        
        if (infixLt175(v_r_40384053, infixMul96(ratio2834, v_r_40394054))) {
          b_k_40355564(v_y_40464051, v_y_40474052)
        } else {
          b_k_40425565()
        }
      }
    } else {
      b_k_40425565()};
  let sizeL2904 = run {size2636[K2671, V2672](l2675)};
  let sizeR2905 = run {size2636[K2671, V2672](r2676)};
  let sizeCombined2906 = run {infixAdd93(infixAdd93(sizeL2904, sizeR2905), 1)}
  
  if (infixLte178(infixAdd93(sizeL2904, sizeR2905), 1)) {
    make Map2616[K2671, V2672] Bin2750(sizeCombined2906, k2673, v2674, l2675, r2676)
  } else {
    if (infixGt181(sizeR2905, infixMul96(delta2835, sizeL2904))) {
      rotateL2892[K2671, V2672](k2673, v2674, l2675, r2676)
    } else {
      if (infixGt181(sizeL2904, infixMul96(delta2835, sizeR2905))) {
        rotateR2901[K2671, V2672](k2673, v2674, l2675, r2676)
      } else {
        make Map2616[K2671, V2672] Bin2750(sizeCombined2906, k2673, v2674, l2675, r2676)
      }
    }
  };
def maxViewSure2690[K2684, V2685](k2686: K2684, v2687: V2685, l2688: Map2616[K2684, V2685], r2689: Map2616[K2684, V2685]) =
  let v_r_40715893 = make Tuple2249[Map2616[K2684, V2685], Map2616[K2684, V2685]] Tuple2330(l2688, r2689);
  def b_k_40725894(l2921: Map2616[K2684, V2685]) =
    make MaxView2680[K2684, V2685] MaxView2907(k2686, v2687, l2921);
  def b_k_40845895(l2922: Map2616[K2684, V2685], kr2923: K2684, vr2924: V2685, rl2925: Map2616[K2684, V2685], rr2926: Map2616[K2684, V2685]) =
    let v_r_40735896 = run {maxViewSure2690[K2684, V2685](kr2923, vr2924, rl2925, rr2926)};
    def b_k_40755897(km2927: K2684, vm2928: V2685, r22929: Map2616[K2684, V2685]) =
      let v_r_40745898 = run {balance2677[K2684, V2685](k2686, v2687, l2922, r22929)}
      
      make MaxView2680[K2684, V2685] MaxView2907(km2927, vm2928, v_r_40745898)
    
    v_r_40735896 match {
      case MaxView2907 { (v_y_40764080: K2684, v_y_40774079: V2685, v_y_40784081: Map2616[K2684, V2685]) => 
        b_k_40755897(v_y_40764080, v_y_40774079, v_y_40784081)
      }
    }
  
  v_r_40715893 match {
    case Tuple2330 { (v_y_40854088: Map2616[K2684, V2685], v_y_40864087: Map2616[K2684, V2685]) => 
      v_y_40864087 match {
        case Tip2761 { () => 
          b_k_40725894(v_y_40854088)
        }
        case Bin2750 { (v_y_40894095: Int398, v_y_40904094: K2684, v_y_40914096: V2685, v_y_40924097: Map2616[K2684, V2685], v_y_40934098: Map2616[K2684, V2685]) => 
          b_k_40845895(v_y_40854088, v_y_40904094, v_y_40914096, v_y_40924097, v_y_40934098)
        }
      }
    }
  };
def minViewSure2697[K2691, V2692](k2693: K2691, v2694: V2692, l2695: Map2616[K2691, V2692], r2696: Map2616[K2691, V2692]) =
  let v_r_41015899 = make Tuple2249[Map2616[K2691, V2692], Map2616[K2691, V2692]] Tuple2330(l2695, r2696);
  def b_k_41025900(r2930: Map2616[K2691, V2692]) =
    make MinView2683[K2691, V2692] MinView2914(k2693, v2694, r2930);
  def b_k_41145901(kl2931: K2691, vl2932: V2692, ll2933: Map2616[K2691, V2692], lr2934: Map2616[K2691, V2692], r2935: Map2616[K2691, V2692]) =
    let v_r_41035902 = run {minViewSure2697[K2691, V2692](kl2931, vl2932, ll2933, lr2934)};
    def b_k_41055903(km2936: K2691, vm2937: V2692, l22938: Map2616[K2691, V2692]) =
      let v_r_41045904 = run {balance2677[K2691, V2692](k2693, v2694, l22938, r2935)}
      
      make MinView2683[K2691, V2692] MinView2914(km2936, vm2937, v_r_41045904)
    
    v_r_41035902 match {
      case MinView2914 { (v_y_41064110: K2691, v_y_41074109: V2692, v_y_41084111: Map2616[K2691, V2692]) => 
        b_k_41055903(v_y_41064110, v_y_41074109, v_y_41084111)
      }
    }
  
  v_r_41015899 match {
    case Tuple2330 { (v_y_41154118: Map2616[K2691, V2692], v_y_41164117: Map2616[K2691, V2692]) => 
      v_y_41154118 match {
        case Tip2761 { () => 
          b_k_41025900(v_y_41164117)
        }
        case Bin2750 { (v_y_41194125: Int398, v_y_41204124: K2691, v_y_41214126: V2692, v_y_41224127: Map2616[K2691, V2692], v_y_41234128: Map2616[K2691, V2692]) => 
          b_k_41145901(v_y_41204124, v_y_41214126, v_y_41224127, v_y_41234128, v_y_41164117)
        }
      }
    }
  };
def glue2702[K2698, V2699](l2700: Map2616[K2698, V2699], r2701: Map2616[K2698, V2699]) =
  let v_r_41315905 = make Tuple2249[Map2616[K2698, V2699], Map2616[K2698, V2699]] Tuple2330(l2700, r2701);
  def b_k_41325906(r2939: Map2616[K2698, V2699]) =
    r2939;
  def b_k_41335907(l2940: Map2616[K2698, V2699]) =
    l2940;
  def b_k_41605908(sizeL2941: Int398, kl2942: K2698, vl2943: V2699, ll2944: Map2616[K2698, V2699], lr2945: Map2616[K2698, V2699], sizeR2946: Int398, kr2947: K2698, vr2948: V2699, rl2949: Map2616[K2698, V2699], rr2950: Map2616[K2698, V2699]) =
    if (infixGt181(sizeL2941, sizeR2946)) {
      let v_r_41345909 = run {maxViewSure2690[K2698, V2699](kl2942, vl2943, ll2944, lr2945)};
      def b_k_41375910(km2951: K2698, m2952: V2699, l22953: Map2616[K2698, V2699]) =
        balance2677[K2698, V2699](km2951, m2952, l22953, r2701)
      
      v_r_41345909 match {
        case MaxView2907 { (v_y_41384142: K2698, v_y_41394141: V2699, v_y_41404143: Map2616[K2698, V2699]) => 
          b_k_41375910(v_y_41384142, v_y_41394141, v_y_41404143)
        }
      }
    } else {
      let v_r_41465911 = run {minViewSure2697[K2698, V2699](kr2947, vr2948, rl2949, rr2950)};
      def b_k_41495912(km2954: K2698, m2955: V2699, r22956: Map2616[K2698, V2699]) =
        balance2677[K2698, V2699](km2954, m2955, l2700, r22956)
      
      v_r_41465911 match {
        case MinView2914 { (v_y_41504154: K2698, v_y_41514153: V2699, v_y_41524155: Map2616[K2698, V2699]) => 
          b_k_41495912(v_y_41504154, v_y_41514153, v_y_41524155)
        }
      }
    }
  
  v_r_41315905 match {
    case Tuple2330 { (v_y_41614164: Map2616[K2698, V2699], v_y_41624163: Map2616[K2698, V2699]) => 
      v_y_41614164 match {
        case Tip2761 { () => 
          b_k_41325906(v_y_41624163)
        }
        case Bin2750 { (v_y_41654171: Int398, v_y_41664170: K2698, v_y_41674172: V2699, v_y_41684173: Map2616[K2698, V2699], v_y_41694174: Map2616[K2698, V2699]) => 
          v_y_41624163 match {
            case Tip2761 { () => 
              b_k_41335907(v_y_41614164)
            }
            case Bin2750 { (v_y_41754181: Int398, v_y_41764180: K2698, v_y_41774182: V2699, v_y_41784183: Map2616[K2698, V2699], v_y_41794184: Map2616[K2698, V2699]) => 
              b_k_41605908(v_y_41654171, v_y_41664170, v_y_41674172, v_y_41684173, v_y_41694174, v_y_41754181, v_y_41764180, v_y_41774182, v_y_41784183, v_y_41794184)
            }
          }
        }
      } else {
        v_y_41624163 match {
          case Tip2761 { () => 
            b_k_41335907(v_y_41614164)
          }
        }}
    }
  };
def link2709[K2703, V2704](k2705: K2703, v2706: V2704, l2707: Map2616[K2703, V2704], r2708: Map2616[K2703, V2704]) =
  let v_r_41875584 = make Tuple2249[Map2616[K2703, V2704], Map2616[K2703, V2704]] Tuple2330(l2707, r2708);
  def b_k_41905585(r2957: Map2616[K2703, V2704]) =
    putMin2715[K2703, V2704](r2957, k2705, v2706);
  def b_k_41935589(l2958: Map2616[K2703, V2704]) =
    putMax2721[K2703, V2704](l2958, k2705, v2706);
  def b_k_42065590(sizeL2959: Int398, kl2960: K2703, vl2961: V2704, ll2962: Map2616[K2703, V2704], lr2963: Map2616[K2703, V2704], sizeR2964: Int398, kr2965: K2703, vr2966: V2704, rl2967: Map2616[K2703, V2704], rr2968: Map2616[K2703, V2704]) =
    if (infixLt175(infixMul96(delta2835, sizeL2959), sizeR2964)) {
      let v_r_41945591 = run {link2709[K2703, V2704](k2705, v2706, l2707, rl2967)}
      
      balance2677[K2703, V2704](kr2965, vr2966, v_r_41945591, rr2968)
    } else {
      if (infixLt175(infixMul96(delta2835, sizeR2964), sizeL2959)) {
        let v_r_41975592 = run {link2709[K2703, V2704](k2705, v2706, lr2963, r2708)}
        
        balance2677[K2703, V2704](kl2960, vl2961, ll2962, v_r_41975592)
      } else {
        bin2670[K2703, V2704](k2705, v2706, l2707, r2708)
      }
    }
  
  v_r_41875584 match {
    case Tuple2330 { (v_y_42074210: Map2616[K2703, V2704], v_y_42084209: Map2616[K2703, V2704]) => 
      v_y_42074210 match {
        case Tip2761 { () => 
          b_k_41905585(v_y_42084209)
        }
        case Bin2750 { (v_y_42114217: Int398, v_y_42124216: K2703, v_y_42134218: V2704, v_y_42144219: Map2616[K2703, V2704], v_y_42154220: Map2616[K2703, V2704]) => 
          v_y_42084209 match {
            case Tip2761 { () => 
              b_k_41935589(v_y_42074210)
            }
            case Bin2750 { (v_y_42214227: Int398, v_y_42224226: K2703, v_y_42234228: V2704, v_y_42244229: Map2616[K2703, V2704], v_y_42254230: Map2616[K2703, V2704]) => 
              b_k_42065590(v_y_42114217, v_y_42124216, v_y_42134218, v_y_42144219, v_y_42154220, v_y_42214227, v_y_42224226, v_y_42234228, v_y_42244229, v_y_42254230)
            }
          }
        }
      } else {
        v_y_42084209 match {
          case Tip2761 { () => 
            b_k_41935589(v_y_42074210)
          }
        }}
    }
  };
def putMin2715[K2710, V2711](m2712: Map2616[K2710, V2711], k2713: K2710, v2714: V2711) =
  def b_k_42355586() =
    singleton2632[K2710, V2711](k2713, v2714);
  def b_k_42395587(k22969: K2710, v22970: V2711, l2971: Map2616[K2710, V2711], r2972: Map2616[K2710, V2711]) =
    let v_r_42365588 = run {putMin2715[K2710, V2711](l2971, k2713, v2714)}
    
    balance2677[K2710, V2711](k22969, v22970, v_r_42365588, r2972)
  
  m2712 match {
    case Tip2761 { () => 
      b_k_42355586()
    }
    case Bin2750 { (v_y_42404246: Int398, v_y_42414245: K2710, v_y_42424247: V2711, v_y_42434248: Map2616[K2710, V2711], v_y_42444249: Map2616[K2710, V2711]) => 
      b_k_42395587(v_y_42414245, v_y_42424247, v_y_42434248, v_y_42444249)
    }
  };
def putMax2721[K2716, V2717](m2718: Map2616[K2716, V2717], k2719: K2716, v2720: V2717) =
  def b_k_42545576() =
    singleton2632[K2716, V2717](k2719, v2720);
  def b_k_42585577(k22973: K2716, v22974: V2717, l2975: Map2616[K2716, V2717], r2976: Map2616[K2716, V2717]) =
    let v_r_42555578 = run {putMax2721[K2716, V2717](r2976, k2719, v2720)}
    
    balance2677[K2716, V2717](k22973, v22974, l2975, v_r_42555578)
  
  m2718 match {
    case Tip2761 { () => 
      b_k_42545576()
    }
    case Bin2750 { (v_y_42594265: Int398, v_y_42604264: K2716, v_y_42614266: V2717, v_y_42624267: Map2616[K2716, V2717], v_y_42634268: Map2616[K2716, V2717]) => 
      b_k_42585577(v_y_42604264, v_y_42614266, v_y_42624267, v_y_42634268)
    }
  };
def for2731[A2727, R2728](){stream2729}{action2730} =
  reset { (){p5607} => 
    def emit5606 = new emit2723[A2727] {
      def emit2978(value2981: A2727) = 
        shift(p5607) { (){k5608} => 
          def resume2982(a5609: Unit394) =
            resume(k5608) {
              a5609
            }
          
          val v_r_42745610: Unit394 = action2730(value2981);
          resume2982(v_r_42745610)
        }
    }
    
    stream2729(emit5606)
  };
def for2735[A2732](){stream2733}{action2734} =
  for2731[A2732, Any395](stream2733, action2734);
  ();
def each2739[K2736, V2737](map2738: Map2616[K2736, V2737]){emit5613} =
  foreach2647[K2736, V2737](map2738, { (k2983: K2736, v2984: V2737) => 
    emit5613.emit(make Tuple2249[K2736, V2737] Tuple2330(k2983, v2984))
  });
def collectMap2744[K2740, V2741, R2742](){stream2743} =
  reset { (){p5913} => 
    def emit5914 = new emit2723[Tuple2249[K2740, V2741]] {
      def emit2978(__tmpRes2985: Tuple2249[K2740, V2741]) = 
        shift(p5913) { (){k5915} => 
          def resume2986(a5916: Unit394) =
            resume(k5915) {
              a5916
            }
          
          def b_k_43015917(k2987: K2740, v2988: V2741) =
            val v_r_42925918: Tuple2249[R2742, Map2616[K2740, V2741]] = resume2986(());
            def b_k_42945919(r2989: R2742, map2990: Map2616[K2740, V2741]) =
              let v_r_42935920 = run {put2642[K2740, V2741](map2990, k2987, v2988)}
              
              make Tuple2249[R2742, Map2616[K2740, V2741]] Tuple2330(r2989, v_r_42935920)
            
            v_r_42925918 match {
              case Tuple2330 { (v_y_42954298: R2742, v_y_42964297: Map2616[K2740, V2741]) => 
                b_k_42945919(v_y_42954298, v_y_42964297)
              }
            }
          
          __tmpRes2985 match {
            case Tuple2330 { (v_y_43024305: K2740, v_y_43034304: V2741) => 
              b_k_43015917(v_y_43024305, v_y_43034304)
            }
          }
        }
    }
    
    val v_r_43105921: R2742 = stream2743(emit5914);
    let v_r_43115922 = run {empty2619[K2740, V2741]()}
    
    make Tuple2249[R2742, Map2616[K2740, V2741]] Tuple2330(v_r_43105921, v_r_43115922)
  };
def collectMap2748[K2745, V2746](){stream2747} =
  val v_r_43145923: Tuple2249[Any395, Map2616[K2745, V2746]] = collectMap2744[K2745, V2746, Any395](stream2747);
  v_r_43145923.second334;
def main2749() =
  let m2991 = run {fromList2663[Int398, Char390](make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(1, 72), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(2, 101), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(3, 108), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(4, 108), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(5, 111), make List910[Tuple2249[Int398, Char390]] Nil1114()))))))}
  
  for2735[Tuple2249[Int398, Char390]]({ (){emit5614} => 
    each2739[Int398, Char390](m2991, emit5614)
  }, { (__tmpRes2992: Tuple2249[Int398, Char390]) => 
    def b_k_43205615(k2993: Int398, v2994: Char390) =
      let v_r_43195616 = println1(infixConcat35(infixConcat35(infixConcat35(infixConcat35(infixConcat35(show14(k2993), ": "), show24(v2994)), " ("), show14(toInt2323(v2994))), ")"))
      
      v_r_43195616
    
    __tmpRes2992 match {
      case Tuple2330 { (v_y_43214324: Int398, v_y_43224323: Char390) => 
        b_k_43205615(v_y_43214324, v_y_43224323)
      }
    }
  })

Opt Core

 let cr999169214 = run {create40328823(level86788726, v_y_38841141068748)}
                
                cr999169214 match {
                  case Tuple3335 { (v_y_3872110102179215: Map2616[Int398, Char390], v_y_3873111103189216: List910[Tuple2249[Int398, Char390]], v_y_3874112104199217: List910[Tuple2249[Int398, Char390]]) => 
                    v_y_3874112104199217 match {
                      case Nil1114 { () => 
                        let v_r_3862103951039389 = run {link2709[Int398, Char390](v_y_38871151078749, v_y_38881161088750, l989059208, v_y_3872110102179215)} // HERE
                        
                        go85778830(bitwiseShl225(level86788726, 1), v_r_3862103951039389, v_y_3873111103189216)
                      }
                    } else {
                      v_y_3873111103189216 match {
                        case Nil1114 { () => 
                          let v_r_3866107991439392 = run {link2709[Int398, Char390](v_y_38871151078749, v_y_38881161088750, l989059208, v_y_3872110102179215)} // and HERE
                          
...
Optimised Core (search for "HERE")
module mapinlined





extern {io525} def println1 = (value2: String393): Unit394 = js value2
extern {} def show14 = (value13: Int398): String393 = js value13
extern {} def show24 = (value23: Char390): String393 = js value23
extern {} def infixConcat35 = (s133: String393, s234: String393): String393 = js s133, s234
extern {} def genericCompareImpl47 = [R44](x45: R44, y46: R44): Int398 = js x45, y46
extern {} def infixEq69 = (x67: Int398, y68: Int398): Bool389 = js x67, y68
extern {} def infixAdd93 = (x91: Int398, y92: Int398): Int398 = js x91, y92
extern {} def infixMul96 = (x94: Int398, y95: Int398): Int398 = js x94, y95
extern {} def infixLt175 = (x173: Int398, y174: Int398): Bool389 = js x173, y174
extern {} def infixLte178 = (x176: Int398, y177: Int398): Bool389 = js x176, y177
extern {} def infixGt181 = (x179: Int398, y180: Int398): Bool389 = js x179, y180
extern {} def bitwiseShl225 = (x223: Int398, y224: Int398): Int398 = js x223, y224
extern {} def bitwiseShr228 = (x226: Int398, y227: Int398): Int398 = js x226, y227
extern {io525} def panic546 = [R544](msg545: String393): R544 = js msg545

extern {} def toInt2323 = (ch2322: Char390): Int398 = js ch2322


type Nothing397 {}
type Ordering43 {
  Less321()
  Equal322()
  Greater323()
}
type Tuple2249[A247, B248] {
  Tuple2330(first333: A247, second334: B248)
}
type Tuple3253[A250, B251, C252] {
  Tuple3335(first339: A250, second340: B251, third341: C252)
}
type Tuple4258[A254, B255, C256, D257] {
  Tuple4342(first347: A254, second348: B255, third349: C256, fourth350: D257)
}
type Tuple5264[A259, B260, C261, D262, E263] {
  Tuple5351(first357: A259, second358: B260, third359: C261, fourth360: D262, fifth361: E263)
}
type Tuple6271[A265, B266, C267, D268, E269, F270] {
  Tuple6362(first369: A265, second370: B266, third371: C267, fourth372: D268, fifth373: E269, sixth374: F270)
}
interface Control272 {
  break375: () => Unit394
  continue376: () => Unit394
}
interface Exception526[E527] {
  raise596: (E527, String393) => Nothing397
}
type MissingValue533 {
  MissingValue597()
}
type OutOfBounds535 {
  OutOfBounds598()
}
type RuntimeError536 {
  RuntimeError599()
}
type WrongFormat540 {
  WrongFormat600()
}
type on568[E567] {
  on617()
}
type Option743[A742] {
  None797()
  Some798(value800: A742)
}
type List910[A911] {
  Nil1114()
  Cons1115(head1118: A911, tail1119: List910[A911])
}
type Result1820[A1819, E1818] {
  Error1838(exception1841: E1818, msg1842: String393)
  Success1843(a1845: A1819)
}
type Map2616[K2614, V2615] {
  Bin2750(size2756: Int398, k2757: K2614, v2758: V2615, left2759: Map2616[K2614, V2615], right2760: Map2616[K2614, V2615])
  Tip2761()
}
type MaxView2680[K2678, V2679] {
  MaxView2907(k2911: K2678, v2912: V2679, m2913: Map2616[K2678, V2679])
}
type MinView2683[K2681, V2682] {
  MinView2914(k2918: K2681, v2919: V2682, m2920: Map2616[K2681, V2682])
}
interface emit2723[A2722] {
  emit2978: (A2722) => Unit394
}
interface read2725[A2724] {
  read2979: (){stop9526: stop2726} => A2724
}
interface stop2726 {
  stop2980: () => Nothing397
}

def put2642[K2637, V2638](m7664: Map2616[K2637, V2638], k2640: K2637, v2641: V2638) =
  def put_worker7663(m2639: Map2616[K2637, V2638]) =
    m2639 match {
      case Tip2761 { () => 
        make Map2616[K2637, V2638] Bin2750(1, k2640, v2641, make Map2616[K2637, V2638] Tip2761(), make Map2616[K2637, V2638] Tip2761())
      }
      case Bin2750 { (v_y_37013707: Int398, v_y_37023706: K2637, v_y_37033708: V2638, v_y_37043709: Map2616[K2637, V2638], v_y_37053710: Map2616[K2637, V2638]) => 
        let v_r_368767690 = run {let v_r_541148383 = genericCompareImpl47[K2637](k2640, v_y_37023706)
        
        if (infixEq69(v_r_541148383, -1)) {
          make Ordering43 Less321()
        } else {
          if (infixEq69(v_r_541148383, 0)) {
            make Ordering43 Equal322()
          } else {
            if (infixEq69(v_r_541148383, 1)) {
              make Ordering43 Greater323()
            } else {
              <>
            }
          }
        }}
        
        v_r_368767690 match {
          case Less321 { () => 
            let v_r_3689818791 = run {put_worker7663(v_y_37043709)}
            
            balance2677[K2637, V2638](v_y_37023706, v_y_37033708, v_r_3689818791, v_y_37053710)
          }
          case Greater323 { () => 
            let v_r_36931018792 = run {put_worker7663(v_y_37053710)}
            
            balance2677[K2637, V2638](v_y_37023706, v_y_37033708, v_y_37043709, v_r_36931018792)
          }
          case Equal322 { () => 
            make Map2616[K2637, V2638] Bin2750(v_y_37013707, k2640, v2641, v_y_37043709, v_y_37053710)
          }
        }
      }
    }
  
  put_worker7663(m7664);
let ratio2834 = 2;
let delta2835 = 3;
def balance2677[K2671, V2672](k2673: K2671, v2674: V2672, l2675: Map2616[K2671, V2672], r2676: Map2616[K2671, V2672]) =
  def doubleL2864[A2858, B2859](k12860: A2858, v12861: B2859, t12862: Map2616[A2858, B2859], m2863: Map2616[A2858, B2859]) =
    m2863 match {
      case Bin2750 { (v_y_39583964: Int398, v_y_39593963: A2858, v_y_39603965: B2859, v_y_39613966: Map2616[A2858, B2859], v_y_39623967: Map2616[A2858, B2859]) => 
        v_y_39613966 match {
          case Bin2750 { (v_y_39683974: Int398, v_y_39693973: A2858, v_y_39703975: B2859, v_y_39713976: Map2616[A2858, B2859], v_y_39723977: Map2616[A2858, B2859]) => 
            let v_r_395188043 = run {let v_r_3913128864 = run {t12862 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_3672778854: Int398, v_y_3673888855: A2858, v_y_3674998856: B2859, v_y_367510108857: Map2616[A2858, B2859], v_y_367611118858: Map2616[A2858, B2859]) => 
                v_y_3672778854
              }
            }};
            let v_r_3914188865 = run {v_y_39713976 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_36727138859: Int398, v_y_36738148860: A2858, v_y_36749158861: B2859, v_y_367510168862: Map2616[A2858, B2859], v_y_367611178863: Map2616[A2858, B2859]) => 
                v_y_36727138859
              }
            }}
            
            make Map2616[A2858, B2859] Bin2750(infixAdd93(infixAdd93(v_r_3913128864, v_r_3914188865), 1), k12860, v12861, t12862, v_y_39713976)};
            let v_r_395298044 = run {let v_r_3913128882 = run {v_y_39723977 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_3672778872: Int398, v_y_3673888873: A2858, v_y_3674998874: B2859, v_y_367510108875: Map2616[A2858, B2859], v_y_367611118876: Map2616[A2858, B2859]) => 
                v_y_3672778872
              }
            }};
            let v_r_3914188883 = run {v_y_39623967 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_36727138877: Int398, v_y_36738148878: A2858, v_y_36749158879: B2859, v_y_367510168880: Map2616[A2858, B2859], v_y_367611178881: Map2616[A2858, B2859]) => 
                v_y_36727138877
              }
            }}
            
            make Map2616[A2858, B2859] Bin2750(infixAdd93(infixAdd93(v_r_3913128882, v_r_3914188883), 1), v_y_39593963, v_y_39603965, v_y_39723977, v_y_39623967)};
            let v_r_3913128900 = run {v_r_395188043 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_3672778890: Int398, v_y_3673888891: A2858, v_y_3674998892: B2859, v_y_367510108893: Map2616[A2858, B2859], v_y_367611118894: Map2616[A2858, B2859]) => 
                v_y_3672778890
              }
            }};
            let v_r_3914188901 = run {v_r_395298044 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_36727138895: Int398, v_y_36738148896: A2858, v_y_36749158897: B2859, v_y_367510168898: Map2616[A2858, B2859], v_y_367611178899: Map2616[A2858, B2859]) => 
                v_y_36727138895
              }
            }}
            
            make Map2616[A2858, B2859] Bin2750(infixAdd93(infixAdd93(v_r_3913128900, v_r_3914188901), 1), v_y_39693973, v_y_39703975, v_r_395188043, v_r_395298044)
          }
        } else {
          let v_r_395618336 = panic546[Map2616[A2858, B2859]]("impossible: doubleL: Tip")
          
          v_r_395618336}
      }
    } else {
      let v_r_395618337 = panic546[Map2616[A2858, B2859]]("impossible: doubleL: Tip")
      
      v_r_395618337};
  def doubleR2878[A2872, B2873](k12874: A2872, v12875: B2873, m2876: Map2616[A2872, B2873], t42877: Map2616[A2872, B2873]) =
    m2876 match {
      case Bin2750 { (v_y_39873993: Int398, v_y_39883992: A2872, v_y_39893994: B2873, v_y_39903995: Map2616[A2872, B2873], v_y_39913996: Map2616[A2872, B2873]) => 
        v_y_39913996 match {
          case Bin2750 { (v_y_39974003: Int398, v_y_39984002: A2872, v_y_39994004: B2873, v_y_40004005: Map2616[A2872, B2873], v_y_40014006: Map2616[A2872, B2873]) => 
            let v_r_398088076 = run {let v_r_3913128918 = run {v_y_39903995 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_3672778908: Int398, v_y_3673888909: A2872, v_y_3674998910: B2873, v_y_367510108911: Map2616[A2872, B2873], v_y_367611118912: Map2616[A2872, B2873]) => 
                v_y_3672778908
              }
            }};
            let v_r_3914188919 = run {v_y_40004005 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_36727138913: Int398, v_y_36738148914: A2872, v_y_36749158915: B2873, v_y_367510168916: Map2616[A2872, B2873], v_y_367611178917: Map2616[A2872, B2873]) => 
                v_y_36727138913
              }
            }}
            
            make Map2616[A2872, B2873] Bin2750(infixAdd93(infixAdd93(v_r_3913128918, v_r_3914188919), 1), v_y_39883992, v_y_39893994, v_y_39903995, v_y_40004005)};
            let v_r_398198077 = run {let v_r_3913128936 = run {v_y_40014006 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_3672778926: Int398, v_y_3673888927: A2872, v_y_3674998928: B2873, v_y_367510108929: Map2616[A2872, B2873], v_y_367611118930: Map2616[A2872, B2873]) => 
                v_y_3672778926
              }
            }};
            let v_r_3914188937 = run {t42877 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_36727138931: Int398, v_y_36738148932: A2872, v_y_36749158933: B2873, v_y_367510168934: Map2616[A2872, B2873], v_y_367611178935: Map2616[A2872, B2873]) => 
                v_y_36727138931
              }
            }}
            
            make Map2616[A2872, B2873] Bin2750(infixAdd93(infixAdd93(v_r_3913128936, v_r_3914188937), 1), k12874, v12875, v_y_40014006, t42877)};
            let v_r_3913128954 = run {v_r_398088076 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_3672778944: Int398, v_y_3673888945: A2872, v_y_3674998946: B2873, v_y_367510108947: Map2616[A2872, B2873], v_y_367611118948: Map2616[A2872, B2873]) => 
                v_y_3672778944
              }
            }};
            let v_r_3914188955 = run {v_r_398198077 match {
              case Tip2761 { () => 
                0
              }
              case Bin2750 { (v_y_36727138949: Int398, v_y_36738148950: A2872, v_y_36749158951: B2873, v_y_367510168952: Map2616[A2872, B2873], v_y_367611178953: Map2616[A2872, B2873]) => 
                v_y_36727138949
              }
            }}
            
            make Map2616[A2872, B2873] Bin2750(infixAdd93(infixAdd93(v_r_3913128954, v_r_3914188955), 1), v_y_39984002, v_y_39994004, v_r_398088076, v_r_398198077)
          }
        } else {
          let v_r_398518338 = panic546[Map2616[A2872, B2873]]("impossible: doubleR: Tip")
          
          v_r_398518338}
      }
    } else {
      let v_r_398518339 = panic546[Map2616[A2872, B2873]]("impossible: doubleR: Tip")
      
      v_r_398518339};
  def rotateL2892[A2886, B2887](k2888: A2886, v2889: B2887, l2890: Map2616[A2886, B2887], r2891: Map2616[A2886, B2887]) =
    r2891 match {
      case Bin2750 { (v_y_40194025: Int398, v_y_40204024: A2886, v_y_40214026: B2887, v_y_40224027: Map2616[A2886, B2887], v_y_40234028: Map2616[A2886, B2887]) => 
        let v_r_40144029 = run {v_y_40224027 match {
          case Tip2761 { () => 
            0
          }
          case Bin2750 { (v_y_367278082: Int398, v_y_367388083: A2886, v_y_367498084: B2887, v_y_3675108085: Map2616[A2886, B2887], v_y_3676118086: Map2616[A2886, B2887]) => 
            v_y_367278082
          }
        }};
        let v_r_40154030 = run {v_y_40234028 match {
          case Tip2761 { () => 
            0
          }
          case Bin2750 { (v_y_367278091: Int398, v_y_367388092: A2886, v_y_367498093: B2887, v_y_3675108094: Map2616[A2886, B2887], v_y_3676118095: Map2616[A2886, B2887]) => 
            v_y_367278091
          }
        }}
        
        if (infixLt175(v_r_40144029, infixMul96(ratio2834, v_r_40154030))) {
          r2891 match {
            case Bin2750 { (v_y_392178390: Int398, v_y_392288391: A2886, v_y_392398392: B2887, v_y_3924108393: Map2616[A2886, B2887], v_y_3925118394: Map2616[A2886, B2887]) => 
              let v_r_39155128395 = run {let v_r_3913128972 = run {l2890 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_3672778962: Int398, v_y_3673888963: A2886, v_y_3674998964: B2887, v_y_367510108965: Map2616[A2886, B2887], v_y_367611118966: Map2616[A2886, B2887]) => 
                  v_y_3672778962
                }
              }};
              let v_r_3914188973 = run {v_y_3924108393 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_36727138967: Int398, v_y_36738148968: A2886, v_y_36749158969: B2887, v_y_367510168970: Map2616[A2886, B2887], v_y_367611178971: Map2616[A2886, B2887]) => 
                  v_y_36727138967
                }
              }}
              
              make Map2616[A2886, B2887] Bin2750(infixAdd93(infixAdd93(v_r_3913128972, v_r_3914188973), 1), k2888, v2889, l2890, v_y_3924108393)};
              let v_r_3913128990 = run {v_r_39155128395 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_3672778980: Int398, v_y_3673888981: A2886, v_y_3674998982: B2887, v_y_367510108983: Map2616[A2886, B2887], v_y_367611118984: Map2616[A2886, B2887]) => 
                  v_y_3672778980
                }
              }};
              let v_r_3914188991 = run {v_y_3925118394 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_36727138985: Int398, v_y_36738148986: A2886, v_y_36749158987: B2887, v_y_367510168988: Map2616[A2886, B2887], v_y_367611178989: Map2616[A2886, B2887]) => 
                  v_y_36727138985
                }
              }}
              
              make Map2616[A2886, B2887] Bin2750(infixAdd93(infixAdd93(v_r_3913128990, v_r_3914188991), 1), v_y_392288391, v_y_392398392, v_r_39155128395, v_y_3925118394)
            }
          } else {
            let v_r_39191138396 = panic546[Map2616[A2886, B2887]]("impossible: singleL: Tip")
            
            v_r_39191138396}
        } else {
          doubleL2864[A2886, B2887](k2888, v2889, l2890, r2891)
        }
      }
    } else {
      doubleL2864[A2886, B2887](k2888, v2889, l2890, r2891)};
  def rotateR2901[A2895, B2896](k2897: A2895, v2898: B2896, l2899: Map2616[A2895, B2896], r2900: Map2616[A2895, B2896]) =
    l2899 match {
      case Bin2750 { (v_y_40434049: Int398, v_y_40444048: A2895, v_y_40454050: B2896, v_y_40464051: Map2616[A2895, B2896], v_y_40474052: Map2616[A2895, B2896]) => 
        let v_r_40384053 = run {v_y_40474052 match {
          case Tip2761 { () => 
            0
          }
          case Bin2750 { (v_y_367278102: Int398, v_y_367388103: A2895, v_y_367498104: B2896, v_y_3675108105: Map2616[A2895, B2896], v_y_3676118106: Map2616[A2895, B2896]) => 
            v_y_367278102
          }
        }};
        let v_r_40394054 = run {v_y_40464051 match {
          case Tip2761 { () => 
            0
          }
          case Bin2750 { (v_y_367278111: Int398, v_y_367388112: A2895, v_y_367498113: B2896, v_y_3675108114: Map2616[A2895, B2896], v_y_3676118115: Map2616[A2895, B2896]) => 
            v_y_367278111
          }
        }}
        
        if (infixLt175(v_r_40384053, infixMul96(ratio2834, v_r_40394054))) {
          l2899 match {
            case Bin2750 { (v_y_393978403: Int398, v_y_394088404: A2895, v_y_394198405: B2896, v_y_3942108406: Map2616[A2895, B2896], v_y_3943118407: Map2616[A2895, B2896]) => 
              let v_r_39335128408 = run {let v_r_3913129008 = run {v_y_3943118407 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_3672778998: Int398, v_y_3673888999: A2895, v_y_3674999000: B2896, v_y_367510109001: Map2616[A2895, B2896], v_y_367611119002: Map2616[A2895, B2896]) => 
                  v_y_3672778998
                }
              }};
              let v_r_3914189009 = run {r2900 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_36727139003: Int398, v_y_36738149004: A2895, v_y_36749159005: B2896, v_y_367510169006: Map2616[A2895, B2896], v_y_367611179007: Map2616[A2895, B2896]) => 
                  v_y_36727139003
                }
              }}
              
              make Map2616[A2895, B2896] Bin2750(infixAdd93(infixAdd93(v_r_3913129008, v_r_3914189009), 1), k2897, v2898, v_y_3943118407, r2900)};
              let v_r_3913129026 = run {v_y_3942108406 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_3672779016: Int398, v_y_3673889017: A2895, v_y_3674999018: B2896, v_y_367510109019: Map2616[A2895, B2896], v_y_367611119020: Map2616[A2895, B2896]) => 
                  v_y_3672779016
                }
              }};
              let v_r_3914189027 = run {v_r_39335128408 match {
                case Tip2761 { () => 
                  0
                }
                case Bin2750 { (v_y_36727139021: Int398, v_y_36738149022: A2895, v_y_36749159023: B2896, v_y_367510169024: Map2616[A2895, B2896], v_y_367611179025: Map2616[A2895, B2896]) => 
                  v_y_36727139021
                }
              }}
              
              make Map2616[A2895, B2896] Bin2750(infixAdd93(infixAdd93(v_r_3913129026, v_r_3914189027), 1), v_y_394088404, v_y_394198405, v_y_3942108406, v_r_39335128408)
            }
          } else {
            let v_r_39371138409 = panic546[Map2616[A2895, B2896]]("impossible: singleR: Tip")
            
            v_r_39371138409}
        } else {
          doubleR2878[A2895, B2896](k2897, v2898, l2899, r2900)
        }
      }
    } else {
      doubleR2878[A2895, B2896](k2897, v2898, l2899, r2900)};
  let sizeL2904 = run {l2675 match {
    case Tip2761 { () => 
      0
    }
    case Bin2750 { (v_y_367278122: Int398, v_y_367388123: K2671, v_y_367498124: V2672, v_y_3675108125: Map2616[K2671, V2672], v_y_3676118126: Map2616[K2671, V2672]) => 
      v_y_367278122
    }
  }};
  let sizeR2905 = run {r2676 match {
    case Tip2761 { () => 
      0
    }
    case Bin2750 { (v_y_367278131: Int398, v_y_367388132: K2671, v_y_367498133: V2672, v_y_3675108134: Map2616[K2671, V2672], v_y_3676118135: Map2616[K2671, V2672]) => 
      v_y_367278131
    }
  }};
  let sizeCombined2906 = infixAdd93(infixAdd93(sizeL2904, sizeR2905), 1)
  
  if (infixLte178(infixAdd93(sizeL2904, sizeR2905), 1)) {
    make Map2616[K2671, V2672] Bin2750(sizeCombined2906, k2673, v2674, l2675, r2676)
  } else {
    if (infixGt181(sizeR2905, infixMul96(delta2835, sizeL2904))) {
      rotateL2892[K2671, V2672](k2673, v2674, l2675, r2676)
    } else {
      if (infixGt181(sizeL2904, infixMul96(delta2835, sizeR2905))) {
        rotateR2901[K2671, V2672](k2673, v2674, l2675, r2676)
      } else {
        make Map2616[K2671, V2672] Bin2750(sizeCombined2906, k2673, v2674, l2675, r2676)
      }
    }
  };
def link2709[K2703, V2704](k2705: K2703, v2706: V2704, l7670: Map2616[K2703, V2704], r7671: Map2616[K2703, V2704]) =
  def link_worker7667(l2707: Map2616[K2703, V2704], r2708: Map2616[K2703, V2704]) =
    let v_r_41875584 = make Tuple2249[Map2616[K2703, V2704], Map2616[K2703, V2704]] Tuple2330(l2707, r2708)
    
    v_r_41875584 match {
      case Tuple2330 { (v_y_42074210: Map2616[K2703, V2704], v_y_42084209: Map2616[K2703, V2704]) => 
        v_y_42074210 match {
          case Tip2761 { () => 
            def putMin_worker68794(m78431: Map2616[K2703, V2704]) =
              m78431 match {
                case Tip2761 { () => 
                  make Map2616[K2703, V2704] Bin2750(1, k2705, v2706, make Map2616[K2703, V2704] Tip2761(), make Map2616[K2703, V2704] Tip2761())
                }
                case Bin2750 { (v_y_424088432: Int398, v_y_424198433: K2703, v_y_4242108434: V2704, v_y_4243118435: Map2616[K2703, V2704], v_y_4244128436: Map2616[K2703, V2704]) => 
                  let v_r_42365138437 = run {putMin_worker68794(v_y_4243118435)}
                  
                  balance2677[K2703, V2704](v_y_424198433, v_y_4242108434, v_r_42365138437, v_y_4244128436)
                }
              }
            
            putMin_worker68794(v_y_42084209)
          }
          case Bin2750 { (v_y_42114217: Int398, v_y_42124216: K2703, v_y_42134218: V2704, v_y_42144219: Map2616[K2703, V2704], v_y_42154220: Map2616[K2703, V2704]) => 
            v_y_42084209 match {
              case Tip2761 { () => 
                def putMax_worker68795(m78443: Map2616[K2703, V2704]) =
                  m78443 match {
                    case Tip2761 { () => 
                      make Map2616[K2703, V2704] Bin2750(1, k2705, v2706, make Map2616[K2703, V2704] Tip2761(), make Map2616[K2703, V2704] Tip2761())
                    }
                    case Bin2750 { (v_y_425988444: Int398, v_y_426098445: K2703, v_y_4261108446: V2704, v_y_4262118447: Map2616[K2703, V2704], v_y_4263128448: Map2616[K2703, V2704]) => 
                      let v_r_42555138449 = run {putMax_worker68795(v_y_4263128448)}
                      
                      balance2677[K2703, V2704](v_y_426098445, v_y_4261108446, v_y_4262118447, v_r_42555138449)
                    }
                  }
                
                putMax_worker68795(v_y_42074210)
              }
              case Bin2750 { (v_y_42214227: Int398, v_y_42224226: K2703, v_y_42234228: V2704, v_y_42244229: Map2616[K2703, V2704], v_y_42254230: Map2616[K2703, V2704]) => 
                if (infixLt175(infixMul96(delta2835, v_y_42114217), v_y_42214227)) {
                  let v_r_4194118460 = run {link_worker7667(l2707, v_y_42244229)}
                  
                  balance2677[K2703, V2704](v_y_42224226, v_y_42234228, v_r_4194118460, v_y_42254230)
                } else {
                  if (infixLt175(infixMul96(delta2835, v_y_42214227), v_y_42114217)) {
                    let v_r_4197128461 = run {link_worker7667(v_y_42154220, r2708)}
                    
                    balance2677[K2703, V2704](v_y_42124216, v_y_42134218, v_y_42144219, v_r_4197128461)
                  } else {
                    let v_r_39137138462 = run {l2707 match {
                      case Tip2761 { () => 
                        0
                      }
                      case Bin2750 { (v_y_367249038: Int398, v_y_367359040: K2703, v_y_367469041: V2704, v_y_367579042: Map2616[K2703, V2704], v_y_367689043: Map2616[K2703, V2704]) => 
                        v_y_367249038
                      }
                    }};
                    let v_r_39148148463 = run {r2708 match {
                      case Tip2761 { () => 
                        0
                      }
                      case Bin2750 { (v_y_367249046: Int398, v_y_367359048: K2703, v_y_367469049: V2704, v_y_367579050: Map2616[K2703, V2704], v_y_367689051: Map2616[K2703, V2704]) => 
                        v_y_367249046
                      }
                    }}
                    
                    make Map2616[K2703, V2704] Bin2750(infixAdd93(infixAdd93(v_r_39137138462, v_r_39148148463), 1), k2705, v2706, l2707, r2708)
                  }
                }
              }
            }
          }
        } else {
          def putMax_worker68796(m78469: Map2616[K2703, V2704]) =
            m78469 match {
              case Tip2761 { () => 
                make Map2616[K2703, V2704] Bin2750(1, k2705, v2706, make Map2616[K2703, V2704] Tip2761(), make Map2616[K2703, V2704] Tip2761())
              }
              case Bin2750 { (v_y_425988470: Int398, v_y_426098471: K2703, v_y_4261108472: V2704, v_y_4262118473: Map2616[K2703, V2704], v_y_4263128474: Map2616[K2703, V2704]) => 
                let v_r_42555138475 = run {putMax_worker68796(v_y_4263128474)}
                
                balance2677[K2703, V2704](v_y_426098471, v_y_4261108472, v_y_4262118473, v_r_42555138475)
              }
            }
          
          putMax_worker68796(v_y_42074210)}
      }
    }
  
  link_worker7667(l7670, r7671);
def main2749() =
  let m2991 = run {def create40328823(level41338688: Int398, pairs42348689: List910[Tuple2249[Int398, Char390]]) =
    pairs42348689 match {
      case Nil1114 { () => 
        make Tuple3253[Map2616[Int398, Char390], List910[Tuple2249[Int398, Char390]], List910[Tuple2249[Int398, Char390]]] Tuple3335(make Map2616[Int398, Char390] Tip2761(), make List910[Tuple2249[Int398, Char390]] Nil1114(), make List910[Tuple2249[Int398, Char390]] Nil1114())
      }
      case Cons1115 { (v_y_384381738722: Tuple2249[Int398, Char390], v_y_384482748723: List910[Tuple2249[Int398, Char390]]) => 
        v_y_384381738722 match {
          case Tuple2330 { (v_y_384783758724: Int398, v_y_384884768725: Char390) => 
            if (infixEq69(level41338688, 1)) {
              let singleton484049131 = make Map2616[Int398, Char390] Bin2750(1, v_y_384783758724, v_y_384884768725, make Map2616[Int398, Char390] Tip2761(), make Map2616[Int398, Char390] Tip2761());
              let v_r_3801494159132 = run {v_y_384482748723 match {
                case Nil1114 { () => 
                  false
                }
                case Cons1115 { (v_y_3774221439297: Tuple2249[Int398, Char390], v_y_3775231549298: List910[Tuple2249[Int398, Char390]]) => 
                  v_y_3774221439297 match {
                    case Tuple2330 { (v_y_3778241659299: Int398, v_y_3779251769300: Char390) => 
                      let v_r_37671810279301 = run {let v_r_541149461 = genericCompareImpl47[Int398](v_y_384783758724, v_y_3778241659299)
                      
                      if (infixEq69(v_r_541149461, -1)) {
                        make Ordering43 Less321()
                      } else {
                        if (infixEq69(v_r_541149461, 0)) {
                          make Ordering43 Equal322()
                        } else {
                          if (infixEq69(v_r_541149461, 1)) {
                            make Ordering43 Greater323()
                          } else {
                            <>
                          }
                        }
                      }}
                      
                      v_r_37671810279301 match {
                        case Less321 { () => 
                          false
                        }
                        case Greater323 { () => 
                          true
                        }
                        case Equal322 { () => 
                          true
                        }
                      }
                    }
                  }
                }
              }}
              
              if (v_r_3801494159132) {
                make Tuple3253[Map2616[Int398, Char390], List910[Tuple2249[Int398, Char390]], List910[Tuple2249[Int398, Char390]]] Tuple3335(singleton484049131, make List910[Tuple2249[Int398, Char390]] Nil1114(), v_y_384482748723)
              } else {
                make Tuple3253[Map2616[Int398, Char390], List910[Tuple2249[Int398, Char390]], List910[Tuple2249[Int398, Char390]]] Tuple3335(singleton484049131, v_y_384482748723, make List910[Tuple2249[Int398, Char390]] Nil1114())
              }
            } else {
              let res504269133 = run {create40328823(bitwiseShr228(level41338688, 1), pairs42348689)}
              
              res504269133 match {
                case Tuple3335 { (v_y_38247466309153: Map2616[Int398, Char390], v_y_38257567319154: List910[Tuple2249[Int398, Char390]], v_y_38267668329155: List910[Tuple2249[Int398, Char390]]) => 
                  v_y_38257567319154 match {
                    case Nil1114 { () => 
                      res504269133
                    }
                    case Cons1115 { (v_y_38307769339156: Tuple2249[Int398, Char390], v_y_38317870349157: List910[Tuple2249[Int398, Char390]]) => 
                      v_y_38307769339156 match {
                        case Tuple2330 { (v_y_38347971359158: Int398, v_y_38358072369159: Char390) => 
                          v_y_38317870349157 match {
                            case Nil1114 { () => 
                              let v_r_380757491359329 = run {def putMax_worker69522(m79467: Map2616[Int398, Char390]) =
                                m79467 match {
                                  case Tip2761 { () => 
                                    make Map2616[Int398, Char390] Bin2750(1, v_y_38347971359158, v_y_38358072369159, make Map2616[Int398, Char390] Tip2761(), make Map2616[Int398, Char390] Tip2761())
                                  }
                                  case Bin2750 { (v_y_425989468: Int398, v_y_426099469: Int398, v_y_4261109470: Char390, v_y_4262119471: Map2616[Int398, Char390], v_y_4263129472: Map2616[Int398, Char390]) => 
                                    let v_r_42555139473 = run {putMax_worker69522(v_y_4263129472)}
                                    
                                    balance2677[Int398, Char390](v_y_426099469, v_y_4261109470, v_y_4262119471, v_r_42555139473)
                                  }
                                }
                              
                              putMax_worker69522(v_y_38247466309153)}
                              
                              make Tuple3253[Map2616[Int398, Char390], List910[Tuple2249[Int398, Char390]], List910[Tuple2249[Int398, Char390]]] Tuple3335(v_r_380757491359329, make List910[Tuple2249[Int398, Char390]] Nil1114(), v_y_38267668329155)
                            }
                          } else {
                            let xs63551959334 = make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(v_y_38347971359158, v_y_38358072369159), v_y_38317870349157);
                            let v_r_380964562069335 = run {v_y_38317870349157 match {
                              case Nil1114 { () => 
                                false
                              }
                              case Cons1115 { (v_y_3774221439476: Tuple2249[Int398, Char390], v_y_3775231549477: List910[Tuple2249[Int398, Char390]]) => 
                                v_y_3774221439476 match {
                                  case Tuple2330 { (v_y_3778241659478: Int398, v_y_3779251769479: Char390) => 
                                    let v_r_37671810289481 = run {let v_r_5411479480 = genericCompareImpl47[Int398](v_y_38347971359158, v_y_3778241659478)
                                    
                                    if (infixEq69(v_r_5411479480, -1)) {
                                      make Ordering43 Less321()
                                    } else {
                                      if (infixEq69(v_r_5411479480, 0)) {
                                        make Ordering43 Equal322()
                                      } else {
                                        if (infixEq69(v_r_5411479480, 1)) {
                                          make Ordering43 Greater323()
                                        } else {
                                          <>
                                        }
                                      }
                                    }}
                                    
                                    v_r_37671810289481 match {
                                      case Less321 { () => 
                                        false
                                      }
                                      case Greater323 { () => 
                                        true
                                      }
                                      case Equal322 { () => 
                                        true
                                      }
                                    }
                                  }
                                }
                              }
                            }}
                            
                            if (v_r_380964562069335) {
                              make Tuple3253[Map2616[Int398, Char390], List910[Tuple2249[Int398, Char390]], List910[Tuple2249[Int398, Char390]]] Tuple3335(v_y_38247466309153, make List910[Tuple2249[Int398, Char390]] Nil1114(), xs63551959334)
                            } else {
                              let v_r_381065572179340 = run {create40328823(bitwiseShr228(level41338688, 1), v_y_38317870349157)}
                              
                              v_r_381065572179340 match {
                                case Tuple3335 { (v_y_3813716327139341: Map2616[Int398, Char390], v_y_3814726428149342: List910[Tuple2249[Int398, Char390]], v_y_3815736529159343: List910[Tuple2249[Int398, Char390]]) => 
                                  let v_r_38117062261249485 = run {link2709[Int398, Char390](v_y_38347971359158, v_y_38358072369159, v_y_38247466309153, v_y_3813716327139341)}
                                  
                                  make Tuple3253[Map2616[Int398, Char390], List910[Tuple2249[Int398, Char390]], List910[Tuple2249[Int398, Char390]]] Tuple3335(v_r_38117062261249485, v_y_3814726428149342, v_y_3815736529159343)
                                }
                              }
                            }}
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    };
  def go85778830(level86788726: Int398, m87798727: Map2616[Int398, Char390], pairs88808728: List910[Tuple2249[Int398, Char390]]) =
    pairs88808728 match {
      case Nil1114 { () => 
        m87798727
      }
      case Cons1115 { (v_y_38831131058747: Tuple2249[Int398, Char390], v_y_38841141068748: List910[Tuple2249[Int398, Char390]]) => 
        v_y_38831131058747 match {
          case Tuple2330 { (v_y_38871151078749: Int398, v_y_38881161088750: Char390) => 
            v_y_38841141068748 match {
              case Nil1114 { () => 
                def putMax_worker69440(m79349: Map2616[Int398, Char390]) =
                  m79349 match {
                    case Tip2761 { () => 
                      make Map2616[Int398, Char390] Bin2750(1, v_y_38871151078749, v_y_38881161088750, make Map2616[Int398, Char390] Tip2761(), make Map2616[Int398, Char390] Tip2761())
                    }
                    case Bin2750 { (v_y_425989350: Int398, v_y_426099351: Int398, v_y_4261109352: Char390, v_y_4262119353: Map2616[Int398, Char390], v_y_4263129354: Map2616[Int398, Char390]) => 
                      let v_r_42555139355 = run {putMax_worker69440(v_y_4263129354)}
                      
                      balance2677[Int398, Char390](v_y_426099351, v_y_4261109352, v_y_4262119353, v_r_42555139355)
                    }
                  }
                
                putMax_worker69440(m87798727)
              }
            } else {
              let v_r_3857978949205 = run {v_y_38841141068748 match {
                case Nil1114 { () => 
                  false
                }
                case Cons1115 { (v_y_3774221439358: Tuple2249[Int398, Char390], v_y_3775231549359: List910[Tuple2249[Int398, Char390]]) => 
                  v_y_3774221439358 match {
                    case Tuple2330 { (v_y_3778241659360: Int398, v_y_3779251769361: Char390) => 
                      let v_r_37671810279362 = run {let v_r_541149489 = genericCompareImpl47[Int398](v_y_38871151078749, v_y_3778241659360)
                      
                      if (infixEq69(v_r_541149489, -1)) {
                        make Ordering43 Less321()
                      } else {
                        if (infixEq69(v_r_541149489, 0)) {
                          make Ordering43 Equal322()
                        } else {
                          if (infixEq69(v_r_541149489, 1)) {
                            make Ordering43 Greater323()
                          } else {
                            <>
                          }
                        }
                      }}
                      
                      v_r_37671810279362 match {
                        case Less321 { () => 
                          false
                        }
                        case Greater323 { () => 
                          true
                        }
                        case Equal322 { () => 
                          true
                        }
                      }
                    }
                  }
                }
              }}
              
              if (v_r_3857978949205) {
                var mapSoFar302239445 = m87798727 ;
                def foreach_worker5109446(l6119370: List910[Tuple2249[Int398, Char390]]) =
                  l6119370 match {
                    case Nil1114 { () => 
                      ()
                    }
                    case Cons1115 { (v_y_47927129371: Tuple2249[Int398, Char390], v_y_47938139372: List910[Tuple2249[Int398, Char390]]) => 
                      val _39149373: Unit394 = v_y_47927129371 match {
                        case Tuple2330 { (v_y_37903729629490: Int398, v_y_37913830739492: Char390) => 
                          val v_r_378535273849493: Map2616[Int398, Char390] = !mapSoFar302239445;
                          let v_r_378636284959494 = run {put2642[Int398, Char390](v_r_378535273849493, v_y_37903729629490, v_y_37913830739492)}
                          
                          mapSoFar302239445 := v_r_378636284959494
                        }
                      };
                      foreach_worker5109446(v_y_47938139372)
                    }
                  }
                
                val _3931159374: Unit394 = foreach_worker5109446(pairs88808728);
                !mapSoFar302239445
              } else {
                let cr999169214 = run {create40328823(level86788726, v_y_38841141068748)}
                
                cr999169214 match {
                  case Tuple3335 { (v_y_3872110102179215: Map2616[Int398, Char390], v_y_3873111103189216: List910[Tuple2249[Int398, Char390]], v_y_3874112104199217: List910[Tuple2249[Int398, Char390]]) => 
                    v_y_3874112104199217 match {
                      case Nil1114 { () => 
                        let v_r_3862103951039389 = run {link2709[Int398, Char390](v_y_38871151078749, v_y_38881161088750, l989059208, v_y_3872110102179215)} // HERE
                        
                        go85778830(bitwiseShl225(level86788726, 1), v_r_3862103951039389, v_y_3873111103189216)
                      }
                    } else {
                      v_y_3873111103189216 match {
                        case Nil1114 { () => 
                          let v_r_3866107991439392 = run {link2709[Int398, Char390](v_y_38871151078749, v_y_38881161088750, l989059208, v_y_3872110102179215)} // and HERE
                          
                          var mapSoFar302239524 = v_r_3866107991439392 ;
                          def foreach_worker549523(l659497: List910[Tuple2249[Int398, Char390]]) =
                            l659497 match {
                              case Nil1114 { () => 
                                ()
                              }
                              case Cons1115 { (v_y_4792769498: Tuple2249[Int398, Char390], v_y_4793879499: List910[Tuple2249[Int398, Char390]]) => 
                                val _39129504: Unit394 = v_y_4792769498 match {
                                  case Tuple2330 { (v_y_37903729289500: Int398, v_y_37913830399501: Char390) => 
                                    val v_r_3785352734109502: Map2616[Int398, Char390] = !mapSoFar302239524;
                                    let v_r_3786362845119503 = run {put2642[Int398, Char390](v_r_3785352734109502, v_y_37903729289500, v_y_37913830399501)}
                                    
                                    mapSoFar302239524 := v_r_3786362845119503
                                  }
                                };
                                foreach_worker549523(v_y_4793879499)
                              }
                            }
                          
                          val _3931139505: Unit394 = foreach_worker549523(v_y_3874112104199217);
                          !mapSoFar302239524
                        }
                      } else {
                        let v_r_38701091011619450 = panic546[Map2616[Int398, Char390]]("create: go: cannot happen, invariant broken!")
                        
                        v_r_38701091011619450}}
                  }
                } else {
                  let v_r_38701091011619451 = panic546[Map2616[Int398, Char390]]("create: go: cannot happen, invariant broken!")
                  
                  v_r_38701091011619451}
              }}
          }
        }
      }
    };
  let v_r_38931171098751 = run {let v_r_37671810279399 = run {let v_r_541149509 = genericCompareImpl47[Int398](1, 2)
  
  if (infixEq69(v_r_541149509, -1)) {
    make Ordering43 Less321()
  } else {
    if (infixEq69(v_r_541149509, 0)) {
      make Ordering43 Equal322()
    } else {
      if (infixEq69(v_r_541149509, 1)) {
        make Ordering43 Greater323()
      } else {
        <>
      }
    }
  }}
  
  v_r_37671810279399 match {
    case Less321 { () => 
      false
    }
    case Greater323 { () => 
      true
    }
    case Equal322 { () => 
      true
    }
  }}
  
  if (v_r_38931171098751) {
    let v_r_38941181108752 = make Map2616[Int398, Char390] Bin2750(1, 1, 72, make Map2616[Int398, Char390] Tip2761(), make Map2616[Int398, Char390] Tip2761())
    
    var mapSoFar302249281 = v_r_38941181108752 ;
    def foreach_worker59410(l69405: List910[Tuple2249[Int398, Char390]]) =
      l69405 match {
        case Nil1114 { () => 
          ()
        }
        case Cons1115 { (v_y_479279407: Tuple2249[Int398, Char390], v_y_479389408: List910[Tuple2249[Int398, Char390]]) => 
          val _399409: Unit394 = v_y_479279407 match {
            case Tuple2330 { (v_y_379037291129510: Int398, v_y_379138301239512: Char390) => 
              val v_r_378535279349513: Map2616[Int398, Char390] = !mapSoFar302249281;
              let v_r_3786362810459514 = run {put2642[Int398, Char390](v_r_378535279349513, v_y_379037291129510, v_y_379138301239512)}
              
              mapSoFar302249281 := v_r_3786362810459514
            }
          };
          foreach_worker59410(v_y_479389408)
        }
      }
    
    val _3931139232: Unit394 = foreach_worker59410(make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(2, 101), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(3, 108), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(4, 108), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(5, 111), make List910[Tuple2249[Int398, Char390]] Nil1114())))));
    !mapSoFar302249281
  } else {
    let v_r_38971191118753 = make Map2616[Int398, Char390] Bin2750(1, 1, 72, make Map2616[Int398, Char390] Tip2761(), make Map2616[Int398, Char390] Tip2761())
    
    go85778830(1, v_r_38971191118753, make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(2, 101), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(3, 108), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(4, 108), make List910[Tuple2249[Int398, Char390]] Cons1115(make Tuple2249[Int398, Char390] Tuple2330(5, 111), make List910[Tuple2249[Int398, Char390]] Nil1114())))))
  }};
  def go69428(m79421: Map2616[Int398, Char390]) =
    m79421 match {
      case Tip2761 { () => 
        ()
      }
      case Bin2750 { (v_y_372189423: Int398, v_y_372299424: Int398, v_y_3723109425: Char390, v_y_3724119426: Map2616[Int398, Char390], v_y_3725129427: Map2616[Int398, Char390]) => 
        val _5139430: Unit394 = go69428(v_y_3724119426);
        let v_r_4319344679520 = println1(infixConcat35(infixConcat35(infixConcat35(infixConcat35(infixConcat35(show14(v_y_372299424), ": "), show24(v_y_3723109425)), " ("), show14(toInt2323(v_y_3723109425))), ")"))
        
        go69428(v_y_3725129427)
      }
    }
  
  val _69527: Any395 = go69428(m2991);
  ()

@b-studios
Copy link
Collaborator

Wouldn't --ir-show core --no-optimize work?

@jiribenes
Copy link
Contributor Author

Wouldn't --ir-show core --no-optimize work?

It does, but it's slightly more work and doesn't work directly in VSCode :/
Also, it doesn't work with --ir-write-all to dump both the optimised and unoptimised core...

@jiribenes jiribenes changed the title [WIP] Immutable ordered finite maps and sets Add immutable ordered finite 'map' and 'set' to the stdlib Nov 26, 2024
@marvinborner
Copy link
Member

After a much too long debugging session, I found that this is a rare inline error that can be fixed by uncommenting the rewrite in

case Definition.Def(id, block) => block //rewrite(block)
...

(as the comment above suggests, though, this may come with additional difficulties)

@marvinborner marvinborner mentioned this pull request Dec 3, 2024
@jiribenes jiribenes force-pushed the feature/stdlib/treemap-treeset branch from d2a31e1 to fd5b9b3 Compare December 13, 2024 19:52
@jiribenes jiribenes added the blocked Blocked on other issues / PRs label Dec 13, 2024
Comment on lines +164 to +167
def compareString(left: String, right: String) =
if (left == right) Equal()
else if (left < right) Less()
else Greater()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work on LLVM, hence #748. Moreover, it's probably worth it to have a few of these compare functions prepared for more types than just Ints and Strings, but that's future work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:stdlib blocked Blocked on other issues / PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants