/*---------------------------------------------------------------------------
  Copyright 2012-2021, Microsoft Research, Daan Leijen.

  This is free software; you can redistribute it and/or modify it under the
  terms of the Apache License, Version 2.0. A copy of the License can be
  found in the LICENSE file at the root of this distribution.
---------------------------------------------------------------------------*/

/* Core functions.

   This module is implicitly imported and all functions and types
   are always available.
   Some types and operations are required to be defined for the compiler
   to work correctly (i.e. types like `:exn` or `:list`)
*/
public module core

public import types
public import hnd

infixr 80  (^)
infixl 70  (*), (%), (/), cdiv, cmod
infixr 60  (++)
infixl 60  (+), (-)
infix  40  (!=), (==), (<=), (>=), (<), (>)

// prefix     (!), (-) 

extern import {
  c  file "core/core-inline"
  cs file "core/core-inline.cs"
  js file "core/core-inline.js"
}

extern import {
  js file "core/core-integer-inline.js"
}
extern import {
  js file "core/core-string-inline.js"
}
extern import {
  js file "core/core-console-inline.js"
}


// ----------------------------------------------------------------------------
// Builtin effects
// ----------------------------------------------------------------------------

// An alias for the empty effect.
alias totalstd/core/total: E = <std/core/types/(<>): E>

// The console effect signifies that a function may write to the console.
type consolestd/core/console: X :: X

// An alias for pure effects: a pure function always returns the same result
// when called with the same arguments but may not terminate or raise an exception.
alias purestd/core/pure: E = <std/core/types/(<>): Eexnstd/core/exn: HX,divstd/core/types/div: X>

// The `:global-scope` is a special type constant to denote the global scope
type global-scopestd/core/global-scope: S :: S

// The `:net` effect signifies a function may access the network
type netstd/core/net: X :: X

// The `:fsys` effect signifies a function may access the file system
type fsysstd/core/fsys: X :: X

// The `:ui` effect signifies a function may access the graphics system
type uistd/core/ui: X :: X

// The `:blocking` effect signifies that a function may block
type blockingstd/core/blocking: X :: X

// The `:io-total` effect is used for functions that perform arbitrary I/O operations, but are terminating without raising exceptions.
alias io-totalstd/core/io-total: E = <std/core/types/(<>): Endetstd/core/types/ndet: X,consolestd/core/console: X,netstd/core/net: X,fsysstd/core/fsys: X,uistd/core/ui: X,ststd/core/types/st: H -> E<globalstd/core/types/global: H>>

// The `:io-noexn` effect is used for functions that perform arbitrary I/O operations, but raise no exceptions
alias io-noexnstd/core/io-noexn: E = <std/core/types/(<>): Edivstd/core/types/div: X,io-totalstd/core/io-total: E>

// The `:io` effect is used for functions that perform arbitrary I/O operations.
alias iostd/core/io: E = <std/core/types/(<>): Eexnstd/core/exn: HX,io-noexnstd/core/io-noexn: E>

// ----------------------------------------------------------------------------
// Masking
// ----------------------------------------------------------------------------

// Add the state effect to a function effect.
// inline extern inject-st : forall<a,b,h,e> ((a) -> e b) -> total ((a) -> <st<h>|e> b)   { inline "#1" }

// Add the state effect to a function effect.
inline extern inject-ststd/core/inject-st: forall<a,h,e> (() -> e a) -> total (() -> <st<h>|e> a) : forall<aa: V,hh: H,ee: E> (() -> ee: E aa: V) -> totalstd/core/total: E (() -> <ststd/core/types/st: H -> E<hh: H>|ee: E> aa: V)  { inline "#1" }


// ----------------------------------------------------------------------------
// Standard Functions
// ----------------------------------------------------------------------------


// The `const` funs returns its first argument and ignores the second.
fun conststd/core/const: forall<a,b> (x : a, y : b) -> a(xx: _2261,yy: _2262) {
  xx: _2261;
}

// Apply a function `f` to a specified argument `x`.
fun applystd/core/apply: forall<a,b,e> (f : (a) -> e b, x : a) -> e b(ff: (_2151) -> _2152 _2153,xx: _2151) {
  ff: (_2151) -> _2152 _2153(xx: _2151);
}

// Compose two funs `f` and `g`.
fun ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b)(ff: (_2778) -> _2775 _2776,gg: (_2774) -> _2775 _2778) {
  (fn(xx: _2774){ ff: (_2778) -> _2775 _2776(gg: (_2774) -> _2775 _2778(xx: _2774)) })
}

// The `ignore` function ignores its argument.
fun ignorestd/core/ignore: forall<a> (x : a) -> ()( xx: $2579 : aa: V ) : (std/core/types/(): V)std/core/types/(): V {
  (std/core/types/(): ())std/core/types/(): ()
}

// Return a 'constant' function that ignores its argument and always returns the same result
fun conststd/core/const.1: forall<a,b> (default : a) -> total ((x : b) -> a)( defaultdefault: $2267 : aa: V ) : totalstd/core/total: E (( x : bb: V ) -> astd/core/types/(<>): E) {
  (fn(_){ defaultdefault: $2267 })
}


// ----------------------------------------------------------------------------
// Standard Data types
// ----------------------------------------------------------------------------


fun intstd/core/int.7: (b : bool) -> int( bb: bool : boolstd/core/types/bool: V ) : intstd/core/types/int: V  { if (std/core/types/True: boolbb: bool) then 1 else 0 }

fun mbintstd/core/mbint: (m : maybe<int>) -> int( mm: maybe<int> : maybestd/core/types/maybe: V -> V<intstd/core/types/int: V> ) : intstd/core/types/int: V {
  match(mm: maybe<int>) {
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> 0
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ii: int) -> ii: int
  }
}

// Convert an int to a boolean, using `False` for 0 and `True` otherwise.
fun boolstd/core/bool: (i : int) -> bool( ii: int : intstd/core/types/int: V ) : boolstd/core/types/bool: V { ii: int!=std/core/(!=).1: (x : int, y : int) -> bool0 }

// Convert a `:maybe` type to a boolean using `False` for `Nothing` and `True` for `Just`.
fun boolstd/core/bool.1: forall<a> (m : maybe<a>) -> bool( mm: maybe<$10122> : maybestd/core/types/maybe: V -> V<aa: V> ) : boolstd/core/types/bool: V {
  match(mm: maybe<$10122>) {
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Falsestd/core/types/False: bool
    _       -> Truestd/core/types/True: bool
  }
}

// Convert a string to a boolean, using `False` for the empty string and `True` otherwise.
fun boolstd/core/bool.2: (s : string) -> bool( ss: string : stringstd/core/types/string: V ) : boolstd/core/types/bool: V { ss: string!=std/core/(!=).3: (string, string) -> bool"" }

// Match a `:maybe` value and either return a default value on `Nothing` or apply a function to the value on `Just`
fun maybestd/core/maybe: forall<a,b,e> (m : maybe<a>, onNothing : b, onJust : (a) -> e b) -> e b( mm: maybe<$14512> : maybestd/core/types/maybe: V -> V<aa: V>, onNothingonNothing: $14513: bb: V, onJustonJust: ($14512) -> $14514 $14513: aa: V -> ee: E bb: V ) : ee: E bb: V
{
  match(mm: maybe<$14512>) {
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> onNothingonNothing: $14513
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $14512) -> onJustonJust: ($14512) -> $14514 $14513(xx: $14512)
  }
}

// Convert a `:maybe<a>` value to `:a`, using the `nothing` parameter for `Nothing`.
// This is an alias for `default`.
fun maybestd/core/maybe.1: forall<a> (m : maybe<a>, nothing : a) -> a( mm: maybe<$14535> : maybestd/core/types/maybe: V -> V<aa: V>, nothingnothing: $14535 : aa: V ) : astd/core/types/(<>): E {
  defaultstd/core/default: forall<a> (m : maybe<a>, nothing : a) -> a(mm: maybe<$14535>,nothingnothing: $14535)
}

// Convert a `:maybe<a>` value to `:a`, using the `nothing` parameter for `Nothing`.
fun defaultstd/core/default: forall<a> (m : maybe<a>, nothing : a) -> a( mm: maybe<$2305> : maybestd/core/types/maybe: V -> V<aa: V>, nothingnothing: $2305 : aa: V ) : astd/core/types/(<>): E {
  match(mm: maybe<$2305>) {
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> nothingnothing: $2305
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $2305) -> xx: $2305
  }
}

// Get the value of the `Just` constructor or raise an exception
fun unjuststd/core/unjust: forall<a> (m : maybe<a>) -> exn a( mm: maybe<$16623> : maybestd/core/types/maybe: V -> V<aa: V> ) : exnstd/core/exn: HX aa: V {
  match(mm: maybe<$16623>) {
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $16623) -> xx: $16623
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/throw: forall<a> (message : string, info : ?exception-info) -> exn a("unexpected Nothing in std/core/unjust")
  }
}

fun mapstd/core/map: forall<a,b,e> (m : maybe<a>, f : (a) -> e b) -> e maybe<b>( mm: maybe<$5380> : maybestd/core/types/maybe: V -> V<aa: V>, ff: ($5380) -> $5382 $5381 : aa: V -> ee: E bb: V ) : ee: E maybestd/core/types/maybe: V -> V<bb: V> {
  match(mm: maybe<$5380>) {
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Nothingstd/core/types/Nothing: forall<a> maybe<a>
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $5380) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ff: ($5380) -> $5382 $5381(xx: $5380))
  }
}

fun (||)std/core/(||): forall<a> (m1 : maybe<a>, m2 : maybe<a>) -> maybe<a>( m1m1: maybe<$7717> : maybestd/core/types/maybe: V -> V<aa: V>, m2m2: maybe<$7717>: maybestd/core/types/maybe: V -> V<aa: V> ) : maybestd/core/types/maybe: V -> V<aa: V> {
  match(m1m1: maybe<$7717>) {
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> m2m2: maybe<$7717>
    _       -> m1m1: maybe<$7717>
  }
}

// Convert a `:either` to a `:maybe` type discarding the value of the `Left` constructor
// and using `Just` for the `Right` constructor.
fun maybestd/core/maybe.2: forall<a,b> (e : either<a,b>) -> maybe<b>( ee: either<$14572,$14573> : eitherstd/core/types/either: (V, V) -> V<aa: V,bb: V> ) : maybestd/core/types/maybe: V -> V<bb: V>
{
  match(ee: either<$14572,$14573>) {
    Leftstd/core/types/Left: forall<a,b> (left : a) -> either<a,b> -> Nothingstd/core/types/Nothing: forall<a> maybe<a>
    Rightstd/core/types/Right: forall<a,b> (right : b) -> either<a,b>(xx: $14573) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $14573)
  }
}

// Map over the `Right` component of an `:either` type.
fun mapstd/core/map.1: forall<a,b,c,e> (e : either<a,b>, f : (b) -> e c) -> e either<a,c>( ee: either<$5407,$5408> : eitherstd/core/types/either: (V, V) -> V<aa: V,bb: V>, ff: ($5408) -> $5410 $5409 : bb: V -> ee: E cc: V  ) : ee: E eitherstd/core/types/either: (V, V) -> V<aa: V,cc: V> {
  match(ee: either<$5407,$5408>) {
    Rightstd/core/types/Right: forall<a,b> (right : b) -> either<a,b>(xx: $5408) -> Rightstd/core/types/Right: forall<a,b> (right : b) -> either<a,b>(ff: ($5408) -> $5410 $5409(xx: $5408))
    Leftstd/core/types/Left: forall<a,b> (left : a) -> either<a,b>(xx: $5407)  -> Leftstd/core/types/Left: forall<a,b> (left : a) -> either<a,b>(xx: $5407)
  }
}

// The type of lists, which can be either empty (`Nil`) or an element followed
// by a list (`Cons`).
type liststd/core/list: V -> V<aa: V> {
  // The empty list.
  con Nillist: list<_1783>
  // A ``head``  element followed by the ``tail``  of the list.
  con Conslist: list<_1794>(headhead: 61:aa: V, tailtail: list<61> : liststd/core/list: V -> V<aa: V> )
}

// Return the head of list if the list is not empty.
fun headstd/core/head.1: forall<a> (xs : list<a>) -> maybe<a>( xsxs: list<$12964> : liststd/core/list: V -> V<aa: V> ) : maybestd/core/types/maybe: V -> V<aa: V> {
  match(xsxs: list<$12964>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $12964) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $12964)
    _       -> Nothingstd/core/types/Nothing: forall<a> maybe<a>
  }
}

// Return the head of list if the list is not empty, or use `default` otherwise
fun headstd/core/head.2: forall<a> (xs : list<a>, default : a) -> a( xsxs: list<$12991> : liststd/core/list: V -> V<aa: V>, defaultdefault: $12991 : aa: V ) : astd/core/types/(<>): E {
  match(xsxs: list<$12991>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $12991) -> xx: $12991
    _       -> defaultdefault: $12991
  }
}

// Return the tail of list. Returns the empty list if `xs` is empty.
fun tailstd/core/tail.1: forall<a> (xs : list<a>) -> list<a>( xsxs: list<$16281> : liststd/core/list: V -> V<aa: V> ) : liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$16281>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(_,xxxx: list<$16281>) -> xxxx: list<$16281>
    _          -> [std/core/Nil: forall<a> list<a>]std/core/Nil: forall<a> list<a>
  }
}

// Is the list empty?
fun is-emptystd/core/is-empty: forall<a> (xs : list<a>) -> bool( xsxs: list<$7636> : liststd/core/list: V -> V<aa: V> ) : boolstd/core/types/bool: V {
  match(xsxs: list<$7636>) {
    Nilstd/core/Nil: forall<a> list<a> -> Truestd/core/types/True: bool
    _   -> Falsestd/core/types/False: bool
  }
}

// A `:stream` is a co-inductive type representing an infinite list of elements.
co type streamstd/core/stream: V -> V<aa: V> {
  con Nextstd/core/Next: forall<a> (head : a, tail : stream<a>) -> stream<a>(headhead: 79:aa: V, tailtail: stream<79>: streamstd/core/stream: V -> V<aa: V> )
}

fun intstd/core/int.8: (x : order) -> int( xx: order : orderstd/core/types/order: V ) : intstd/core/types/int: V
{
  match(xx: order) {
    Ltstd/core/types/Lt: order -> 0 -std/core/(-): (int, int) -> int 1
    Eqstd/core/types/Eq: order -> 0
    Gtstd/core/types/Gt: order -> 1
  }
}

fun orderstd/core/order: (i : int) -> order( ii: int : intstd/core/types/int: V ) : orderstd/core/types/order: V
{
  if (std/core/types/True: boolii: int <std/core/(<).1: (x : int, y : int) -> bool 0) then Ltstd/core/types/Lt: order
  elif (std/core/types/True: boolii: int >std/core/(>).1: (x : int, y : int) -> bool 0) then Gtstd/core/types/Gt: order
  else Eqstd/core/types/Eq: order
}

fun (==)std/core/(==).4: (x : order, y : order) -> bool( xx: order : orderstd/core/types/order: V, yy: order : orderstd/core/types/order: V ) : boolstd/core/types/bool: V { xx: order.intstd/core/int.8: (x : order) -> int ==std/core/(==).1: (x : int, y : int) -> bool yy: order.intstd/core/int.8: (x : order) -> int }
fun (!=)std/core/(!=).4: (x : order, y : order) -> bool( xx: order : orderstd/core/types/order: V, yy: order : orderstd/core/types/order: V ) : boolstd/core/types/bool: V { xx: order.intstd/core/int.8: (x : order) -> int !=std/core/(!=).1: (x : int, y : int) -> bool yy: order.intstd/core/int.8: (x : order) -> int }
fun (>=)std/core/(>=).4: (x : order, y : order) -> bool( xx: order : orderstd/core/types/order: V, yy: order : orderstd/core/types/order: V ) : boolstd/core/types/bool: V { xx: order.intstd/core/int.8: (x : order) -> int >=std/core/(>=).1: (x : int, y : int) -> bool yy: order.intstd/core/int.8: (x : order) -> int }
fun (<=)std/core/(<=).5: (x : order, y : order) -> bool( xx: order : orderstd/core/types/order: V, yy: order : orderstd/core/types/order: V ) : boolstd/core/types/bool: V { xx: order.intstd/core/int.8: (x : order) -> int <=std/core/(<=).1: (x : int, y : int) -> bool yy: order.intstd/core/int.8: (x : order) -> int }
fun (>)std/core/(>).3: (x : order, y : order) -> bool( xx: order : orderstd/core/types/order: V, yy: order : orderstd/core/types/order: V ) : boolstd/core/types/bool: V  { xx: order.intstd/core/int.8: (x : order) -> int >std/core/(>).1: (x : int, y : int) -> bool yy: order.intstd/core/int.8: (x : order) -> int }
fun (<)std/core/(<).5: (x : order, y : order) -> bool( xx: order : orderstd/core/types/order: V, yy: order : orderstd/core/types/order: V ) : boolstd/core/types/bool: V  { xx: order.intstd/core/int.8: (x : order) -> int <std/core/(<).1: (x : int, y : int) -> bool yy: order.intstd/core/int.8: (x : order) -> int }

fun mapstd/core/map.2: forall<a,b,e> (t : (a, a), f : (a) -> e b) -> e (b, b)( tt: ($5445, $5445) : (std/core/types/(,): (V, V) -> Vaa: V,aa: V)std/core/types/(,): (V, V) -> V, ff: ($5445) -> $5447 $5446 : aa: V -> ee: E bb: V ) : ee: E (std/core/types/(,): (V, V) -> Vbb: V, bb: V)std/core/types/(,): (V, V) -> V {
  (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)tt: ($5445, $5445).fststd/core/types/fst: forall<a,b> ((a, b)) -> a.ff: ($5445) -> $5447 $5446, tt: ($5445, $5445).sndstd/core/types/snd: forall<a,b> ((a, b)) -> b.ff: ($5445) -> $5447 $5446)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
}

fun mapstd/core/map.3: forall<a,b,e> (t : (a, a, a), f : (a) -> e b) -> e (b, b, b)( tt: ($5590, $5590, $5590) : (std/core/types/(,,): (V, V, V) -> Vaa: V,aa: V,aa: V)std/core/types/(,,): (V, V, V) -> V, ff: ($5590) -> $5592 $5591 : aa: V -> ee: E bb: V ) : ee: E (std/core/types/(,,): (V, V, V) -> Vbb: V, bb: V, bb: V)std/core/types/(,,): (V, V, V) -> V {
  (std/core/types/(,,): forall<a,b,c> (fst : a, snd : b, thd : c) -> (a, b, c)tt: ($5590, $5590, $5590).fststd/core/types/fst.1: forall<a,b,c> ((a, b, c)) -> a.ff: ($5590) -> $5592 $5591, tt: ($5590, $5590, $5590).sndstd/core/types/snd.1: forall<a,b,c> ((a, b, c)) -> b.ff: ($5590) -> $5592 $5591, tt: ($5590, $5590, $5590).thdstd/core/types/thd: forall<a,b,c> ((a, b, c)) -> c.ff: ($5590) -> $5592 $5591)std/core/types/(,,): forall<a,b,c> (fst : a, snd : b, thd : c) -> (a, b, c)
}

fun mapstd/core/map.4: forall<a,b,e> (t : (a, a, a, a), f : (a) -> e b) -> e (b, b, b, b)( tt: ($5808, $5808, $5808, $5808) : (std/core/types/(,,,): (V, V, V, V) -> Vaa: V,aa: V,aa: V,aa: V)std/core/types/(,,,): (V, V, V, V) -> V, ff: ($5808) -> $5810 $5809 : aa: V -> ee: E bb: V ) : ee: E (std/core/types/(,,,): (V, V, V, V) -> Vbb: V,bb: V,bb: V,bb: V)std/core/types/(,,,): (V, V, V, V) -> V {
  (std/core/types/(,,,): forall<a,b,c,d> (fst : a, snd : b, thd : c, field4 : d) -> (a, b, c, d)tt: ($5808, $5808, $5808, $5808).fststd/core/types/fst.2: forall<a,b,c,d> ((a, b, c, d)) -> a.ff: ($5808) -> $5810 $5809, tt: ($5808, $5808, $5808, $5808).sndstd/core/types/snd.2: forall<a,b,c,d> ((a, b, c, d)) -> b.ff: ($5808) -> $5810 $5809, tt: ($5808, $5808, $5808, $5808).thdstd/core/types/thd.1: forall<a,b,c,d> ((a, b, c, d)) -> c.ff: ($5808) -> $5810 $5809, tt: ($5808, $5808, $5808, $5808).field4std/core/types/field4: forall<a,b,c,d> ((a, b, c, d)) -> d.ff: ($5808) -> $5810 $5809)std/core/types/(,,,): forall<a,b,c,d> (fst : a, snd : b, thd : c, field4 : d) -> (a, b, c, d)
}

// ----------------------------------------------------------------------------
// List funs
// ----------------------------------------------------------------------------

// Returns a singleton list.
fun singlestd/core/single: forall<a> (x : a) -> list<a>(xx: _2915) {
  returnreturn: list<_2915> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: _2915,Nilstd/core/Nil: forall<a> list<a>)
}

// Returns the length of a list.
fun lengthstd/core/length.1: forall<a> (xs : list<a>) -> int(xsxs: list<_4501>) {
  fun lenlen: forall<a> (list<a>, int) -> int(ysys: list<_4474>,accacc: int) {
    match(ysys: list<_4474>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(_,yyyy: list<_4474>) -> yyyy: list<_4474>.lenlen: (list<_4474>, int) -> int(accacc: int+std/core/(+): (int, int) -> int1)
      Nilstd/core/Nil: forall<a> list<a>        -> accacc: int
    }
  }
  xsxs: list<_4501>.lenlen: forall<a> (list<a>, int) -> int(0)
}

// Returns an integer list of increasing elements from `lo`  to `hi`
// (including both `lo`  and `hi` ).
// If `lo > hi`  the function returns the empty list.
fun liststd/core/list: (lo : int, hi : int) -> total list<int>( lolo: int: intstd/core/types/int: V, hihi: int: intstd/core/types/int: V ) : totalstd/core/total: E liststd/core/list: V -> V<intstd/core/types/int: V> {
  if (std/core/types/True: boollolo: int <=std/core/(<=).1: (x : int, y : int) -> bool hihi: int) 
    then Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>( lolo: int, liststd/core/list: (lo : int, hi : int) -> total list<int>( unsafe-decreasingstd/core/types/unsafe-decreasing: forall<a> (x : a) -> a(lolo: int.incstd/core/inc: (i : int) -> int), hihi: int ) )
    else Nilstd/core/Nil: forall<a> list<a>
}

// Returns an integer list of increasing elements from `lo`  to `hi` with stride `stride`.
// If `lo > hi`  the function returns the empty list.
fun liststd/core/list.1: (lo : int, hi : int, stride : int) -> total list<int>( lolo: int: intstd/core/types/int: V, hihi: int: intstd/core/types/int: V, stridestride: int : intstd/core/types/int: V ) : totalstd/core/total: E liststd/core/list: V -> V<intstd/core/types/int: V> {
  if (std/core/types/True: boollolo: int <=std/core/(<=).1: (x : int, y : int) -> bool hihi: int) 
    then Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>( lolo: int, liststd/core/list.1: (lo : int, hi : int, stride : int) -> total list<int>(  unsafe-decreasingstd/core/types/unsafe-decreasing: forall<a> (x : a) -> a(lolo: int +std/core/(+): (int, int) -> int stridestride: int), hihi: int, stridestride: int ))
    else Nilstd/core/Nil: forall<a> list<a>  
}


// Applies a function `f` to list of increasing elements from `lo`  to `hi`
// (including both `lo`  and `hi` ).
// If `lo > hi`  the function returns the empty list.
fun liststd/core/list.2: forall<a,e> (lo : int, hi : int, f : (int) -> e a) -> e list<a>( lolo: int: intstd/core/types/int: V, hihi: int: intstd/core/types/int: V, ff: (int) -> $4909 $4908 : intstd/core/types/int: V -> ee: E aa: V ) : ee: E liststd/core/list: V -> V<aa: V> {
  if (std/core/types/True: boollolo: int <=std/core/(<=).1: (x : int, y : int) -> bool hihi: int) 
    then Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>( ff: (int) -> $4909 $4908(lolo: int), liststd/core/list.2: forall<a,e> (lo : int, hi : int, f : (int) -> e a) -> e list<a>( unsafe-decreasingstd/core/types/unsafe-decreasing: forall<a> (x : a) -> a(lolo: int.incstd/core/inc: (i : int) -> int), hihi: int, ff: (int) -> $4909 $4908 ))
    else Nilstd/core/Nil: forall<a> list<a>  
}

// Returns an integer list of increasing elements from `lo`  to `hi` with stride `stride`.
// If `lo > hi`  the function returns the empty list.
fun liststd/core/list.3: forall<a,e> (lo : int, hi : int, stride : int, f : (int) -> e a) -> e list<a>( lolo: int: intstd/core/types/int: V, hihi: int: intstd/core/types/int: V, stridestride: int : intstd/core/types/int: V, ff: (int) -> $5019 $5018 : intstd/core/types/int: V -> ee: E aa: V ) : ee: E liststd/core/list: V -> V<aa: V> {
  if (std/core/types/True: boollolo: int <=std/core/(<=).1: (x : int, y : int) -> bool hihi: int) 
    then Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>( ff: (int) -> $5019 $5018(lolo: int), liststd/core/list.3: forall<a,e> (lo : int, hi : int, stride : int, f : (int) -> e a) -> e list<a>(  unsafe-decreasingstd/core/types/unsafe-decreasing: forall<a> (x : a) -> a(lolo: int +std/core/(+): (int, int) -> int stridestride: int), hihi: int, stridestride: int, ff: (int) -> $5019 $5018 ))
    else Nilstd/core/Nil: forall<a> list<a>  
}

// Create a list of characters from `lo`  to `hi`  (inclusive).
fun liststd/core/list.4: (lo : char, hi : char) -> total list<char>( lolo: char : charstd/core/types/char: V, hihi: char : charstd/core/types/char: V ) : totalstd/core/total: E liststd/core/list: V -> V<charstd/core/types/char: V> {
  liststd/core/list: (lo : int, hi : int) -> total list<int>(lolo: char.intstd/core/int: (char) -> int, hihi: char.intstd/core/int: (char) -> int).mapstd/core/map.5: forall<a,b,e> (xs : list<a>, f : (a) -> e b) -> e list<b>( charstd/core/char: (i : int) -> char )
}

private val maxListStackstd/core/maxListStack: int = 50


private fun zipwith-accstd/core/zipwith-acc: forall<a,b,c,e> ((int, a, b) -> e c, int, list<c>, list<a>, list<b>) -> e list<c>( ff: (int, _16951, _16961) -> _16948 _16953, ii: int, accacc: list<_16953>, xsxs: list<_16951>, ysys: list<_16961> )
{
  match(xsxs: list<_16951>) {
    Nilstd/core/Nil: forall<a> list<a> -> reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>(accacc: list<_16953>)
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: _16951,xxxx: list<_16951>) ->
      match(ysys: list<_16961>) {
        Nilstd/core/Nil: forall<a> list<a> -> reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>(accacc: list<_16953>)
        Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: _16961,yyyy: list<_16961>) -> zipwith-accstd/core/zipwith-acc: ((int, _16951, _16961) -> _16948 _16953, int, list<_16953>, list<_16951>, list<_16961>) -> _16948 list<_16953>(ff: (int, _16951, _16961) -> _16948 _16953, ii: int+std/core/(+): (int, int) -> int1, Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>( ff: (int, _16951, _16961) -> _16948 _16953(ii: int,xx: _16951,yy: _16961), accacc: list<_16953>), xxxx: list<_16951>, yyyy: list<_16961>)
      }
  }
}

// Zip two lists together by pairing the corresponding elements.
// The returned list is only as long as the smallest input list.
fun zipstd/core/zip: forall<a,b> (xs : list<a>, ys : list<b>) -> list<(a, b)>( xsxs: list<$16851> : liststd/core/list: V -> V<aa: V>, ysys: list<$16852> : liststd/core/list: V -> V<bb: V> ) : liststd/core/list: V -> V<(std/core/types/(,): (V, V) -> Vaa: V,bb: V)std/core/types/(,): (V, V) -> V> {
  match(xsxs: list<$16851>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $16851,xxxx: list<$16851>) -> match(ysys: list<$16852>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $16852,yyyy: list<$16852>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>((std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)xx: $16851,yy: $16852)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b),zipstd/core/zip: forall<a,b> (xs : list<a>, ys : list<b>) -> list<(a, b)>(xxxx: list<$16851>,yyyy: list<$16852>))
      Nilstd/core/Nil: forall<a> list<a>        -> Nilstd/core/Nil: forall<a> list<a>
    }
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Zip two lists together by apply a function `f` to all corresponding elements.
// The returned list is only as long as the smallest input list.
fun zipwithstd/core/zipwith: forall<a,b,c,e> (xs : list<a>, ys : list<b>, f : (a, b) -> e c) -> e list<c>( xsxs: list<$16894> : liststd/core/list: V -> V<aa: V>, ysys: list<$16895> :liststd/core/list: V -> V<bb: V>, ff: ($16894, $16895) -> $16897 $16896 : (aa: V,bb: V) -> ee: E cc: V ) : ee: E liststd/core/list: V -> V<cc: V> {
  match(xsxs: list<$16894>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $16894,xxxx: list<$16894>) -> match(ysys: list<$16895>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $16895,yyyy: list<$16895>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(ff: ($16894, $16895) -> $16897 $16896(xx: $16894,yy: $16895),zipwithstd/core/zipwith: forall<a,b,c,e> (xs : list<a>, ys : list<b>, f : (a, b) -> e c) -> e list<c>(xxxx: list<$16894>,yyyy: list<$16895>,ff: ($16894, $16895) -> $16897 $16896))
      Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
    }
    _ -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Zip two lists together by apply a function `f` to all corresponding elements
// and their index in the list.
// The returned list is only as long as the smallest input list.
fun zipwith-indexedstd/core/zipwith-indexed: forall<a,b,c,e> (xs0 : list<a>, ys0 : list<b>, f : (int, a, b) -> e c) -> e list<c>( xs0xs0: list<$17010> : liststd/core/list: V -> V<aa: V>, ys0ys0: list<$17011> :liststd/core/list: V -> V<bb: V>, ff: (int, $17010, $17011) -> $17013 $17012 : (intstd/core/types/int: V,aa: V,bb: V) -> ee: E cc: V ) : ee: E liststd/core/list: V -> V<cc: V> {
  fun zipwith-iterzipwith-iter: (int, list<$17010>, list<$17011>) -> $17013 list<$17012>( ii: int, xsxs: list<$17010>, ysys: list<$17011> ) {
    match(xsxs: list<$17010>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $17010,xxxx: list<$17010>) -> match(ysys: list<$17011>) {
        Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $17011,yyyy: list<$17011>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(ff: (int, $17010, $17011) -> $17013 $17012(ii: int,xx: $17010,yy: $17011),zipwith-iterzipwith-iter: (int, list<$17010>, list<$17011>) -> $17013 list<$17012>(ii: int+std/core/(+): (int, int) -> int1,xxxx: list<$17010>,yyyy: list<$17011>))
        Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
      }
      Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
    }
  }
  zipwith-iterzipwith-iter: (int, list<$17010>, list<$17011>) -> $17013 list<$17012>(0,xs0xs0: list<$17010>,ys0ys0: list<$17011>)
}

// Unzip a list of pairs into two lists
fun unzipstd/core/unzip: forall<a,b> (xs : list<(a, b)>) -> (list<a>, list<b>)( xsxs: list<($16671, $16672)> : liststd/core/list: V -> V<(std/core/types/(,): (V, V) -> Vaa: V,bb: V)std/core/types/(,): (V, V) -> V> ) : (std/core/types/(,): (V, V) -> Vliststd/core/list: V -> V<aa: V>,liststd/core/list: V -> V<bb: V>)std/core/types/(,): (V, V) -> V {
  // todo: implement TRMC for multiple results
  fun iteriter: forall<a,b> (list<(a, b)>, list<a>, list<b>) -> (list<a>, list<b>)( ysys: list<(_16682, _16683)>, acc1acc1: list<_16682>, acc2acc2: list<_16683> ) {
    match(ysys: list<(_16682, _16683)>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>((std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)xx: _16682,yy: _16683)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b),xxxx: list<(_16682, _16683)>) -> iteriter: (list<(_16682, _16683)>, list<_16682>, list<_16683>) -> (list<_16682>, list<_16683>)(xxxx: list<(_16682, _16683)>,Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: _16682,acc1acc1: list<_16682>),Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: _16683,acc2acc2: list<_16683>))
      Nilstd/core/Nil: forall<a> list<a>            -> (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>(acc1acc1: list<_16682>),reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>(acc2acc2: list<_16683>))std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
    }
  }
  iteriter: forall<a,b> (list<(a, b)>, list<a>, list<b>) -> (list<a>, list<b>)(xsxs: list<($16671, $16672)>,[std/core/Nil: forall<a> list<a>]std/core/Nil: forall<a> list<a>,[std/core/Nil: forall<a> list<a>]std/core/Nil: forall<a> list<a>)
}

// Take the first `n` elements of a list (or fewer if the list is shorter than `n`)
fun takestd/core/take: forall<a> (xs : list<a>, n : int) -> list<a>( xsxs: list<$13860> : liststd/core/list: V -> V<aa: V>, nn: int : intstd/core/types/int: V ) : liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$13860>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $13860,xxxx: list<$13860>) | nn: int >std/core/(>).1: (x : int, y : int) -> bool 0 -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $13860, takestd/core/take: forall<a> (xs : list<a>, n : int) -> list<a>(xxxx: list<$13860>, nn: int -std/core/(-): (int, int) -> int 1))
    _ -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Drop the first `n` elements of a list (or fewer if the list is shorter than `n`)
fun dropstd/core/drop: forall<a> (xs : list<a>, n : int) -> list<a>( xsxs: list<$11585> : liststd/core/list: V -> V<aa: V>, nn: int : intstd/core/types/int: V ) : liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$11585>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(_,xxxx: list<$11585>) | nn: int >std/core/(>).1: (x : int, y : int) -> bool 0 -> dropstd/core/drop: forall<a> (xs : list<a>, n : int) -> list<a>(xxxx: list<$11585>, nn: int -std/core/(-): (int, int) -> int 1)
    _ -> xsxs: list<$11585>
  }
}

// Apply a function `f`  to each element of the input list in sequence.
fun mapstd/core/map.5: forall<a,b,e> (xs : list<a>, f : (a) -> e b) -> e list<b>(xsxs: list<$6095> : liststd/core/list: V -> V<aa: V>, ff: ($6095) -> $6097 $6096 : aa: V -> ee: E bb: V) : ee: E liststd/core/list: V -> V<bb: V> {
  match(xsxs: list<$6095>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $6095,xxxx: list<$6095>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(ff: ($6095) -> $6097 $6096(xx: $6095), xxxx: list<$6095>.mapstd/core/map.5: forall<a,b,e> (xs : list<a>, f : (a) -> e b) -> e list<b>(ff: ($6095) -> $6097 $6096))
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Apply a function `f`  to each element of the input list in sequence where takes
// both the index of the current element and the element itself as arguments.
fun map-indexedstd/core/map-indexed: forall<a,b,e> (xs : list<a>, f : (idx : int, value : a) -> e b) -> e list<b>(xsxs: list<$14259> : liststd/core/list: V -> V<aa: V>, ff: (idx : int, value : $14259) -> $14261 $14260 : (idx : intstd/core/types/int: V, value : aa: V) -> ee: E bb: V) : ee: E liststd/core/list: V -> V<bb: V> {
  fun map-idxmap-idx: (list<$14259>, int) -> $14261 list<$14260>(ysys: list<$14259>,ii: int) {
    match(ysys: list<$14259>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $14259,yyyy: list<$14259>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(ff: (idx : int, value : $14259) -> $14261 $14260(ii: int,yy: $14259), map-idxmap-idx: (list<$14259>, int) -> $14261 list<$14260>(yyyy: list<$14259>,ii: int+std/core/(+): (int, int) -> int1))
      Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
    }
  }
  map-idxmap-idx: (list<$14259>, int) -> $14261 list<$14260>(xsxs: list<$14259>,0)
}

// Apply a function `f`  to each element of the input list in sequence where `f` takes
// both the current element and the tail list as arguments.
fun map-peekstd/core/map-peek: forall<a,b,e> (xs : list<a>, f : (value : a, rest : list<a>) -> e b) -> e list<b>(xsxs: list<$2697> : liststd/core/list: V -> V<aa: V>, ff: (value : $2697, rest : list<$2697>) -> $2699 $2698 : (value : aa: V, rest : liststd/core/list: V -> V<aa: V>) -> ee: E bb: V) : ee: E liststd/core/list: V -> V<bb: V> {
  fun mappeekmappeek: (list<$2697>) -> $2699 list<$2698>(ysys: list<$2697>) {
    match(ysys: list<$2697>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $2697,yyyy: list<$2697>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(ff: (value : $2697, rest : list<$2697>) -> $2699 $2698(yy: $2697,yyyy: list<$2697>), yyyy: list<$2697>.mappeekmappeek: (list<$2697>) -> $2699 list<$2698>)
      Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
    }
  }
  mappeekmappeek: (list<$2697>) -> $2699 list<$2698>(xsxs: list<$2697>)
}



// Apply a function `f`  to each element of the input list in sequence where takes
// both the index of the current element, the element itself, and the tail list as arguments.
fun map-indexed-peekstd/core/map-indexed-peek: forall<a,b,e> (xs : list<a>, f : (idx : int, value : a, rest : list<a>) -> e b) -> e list<b>(xsxs: list<$14315> : liststd/core/list: V -> V<aa: V>, ff: (idx : int, value : $14315, rest : list<$14315>) -> $14317 $14316 : (idx : intstd/core/types/int: V, value : aa: V, rest : liststd/core/list: V -> V<aa: V> ) -> ee: E bb: V) : ee: E liststd/core/list: V -> V<bb: V>
{
  fun mapidxmapidx: (list<$14315>, int) -> $14317 list<$14316>(ysys: list<$14315>,ii: int) {
    match(ysys: list<$14315>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $14315,yyyy: list<$14315>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(ff: (idx : int, value : $14315, rest : list<$14315>) -> $14317 $14316(ii: int,yy: $14315,yyyy: list<$14315>), mapidxmapidx: (list<$14315>, int) -> $14317 list<$14316>(yyyy: list<$14315>,ii: int+std/core/(+): (int, int) -> int1))
      Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
    }
  }
  mapidxmapidx: (list<$14315>, int) -> $14317 list<$14316>(xsxs: list<$14315>,0)
}

// Reverse a list.
fun reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>(xsxs: list<$12494> : liststd/core/list: V -> V<aa: V>) : liststd/core/list: V -> V<aa: V> {
  reverse-appendstd/core/reverse-append: forall<a> (xs : list<a>, tl : list<a>) -> list<a>( xsxs: list<$12494>, Nilstd/core/Nil: forall<a> list<a> )
}

// Efficiently reverse a list `xs` and append it to `tl`:
// `reverse-append(xs,tl) == reserve(xs) ++ tl
fun reverse-appendstd/core/reverse-append: forall<a> (xs : list<a>, tl : list<a>) -> list<a>( xsxs: list<$2429> : liststd/core/list: V -> V<aa: V>, tltl: list<$2429> : liststd/core/list: V -> V<aa: V> ) : liststd/core/list: V -> V<aa: V> {
  fun reverse-accreverse-acc: forall<a> (acc : list<a>, ys : list<a>) -> list<a>(accacc: list<$2430> : liststd/core/list: V -> V<aa: V>, ysys: list<$2430> : liststd/core/list: V -> V<aa: V> ) : liststd/core/list: V -> V<aa: V> {
    match(ysys: list<$2430>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $2430,xxxx: list<$2430>) -> reverse-accreverse-acc: forall<a> (acc : list<a>, ys : list<a>) -> list<a>(Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $2430,accacc: list<$2430>),xxxx: list<$2430>)
      _          -> accacc: list<$2430>
    }
  }
  reverse-accreverse-acc: forall<a> (acc : list<a>, ys : list<a>) -> list<a>(tltl: list<$2429>,xsxs: list<$2429>)
}

// Append two lists.
fun (++)std/core/(++): forall<a> (xs : list<a>, ys : list<a>) -> list<a>(xsxs: list<$3292> : liststd/core/list: V -> V<aa: V>, ysys: list<$3292> : liststd/core/list: V -> V<aa: V> ) : liststd/core/list: V -> V<aa: V> {
  appendstd/core/append: forall<a> (xs : list<a>, ys : list<a>) -> list<a>(xsxs: list<$3292>,ysys: list<$3292>)
}

// Append two lists.
fun appendstd/core/append: forall<a> (xs : list<a>, ys : list<a>) -> list<a>(xsxs: list<$3269> : liststd/core/list: V -> V<aa: V>, ysys: list<$3269> : liststd/core/list: V -> V<aa: V> ) : liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$3269>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $3269,xxxx: list<$3269>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $3269,appendstd/core/append: forall<a> (xs : list<a>, ys : list<a>) -> list<a>(xxxx: list<$3269>,ysys: list<$3269>))
    Nilstd/core/Nil: forall<a> list<a> -> ysys: list<$3269>
  }
}

// Fold a list from the right, i.e. `foldr([1,2],0,(+)) == 1+(2+0)`
// Note, `foldr` is less efficient than `foldl` as it reverses the list first.
fun foldrstd/core/foldr: forall<a,b,e> (xs : list<a>, z : b, f : (a, b) -> e b) -> e b(xsxs: list<_12511>,zz: _12507,ff: (_12511, _12507) -> _12509 _12507) {
  xsxs: list<_12511>.reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>.foldlstd/core/foldl: forall<a,b,e> (list<a>, b, (b, a) -> e b) -> e b(zz: _12507,fn(xx: _12507,yy: _12511){ff: (_12511, _12507) -> _12509 _12507(yy: _12511,xx: _12507)})
}

// Fold a list from the left, i.e. `foldl([1,2],0,(+)) == (0+1)+2`
// Since `foldl` is tail recursive, it is preferred over `foldr` when using an associative function `f`
fun foldlstd/core/foldl: forall<a,b,e> (list<a>, b, (b, a) -> e b) -> e b(xsxs: list<_12425>,zz: _12420,ff: (_12420, _12425) -> _12422 _12420) {
  match(xsxs: list<_12425>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: _12425,xxxx: list<_12425>) -> foldlstd/core/foldl: (list<_12425>, _12420, (_12420, _12425) -> _12422 _12420) -> _12422 _12420(xxxx: list<_12425>,ff: (_12420, _12425) -> _12422 _12420(zz: _12420,xx: _12425),ff: (_12420, _12425) -> _12422 _12420)
    Nilstd/core/Nil: forall<a> list<a>        -> zz: _12420
  }
}

fun foldl1std/core/foldl1: forall<a,e> (xs : list<a>, f : (a, a) -> <exn|e> a) -> <exn|e> a(xsxs: list<$12448> : liststd/core/list: V -> V<aa: V>, ff: ($12448, $12448) -> <exn|$12449> $12448 : (aa: V,aa: V) -> <exnstd/core/exn: HX|std/core/types/(<|>): (X, E) -> Eee: E> aa: V) : <exnstd/core/exn: HX|std/core/types/(<|>): (X, E) -> Eee: E> aa: V {
  match(xsxs: list<$12448>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $12448,xxxx: list<$12448>) -> xxxx: list<$12448>.foldlstd/core/foldl: forall<a,b,e> (list<a>, b, (b, a) -> e b) -> e b(xx: $12448,ff: ($12448, $12448) -> <exn|$12449> $12448)
    Nilstd/core/Nil: forall<a> list<a>        -> throwstd/core/throw: forall<a> (message : string, info : ?exception-info) -> exn a("unexpected Nil in std/core/foldl1")
  }
}

fun foldr1std/core/foldr1: forall<a,e> (xs : list<a>, f : (a, a) -> <exn|e> a) -> <exn|e> a(xsxs: list<$12529> : liststd/core/list: V -> V<aa: V>, ff: ($12529, $12529) -> <exn|$12530> $12529 : (aa: V,aa: V) -> <exnstd/core/exn: HX|std/core/types/(<|>): (X, E) -> Eee: E> aa: V) : <exnstd/core/exn: HX|std/core/types/(<|>): (X, E) -> Eee: E> aa: V {
  xsxs: list<$12529>.reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>.foldl1std/core/foldl1: forall<a,e> (xs : list<a>, f : (a, a) -> <exn|e> a) -> <exn|e> a(ff: ($12529, $12529) -> <exn|$12530> $12529)
}

// Create a list of `n`  repeated elementes `x`
fun replicatestd/core/replicate: forall<a> (x : a, n : int) -> list<a>( xx: $15841 : aa: V, nn: int : intstd/core/types/int: V ) : liststd/core/list: V -> V<aa: V> {
  if (std/core/types/True: boolnn: int >std/core/(>).1: (x : int, y : int) -> bool 0)
   then Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $15841, replicatestd/core/replicate: forall<a> (x : a, n : int) -> list<a>(xx: $15841,unsafe-decreasingstd/core/types/unsafe-decreasing: forall<a> (x : a) -> a(nn: int.decstd/core/dec: (i : int) -> int)))
   else Nilstd/core/Nil: forall<a> list<a>
}

// split a list at position `n`
fun splitstd/core/split: forall<a> (xs : list<a>, n : int) -> (list<a>, list<a>)( xsxs: list<$13906> : liststd/core/list: V -> V<aa: V>, nn: int : intstd/core/types/int: V ) : (std/core/types/(,): (V, V) -> Vliststd/core/list: V -> V<aa: V>, liststd/core/list: V -> V<aa: V>)std/core/types/(,): (V, V) -> V {
  (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)xsxs: list<$13906>.takestd/core/take: forall<a> (xs : list<a>, n : int) -> list<a>(nn: int), xsxs: list<$13906>.dropstd/core/drop: forall<a> (xs : list<a>, n : int) -> list<a>(nn: int))std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
}


fun spanstd/core/span: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e (list<a>, list<a>)( xsxs: list<$16200> : liststd/core/list: V -> V<aa: V>, predicatepredicate: ($16200) -> $16201 bool : aa: V -> ee: E boolstd/core/types/bool: V ) : ee: E (std/core/types/(,): (V, V) -> Vliststd/core/list: V -> V<aa: V>,liststd/core/list: V -> V<aa: V>)std/core/types/(,): (V, V) -> V
{
  // todo: implement TRMC with multiple results to avoid the reverse
  fun span-accspan-acc: (list<$16200>, list<$16200>) -> $16201 (list<$16200>, list<$16200>)( ysys: list<$16200>, accacc: list<$16200>) {
    match(ysys: list<$16200>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $16200,yyyy: list<$16200>) -> if (std/core/types/True: boolyy: $16200.predicatepredicate: ($16200) -> $16201 bool) then yyyy: list<$16200>.span-accspan-acc: (list<$16200>, list<$16200>) -> $16201 (list<$16200>, list<$16200>)(Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $16200,accacc: list<$16200>)) else (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)accacc: list<$16200>.reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>,ysys: list<$16200>)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
      _ -> (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)accacc: list<$16200>.reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>, ysys: list<$16200>)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
    }
  }
  xsxs: list<$16200>.span-accspan-acc: (list<$16200>, list<$16200>) -> $16201 (list<$16200>, list<$16200>)( [std/core/Nil: forall<a> list<a>]std/core/Nil: forall<a> list<a> )
}

// Keep only those initial elements that satisfy `predicate`
fun take-whilestd/core/take-while: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e list<a>( xsxs: list<$16336> : liststd/core/list: V -> V<aa: V>, predicatepredicate: ($16336) -> $16337 bool : aa: V -> ee: E boolstd/core/types/bool: V ) : ee: E liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$16336>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $16336,xxxx: list<$16336>) -> if (std/core/types/True: boolxx: $16336.predicatepredicate: ($16336) -> $16337 bool) then Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $16336, xxxx: list<$16336>.take-whilestd/core/take-while: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e list<a>(predicatepredicate: ($16336) -> $16337 bool) ) else Nilstd/core/Nil: forall<a> list<a>
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Drop all initial elements that satisfy `predicate`
fun drop-whilestd/core/drop-while: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e list<a>( xsxs: list<$11626> : liststd/core/list: V -> V<aa: V>, predicatepredicate: ($11626) -> $11627 bool : aa: V -> ee: E boolstd/core/types/bool: V ) : ee: E liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$11626>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $11626,xxxx: list<$11626>) -> if (std/core/types/True: boolxx: $11626.predicatepredicate: ($11626) -> $11627 bool) then xxxx: list<$11626>.drop-whilestd/core/drop-while: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e list<a>(predicatepredicate: ($11626) -> $11627 bool) else xsxs: list<$11626>
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Retain only those elements of a list that satisfy the given predicate `pred`.
// For example: `filter([1,2,3],odd?) == [1,3]`
fun filterstd/core/filter: forall<a,e> (xs : list<a>, pred : (a) -> e bool) -> e list<a>( xsxs: list<$11915> : liststd/core/list: V -> V<aa: V>, predpred: ($11915) -> $11916 bool : aa: V -> ee: E boolstd/core/types/bool: V ) : ee: E liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$11915>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $11915,xxxx: list<$11915>) -> if (std/core/types/True: boolpredpred: ($11915) -> $11916 bool(xx: $11915)) then Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $11915,xxxx: list<$11915>.filterstd/core/filter: forall<a,e> (xs : list<a>, pred : (a) -> e bool) -> e list<a>(predpred: ($11915) -> $11916 bool)) else xxxx: list<$11915>.filterstd/core/filter: forall<a,e> (xs : list<a>, pred : (a) -> e bool) -> e list<a>(predpred: ($11915) -> $11916 bool)
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
  }
}


// Remove those elements of a list that satisfy the given predicate `pred`.
// For example: `remove([1,2,3],odd?) == [2]`
fun removestd/core/remove: forall<a> (xs : list<a>, pred : (a) -> bool) -> list<a>( xsxs: list<$15783> : liststd/core/list: V -> V<aa: V>, predpred: ($15783) -> bool : aa: V -> boolstd/core/types/bool: V ) : liststd/core/list: V -> V<aa: V> {
  xsxs: list<$15783>.filterstd/core/filter: forall<a,e> (xs : list<a>, pred : (a) -> e bool) -> e list<a>( fn(xx: $15783) { !std/core/types/(!).1: (b : bool) -> boolpredpred: ($15783) -> bool(xx: $15783) } )
}

// Partition a list in two lists where the first list contains
// those elements that satisfy the given predicate `pred`.
// For example: `partition([1,2,3],odd?) == ([1,3],[2])`
fun partitionstd/core/partition: forall<a,e> (xs : list<a>, pred : (a) -> e bool) -> e (list<a>, list<a>)( xsxs: list<$15748> : liststd/core/list: V -> V<aa: V>, predpred: ($15748) -> $15749 bool : aa: V -> ee: E boolstd/core/types/bool: V ) : ee: E (std/core/types/(,): (V, V) -> Vliststd/core/list: V -> V<aa: V>,liststd/core/list: V -> V<aa: V>)std/core/types/(,): (V, V) -> V {
  partition-accstd/core/partition-acc: forall<a,e> (xs : list<a>, pred : (a) -> e bool, acc1 : list<a>, acc2 : list<a>) -> e (list<a>, list<a>)( xsxs: list<$15748>, predpred: ($15748) -> $15749 bool, Nilstd/core/Nil: forall<a> list<a>, Nilstd/core/Nil: forall<a> list<a>)
}

private fun partition-accstd/core/partition-acc: forall<a,e> (xs : list<a>, pred : (a) -> e bool, acc1 : list<a>, acc2 : list<a>) -> e (list<a>, list<a>)( xsxs: list<$15693> : liststd/core/list: V -> V<aa: V>, predpred: ($15693) -> $15694 bool : aa: V -> ee: E boolstd/core/types/bool: V, acc1acc1: list<$15693> : liststd/core/list: V -> V<aa: V>, acc2acc2: list<$15693> : liststd/core/list: V -> V<aa: V> ) : ee: E (std/core/types/(,): (V, V) -> Vliststd/core/list: V -> V<aa: V>, liststd/core/list: V -> V<aa: V>)std/core/types/(,): (V, V) -> V {
  match(xsxs: list<$15693>) {
    Nilstd/core/Nil: forall<a> list<a> -> (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)acc1acc1: list<$15693>.reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>, acc2acc2: list<$15693>.reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $15693,xxxx: list<$15693>) -> if (std/core/types/True: boolpredpred: ($15693) -> $15694 bool(xx: $15693))
                    then partition-accstd/core/partition-acc: forall<a,e> (xs : list<a>, pred : (a) -> e bool, acc1 : list<a>, acc2 : list<a>) -> e (list<a>, list<a>)(xxxx: list<$15693>,predpred: ($15693) -> $15694 bool,Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $15693,acc1acc1: list<$15693>),acc2acc2: list<$15693>)
                    else partition-accstd/core/partition-acc: forall<a,e> (xs : list<a>, pred : (a) -> e bool, acc1 : list<a>, acc2 : list<a>) -> e (list<a>, list<a>)(xxxx: list<$15693>,predpred: ($15693) -> $15694 bool,acc1acc1: list<$15693>,Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $15693,acc2acc2: list<$15693>))
  }
}


// Retain only those elements of a list that satisfy the given predicate `pred`.
// For example: `filterMap([1,2,3],fn(i) { if (i.odd?) then Nothing else Just(i*i) }) == [4]`
fun filter-mapstd/core/filter-map: forall<a,b,e> (xs : list<a>, pred : (a) -> e maybe<b>) -> e list<b>( xsxs: list<$11952> : liststd/core/list: V -> V<aa: V>, predpred: ($11952) -> $11954 maybe<$11953> : aa: V -> ee: E maybestd/core/types/maybe: V -> V<bb: V> ) : ee: E liststd/core/list: V -> V<bb: V> {
  match(xsxs: list<$11952>) {
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $11952,xxxx: list<$11952>) -> match(predpred: ($11952) -> $11954 maybe<$11953>(xx: $11952)) {
      Nothingstd/core/types/Nothing: forall<a> maybe<a> -> xxxx: list<$11952>.filter-mapstd/core/filter-map: forall<a,b,e> (xs : list<a>, pred : (a) -> e maybe<b>) -> e list<b>(predpred: ($11952) -> $11954 maybe<$11953>)
      Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $11953) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $11953,xxxx: list<$11952>.filter-mapstd/core/filter-map: forall<a,b,e> (xs : list<a>, pred : (a) -> e maybe<b>) -> e list<b>(predpred: ($11952) -> $11954 maybe<$11953>))
    }
  }
}

// Find the first element satisfying some predicate
fun findstd/core/find: forall<a> (xs : list<a>, pred : (a) -> bool) -> maybe<a>( xsxs: list<$12023> : liststd/core/list: V -> V<aa: V>, predpred: ($12023) -> bool : aa: V -> boolstd/core/types/bool: V ) : maybestd/core/types/maybe: V -> V<aa: V> {
  xsxs: list<$12023>.foreach-whilestd/core/foreach-while: forall<a,b,e> (xs : list<a>, action : (a) -> e maybe<b>) -> e maybe<b> fn(xx: $12023) {
    if (std/core/types/True: boolpredpred: ($12023) -> bool(xx: $12023)) then Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $12023) else Nothingstd/core/types/Nothing: forall<a> maybe<a>
  }
}

// Find the first element satisfying some predicate and return it.
fun find-maybestd/core/find-maybe: forall<a,b> (xs : list<a>, pred : (a) -> maybe<b>) -> maybe<b>( xsxs: list<$12191> : liststd/core/list: V -> V<aa: V>, predpred: ($12191) -> maybe<$12192> : aa: V -> maybestd/core/types/maybe: V -> V<bb: V> ) : maybestd/core/types/maybe: V -> V<bb: V>
{
  xsxs: list<$12191>.foreach-whilestd/core/foreach-while: forall<a,b,e> (xs : list<a>, action : (a) -> e maybe<b>) -> e maybe<b>(predpred: ($12191) -> maybe<$12192>)
}

// Lookup the first element satisfying some predicate
fun lookupstd/core/lookup: forall<a,b> (xs : list<(a, b)>, pred : (a) -> bool) -> maybe<b>( xsxs: list<($14059, $14060)> : liststd/core/list: V -> V<(std/core/types/(,): (V, V) -> Vaa: V,bb: V)std/core/types/(,): (V, V) -> V>, predpred: ($14059) -> bool : aa: V -> boolstd/core/types/bool: V ) : maybestd/core/types/maybe: V -> V<bb: V>
{
  xsxs: list<($14059, $14060)>.foreach-whilestd/core/foreach-while: forall<a,b,e> (xs : list<a>, action : (a) -> e maybe<b>) -> e maybe<b> fn(kvkv: ($14059, $14060)) {
    if (std/core/types/True: boolpredpred: ($14059) -> bool(kvkv: ($14059, $14060).fststd/core/types/fst: forall<a,b> ((a, b)) -> a)) then Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(kvkv: ($14059, $14060).sndstd/core/types/snd: forall<a,b> ((a, b)) -> b) else Nothingstd/core/types/Nothing: forall<a> maybe<a>
  }
}

// Convert a list to a `:maybe` type, using `Nothing` for an empty list, and otherwise `Just` on the head element.
// Note: this is just `head`.
fun maybestd/core/maybe.3: forall<a> (xs : list<a>) -> maybe<a>( xsxs: list<$14622> : liststd/core/list: V -> V<aa: V> ) : maybestd/core/types/maybe: V -> V<aa: V>
{
  match(xsxs: list<$14622>) {
    Nilstd/core/Nil: forall<a> list<a> -> Nothingstd/core/types/Nothing: forall<a> maybe<a>
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $14622,_) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $14622)
  }
}

// Convert a `:maybe` type to a list type.
fun liststd/core/list.5: forall<a> (m : maybe<a>) -> list<a>( mm: maybe<$5342> : maybestd/core/types/maybe: V -> V<aa: V> ) : liststd/core/list: V -> V<aa: V>
{
  match(mm: maybe<$5342>) {
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Nilstd/core/Nil: forall<a> list<a>
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $5342) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $5342,Nilstd/core/Nil: forall<a> list<a>)
  }
}

private fun index-of-accstd/core/index-of-acc: forall<a> (xs : list<a>, pred : (a) -> bool, idx : int) -> int( xsxs: list<$13101> : liststd/core/list: V -> V<aa: V>, predpred: ($13101) -> bool : aa: V -> boolstd/core/types/bool: V, idxidx: int : intstd/core/types/int: V ) : intstd/core/types/int: V
{
  match(xsxs: list<$13101>) {
    Nilstd/core/Nil: forall<a> list<a> -> 0 -std/core/(-): (int, int) -> int 1
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $13101,xxxx: list<$13101>) -> if (std/core/types/True: boolpredpred: ($13101) -> bool(xx: $13101)) then idxidx: int else index-of-accstd/core/index-of-acc: forall<a> (xs : list<a>, pred : (a) -> bool, idx : int) -> int(xxxx: list<$13101>,predpred: ($13101) -> bool,idxidx: int+std/core/(+): (int, int) -> int1)
  }
}

// Returns the index of the first element where `pred` holds, or `-1` if no such element exists.
fun index-ofstd/core/index-of: forall<a> (xs : list<a>, pred : (a) -> bool) -> int( xsxs: list<$13151> : liststd/core/list: V -> V<aa: V>, predpred: ($13151) -> bool : aa: V -> boolstd/core/types/bool: V ) : intstd/core/types/int: V
{
  index-of-accstd/core/index-of-acc: forall<a> (xs : list<a>, pred : (a) -> bool, idx : int) -> int( xsxs: list<$13151>, predpred: ($13151) -> bool, 0 )
}

// Invoke `action` for each element of a list
fun foreachstd/core/foreach: forall<a,e> (xs : list<a>, action : (a) -> e ()) -> e ()( xsxs: list<$11024> : liststd/core/list: V -> V<aa: V>, actionaction: ($11024) -> $11025 () : (aa: V) -> ee: E (std/core/types/(): V)std/core/types/(): V ) : ee: E (std/core/types/(): V)std/core/types/(): V
{
  match(xsxs: list<$11024>) {
    Nilstd/core/Nil: forall<a> list<a>        -> returnreturn: () (std/core/types/(): ())std/core/types/(): ()
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $11024,xxxx: list<$11024>) -> { actionaction: ($11024) -> $11025 ()(xx: $11024); xxxx: list<$11024>.foreachstd/core/foreach: forall<a,e> (xs : list<a>, action : (a) -> e ()) -> e ()(actionaction: ($11024) -> $11025 ()) }
  }
}

// Invoke `action` for each element of a list while `action` return `Nothing`
fun foreach-whilestd/core/foreach-while: forall<a,b,e> (xs : list<a>, action : (a) -> e maybe<b>) -> e maybe<b>( xsxs: list<$10618> : liststd/core/list: V -> V<aa: V>, actionaction: ($10618) -> $10620 maybe<$10619> : (aa: V) -> ee: E maybestd/core/types/maybe: V -> V<bb: V> ) : ee: E maybestd/core/types/maybe: V -> V<bb: V>
{
  match(xsxs: list<$10618>) {
    Nilstd/core/Nil: forall<a> list<a>        -> Nothingstd/core/types/Nothing: forall<a> maybe<a>
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $10618,xxxx: list<$10618>) -> {
      match(actionaction: ($10618) -> $10620 maybe<$10619>(xx: $10618)) {
        Nothingstd/core/types/Nothing: forall<a> maybe<a> -> xxxx: list<$10618>.foreach-whilestd/core/foreach-while: forall<a,b,e> (xs : list<a>, action : (a) -> e maybe<b>) -> e maybe<b>(actionaction: ($10618) -> $10620 maybe<$10619>)
        justjust: maybe<$10619>    -> justjust: maybe<$10619>
      }
    }
  }
}

// Invoke `action` on each element of a list while `action` returns `Just`
fun map-whilestd/core/map-while: forall<a,b,e> (xs : list<a>, action : (a) -> e maybe<b>) -> e list<b>( xsxs: list<$14372> : liststd/core/list: V -> V<aa: V>, actionaction: ($14372) -> $14374 maybe<$14373> : (aa: V) -> ee: E maybestd/core/types/maybe: V -> V<bb: V> ) : ee: E liststd/core/list: V -> V<bb: V>
{
  match(xsxs: list<$14372>) {
    Nilstd/core/Nil: forall<a> list<a>        -> Nilstd/core/Nil: forall<a> list<a>
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $14372,xxxx: list<$14372>) -> {
      match(actionaction: ($14372) -> $14374 maybe<$14373>(xx: $14372)) {
        Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $14373) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $14373,xxxx: list<$14372>.map-whilestd/core/map-while: forall<a,b,e> (xs : list<a>, action : (a) -> e maybe<b>) -> e list<b>(actionaction: ($14372) -> $14374 maybe<$14373>))
        Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Nilstd/core/Nil: forall<a> list<a>
      }
    }
  }
}

// Invoke `action` for each element of a list, passing also the position of the element.
fun foreach-indexedstd/core/foreach-indexed: forall<a,e> (xs : list<a>, action : (int, a) -> e ()) -> e ()( xsxs: list<$12634> : liststd/core/list: V -> V<aa: V>, actionaction: (int, $12634) -> $12635 () : (intstd/core/types/int: V,aa: V) -> ee: E (std/core/types/(): V)std/core/types/(): V ) : ee: E (std/core/types/(): V)std/core/types/(): V
{
  varstd/core/hnd/local-var: forall<a,b,e,h> (init : a, action : (l : local-var<h,a>) -> <local<h>|e> b) -> <local<h>|e> b istd/core/types/local-scope: forall<a,e> (action : forall<h> () -> <local<h>|e> a) -> e a := 0
  xsxs: list<$12634>.foreachstd/core/foreach: forall<a,e> (xs : list<a>, action : (a) -> e ()) -> e () fn(xx: $12634){
    val jj: int = ii: int; // don't dereference `i` inside the inject
    mask_1: H<localstd/core/types/local: H -> X>{actionaction: (int, $12634) -> $12635 ()(jj: int,xx: $12634)}
    ii: local-var<$12639,int> :=std/core/types/local-set: forall<a,e,h> (v : local-var<h,a>, assigned : a) -> <local<h>|e> () ii: int+std/core/(+): (int, int) -> int1
  }
}

// Insert a separator `sep`  between all elements of a list `xs` .
fun interspersestd/core/intersperse: forall<a> (xs : list<a>, sep : a) -> list<a>( xsxs: list<$2591> : liststd/core/list: V -> V<aa: V>, sepsep: $2591 : aa: V ) : liststd/core/list: V -> V<aa: V> {
  fun beforebefore: forall<a> (list<a>, a) -> list<a>(ysys: list<_2594>,ss: _2594) {
    match(ysys: list<_2594>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: _2594,yyyy: list<_2594>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(ss: _2594,Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: _2594,beforebefore: (list<_2594>, _2594) -> list<_2594>(yyyy: list<_2594>,ss: _2594)))
      Nilstd/core/Nil: forall<a> list<a>        -> Nilstd/core/Nil: forall<a> list<a>
    }
  }
  match(xsxs: list<$2591>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $2591,xxxx: list<$2591>)  -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $2591, xxxx: list<$2591>.beforebefore: forall<a> (list<a>, a) -> list<a>(sepsep: $2591))
    Nilstd/core/Nil: forall<a> list<a>         -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Concatenate all strings in a list
private fun joinsepstd/core/joinsep: (xs : list<string>, sep : string) -> string( xsxs: list<string> : liststd/core/list: V -> V<stringstd/core/types/string: V>, sepsep: string : stringstd/core/types/string: V ) : stringstd/core/types/string: V
{
  fun join-accjoin-acc: (ys : list<string>, acc : string) -> string( ysys: list<string> : liststd/core/list: V -> V<stringstd/core/types/string: V>, accacc: string : stringstd/core/types/string: V ) {
    match(ysys: list<string>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: string,yyyy: list<string>) -> join-accjoin-acc: (ys : list<string>, acc : string) -> string(yyyy: list<string>, accacc: string ++std/core/(++).1: (x : string, y : string) -> string sepsep: string ++std/core/(++).1: (x : string, y : string) -> string yy: string)  // todo: use string builder
      Nilstd/core/Nil: forall<a> list<a> -> accacc: string
    }
  }
  match(xsxs: list<string>) {
    Nilstd/core/Nil: forall<a> list<a> -> ""
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: string,xxxx: list<string>) -> join-accjoin-acc: (ys : list<string>, acc : string) -> string(xxxx: list<string>,xx: string)
  }
}

// Concatenate all strings in a list
fun joinstd/core/join.2: (xs : list<string>) -> string( xsxs: list<string> : liststd/core/list: V -> V<stringstd/core/types/string: V> ) : stringstd/core/types/string: V {
  returnreturn: string xsxs: list<string>.joinsepstd/core/joinsep: (xs : list<string>, sep : string) -> string("")
}

// Concatenate all strings in a list using a specific separator
fun joinstd/core/join.3: (xs : list<string>, sep : string) -> string( xsxs: list<string> : liststd/core/list: V -> V<stringstd/core/types/string: V>, sepsep: string : stringstd/core/types/string: V ) : stringstd/core/types/string: V {
  returnreturn: string xsxs: list<string>.joinsepstd/core/joinsep: (xs : list<string>, sep : string) -> string(sepsep: string)
}

// Concatenate all strings in a list in reverse order
fun reverse-joinstd/core/reverse-join: (xs : list<string>) -> string( xsxs: list<string> : liststd/core/list: V -> V<stringstd/core/types/string: V> ) : stringstd/core/types/string: V {
  returnreturn: string xsxs: list<string>.reversestd/core/reverse: forall<a> (xs : list<a>) -> list<a>.joinsepstd/core/joinsep: (xs : list<string>, sep : string) -> string("")  
}

// Append `end` to each string in the list `xs` and join them all together.\
// `join-end([],end) === ""`\
// `join-end(["a","b"],"/") === "a/b/"`
fun join-endstd/core/join-end: (xs : list<string>, end : string) -> string( xsxs: list<string> : liststd/core/list: V -> V<stringstd/core/types/string: V>, endend: string : stringstd/core/types/string: V) : stringstd/core/types/string: V {
  match(xsxs: list<string>) {
    Nilstd/core/Nil: forall<a> list<a> -> ""
    _   -> xsxs: list<string>.joinsepstd/core/joinsep: (xs : list<string>, sep : string) -> string(endend: string) ++std/core/(++).1: (x : string, y : string) -> string endend: string
  }
}


// Concatenate all lists in a list (e.g. flatten the list). (tail-recursive)
fun concatstd/core/concat: forall<a> (xss : list<list<a>>) -> list<a>( xssxss: list<list<$2209>> : liststd/core/list: V -> V<liststd/core/list: V -> V<aa: V>> ) : liststd/core/list: V -> V<aa: V>
{
  fun concat-preconcat-pre: forall<a> (ys : list<a>, zss : list<list<a>>) -> list<a>( ysys: list<$2210> : liststd/core/list: V -> V<aa: V>, zsszss: list<list<$2210>> : liststd/core/list: V -> V<liststd/core/list: V -> V<aa: V>> ) : liststd/core/list: V -> V<aa: V> {
    match(ysys: list<$2210>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $2210,yyyy: list<$2210>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $2210,concat-preconcat-pre: forall<a> (ys : list<a>, zss : list<list<a>>) -> list<a>(yyyy: list<$2210>,zsszss: list<list<$2210>>))
      Nilstd/core/Nil: forall<a> list<a> -> match(zsszss: list<list<$2210>>) {
        Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(zszs: list<$2210>,zzszzs: list<list<$2210>>) -> concat-preconcat-pre: forall<a> (ys : list<a>, zss : list<list<a>>) -> list<a>(zszs: list<$2210>,zzszzs: list<list<$2210>>)
        Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
      }
    }
  }
  concat-preconcat-pre: forall<a> (ys : list<a>, zss : list<list<a>>) -> list<a>([std/core/Nil: forall<a> list<a>]std/core/Nil: forall<a> list<a>,xssxss: list<list<$2209>>)
}

// Concatenate the result lists from applying a function to all elements.
fun flatmapstd/core/flatmap: forall<a,b,e> (xs : list<a>, f : (a) -> e list<b>) -> e list<b>( xsxs: list<$2375>: liststd/core/list: V -> V<aa: V>, ff: ($2375) -> $2377 list<$2376> : aa: V -> ee: E liststd/core/list: V -> V<bb: V> ) : ee: E liststd/core/list: V -> V<bb: V>
{
  fun flatmap-preflatmap-pre: (list<$2376>, list<$2375>) -> $2377 list<$2376>( ysys: list<$2376>, zszs: list<$2375> ) {
    match(ysys: list<$2376>) {
      Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $2376,yyyy: list<$2376>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $2376,flatmap-preflatmap-pre: (list<$2376>, list<$2375>) -> $2377 list<$2376>(yyyy: list<$2376>,zszs: list<$2375>))
      Nilstd/core/Nil: forall<a> list<a> -> match(zszs: list<$2375>) {
        Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(zz: $2375,zzzz: list<$2375>) -> flatmap-preflatmap-pre: (list<$2376>, list<$2375>) -> $2377 list<$2376>(ff: ($2375) -> $2377 list<$2376>(zz: $2375),zzzz: list<$2375>)
        Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
      }
    }
  }
  flatmap-preflatmap-pre: (list<$2376>, list<$2375>) -> $2377 list<$2376>([std/core/Nil: forall<a> list<a>]std/core/Nil: forall<a> list<a>,xsxs: list<$2375>)
}

// Concatenate the `Just` result elements from applying a function to all elements.
fun flatmap-maybestd/core/flatmap-maybe: forall<a,b,e> (xs : list<a>, f : (a) -> e maybe<b>) -> e list<b>( xsxs: list<$12256> : liststd/core/list: V -> V<aa: V>, ff: ($12256) -> $12258 maybe<$12257> : aa: V -> ee: E maybestd/core/types/maybe: V -> V<bb: V> ) : ee: E liststd/core/list: V -> V<bb: V> {
  match(xsxs: list<$12256>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $12256,xxxx: list<$12256>) -> match(ff: ($12256) -> $12258 maybe<$12257>(xx: $12256)) {
      Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $12257) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $12257, xxxx: list<$12256>.flatmap-maybestd/core/flatmap-maybe: forall<a,b,e> (xs : list<a>, f : (a) -> e maybe<b>) -> e list<b>(ff: ($12256) -> $12258 maybe<$12257>))
      Nothingstd/core/types/Nothing: forall<a> maybe<a> -> xxxx: list<$12256>.flatmap-maybestd/core/flatmap-maybe: forall<a,b,e> (xs : list<a>, f : (a) -> e maybe<b>) -> e list<b>(ff: ($12256) -> $12258 maybe<$12257>)
    }
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Concatenate a list of `:maybe` values
fun concat-maybestd/core/concat-maybe: forall<a> (xs : list<maybe<a>>) -> list<a>( xsxs: list<maybe<$10477>> : liststd/core/list: V -> V<maybestd/core/types/maybe: V -> V<aa: V>> ) : liststd/core/list: V -> V<aa: V>
{
  match(xsxs: list<maybe<$10477>>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: maybe<$10477>,xxxx: list<maybe<$10477>>) -> match(xx: maybe<$10477>) {
      Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $10477) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(yy: $10477, xxxx: list<maybe<$10477>>.concat-maybestd/core/concat-maybe: forall<a> (xs : list<maybe<a>>) -> list<a>)
      Nothingstd/core/types/Nothing: forall<a> maybe<a> -> xxxx: list<maybe<$10477>>.concat-maybestd/core/concat-maybe: forall<a> (xs : list<maybe<a>>) -> list<a>
    }
    Nilstd/core/Nil: forall<a> list<a> -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Return the last element of a list (or `Nothing` for the empty list)
fun laststd/core/last: forall<a> (xs : list<a>) -> maybe<a>( xsxs: list<$13700> : liststd/core/list: V -> V<aa: V> ) : maybestd/core/types/maybe: V -> V<aa: V>
{
  match(xsxs: list<$13700>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $13700,Nilstd/core/Nil: forall<a> list<a>) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $13700)
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(_,xxxx: list<$13700>)  -> laststd/core/last: forall<a> (xs : list<a>) -> maybe<a>(xxxx: list<$13700>)
    Nilstd/core/Nil: forall<a> list<a>         -> Nothingstd/core/types/Nothing: forall<a> maybe<a>
  }
}

// Return the last element of a list (or `default` for the empty list)
fun laststd/core/last.1: forall<a> (xs : list<a>, default : a) -> a( xsxs: list<$13738> : liststd/core/list: V -> V<aa: V>, defaultdefault: $13738 : aa: V ) : astd/core/types/(<>): E
{
  match(xsxs: list<$13738>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $13738,Nilstd/core/Nil: forall<a> list<a>) -> xx: $13738
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(_,xxxx: list<$13738>)  -> laststd/core/last.1: forall<a> (xs : list<a>, default : a) -> a(xxxx: list<$13738>,defaultdefault: $13738)
    Nilstd/core/Nil: forall<a> list<a>         -> defaultdefault: $13738
  }
}

// Return the list without its last element.
// Return an empty list for an empty list.
fun initstd/core/init: forall<a> (xs : list<a>) -> list<a>( xsxs: list<$13164> : liststd/core/list: V -> V<aa: V> ) : liststd/core/list: V -> V<aa: V> {
  match(xsxs: list<$13164>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $13164, xxxx: list<$13164> as Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>) -> Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $13164,initstd/core/init: forall<a> (xs : list<a>) -> list<a>(xxxx: list<$13164>))
    _ -> Nilstd/core/Nil: forall<a> list<a>
  }
}

// Get (zero-based) element `n`  of a list. Return a `:maybe` type.
fun [std/core/([]).2: forall<a> (xs : list<a>, n : int) -> maybe<a>]( xsxs: list<$9780> : liststd/core/list: V -> V<aa: V>, nn: int : intstd/core/types/int: V ) : maybestd/core/types/maybe: V -> V<aa: V> {
  match(xsxs: list<$9780>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $9780,xxxx: list<$9780>) -> if (std/core/types/True: boolnn: int>std/core/(>).1: (x : int, y : int) -> bool0) then xxxx: list<$9780>[std/core/([]).2: forall<a> (xs : list<a>, n : int) -> maybe<a>nn: int -std/core/(-): (int, int) -> int 1]
                  elif (std/core/types/True: boolnn: int==std/core/(==).1: (x : int, y : int) -> bool0) then Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $9780)
                  else Nothingstd/core/types/Nothing: forall<a> maybe<a>
    Nilstd/core/Nil: forall<a> list<a> -> Nothingstd/core/types/Nothing: forall<a> maybe<a>
  }
}

// Do all elements satisfy a predicate ?
fun allstd/core/all: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e bool( xsxs: list<$9968> : liststd/core/list: V -> V<aa: V>, predicatepredicate: ($9968) -> $9969 bool : aa: V -> ee: E boolstd/core/types/bool: V ) : ee: E boolstd/core/types/bool: V
{
  match(xsxs: list<$9968>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $9968,xxxx: list<$9968>) -> if (std/core/types/True: boolpredicatepredicate: ($9968) -> $9969 bool(xx: $9968)) then xxxx: list<$9968>.allstd/core/all: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e bool(predicatepredicate: ($9968) -> $9969 bool) else Falsestd/core/types/False: bool
    Nilstd/core/Nil: forall<a> list<a> -> Truestd/core/types/True: bool
  }
}


// Are there any elements in a list that satisfy a predicate ?
fun anystd/core/any: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e bool( xsxs: list<$9996> : liststd/core/list: V -> V<aa: V>, predicatepredicate: ($9996) -> $9997 bool : aa: V -> ee: E boolstd/core/types/bool: V ) : ee: E boolstd/core/types/bool: V
{
  match(xsxs: list<$9996>) {
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: $9996,xxxx: list<$9996>) -> if (std/core/types/True: boolpredicatepredicate: ($9996) -> $9997 bool(xx: $9996)) then Truestd/core/types/True: bool else xxxx: list<$9996>.anystd/core/any: forall<a,e> (xs : list<a>, predicate : (a) -> e bool) -> e bool(predicatepredicate: ($9996) -> $9997 bool)
    Nilstd/core/Nil: forall<a> list<a> -> Falsestd/core/types/False: bool
  }
}

// ----------------------------------------------------------------------------
// Characters
// ----------------------------------------------------------------------------

inline extern (==)std/core/(==): (char, char) -> bool : (charstd/core/types/char: V,charstd/core/types/char: V) -> boolstd/core/types/bool: V  { inline "(#1 == #2)"; js inline "(#1 === #2)" }
inline extern (!=)std/core/(!=): (char, char) -> bool : (charstd/core/types/char: V,charstd/core/types/char: V) -> boolstd/core/types/bool: V  { inline "(#1 != #2)"; js inline "(#1 !== #2)" }
inline extern (<=)std/core/(<=): (char, char) -> bool : (charstd/core/types/char: V,charstd/core/types/char: V) -> boolstd/core/types/bool: V  { inline "(#1 <= #2)" }
inline extern (>=)std/core/(>=): (char, char) -> bool : (charstd/core/types/char: V,charstd/core/types/char: V) -> boolstd/core/types/bool: V  { inline "(#1 >= #2)" }
inline extern (<)std/core/(<): (char, char) -> bool  : (charstd/core/types/char: V,charstd/core/types/char: V) -> boolstd/core/types/bool: V  { inline "(#1 < #2)" }
inline extern (>)std/core/(>): (char, char) -> bool  : (charstd/core/types/char: V,charstd/core/types/char: V) -> boolstd/core/types/bool: V  { inline "(#1 > #2)" }

fun comparestd/core/compare.1: (x : char, y : char) -> order( xx: char : charstd/core/types/char: V, yy: char : charstd/core/types/char: V ) : orderstd/core/types/order: V
{
  if (std/core/types/True: boolxx: char <std/core/(<): (char, char) -> bool yy: char) then Ltstd/core/types/Lt: order
  elif (std/core/types/True: boolxx: char >std/core/(>): (char, char) -> bool yy: char) then Gtstd/core/types/Gt: order
  else Eqstd/core/types/Eq: order
}

// Convert a character to its unicode code point
inline extern intstd/core/int: (char) -> int : (charstd/core/types/char: V) -> intstd/core/types/int: V {
  inline "#1"
  c "kk_integer_from_int"
  cs inline "new BigInteger(#1)"
}

// Convert a unicode code point to a character
inline extern charstd/core/char: (i : int) -> char( ^i : intstd/core/types/int: V) : charstd/core/types/char: V {
  inline "(#1)";
  c  inline "kk_integer_clamp32_borrow(#1)"
  cs inline "Primitive.IntToInt32(#1)"
}

// Add two character code points
fun (+)std/core/(+).4: (c : char, d : char) -> total char(cc: char : charstd/core/types/char: V, dd: char : charstd/core/types/char: V) : totalstd/core/total: E charstd/core/types/char: V { (cc: char.intstd/core/int: (char) -> int +std/core/(+): (int, int) -> int dd: char.intstd/core/int: (char) -> int).charstd/core/char: (i : int) -> char }
// Substract two character codePoints
fun (-)std/core/(-).4: (c : char, d : char) -> total char(cc: char : charstd/core/types/char: V, dd: char : charstd/core/types/char: V) : totalstd/core/total: E charstd/core/types/char: V { (cc: char.intstd/core/int: (char) -> int -std/core/(-): (int, int) -> int dd: char.intstd/core/int: (char) -> int).charstd/core/char: (i : int) -> char }

// Is the character a lower-case ASCII character ?
fun is-lowerstd/core/is-lower: (c : char) -> bool( cc: char : charstd/core/types/char: V ) : boolstd/core/types/bool: V { cc: char >=std/core/(>=): (char, char) -> bool 'a' &&std/core/types/(&&): (x : bool, y : bool) -> bool cc: char <=std/core/(<=): (char, char) -> bool 'z' }
// Is the character an upper-case ASCII character ?
fun is-upperstd/core/is-upper: (c : char) -> bool( cc: char : charstd/core/types/char: V ) : boolstd/core/types/bool: V { cc: char >=std/core/(>=): (char, char) -> bool 'A' &&std/core/types/(&&): (x : bool, y : bool) -> bool cc: char <=std/core/(<=): (char, char) -> bool 'Z' }
// Is the character an ASCII digit ?
fun is-digitstd/core/is-digit: (c : char) -> bool( cc: char : charstd/core/types/char: V ) : boolstd/core/types/bool: V { cc: char >=std/core/(>=): (char, char) -> bool '0' &&std/core/types/(&&): (x : bool, y : bool) -> bool cc: char <=std/core/(<=): (char, char) -> bool '9' }
// Is the character an ASCII hexa-decimal digit ?
fun is-hex-digitstd/core/is-hex-digit: (c : char) -> bool( cc: char : charstd/core/types/char: V ) : boolstd/core/types/bool: V { cc: char.is-digitstd/core/is-digit: (c : char) -> bool ||std/core/types/(||): (x : bool, y : bool) -> bool (cc: char >=std/core/(>=): (char, char) -> bool 'a' &&std/core/types/(&&): (x : bool, y : bool) -> bool cc: char <=std/core/(<=): (char, char) -> bool 'f') ||std/core/types/(||): (x : bool, y : bool) -> bool (cc: char >=std/core/(>=): (char, char) -> bool 'A'  &&std/core/types/(&&): (x : bool, y : bool) -> bool cc: char <=std/core/(<=): (char, char) -> bool 'F') }
// Is the character an ASCII letter is-
fun is-alphastd/core/is-alpha: (c : char) -> bool( cc: char : charstd/core/types/char: V ) : boolstd/core/types/bool: V { cc: char.is-lowerstd/core/is-lower: (c : char) -> bool ||std/core/types/(||): (x : bool, y : bool) -> bool cc: char.is-upperstd/core/is-upper: (c : char) -> bool }
// Is the character ASCII letter or digit?
fun is-alpha-numstd/core/is-alpha-num: (c : char) -> bool( cc: char : charstd/core/types/char: V ) : boolstd/core/types/bool: V { cc: char.is-alphastd/core/is-alpha: (c : char) -> bool ||std/core/types/(||): (x : bool, y : bool) -> bool cc: char.is-digitstd/core/is-digit: (c : char) -> bool }
// Is the character an ASCII character, e.g. `c <= '\x7F'`  ?
fun is-asciistd/core/is-ascii: (c : char) -> bool( cc: char : charstd/core/types/char: V )     : boolstd/core/types/bool: V { cc: char <=std/core/(<=): (char, char) -> bool '\DEL' }
// Is the character an ASCII control character, e.g. `c < ' '`  ?
fun is-controlstd/core/is-control: (c : char) -> bool( cc: char : charstd/core/types/char: V )   : boolstd/core/types/bool: V { cc: char <std/core/(<): (char, char) -> bool ' ' }
// Tests if a character is an element of `" \t\n\r"`
fun is-whitestd/core/is-white: (c : char) -> bool( cc: char : charstd/core/types/char: V )     : boolstd/core/types/bool: V { cc: char ==std/core/(==): (char, char) -> bool ' ' ||std/core/types/(||): (x : bool, y : bool) -> bool cc: char ==std/core/(==): (char, char) -> bool '\t' ||std/core/types/(||): (x : bool, y : bool) -> bool cc: char ==std/core/(==): (char, char) -> bool '\n' ||std/core/types/(||): (x : bool, y : bool) -> bool cc: char ==std/core/(==): (char, char) -> bool '\r' }


// ----------------------------------------------------------------------------
// Booleans
// ----------------------------------------------------------------------------

fun (==)std/core/(==).5: (x : bool, y : bool) -> bool( xx: bool : boolstd/core/types/bool: V, yy: bool : boolstd/core/types/bool: V) : boolstd/core/types/bool: V { if (std/core/types/True: boolxx: bool) then yy: bool else !std/core/types/(!).1: (b : bool) -> boolyy: bool }
fun (!=)std/core/(!=).5: (x : bool, y : bool) -> bool( xx: bool : boolstd/core/types/bool: V, yy: bool : boolstd/core/types/bool: V) : boolstd/core/types/bool: V { if (std/core/types/True: boolxx: bool) then !std/core/types/(!).1: (b : bool) -> boolyy: bool else yy: bool }

fun (<)std/core/(<).6: (x : bool, y : bool) -> bool( xx: bool : boolstd/core/types/bool: V, yy: bool : boolstd/core/types/bool: V) : boolstd/core/types/bool: V  { (!std/core/types/(!).1: (b : bool) -> boolxx: bool &&std/core/types/(&&): (x : bool, y : bool) -> bool yy: bool) }
fun (<=)std/core/(<=).6: (x : bool, y : bool) -> bool( xx: bool : boolstd/core/types/bool: V, yy: bool : boolstd/core/types/bool: V) : boolstd/core/types/bool: V { !std/core/types/(!).1: (b : bool) -> bool(xx: bool >std/core/(>).4: (x : bool, y : bool) -> bool yy: bool) }
fun (>)std/core/(>).4: (x : bool, y : bool) -> bool( xx: bool : boolstd/core/types/bool: V, yy: bool : boolstd/core/types/bool: V) : boolstd/core/types/bool: V  { (xx: bool &&std/core/types/(&&): (x : bool, y : bool) -> bool !std/core/types/(!).1: (b : bool) -> boolyy: bool) }
fun (>=)std/core/(>=).5: (x : bool, y : bool) -> bool( xx: bool : boolstd/core/types/bool: V, yy: bool : boolstd/core/types/bool: V) : boolstd/core/types/bool: V { !std/core/types/(!).1: (b : bool) -> bool(xx: bool <std/core/(<).6: (x : bool, y : bool) -> bool yy: bool) }

fun comparestd/core/compare.2: (x : bool, y : bool) -> order( xx: bool : boolstd/core/types/bool: V, yy: bool : boolstd/core/types/bool: V) : orderstd/core/types/order: V
{
  if (std/core/types/True: boolxx: bool <std/core/(<).6: (x : bool, y : bool) -> bool yy: bool) then Ltstd/core/types/Lt: order
  elif (std/core/types/True: boolxx: bool >std/core/(>).4: (x : bool, y : bool) -> bool yy: bool) then Gtstd/core/types/Gt: order
  else Eqstd/core/types/Eq: order
}

// Transform a boolean to a maybe type, using `Nothing` for `False`
fun maybestd/core/maybe.4: (b : bool) -> maybe<()>( bb: bool : boolstd/core/types/bool: V ) : maybestd/core/types/maybe: V -> V<(std/core/types/(): V)std/core/types/(): V> {
    if (std/core/types/True: boolbb: bool) then Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>((std/core/types/(): ())std/core/types/(): ()) else Nothingstd/core/types/Nothing: forall<a> maybe<a>
}

// ----------------------------------------------------------------------------
// Integers
// ----------------------------------------------------------------------------

// Compare two integers
inline extern comparestd/core/compare: (x : int, y : int) -> order(^x : intstd/core/types/int: V, ^y : intstd/core/types/int: V) : orderstd/core/types/order: V  {
  c inline "kk_int_as_order(kk_integer_cmp_borrow(#1,#2,kk_context()),kk_context())"
  cs "Primitive.IntCompare"
  js "$std_core._int_compare"
}


// Are two integers equal?
inline extern (==)std/core/(==): (x : int, y : int) -> bool(^x : intstd/core/types/int: V, ^y : intstd/core/types/int: V) : boolstd/core/types/bool: V  {
  c  "kk_integer_eq_borrow"
  cs inline "(#1 == #2)"
  js "$std_core._int_eq"
}

// Are two integers not equal?
inline extern (!=)std/core/(!=): (x : int, y : int) -> bool(^x : intstd/core/types/int: V, ^y : intstd/core/types/int: V) : boolstd/core/types/bool: V  {
  c  "kk_integer_neq_borrow"
  cs inline "(#1 != #2)"
  js "$std_core._int_ne"
}

// Is the first integer smaller or equal to the second?
inline extern (<=)std/core/(<=): (x : int, y : int) -> bool(^x : intstd/core/types/int: V, ^y : intstd/core/types/int: V) : boolstd/core/types/bool: V  {
  c  "kk_integer_lte_borrow"
  cs inline "(#1 <= #2)"
  js "$std_core._int_le"
}

// Is the first integer greater or equal to the second?
inline extern (>=)std/core/(>=): (x : int, y : int) -> bool(^x : intstd/core/types/int: V, ^y : intstd/core/types/int: V) : boolstd/core/types/bool: V  {
  c  "kk_integer_gte_borrow"
  cs inline "(#1 >= #2)"
  js "$std_core._int_ge"
}

// Is the first integer smaller than the second?
inline extern (<)std/core/(<): (x : int, y : int) -> bool(^x : intstd/core/types/int: V, ^y : intstd/core/types/int: V) : boolstd/core/types/bool: V  {
  c  "kk_integer_lt_borrow"
  cs inline "(#1 < #2)"
  js "$std_core._int_lt"
}

// Is the first integer greater than the second?
inline extern (>)std/core/(>): (x : int, y : int) -> bool(^x : intstd/core/types/int: V, ^y : intstd/core/types/int: V) : boolstd/core/types/bool: V  {
  c  "kk_integer_gt_borrow"
  cs inline "(#1 > #2)"
  js "$std_core._int_gt"
}

// Add two integers.
inline extern (+)std/core/(+): (int, int) -> int : (intstd/core/types/int: V,intstd/core/types/int: V) -> intstd/core/types/int: V {
  c  "kk_integer_add"
  cs inline "(#1 + #2)"
  js "$std_core._int_add"
}

// Substract two integers.
inline extern (-)std/core/(-): (int, int) -> int : (intstd/core/types/int: V,intstd/core/types/int: V) -> intstd/core/types/int: V {
  c  "kk_integer_sub"
  cs inline "(#1 - #2)"
  js "$std_core._int_sub"
}

// Multiply two integers.
inline extern (*)std/core/(*): (int, int) -> int : (intstd/core/types/int: V,intstd/core/types/int: V) -> intstd/core/types/int: V {
  c  "kk_integer_mul"
  cs inline "(#1 * #2)"
  js "$std_core._int_mul"
}

// Euclidean-0 division of two integers. See also `divmod:(x : int, y : int) -> (int,int)`.
inline extern (/)std/core/(/): (x : int, y : int) -> int(x:intstd/core/types/int: V,y:intstd/core/types/int: V) : intstd/core/types/int: V {
  c  "kk_integer_div"
  cs "Primitive.IntDiv"
  js "$std_core._int_div"
}
// Euclidean modulus of two integers; always a non-negative number. See also `divmod:(x : int, y : int) -> (int,int)`.
inline extern (%)std/core/(%): (int, int) -> int  : (intstd/core/types/int: V,intstd/core/types/int: V) -> intstd/core/types/int: V {
  c  "kk_integer_mod"
  cs "Primitive.IntMod"
  js "$std_core._int_mod"
}

// Euclidean-0 division & modulus.
// Euclidean division is defined as: For any `D`  and `d`  where `d!=0` , we have:
//
// 1. `D == d*(D/d) + (D%d)`
// 2. `D%d`  is always positive where `0 <= D%d < abs(d)`
//
// Moreover, Euclidean-0 is a total function, for the case where `d==0`  we have
// that `D%0 == D`  and `D/0 == 0` . So property (1) still holds, but not property (2).
//
// Useful laws that hold for Euclidean-0 division:
//
// * `D/(-d) == -(D/d)`
// * `D%(-d) == D%d`
// * `D/(2^n) == sar(D,n)         `  (with `0 <= n <= 31`  and `2^n`  means `2`  to the power of `n` )
// * `D%(2^n) == D & ((2^n) - 1)  `  (with `0 <= n <= 31`  and `2^n`  means `2`  to the power of `n` )
//
// See also _Division and modulus for computer scientists, Daan Leijen, 2001_ for further information
// available at: <http://research.microsoft.com/pubs/151917/divmodnote.pdf> .
inline extern divmodstd/core/divmod: (x : int, y : int) -> (int, int)(x:intstd/core/types/int: V,y:intstd/core/types/int: V) : (std/core/types/(,): (V, V) -> Vintstd/core/types/int: V,intstd/core/types/int: V)std/core/types/(,): (V, V) -> V {
  c  "kk_integer_div_mod_tuple"
  cs "Primitive.IntDivMod"
  js "$std_core._int_divmod"
}

fun negatestd/core/negate: (i : int) -> int(ii: int : intstd/core/types/int: V) : intstd/core/types/int: V {
  ~std/core/(~): (i : int) -> intii: int
}

// Negate an integer.
inline extern (~)std/core/(~): (i : int) -> int(i:intstd/core/types/int: V) : intstd/core/types/int: V {
  c  "kk_integer_neg"
  cs inline "(-#1)"
  js "$std_core._int_negate"
}

// Convert an integer to a `:double`. May return `nan` if the integer is too large to represent as a `:double`.
inline extern doublestd/core/double: (i : int) -> double( ^i : intstd/core/types/int: V) : doublestd/core/types/double: V  {
  c  inline "kk_integer_as_double_borrow(#1)"
  cs "Primitive.IntToDouble"
  js "$std_core._int_to_double"
}

// Is this an odd integer?
inline extern is-oddstd/core/is-odd: (int) -> bool   : ( intstd/core/types/int: V ) -> boolstd/core/types/bool: V  {
  c  "kk_integer_is_odd"
  cs inline "!(#1.IsEven)"
  js "$std_core._int_isodd"
}

// Is this equal to zero?
inline extern is-zerostd/core/is-zero: (x : int) -> bool( ^x : intstd/core/types/int: V) : boolstd/core/types/bool: V  {
  c  inline "kk_integer_is_zero_borrow(#1)"
  cs inline "(#1.IsZero)"
  js "$std_core._int_iszero"
}


// Return the absolute value of an integer.
inline extern absstd/core/abs: (i : int) -> int(i : intstd/core/types/int: V) : intstd/core/types/int: V {
  c  "kk_integer_abs"
  cs "BigInteger.Abs"
  js "$std_core._int_abs"
}

fun incstd/core/inc: (i : int) -> int( ii: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  ii: int +std/core/(+): (int, int) -> int 1
}

fun decstd/core/dec: (i : int) -> int( ii: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  ii: int -std/core/(-): (int, int) -> int 1
}

// Calculate `10^exp`
fun exp10std/core/exp10: (exp : int) -> int( expexp: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  1.mul-exp10std/core/mul-exp10: (i : int, n : int) -> int(expexp: int)
}

// Raise an integer `i` to the power of `exp`.
extern powstd/core/pow: (i : int, exp : int) -> int( ii: int : intstd/core/types/int: V, expexp: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  c  "kk_integer_pow"
  cs "Primitive.IntPow"
  js "_int_pow"
}

// Raise an integer `i` to the power of `exp`.
fun (^)std/core/(^).1: (i : int, exp : int) -> int(ii: int : intstd/core/types/int: V, expexp: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  powstd/core/pow: (i : int, exp : int) -> int(ii: int,expexp: int);
}

// Calculate `2^exp`.
fun exp2std/core/exp2: (exp : int) -> int( expexp: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  powstd/core/pow: (i : int, exp : int) -> int(2,expexp: int)
}

// Return the number of ending `0` digits of `i`. Return `0` when `i==0`.
extern is-exp10std/core/is-exp10: (i : int) -> int( ii: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  c  "kk_integer_ctz"
  cs "Primitive.IntCountPow10"
  js "_int_count_pow10"
}

// Return the number of decimal digits of `i`. Return `0` when `i==0`.
extern count-digitsstd/core/count-digits: (i : int) -> int( ii: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  c  "kk_integer_count_digits"
  cs "Primitive.IntCountDigits"
  js "_int_count_digits"
}

extern mul-exp10std/core/mul-exp10: (i : int, n : int) -> int( ii: int : intstd/core/types/int: V, nn: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  c  "kk_integer_mul_pow10"
  cs "Primitive.IntMulPow10"
  js "_int_mul_pow10"
}

extern cdiv-exp10std/core/cdiv-exp10: (i : int, n : int) -> int( ii: int : intstd/core/types/int: V, nn: int : intstd/core/types/int: V ) : intstd/core/types/int: V {
  c  "kk_integer_cdiv_pow10"
  cs "Primitive.IntCDivPow10"
  js "_int_cdiv_pow10"
}

fun cdivmod-exp10std/core/cdivmod-exp10: (i : int, n : int) -> (int, int)( ii: int : intstd/core/types/int: V, nn: int : intstd/core/types/int: V ) : (std/core/types/(,): (V, V) -> Vintstd/core/types/int: V,intstd/core/types/int: V)std/core/types/(,): (V, V) -> V {
  if (std/core/types/True: boolnn: int <=std/core/(<=).1: (x : int, y : int) -> bool 0) returnreturn: (int, int) (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)ii: int,0)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
  val cqcq: int = ii: int.cdiv-exp10std/core/cdiv-exp10: (i : int, n : int) -> int(nn: int)
  val crcr: int = ii: int -std/core/(-): (int, int) -> int cqcq: int.mul-exp10std/core/mul-exp10: (i : int, n : int) -> int(nn: int)
  (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)cqcq: int,crcr: int)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
}

fun divmod-exp10std/core/divmod-exp10: (i : int, n : int) -> (int, int)( ii: int : intstd/core/types/int: V, nn: int : intstd/core/types/int: V ) : (std/core/types/(,): (V, V) -> Vintstd/core/types/int: V,intstd/core/types/int: V)std/core/types/(,): (V, V) -> V {
  val (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)cqcq: int,crcr: int)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b) = cdivmod-exp10std/core/cdivmod-exp10: (i : int, n : int) -> (int, int)(ii: int,nn: int)
  if (std/core/types/True: bool!std/core/types/(!).1: (b : bool) -> boolcrcr: int.is-negstd/core/is-neg.2: (i : int) -> bool) then (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)cqcq: int,crcr: int)std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b) else (std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)cqcq: int.decstd/core/dec: (i : int) -> int, crcr: int +std/core/(+): (int, int) -> int exp10std/core/exp10: (exp : int) -> int(nn: int))std/core/types/(,): forall<a,b> (fst : a, snd : b) -> (a, b)
}

// Is this an even integer?
fun is-evenstd/core/is-even: (i : int) -> bool(ii: int:intstd/core/types/int: V) : boolstd/core/types/bool: V { !std/core/types/(!).1: (b : bool) -> boolis-oddstd/core/is-odd: (int) -> bool(ii: int) }

// Is the integer positive (stricly greater than zero)
fun is-posstd/core/is-pos.2: (i : int) -> bool(ii: int : intstd/core/types/int: V ) : boolstd/core/types/bool: V { ii: int.signstd/core/sign: (i : int) -> order ==std/core/(==).4: (x : order, y : order) -> bool Gtstd/core/types/Gt: order }

// Is the integer negative (stricly smaller than zero)
fun is-negstd/core/is-neg.2: (i : int) -> bool(ii: int : intstd/core/types/int: V ) : boolstd/core/types/bool: V { ii: int.signstd/core/sign: (i : int) -> order ==std/core/(==).4: (x : order, y : order) -> bool Ltstd/core/types/Lt: order }

inline extern signstd/core/sign: (i : int) -> order( ^i : intstd/core/types/int: V ) : orderstd/core/types/order: V {
  c  inline "kk_int_as_order(kk_integer_signum_borrow(#1),kk_context())"
  cs "Primitive.IntSign"
  js "$std_core._int_sign"
}

// Return the minimum of two integers
fun minstd/core/min: (i : int, j : int) -> int( ii: int : intstd/core/types/int: V, jj: int : intstd/core/types/int: V ) : intstd/core/types/int: V { if (std/core/types/True: boolii: int <=std/core/(<=).1: (x : int, y : int) -> bool jj: int) then ii: int else jj: int }

// Return the maximum of two integers
fun maxstd/core/max: (i : int, j : int) -> int( ii: int : intstd/core/types/int: V, jj: int : intstd/core/types/int: V ) : intstd/core/types/int: V { if (std/core/types/True: boolii: int >=std/core/(>=).1: (x : int, y : int) -> bool jj: int) then ii: int else jj: int }

// Returns the smallest element of a list of integers (or `default` (=`0`) for the empty list)
fun minimumstd/core/minimum: (xs : list<int>, default : ?int) -> int( xsxs: list<int> : liststd/core/list: V -> V<intstd/core/types/int: V>, defaultdefault: ?int : intstd/core/types/optional: V -> V = 0 ) : intstd/core/types/int: V
{
  match(xsxs: list<int>) {
    Nilstd/core/Nil: forall<a> list<a> -> defaultdefault: int
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: int,xxxx: list<int>) -> xxxx: list<int>.foldlstd/core/foldl: forall<a,b,e> (list<a>, b, (b, a) -> e b) -> e b( xx: int, minstd/core/min: (i : int, j : int) -> int )
  }
}

// Returns the largest element of a list of integers (or `default` (=`0`) for the empty list)
fun maximumstd/core/maximum: (xs : list<int>, default : ?int) -> int( xsxs: list<int> : liststd/core/list: V -> V<intstd/core/types/int: V>, defaultdefault: ?int : intstd/core/types/optional: V -> V = 0 ) : intstd/core/types/int: V
{
  match(xsxs: list<int>) {
    Nilstd/core/Nil: forall<a> list<a> -> defaultdefault: int
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: int,xxxx: list<int>) -> xxxx: list<int>.foldlstd/core/foldl: forall<a,b,e> (list<a>, b, (b, a) -> e b) -> e b( xx: int, maxstd/core/max: (i : int, j : int) -> int )
  }
}

// Return the sum of a list of integers
fun sumstd/core/sum: (xs : list<int>) -> int( xsxs: list<int> : liststd/core/list: V -> V<intstd/core/types/int: V> ) : intstd/core/types/int: V
{
  xsxs: list<int>.foldlstd/core/foldl: forall<a,b,e> (list<a>, b, (b, a) -> e b) -> e b( 0, fn(xx: int,yy: int) { xx: int +std/core/(+): (int, int) -> int yy: int } )
}

// Transform an integer to a maybe type, using `Nothing` for `0`
fun maybestd/core/maybe.5: (i : int) -> maybe<int>( ii: int : intstd/core/types/int: V ) : maybestd/core/types/maybe: V -> V<intstd/core/types/int: V> {
  if (std/core/types/True: boolii: int==std/core/(==).1: (x : int, y : int) -> bool0) then Nothingstd/core/types/Nothing: forall<a> maybe<a> else Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ii: int)
}

// fold over the integers between [`start`,`end`] (inclusive).
fun fold-intstd/core/fold-int: forall<a,e> (start : int, end : int, init : a, f : (int, a) -> e a) -> e a( startstart: int : intstd/core/types/int: V, endend: int : intstd/core/types/int: V, initinit: $12306 : aa: V, ff: (int, $12306) -> $12307 $12306 : (intstd/core/types/int: V,aa: V) -> ee: E aa: V ) : ee: E aa: V {
  if (std/core/types/True: boolstartstart: int >=std/core/(>=).1: (x : int, y : int) -> bool endend: int) then initinit: $12306 else
    val xx: $12306 = ff: (int, $12306) -> $12307 $12306(startstart: int,initinit: $12306)
    fold-intstd/core/fold-int: forall<a,e> (start : int, end : int, init : a, f : (int, a) -> e a) -> e a(unsafe-decreasingstd/core/types/unsafe-decreasing: forall<a> (x : a) -> a(startstart: int.incstd/core/inc: (i : int) -> int), endend: int, xx: $12306, ff: (int, $12306) -> $12307 $12306)
}

// fold over the integers between [0,`upto`)  (not including `upto`).
fun fold-intstd/core/fold-int.1: forall<a,e> (upto : int, init : a, f : (int, a) -> e a) -> e a( uptoupto: int : intstd/core/types/int: V, initinit: $12362 : aa: V, ff: (int, $12362) -> $12363 $12362 : (intstd/core/types/int: V,aa: V) -> ee: E aa: V ) : ee: E aa: V {
  fold-intstd/core/fold-int: forall<a,e> (start : int, end : int, init : a, f : (int, a) -> e a) -> e a( 0, uptoupto: int.decstd/core/dec: (i : int) -> int, initinit: $12362, ff: (int, $12362) -> $12363 $12362 )
}



// ----------------------------------------------------------------------------
// 32-bit integers
// Just define the operations needed for defining the std/core interface but
// don't export any definitions here. Full operations are defined in `std/int32`.
// ----------------------------------------------------------------------------

// Convert an `:int32` to an `:int`.
inline extern intstd/core/int: (i : int32) -> int( i : int32std/core/types/int32: V ) : intstd/core/types/int: V {
  c  "kk_integer_from_int"
  cs inline "(new BigInteger(#1))"
  js "$std_core._int_from_int32"
}

// Convert an integer to an `:int32`. The number is _clamped_ to the maximal or minimum `:int32`
// value if it is outside the range of an `:int32`.
extern int32std/core/int32: (i : int) -> int32( ii: int : intstd/core/types/int: V) : int32std/core/types/int32: V   {
  c  "kk_integer_clamp32" // "kk_integer_clamp32_borrow(#1)"
  cs "Primitive.IntToInt32"
  js "$std_core._int_clamp32"
}


// Minimal set of operations that we need in `std/core`.
private inline extern (<=)std/core/(<=): (int32, int32) -> bool : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> boolstd/core/types/bool: V  { inline "(#1 <= #2)"; js inline "(#1 <= #2)" }
private inline extern (<)std/core/(<): (int32, int32) -> bool  : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> boolstd/core/types/bool: V  { inline "(#1 < #2)"; js inline "(#1 < #2)" }
private inline extern (+)std/core/(+): (int32, int32) -> int32  : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V { inline "(#1 + #2)"; js inline "((#1 + #2)|0)" }
private inline extern (-)std/core/(-): (int32, int32) -> int32  : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V { inline "(#1 - #2)"; js inline "((#1 - #2)|0)" }
private inline extern is-posstd/core/is-pos: (i : int32) -> bool( i : int32std/core/types/int32: V ) : boolstd/core/types/bool: V { inline "(#1>0)" }
private inline extern is-negstd/core/is-neg: (i : int32) -> bool( i : int32std/core/types/int32: V ) : boolstd/core/types/bool: V { inline "(#1<0)" }

private fun incrstd/core/incr: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V ) : int32std/core/types/int32: V {
  ii: int32 +std/core/(+).1: (int32, int32) -> int32 1.int32std/core/int32: (i : int) -> int32
}

private fun decrstd/core/decr: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V ) : int32std/core/types/int32: V {
  ii: int32 -std/core/(-).1: (int32, int32) -> int32 1.int32std/core/int32: (i : int) -> int32
}

// ----------------------------------------------------------------------------
// N-bit ssize_t
// Just define the operations needed for defining the std/core interface but
// don't export any definitions here.
// ----------------------------------------------------------------------------

// Convert an integer to an `:ssize_t`. The number is _clamped_ to the maximal or minimum `:ssize_t`
// value if it is outside the range of an `:ssize_t`.
extern ssize_tstd/core/ssize_t: (i : int) -> ssize_t( ii: int : intstd/core/types/int: V) : ssize_tstd/core/types/ssize_t: V   {
  c  "kk_integer_clamp_ssize_t" 
  cs "Primitive.IntToInt32"
  js "$std_core._int_clamp32"
}

// Convert an `:ssize_t` to an `:int`.
inline extern intstd/core/int: (i : ssize_t) -> int( i : ssize_tstd/core/types/ssize_t: V ) : intstd/core/types/int: V {
  c  "kk_integer_from_ssize_t"
  cs inline "(new BigInteger(#1))"
  js "$std_core._int_from_int32"
}


// Minimal set of operations that we need in `std/core`.
private inline extern (<=)std/core/(<=): (ssize_t, ssize_t) -> bool : (ssize_tstd/core/types/ssize_t: V,ssize_tstd/core/types/ssize_t: V) -> boolstd/core/types/bool: V   { inline "(#1 <= #2)" }
private inline extern (>=)std/core/(>=): (ssize_t, ssize_t) -> bool : (ssize_tstd/core/types/ssize_t: V,ssize_tstd/core/types/ssize_t: V) -> boolstd/core/types/bool: V   { inline "(#1 >= #2)" }
private inline extern (<)std/core/(<): (ssize_t, ssize_t) -> bool  : (ssize_tstd/core/types/ssize_t: V,ssize_tstd/core/types/ssize_t: V) -> boolstd/core/types/bool: V   { inline "(#1 < #2)" }
private inline extern (+)std/core/(+): (ssize_t, ssize_t) -> ssize_t  : (ssize_tstd/core/types/ssize_t: V,ssize_tstd/core/types/ssize_t: V) -> ssize_tstd/core/types/ssize_t: V { inline "(#1 + #2)"; js inline "((#1 + #2)|0)" }
private inline extern (-)std/core/(-): (ssize_t, ssize_t) -> ssize_t  : (ssize_tstd/core/types/ssize_t: V,ssize_tstd/core/types/ssize_t: V) -> ssize_tstd/core/types/ssize_t: V { inline "(#1 - #2)"; js inline "((#1 - #2)|0)" }
private inline extern is-posstd/core/is-pos: (i : ssize_t) -> bool( i : ssize_tstd/core/types/ssize_t: V ) : boolstd/core/types/bool: V { inline "(#1 > 0)" }
private inline extern is-negstd/core/is-neg: (i : ssize_t) -> bool( i : ssize_tstd/core/types/ssize_t: V ) : boolstd/core/types/bool: V { inline "(#1 < 0)" }
private extern is-zerostd/core/is-zero.1: (i : ssize_t) -> bool( ii: ssize_t : ssize_tstd/core/types/ssize_t: V ) : boolstd/core/types/bool: V { inline "(#1 == 0)"; js inline "(#1 === 0)" }
private extern decrstd/core/decr.1: (i : ssize_t) -> ssize_t(ii: ssize_t : ssize_tstd/core/types/ssize_t: V ) : ssize_tstd/core/types/ssize_t: V { inline "(#1 - 1)" }
private extern incrstd/core/incr.1: (i : ssize_t) -> ssize_t(ii: ssize_t : ssize_tstd/core/types/ssize_t: V ) : ssize_tstd/core/types/ssize_t: V { inline "(#1 + 1)" }



// ----------------------------------------------------------------------------
// int64
// ----------------------------------------------------------------------------

// clamp an `:int` to fit in an `:int64_t`.
extern int64std/core/int64: (i : int) -> int64( ii: int : intstd/core/types/int: V) : int64std/core/types/int64: V {
  c  "kk_integer_clamp64"
  cs "Primitive.IntToInt64"
  js "$std_core._int_clamp64"
}

// Convert an `:int64_t` to an `:int`.
inline extern intstd/core/int: (i : int64) -> int( i : int64std/core/types/int64: V ) : intstd/core/types/int: V {
  c  "kk_integer_from_int64"
  cs inline "(new BigInteger(#1))"
  js "$std_core._int_from_int64"
}

// ----------------------------------------------------------------------------
// intptr_t
// ----------------------------------------------------------------------------

// clamp an `:int` to fit in an `:intptr_t`. 
extern intptr_tstd/core/intptr_t: (i : int) -> intptr_t( ii: int : intstd/core/types/int: V) : intptr_tstd/core/types/intptr_t: V {
  c  "kk_integer_clamp_intptr_t"
  cs "Primitive.IntToInt64"
  js "$std_core._int_clamp64"
}

// Convert an `:intptr_t` to an `:int`.
inline extern intstd/core/int: (i : intptr_t) -> int( i : intptr_tstd/core/types/intptr_t: V ) : intstd/core/types/int: V {
  c  "kk_integer_from_intptr_t"
  cs inline "(new BigInteger(#1))"
  js "$std_core._int_from_int64"
}

// ----------------------------------------------------------------------------
// Byte
// ----------------------------------------------------------------------------

// clamp an `:int` to fit in a `:byte`.
extern bytestd/core/byte: (i : int) -> byte( ii: int : intstd/core/types/int: V) : bytestd/core/types/byte: V {
  c  "kk_integer_clamp_byte"
  cs inline "Primitive.IntToByte"
  js "$std_core._int_clamp_byte"
}

// Convert an `:byte` to an `:int`.
inline extern intstd/core/int: (i : byte) -> int( i : bytestd/core/types/byte: V ) : intstd/core/types/int: V {
  c  "kk_integer_from_byte"
  cs inline "(new BigInteger(#1))"
  js "$std_core._int_from_int32"
}

// ----------------------------------------------------------------------------
// Parse numbers
// ----------------------------------------------------------------------------

// Parse an integer using `parseInt`. If an illegal digit character is encountered the
// `default` value is returned. An empty string will also result in `default`.
fun parse-int-defaultstd/core/parse-int-default: (s : string, default : ?int, hex : ?bool) -> int( ss: string : stringstd/core/types/string: V, defaultdefault: ?int : intstd/core/types/optional: V -> V = 0, hexhex: ?bool : boolstd/core/types/optional: V -> V = Falsestd/core/types/False: bool ) : intstd/core/types/int: V {
  if (std/core/types/True: boolss: string.is-emptystd/core/is-empty.2: (s : string) -> bool) then defaultdefault: int else ss: string.parse-intstd/core/parse-int: (s : string, hex : ?bool) -> maybe<int>(hexhex: bool).maybestd/core/maybe.1: forall<a> (m : maybe<a>, nothing : a) -> a(defaultdefault: int) }

// Parse an integer after trimming whitespace.
// If an illegal digit character is encountered `Nothing` is returned.
// An empty string will result in `Just(0)`.
// A string can start with a `-` sign for negative numbers,
// and with `0x` or `0X` for hexadecimal numbers (in which case the `hex` parameter is ignored).
fun parse-intstd/core/parse-int: (s : string, hex : ?bool) -> maybe<int>( ss: string : stringstd/core/types/string: V, hexhex: ?bool : boolstd/core/types/optional: V -> V = Falsestd/core/types/False: bool) : maybestd/core/types/maybe: V -> V<intstd/core/types/int: V> {
  ss: string.trimstd/core/trim: (s : string) -> string.xparse-intstd/core/xparse-int: (s : string, hex : bool) -> maybe<int>(hexhex: bool)
}

private extern xparse-intstd/core/xparse-int: (s : string, hex : bool) -> maybe<int>( ss: string : stringstd/core/types/string: V, hexhex: bool : boolstd/core/types/bool: V ) : maybestd/core/types/maybe: V -> V<intstd/core/types/int: V> {
  c  "kk_integer_xparse"
  cs "Primitive.IntParse"
  js "_int_parse"
}


// ----------------------------------------------------------------------------
// Doubles 
// ----------------------------------------------------------------------------

inline extern (==)std/core/(==): (double, double) -> bool : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> boolstd/core/types/bool: V  { inline "(#1 == #2)"; js inline "(#1 === #2)" }
inline extern (!=)std/core/(!=): (double, double) -> bool : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> boolstd/core/types/bool: V  { inline "(#1 != #2)"; js inline "(#1 !== #2)" }
inline extern (<=)std/core/(<=): (double, double) -> bool : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> boolstd/core/types/bool: V  { inline "(#1 <= #2)" }
inline extern (>=)std/core/(>=): (double, double) -> bool : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> boolstd/core/types/bool: V  { inline "(#1 >= #2)" }
inline extern (<)std/core/(<): (double, double) -> bool  : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> boolstd/core/types/bool: V  { inline "(#1 < #2)" }
inline extern (>)std/core/(>): (double, double) -> bool  : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> boolstd/core/types/bool: V  { inline "(#1 > #2)" }

inline extern (+)std/core/(+): (double, double) -> double : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> doublestd/core/types/double: V { inline "(#1 + #2)" }
inline extern (-)std/core/(-): (double, double) -> double : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> doublestd/core/types/double: V { inline "(#1 - #2)" }
inline extern (*)std/core/(*): (double, double) -> double : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> doublestd/core/types/double: V { inline "(#1 * #2)" }
inline extern (/)std/core/(/): (double, double) -> double : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> doublestd/core/types/double: V { inline "(#1 / #2)" }
inline extern (%)std/core/(%): (double, double) -> double : (doublestd/core/types/double: V,doublestd/core/types/double: V) -> doublestd/core/types/double: V { c inline "fmod(#1,#2)"; inline "(#1 % #2)" }

fun comparestd/core/compare.3: (x : double, y : double) -> order( xx: double : doublestd/core/types/double: V, yy: double : doublestd/core/types/double: V) : orderstd/core/types/order: V
{
  if (std/core/types/True: boolxx: double <std/core/(<).4: (double, double) -> bool yy: double) then Ltstd/core/types/Lt: order
  elif (std/core/types/True: boolxx: double >std/core/(>).2: (double, double) -> bool yy: double) then Gtstd/core/types/Gt: order
  else Eqstd/core/types/Eq: order
}


// Is the value negative?
fun is-negstd/core/is-neg.3: (d : double) -> bool( dd: double : doublestd/core/types/double: V ) : boolstd/core/types/bool: V {
  dd: double <std/core/(<).4: (double, double) -> bool 0.0
}

// Is the value positive?
fun is-posstd/core/is-pos.3: (d : double) -> bool( dd: double : doublestd/core/types/double: V ) : boolstd/core/types/bool: V {
  dd: double >std/core/(>).2: (double, double) -> bool 0.0
}

// Is the value zero?
fun is-zerostd/core/is-zero.2: (d : double) -> bool( dd: double : doublestd/core/types/double: V ) : boolstd/core/types/bool: V {
  dd: double ==std/core/(==).2: (double, double) -> bool 0.0
}

fun signstd/core/sign.1: (d : double) -> order( dd: double : doublestd/core/types/double: V ) : orderstd/core/types/order: V {
  if (std/core/types/True: booldd: double<std/core/(<).4: (double, double) -> bool0.0) then Ltstd/core/types/Lt: order elif (std/core/types/True: booldd: double>std/core/(>).2: (double, double) -> bool0.0) then Gtstd/core/types/Gt: order else Eqstd/core/types/Eq: order
}

// Negate a `:double`.
inline extern (~)std/core/(~): (d : double) -> double(d : doublestd/core/types/double: V ) : doublestd/core/types/double: V {
  inline "(-#1)"  // inline so `~0.0` becomes negative zero
}


// convert a `:double` to an `:int` using `round` to round to its nearest integer.
// (rounding to an even number on a tie)
// Returns `0` if the argument is not `finite?`.
inline extern intstd/core/int: (d : double) -> int(d : doublestd/core/types/double: V ) : intstd/core/types/int: V {
  c  "kk_integer_from_double"
  cs "Primitive.IntDouble"
  js "$std_core._int_double"
}


// Returns the value `d`  raised to the power `p` .
inline extern (^)std/core/(^): (d : double, p : double) -> double : (d:doublestd/core/types/double: V,p:doublestd/core/types/double: V) -> doublestd/core/types/double: V {
  c inline "pow(#1,#2)"
  cs "Math.Pow"
  js "Math.pow"
}

// Return the absolute value of a `:double` `d`
inline extern absstd/core/abs: (d : double) -> double : (d:doublestd/core/types/double: V) -> doublestd/core/types/double: V  {
  c inline "kk_double_abs(#1)"
  cs "Math.Abs"
  js "Math.abs"
}

// Returns the smallest of two doubles
fun minstd/core/min.1: (x : double, y : double) -> double( xx: double : doublestd/core/types/double: V, yy: double : doublestd/core/types/double: V ) : doublestd/core/types/double: V { if (std/core/types/True: boolxx: double <=std/core/(<=).4: (double, double) -> bool yy: double) then xx: double else yy: double }

// Returns the largest of two doubles
fun maxstd/core/max.1: (x : double, y : double) -> double( xx: double : doublestd/core/types/double: V, yy: double : doublestd/core/types/double: V ) : doublestd/core/types/double: V { if (std/core/types/True: boolxx: double >=std/core/(>=).3: (double, double) -> bool yy: double) then xx: double else yy: double }

// Returns the smallest element of a list of doubles (or `0` for the empty list)
fun minimumstd/core/minimum.1: (xs : list<double>) -> double( xsxs: list<double> : liststd/core/list: V -> V<doublestd/core/types/double: V> ) : doublestd/core/types/double: V
{
  match(xsxs: list<double>) {
    Nilstd/core/Nil: forall<a> list<a> -> 0.0
    Consstd/core/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: double,xxxx: list<double>) -> xxxx: list<double>.foldlstd/core/foldl: forall<a,b,e> (list<a>, b, (b, a) -> e b) -> e b( xx: double, minstd/core/min.1: (x : double, y : double) -> double )
  }
}

// Returns the largest element of a list of doubles (or `0` for the empty list)
fun maximumstd/core/maximum.1: (xs : list<double>) -> double( xsxs: list<double> : liststd/core/list: V -> V<doublestd/core/types/double: V> ) :