/*---------------------------------------------------------------------------
  Copyright 2012-2023, 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.
---------------------------------------------------------------------------*/

// 32-bit signed integers.
//
// Using 32-bit signed two's complement representation with wrapping on overflow,
// e.g. `max-int32 + 1.int32 == min-int32`.
module std/num/int32std/num/int32

import std/core/undivstd/core/undiv

extern import
  c  file "int32-inline.c"

// The maximal integer value before overflow happens
pub val max-int32std/num/int32/max-int32: int32 : int32std/core/types/int32: V = 0x7FFF_FFFFliteral: int
dec = 2147483647
hex32= 0x7FFFFFFF
bit32= 0b01111111111111111111111111111111
.int32std/num/int32/int32: (i : int) -> int32
// The minimal integer value before underflow happens pub val min-int32std/num/int32/min-int32: int32 : int32std/core/types/int32: V = -0x8000_0000literal: int
dec = -2147483648
hex32= 0x80000000
bit32= 0b10000000000000000000000000000000
.int32std/num/int32/int32: (i : int) -> int32
// The number of bits in an `:int32` (always 32) pub val bits-int32std/num/int32/bits-int32: int32 : int32std/core/types/int32: V = 32literal: int
dec = 32
hex8 = 0x20
bit8 = 0b00100000
.int32std/num/int32/int32: (i : int) -> int32
// Convert an `:int32` to a string pub fun showstd/num/int32/show: (i : int32) -> string( ii: int32 : int32std/core/types/int32: V )result: -> total string : stringstd/core/types/string: V showstd/core/int/show: (i : int) -> string(ii: int32.intstd/num/int32/int: (i : int32) -> int) // Convenient shorthand to `int32`, e.g. `1234.i32` pub inline fip fun i32std/num/int32/i32: (i : int) -> int32( ii: int : intstd/core/types/int: V )result: -> total int32 : int32std/core/types/int32: V ii: int.int32std/num/int32/int32: (i : int) -> 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`. // Needed for control flow contexts in `module std/core/hnd` pub inline fip extern int32std/num/int32/int32: (i : int) -> int32( i : intstd/core/types/int: V) : int32std/core/types/int32: V c "kk_integer_clamp32" cs "Primitive.IntToInt32" js "$std_core_types._int_clamp32" // Convert an `:int32` to an `:int`. pub inline fip extern intstd/num/int32/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_types._int_from_int32" /* // Convert a `:float64` to an `:int32`. The float64 is clamped to the // minimum or maximum `:int32` value if it is outside the range. A `nan` is // mapped to `0`. pub inline extern int32( f : float64 ) : int32 cs "Primitive.DoubleToInt32" js "$std_core_types._double_to_int32" */ // Convert an `:int32` to a boolean. pub fip fun boolstd/num/int32/bool: (i : int32) -> bool( ii: int32 : int32std/core/types/int32: V )result: -> total bool : boolstd/core/types/bool: V (ii: int32!=std/num/int32/(!=): (x : int32, y : int32) -> boolzerostd/num/int32/zero: int32) // Convert a boolean to an `:int32`. pub fip fun bool/int32std/num/int32/bool/int32: (b : bool) -> int32( bb: bool : boolstd/core/types/bool: V )result: -> total int32 : int32std/core/types/int32: V if (bb: bool) then onestd/num/int32/one: int32 else zerostd/num/int32/zero: int32 // Show an `:int32` in hexadecimal notation // The `width` parameter specifies how wide the hex value is where `'0'` is used to align.\ // The `use-capitals` parameter (= `True`) determines if captical letters should be used to display the hexadecimal digits.\ // The `pre` (=`"0x"`) is an optional prefix for the number (goes between the sign and the number). pub fun show-hexstd/num/int32/show-hex: (i : int32, width : ? int, use-capitals : ? bool, pre : ? string) -> string( ii: int32 : int32std/core/types/int32: V, widthwidth: ? int : intstd/core/types/int: V = 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
, use-capitalsuse-capitals: ? bool : boolstd/core/types/bool: V = Truestd/core/types/True: bool, prepre: ? string : stringstd/core/types/string: V = "0x"literal: string
count= 2
)result: -> total string : stringstd/core/types/string: V std/core/show/show-hexstd/core/show/show-hex: (i : int, width : ? int, use-capitals : ? bool, pre : ? string) -> string(ii: int32.intstd/num/int32/int: (i : int32) -> int,widthwidth: int,use-capitalsuse-capitals: bool,prepre: string
) // Convert an `:int` to `:int32` but interpret the `int` as an unsigned 32-bit value. // `i` is clamped between `0` and `0xFFFFFFFF`.\ // `0x7FFF_FFFF.uint32 == 0x7FFF_FFFF.int32 == max-int32`\ // `0x8000_0000.uint32 == -0x8000_0000.int32 == min-int32`\ // `0xFFFF_FFFF.uint32 == -1.int32`\ pub fun uint32std/num/int32/uint32: (i : int) -> int32( ii: int : intstd/core/types/int: V )result: -> total int32 : int32std/core/types/int32: V if (ii: int >std/core/int/(>): (x : int, y : int) -> bool max-int32std/num/int32/max-int32: int32.intstd/num/int32/int: (i : int32) -> int) then (ii: int -std/core/int/(-): (x : int, y : int) -> int 0x1_0000_0000literal: int
dec = 4294967296
hex64= 0x0000000100000000
bit64= 0b0000000000000000000000000000000100000000000000000000000000000000
).int32std/num/int32/int32: (i : int) -> int32 else ii: int.int32std/num/int32/int32: (i : int) -> int32
// Convert an `:int32` to an `:int` but interpret the `:int32` as a 32-bit unsigned value. pub fun uintstd/num/int32/uint: (i : int32) -> int( ii: int32 : int32std/core/types/int32: V )result: -> total int : intstd/core/types/int: V if (ii: int32.is-negstd/num/int32/is-neg: (i : int32) -> bool) then 0x1_0000_0000literal: int
dec = 4294967296
hex64= 0x0000000100000000
bit64= 0b0000000000000000000000000000000100000000000000000000000000000000
+std/core/int/(+): (x : int, y : int) -> int ii: int32.intstd/num/int32/int: (i : int32) -> int else ii: int32.intstd/num/int32/int: (i : int32) -> int
// Show an `:int32` in hexadecimal notation interpreted as an unsigned 32-bit value. // The `width` parameter specifies how wide the hex value is where `'0'` is used to align.\ // The `use-capitals` parameter (= `True`) determines if captical letters should be used to display the hexadecimal digits.\ // The `pre` (=`"0x"`) is an optional prefix for the number. pub fun show-hex32std/num/int32/show-hex32: (i : int32, width : ? int, use-capitals : ? bool, pre : ? string) -> string( ii: int32 : int32std/core/types/int32: V, widthwidth: ? int : intstd/core/types/int: V = 8literal: int
dec = 8
hex8 = 0x08
bit8 = 0b00001000
, use-capitalsuse-capitals: ? bool : boolstd/core/types/bool: V = Truestd/core/types/True: bool, prepre: ? string : stringstd/core/types/string: V = "0x"literal: string
count= 2
)result: -> total string : stringstd/core/types/string: V std/core/show/show-hexstd/core/show/show-hex: (i : int, width : ? int, use-capitals : ? bool, pre : ? string) -> string(ii: int32.uintstd/num/int32/uint: (i : int32) -> int,widthwidth: int,use-capitalsuse-capitals: bool,prepre: string
) // Are two 32-bit integers equal? pub inline fip extern (==)std/num/int32/(==): (x : int32, y : int32) -> bool(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : boolstd/core/types/bool: V inline "(#1 == #2)" js inline "(#1 === #2)" // Are two 32-bit integers not equal? pub inline fip extern (!=)std/num/int32/(!=): (x : int32, y : int32) -> bool(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : boolstd/core/types/bool: V inline "(#1 != #2)" js inline "(#1 !== #2)" // Is the first 32-bit integer smaller or equal to the second? pub inline fip extern (<=)std/num/int32/(<=): (x : int32, y : int32) -> bool(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : boolstd/core/types/bool: V inline "(#1 <= #2)" // Is the first 32-bit integer larger or equal to the second? pub inline fip extern (>=)std/num/int32/(>=): (x : int32, y : int32) -> bool(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : boolstd/core/types/bool: V inline "(#1 >= #2)" // Is the first 32-bit integer smaller than the second? pub inline fip extern (<)std/num/int32/(<): (x : int32, y : int32) -> bool(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : boolstd/core/types/bool: V inline "(#1 < #2)" // Is the first 32-bit integer larger than the second? pub inline fip extern (>)std/num/int32/(>): (x : int32, y : int32) -> bool(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : boolstd/core/types/bool: V inline "(#1 > #2)" // Add two 32-bit integers. pub inline fip extern (+)std/num/int32/(+): (x : int32, y : int32) -> int32(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : int32std/core/types/int32: V c inline "(int32_t)((uint32_t)#1 + (uint32_t)#2)" // avoid UB js inline "((#1 + #2)|0)" // Subtract two 32-bit integers. pub inline fip extern (-)std/num/int32/(-): (x : int32, y : int32) -> int32(x : int32std/core/types/int32: V, y : int32std/core/types/int32: V) : int32std/core/types/int32: V c inline "(int32_t)((uint32_t)#1 - (uint32_t)#2)" // avoid UB js inline "((#1 - #2)|0)" // Is the 32-bit integer negative? pub inline fip extern is-negstd/num/int32/is-neg: (i : int32) -> bool( i : int32std/core/types/int32: V ) : boolstd/core/types/bool: V inline "0 > #1" // Is the 32-bit integer positive (i.e. larger than zero)? pub inline fip extern is-posstd/num/int32/is-pos: (i : int32) -> bool( i : int32std/core/types/int32: V ) : boolstd/core/types/bool: V inline "0 < #1" // Is the 32-bit integer equal to zero? pub inline fip extern is-zerostd/num/int32/is-zero: (i : int32) -> bool( i : int32std/core/types/int32: V ) : boolstd/core/types/bool: V inline "0 == #1" js inline "0 === #1" // The zero 32-bit integer. pub val zerostd/num/int32/zero: int32 = 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32
// The 32-bit integer with value 1. pub val onestd/num/int32/one: int32 = 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32
// Compare the argument against zero. pub fip fun signstd/num/int32/sign: (i : int32) -> order( ii: int32 : int32std/core/types/int32: V )result: -> total order : orderstd/core/types/order: V if (ii: int32.is-posstd/num/int32/is-pos: (i : int32) -> bool) then Gtstd/core/types/Gt: order elif (ii: int32.is-negstd/num/int32/is-neg: (i : int32) -> bool) then Ltstd/core/types/Lt: order else Eqstd/core/types/Eq: order // Returns `true` if the integer `i` is an odd number. pub fip fun is-oddstd/num/int32/is-odd: (i : int32) -> bool( ii: int32 : int32std/core/types/int32: V )result: -> total bool : boolstd/core/types/bool: V andstd/num/int32/and: (int32, int32) -> int32(ii: int32,1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32) ==std/num/int32/(==): (x : int32, y : int32) -> bool 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32
// Returns `true` if the integer `i` is an even number. pub fip fun is-evenstd/num/int32/is-even: (i : int32) -> bool( ii: int32 : int32std/core/types/int32: V )result: -> total bool : boolstd/core/types/bool: V andstd/num/int32/and: (int32, int32) -> int32(ii: int32,1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32) ==std/num/int32/(==): (x : int32, y : int32) -> bool 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32
// Increment a 32-bit integer. pub fip fun incstd/num/int32/inc: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V ii: int32 +std/num/int32/(+): (x : int32, y : int32) -> int32 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32
// Decrement a 32-bit integer. pub fip fun decstd/num/int32/dec: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V ii: int32 -std/num/int32/(-): (x : int32, y : int32) -> int32 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32
// Multiply two 32-bit integers. pub inline fip extern (*)std/num/int32/(*): (int32, int32) -> int32 : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V inline "(int32_t)((uint32_t)#1 * (uint32_t)#2)" // avoid UB js "$std_core_types._int32_multiply" pub fip fun cmpstd/num/int32/cmp: (x : int32, y : int32) -> order( xx: int32 : int32std/core/types/int32: V, yy: int32 : int32std/core/types/int32: V)result: -> total order : orderstd/core/types/order: V if (xx: int32 <std/num/int32/(<): (x : int32, y : int32) -> bool yy: int32) then Ltstd/core/types/Lt: order elif (xx: int32 >std/num/int32/(>): (x : int32, y : int32) -> bool yy: int32) then Gtstd/core/types/Gt: order else Eqstd/core/types/Eq: order // Return the absolute value of an integer. // Raises an exception if the `:int32` is `min-int32` // (since the negation of `min-int32` equals itself and is still negative) pub fun absstd/num/int32/abs: (i : int32) -> exn int32( ii: int32 : int32std/core/types/int32: V )result: -> exn int32 : exnstd/core/exn/exn: (E, V) -> V int32std/core/types/int32: V if (!std/core/types/bool/(!): (b : bool) -> exn boolii: int32.is-negstd/num/int32/is-neg: (i : int32) -> exn bool) then ii: int32 elif (ii: int32 >std/num/int32/(>): (x : int32, y : int32) -> exn bool min-int32std/num/int32/min-int32: int32) then negatestd/num/int32/negate: (i : int32) -> exn int32(ii: int32) else throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn int32( "std/num/int32/abs: cannot make min-int32 into a positive int32 without overflow"literal: string
count= 79
) // Return the absolute value of an integer. // Returns 0 if the `:int32` is `min-int32` // (since the negation of `min-int32` equals itself and is still negative) pub fip fun abs0std/num/int32/abs0: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V if (!std/core/types/bool/(!): (b : bool) -> boolii: int32.is-negstd/num/int32/is-neg: (i : int32) -> bool) then ii: int32 elif (ii: int32 >std/num/int32/(>): (x : int32, y : int32) -> bool min-int32std/num/int32/min-int32: int32) then negatestd/num/int32/negate: (i : int32) -> int32(ii: int32) else 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32
// Take the bitwise _and_ of two `:int32`s pub inline fip extern andstd/num/int32/and: (int32, int32) -> int32 : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V inline "(#1 & #2)"; // Take the bitwise _or_ of two `:int32`s pub inline fip extern orstd/num/int32/or: (int32, int32) -> int32 : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V inline "(#1 | #2)"; // Take the bitwise _xor_ of two `:int32`s pub inline fip extern xorstd/num/int32/xor: (int32, int32) -> int32 : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V inline "(#1 ^ #2)"; // Take the bitwise _xor_ of two `:int32`s pub fip fun(^)std/num/int32/(^): (x : int32, y : int32) -> int32( xx: int32 : int32std/core/types/int32: V, yy: int32 : int32std/core/types/int32: V)result: -> total int32 : int32std/core/types/int32: V xorstd/num/int32/xor: (int32, int32) -> int32(xx: int32,yy: int32) // Bitwise _not_ of an `:int32`, i.e. flips all bits. pub inline fip extern notstd/num/int32/not: (i : int32) -> int32 : ( i : int32std/core/types/int32: V ) -> int32std/core/types/int32: V inline "(~#1)" // Shift an `:int32` `i` to the left by `n & 31` bits. inline fip extern shl32std/num/int32/shl32: (int32, int32) -> int32 : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V c inline "kk_shl32(#1,#2)" js inline "#1 << #2" // javascript masks the shift already // Shift an `:int32` `i` to the left by `n & 31` bits. pub fip fun shlstd/num/int32/shl: (i : int32, shift : int) -> int32( ii: int32 : int32std/core/types/int32: V, shiftshift: int : intstd/core/types/int: V )result: -> total int32 : int32std/core/types/int32: V shl32std/num/int32/shl32: (int32, int32) -> int32( ii: int32, shiftshift: int.int32std/num/int32/int32: (i : int) -> int32 ) // Logical shift an `:int32` to the right by `n % 32` bits. Shift in zeros from the left. inline fip extern shr32std/num/int32/shr32: (int32, int32) -> int32 : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V c inline "(int32_t)kk_shr32(#1,#2)" cs inline "(Int32)(((UInt32)#1)>>#2)" js inline "#1 >>> #2" // Logical shift an `:int32` to the right by `n % 32` bits. Shift in zeros from the left. pub fip fun shrstd/num/int32/shr: (i : int32, shift : int) -> int32( ii: int32 : int32std/core/types/int32: V, shiftshift: int : intstd/core/types/int: V )result: -> total int32 : int32std/core/types/int32: V shr32std/num/int32/shr32: (int32, int32) -> int32( ii: int32, shiftshift: int.int32std/num/int32/int32: (i : int) -> int32 ) // Arithmetic shift an `:int32` to the right by `n % 32` bits. Shifts in the sign bit from the left. inline fip extern sar32std/num/int32/sar32: (int32, int32) -> int32 : (int32std/core/types/int32: V,int32std/core/types/int32: V) -> int32std/core/types/int32: V c inline "kk_sar32(#1,#2)" cs inline "(#1>>#2)" js inline "#1 >> #2" // Arithmetic shift an `:int32` to the right by `n % 32` bits. Shifts in the sign bit from the left. pub fip fun sarstd/num/int32/sar: (i : int32, shift : int) -> int32( ii: int32 : int32std/core/types/int32: V, shiftshift: int : intstd/core/types/int: V )result: -> total int32 : int32std/core/types/int32: V sar32std/num/int32/sar32: (int32, int32) -> int32( ii: int32, shiftshift: int.int32std/num/int32/int32: (i : int) -> int32 ) // Return the top 16-bits of an `:int32`. // Preserves the sign. pub fun histd/num/int32/hi: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V sar32std/num/int32/sar32: (int32, int32) -> int32(ii: int32,16literal: int
dec = 16
hex8 = 0x10
bit8 = 0b00010000
.int32std/num/int32/int32: (i : int) -> int32
) // Return the low 16-bits of an `:int32`. pub fun lostd/num/int32/lo: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V andstd/num/int32/and: (int32, int32) -> int32(ii: int32,0xFFFFliteral: int
dec = 65535
hex32= 0x0000FFFF
bit32= 0b00000000000000001111111111111111
.int32std/num/int32/int32: (i : int) -> int32
) // Create an `:int32` from the give `hi` and `lo` 16-bit numbers. // Preserves the sign of `hi`. pub fun hilo/int32std/num/int32/hilo/int32: (hi : int32, lo : int32) -> int32( hihi: int32 : int32std/core/types/int32: V, lolo: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V orstd/num/int32/or: (int32, int32) -> int32( shl32std/num/int32/shl32: (int32, int32) -> int32(hihi: int32,16literal: int
dec = 16
hex8 = 0x10
bit8 = 0b00010000
.int32std/num/int32/int32: (i : int) -> int32), andstd/num/int32/and: (int32, int32) -> int32(lolo: int32,0xFFFFliteral: int
dec = 65535
hex32= 0x0000FFFF
bit32= 0b00000000000000001111111111111111
.int32std/num/int32/int32: (i : int) -> int32)
) // Bitwise rotate an `:int32` `n % 32` bits to the left. inline fip extern rotl32std/num/int32/rotl32: (i : int32, n : int32) -> int32( i : int32std/core/types/int32: V, n : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "(int32_t)kk_bits_rotl32(#1,#2)" js "$std_core_types._int32_rotl" // Bitwise rotate an `:int32` `n % 32` bits to the left. pub fip fun rotlstd/num/int32/rotl: (i : int32, shift : int) -> int32( ii: int32 : int32std/core/types/int32: V, shiftshift: int : intstd/core/types/int: V )result: -> total int32 : int32std/core/types/int32: V rotl32std/num/int32/rotl32: (i : int32, n : int32) -> int32( ii: int32, shiftshift: int.int32std/num/int32/int32: (i : int) -> int32 ) // Bitwise rotate an `:int32` `n % 32` bits to the right. inline fip extern rotr32std/num/int32/rotr32: (i : int32, n : int32) -> int32( i : int32std/core/types/int32: V, n : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "(int32_t)kk_bits_rotr32(#1,#2)" js "$std_core_types._int32_rotr" // Bitwise rotate an `:int32` `n % 32` bits to the right. pub fip fun rotrstd/num/int32/rotr: (i : int32, shift : int) -> int32( ii: int32 : int32std/core/types/int32: V, shiftshift: int : intstd/core/types/int: V )result: -> total int32 : int32std/core/types/int32: V rotr32std/num/int32/rotr32: (i : int32, n : int32) -> int32( ii: int32, shiftshift: int.int32std/num/int32/int32: (i : int) -> int32 ) // Count trailing zero bits. Returns 32 if `i` is zero. pub inline fip extern ctzstd/num/int32/ctz: (i : int32) -> int( i : int32std/core/types/int32: V ) : intstd/core/types/int: V c inline "kk_integer_from_small(kk_bits_ctz32(#1))" js "$std_core_types._int32_ctz" // Count leading zero bits. Returns 32 if `i` is zero. pub inline fip extern clzstd/num/int32/clz: (i : int32) -> int( i : int32std/core/types/int32: V ) : intstd/core/types/int: V c inline "kk_integer_from_small(kk_bits_clz32(#1))" js "$std_core_types._int32_clz" // Count leading redundant sign bits (i.e. the number of bits // following the most significant bit that are identical to it). pub inline fip extern clrsbstd/num/int32/clrsb: (i : int32) -> int( i : int32std/core/types/int32: V ) : intstd/core/types/int: V c inline "kk_integer_from_small(kk_bits_clrsb32(#1))" js "$std_core_types._int32_clrsb" // Count number of 1-bits (also known as _population count_ or _hamming weight_). pub inline fip extern popcountstd/num/int32/popcount: (i : int32) -> int( i : int32std/core/types/int32: V ) : intstd/core/types/int: V c inline "kk_integer_from_small(kk_bits_popcount32(#1))" js "$std_core_types._int32_bits_popcount" // Is the `popcount` even? pub inline fip extern paritystd/num/int32/parity: (i : int32) -> bool( i : int32std/core/types/int32: V ) : boolstd/core/types/bool: V c inline "kk_bits_parity32(#1)" js "$std_core_types._int32_parity" // Reverse the bytes in an `:int32`. pub inline fip extern byte-reversestd/num/int32/byte-reverse: (i : int32) -> int32( i : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "kk_bits_bswap32(#1)" js "$std_core_types._int32_bswap" // Reverse the bits in an `:int32`. pub inline fip extern bit-reversestd/num/int32/bit-reverse: (i : int32) -> int32( i : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "kk_bits_reverse32(#1)" js "$std_core_types._int32_breverse" // Bit gather (also known as _pext_ or parallel bit extract). // For each 1-bit in mask `m`, extract the corresponding bit from `i` and write it // into contiguous lower bits in the result. The remaining bits in the result are zero. // // `bgather(0x1234.int32, 0x0F0F.int32).show-hex == "0x24"` // // Here is how to decode a 4-byte utf-8 encoded character `i`: // // `bgather(i,0x3F3F3F3F.int32)` pub inline fip extern bit-gatherstd/num/int32/bit-gather: (i : int32, m : int32) -> int32( i : int32std/core/types/int32: V, m : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "kk_bits_gather32(#1,#2)" // Bit scatter (also known as _pdep_ or parallel bit deposit). // For each 1-bit in mask `m`, set the corresponding bit in the result from the // contiguous lower bits of `i`. Any bits not set according to the mask are set to zero. // // `bscatter(0x1234.int32, 0x0F0F.int32).show-hex == "0x304"` pub inline fip extern bit-scatterstd/num/int32/bit-scatter: (i : int32, m : int32) -> int32( i : int32std/core/types/int32: V, m : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "kk_bits_scatter32(#1,#2)" // Interleave the hi 16-bits with the lo 16-bits of the argument `i` such // that the hi bits are spread over the odd bits and the lo bits over the even bits // of the result (starting at bit 0). `bit-zip(x.and(0xFFFF)) == bit-scatter(x,0x5555_5555.int32)` pub inline fip extern bit-zipstd/num/int32/bit-zip: (i : int32) -> int32( i : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "kk_bits_zip32(#1)" // De-interleave the bits in `i` such that the odd bits are gathered in the // hi 16-bits of the result, and the even bits (starting at bit 0) // end up in the lo 16-bits of the result. `bit-unzip(x).and(0xFFFF) == bit-gather(x,0x5555_5555.int32)` pub inline fip extern bit-unzipstd/num/int32/bit-unzip: (i : int32) -> int32( i : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "kk_bits_unzip32(#1)" // Or-combine: for every byte `b` in the argument `i`, the corresponding // byte in the result becomes 0 if `b==0`, and `0xFF` otherwise. pub inline fip extern orcstd/num/int32/orc: (i : int32) -> int32( i : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "kk_bits_orc32(#1)" // Carry-less multiplication (or _xor_ multiplication). // See also <https://en.wikipedia.org/wiki/Carry-less_product> // Interesting cases: // - `clmul(x,x)` : _bit spread_, where a zero bit is put in between each input bit. // `clmul(x,x) == bit-scatter(x,0x5555_5555.int32) == bit-zip(and(x,0xFFFF))` // - `clmul(x,-1.int32)`: _prefix xor_, where each bit is the parity of the corresponding input bit // and all input bits before it (the _xor sum_). // `clmul(x,-1.int32).shr(31).bool == x.parity` pub inline fip extern clmulstd/num/int32/clmul: (x : int32, y : int32) -> int32( x : int32std/core/types/int32: V, y : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "(int32_t)kk_clmul32((uint32_t)#1,(uint32_t)#2)" // Wide carry-less multiplication (or _xor_ multiplication) to `(hi,lo)`. // where `(hi,lo).int == hi.int * 0x1_0000_0000 + lo.uint` // See also <https://en.wikipedia.org/wiki/Carry-less_product> pub fip extern clmul_widestd/num/int32/clmul_wide: (x : int32, y : int32) -> (int32, int32)( xx: int32 : int32std/core/types/int32: V, yy: int32 : int32std/core/types/int32: V ) : (std/core/types/tuple2: (V, V) -> Vint32std/core/types/int32: V,int32std/core/types/int32: V) c "kk_clmul32x_wide" // Reverse carry-less multiplication. // Defined as `clmulr(x,y) == clmul(x.bit-reverse,y.bitreverse).bitreverse` // Also `clmulr(x,x) == zip(x.and(0xFFFF0000.int32)`, and // `zip(x) == clmul(x,x) | clmulr(x,x)`. pub inline fip extern clmulrstd/num/int32/clmulr: (x : int32, y : int32) -> int32( x : int32std/core/types/int32: V, y : int32std/core/types/int32: V ) : int32std/core/types/int32: V c inline "(int32_t)kk_clmulr32((uint32_t)#1,(uint32_t)#2)" // Return the minimum of two integers pub fip fun minstd/num/int32/min: (i : int32, j : int32) -> int32( ii: int32 : int32std/core/types/int32: V, jj: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V if (ii: int32 <=std/num/int32/(<=): (x : int32, y : int32) -> bool jj: int32) then ii: int32 else jj: int32 // Return the maximum of two integers pub fip fun maxstd/num/int32/max: (i : int32, j : int32) -> int32( ii: int32 : int32std/core/types/int32: V, jj: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V if (ii: int32 >=std/num/int32/(>=): (x : int32, y : int32) -> bool jj: int32) then ii: int32 else jj: int32 // Full 32x32 bit unsigned multiply to `(hi,lo)`. // where `umul(x,y).uint == x.uint * y.uint` pub fip extern umulstd/num/int32/umul: (i : int32, j : int32) -> (int32, int32)( ii: int32 : int32std/core/types/int32: V, jj: int32 : int32std/core/types/int32: V ) : (std/core/types/tuple2: (V, V) -> Vint32std/core/types/int32: V,int32std/core/types/int32: V) c "kk_umul32x_wide" js "$std_core_types._int32_umul" // Full 32x32 bit signed multiply to `(hi,lo)`. // where `imul(x,y).int == x.int * y.int` pub fip extern imulstd/num/int32/imul: (i : int32, j : int32) -> (int32, int32)( ii: int32 : int32std/core/types/int32: V, jj: int32 : int32std/core/types/int32: V ) : (std/core/types/tuple2: (V, V) -> Vint32std/core/types/int32: V,int32std/core/types/int32: V) c "kk_imul32x_wide" js "$std_core_types._int32_imul" // Convert a pair `(hi,lo)` to a signed integer, // where `(hi,lo).int == hi.int * 0x1_0000_0000 + lo.uint` pub fun hilo/intstd/num/int32/hilo/int: ((int32, int32)) -> int( (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)hihi: int32,lolo: int32)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) : (std/core/types/tuple2: (V, V) -> Vint32std/core/types/int32: V,int32std/core/types/int32: V) )result: -> total int : intstd/core/types/int: V hihi: int32.intstd/num/int32/int: (i : int32) -> int *std/core/int/(*): (int, int) -> int 0x1_0000_0000literal: int
dec = 4294967296
hex64= 0x0000000100000000
bit64= 0b0000000000000000000000000000000100000000000000000000000000000000
+std/core/int/(+): (x : int, y : int) -> int lolo: int32.uintstd/num/int32/uint: (i : int32) -> int
// Convert a pair `(hi,lo)` to an unsigned integer, // where `(hi,lo).uint == hi.uint * 0x1_0000_0000 + lo.uint` pub fun hilo/uintstd/num/int32/hilo/uint: ((int32, int32)) -> int( (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)hihi: int32,lolo: int32)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) : (std/core/types/tuple2: (V, V) -> Vint32std/core/types/int32: V,int32std/core/types/int32: V) )result: -> total int : intstd/core/types/int: V hihi: int32.uintstd/num/int32/uint: (i : int32) -> int *std/core/int/(*): (int, int) -> int 0x1_0000_0000literal: int
dec = 4294967296
hex64= 0x0000000100000000
bit64= 0b0000000000000000000000000000000100000000000000000000000000000000
+std/core/int/(+): (x : int, y : int) -> int lolo: int32.uintstd/num/int32/uint: (i : int32) -> int
// Truncated division (as in C). See also `(/):(x : int32, y : int32) -> int32`. pub fun cdivstd/num/int32/cdiv: (i : int32, j : int32) -> exn int32(ii: int32:int32std/core/types/int32: V, jj: int32:int32std/core/types/int32: V)result: -> exn int32 : exnstd/core/exn/exn: (E, V) -> V int32std/core/types/int32: V if (jj: int32.is-zerostd/num/int32/is-zero: (i : int32) -> exn bool) then throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn int32("std/num/int32/cdiv: modulus by zero"literal: string
count= 35
) elif (jj: int32 ==std/num/int32/(==): (x : int32, y : int32) -> exn bool -1literal: int
dec = -1
hex8 = 0xFF
bit8 = 0b11111111
.int32std/num/int32/int32: (i : int) -> exn int32 &&std/core/types/(&&): (x : bool, y : bool) -> exn bool ii: int32==std/num/int32/(==): (x : int32, y : int32) -> exn boolmin-int32std/num/int32/min-int32: int32) then throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn int32("std/num/int32/cdiv: modulus overflow in cdiv(min-int32, -1.int32)"literal: string
count= 65
) else unsafe-cdivstd/num/int32/unsafe-cdiv: (int32, int32) -> exn int32(ii: int32,jj: int32
) // Truncated modulus (as in C). See also `(%):(x : int32, y : int32) -> int32`. pub fun cmodstd/num/int32/cmod: (i : int32, j : int32) -> exn int32(ii: int32:int32std/core/types/int32: V, jj: int32:int32std/core/types/int32: V)result: -> exn int32 : exnstd/core/exn/exn: (E, V) -> V int32std/core/types/int32: V if (jj: int32.is-zerostd/num/int32/is-zero: (i : int32) -> exn bool) then throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn int32("std/num/int32/cmod: modulus by zero"literal: string
count= 35
) elif (jj: int32 ==std/num/int32/(==): (x : int32, y : int32) -> exn bool -1literal: int
dec = -1
hex8 = 0xFF
bit8 = 0b11111111
.int32std/num/int32/int32: (i : int) -> exn int32 &&std/core/types/(&&): (x : bool, y : bool) -> exn bool ii: int32==std/num/int32/(==): (x : int32, y : int32) -> exn boolmin-int32std/num/int32/min-int32: int32) then throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn int32("std/num/int32/cmod: modulus overflow in cmod(min-int32, -1.int32)"literal: string
count= 65
) else unsafe-cmodstd/num/int32/unsafe-cmod: (int32, int32) -> exn int32(ii: int32,jj: int32
) // Truncated division (as in C). See also `(/):(x : int32, y : int32) -> int32`. pub inline fip extern unsafe-cdivstd/num/int32/unsafe-cdiv: (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)" // Truncated modulus (as in C). See also `(%):(x : int32, y : int32) -> int32`. pub inline fip extern unsafe-cmodstd/num/int32/unsafe-cmod: (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)" // Convert an 32-bit integer to a float64. pub inline fip extern float64std/num/int32/float64: (i : int32) -> float64( i : int32std/core/types/int32: V) : float64std/core/types/float64: V c inline "(double)(#1)" cs inline "(double)(#1)" js inline "(#1)" // Negate a 32-bit integer pub fip fun negatestd/num/int32/negate: (i : int32) -> int32( ii: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V (0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32 -std/num/int32/(-): (x : int32, y : int32) -> int32 ii: int32
) // Negate an 32-bit integer pub fip fun (~)std/num/int32/(~): (i : int32) -> int32(ii: int32 : int32std/core/types/int32: V)result: -> total int32 : totalstd/core/types/total: E int32std/core/types/int32: V (0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32 -std/num/int32/(-): (x : int32, y : int32) -> int32 ii: int32
) /* Euclidean-0 division. 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`) * `D%(2^n) == D & ((2^n) - 1) ` (with `0 <= n <= 31`) Note that an interesting edge case is `min-int32 / -1` which equals `min-int32` since in modulo 32-bit arithmetic `min-int32 == -1 * min-int32 == -1 * (min-int32 / -1) + (min-int32 % -1)` satisfying property (1). Of course `(min-int32 + 1) / -1` is again positive (namely `max-int32`). See also _Division and modulus for computer scientists, Daan Leijen, 2001_ [pdf](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf) . */ pub fip fun (/)std/num/int32/(/): (x : int32, y : int32) -> int32( xx: int32 : int32std/core/types/int32: V, yy: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V if (yy: int32 ==std/num/int32/(==): (x : int32, y : int32) -> bool 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32) then returnreturn: int32 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32 if (yy: int32 ==std/num/int32/(==): (x : int32, y : int32) -> bool -1literal: int
dec = -1
hex8 = 0xFF
bit8 = 0b11111111
.int32std/num/int32/int32: (i : int) -> int32 &&std/core/types/(&&): (x : bool, y : bool) -> bool xx: int32==std/num/int32/(==): (x : int32, y : int32) -> boolmin-int32std/num/int32/min-int32: int32) returnreturn: int32 xx: int32 val qq: int32 = unsafe-cdivstd/num/int32/unsafe-cdiv: (int32, int32) -> int32(xx: int32,yy: int32) val rr: int32 = unsafe-cmodstd/num/int32/unsafe-cmod: (int32, int32) -> int32(xx: int32,yy: int32) if (rr: int32 >=std/num/int32/(>=): (x : int32, y : int32) -> bool 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32) then qq: int32 elif (yy: int32 >std/num/int32/(>): (x : int32, y : int32) -> bool 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32) then qq: int32 -std/num/int32/(-): (x : int32, y : int32) -> int32 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32 else qq: int32 +std/num/int32/(+): (x : int32, y : int32) -> int32 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
.int32std/num/int32/int32: (i : int) -> int32
// Euclidean-0 modulus. See `(/):(x : int32, y : int32) -> int32` division for more information. pub fip fun (%)std/num/int32/(%): (x : int32, y : int32) -> int32( xx: int32 : int32std/core/types/int32: V, yy: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V if (yy: int32 ==std/num/int32/(==): (x : int32, y : int32) -> bool 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32) then returnreturn: int32 xx: int32 if (yy: int32 ==std/num/int32/(==): (x : int32, y : int32) -> bool -1literal: int
dec = -1
hex8 = 0xFF
bit8 = 0b11111111
.int32std/num/int32/int32: (i : int) -> int32 &&std/core/types/(&&): (x : bool, y : bool) -> bool xx: int32==std/num/int32/(==): (x : int32, y : int32) -> boolmin-int32std/num/int32/min-int32: int32) returnreturn: int32 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32 val rr: int32 = unsafe-cmodstd/num/int32/unsafe-cmod: (int32, int32) -> int32(xx: int32,yy: int32) if (rr: int32 >=std/num/int32/(>=): (x : int32, y : int32) -> bool 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32) then rr: int32 elif (yy: int32 >std/num/int32/(>): (x : int32, y : int32) -> bool 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32) then (rr: int32+std/num/int32/(+): (x : int32, y : int32) -> int32yy: int32) else (rr: int32 -std/num/int32/(-): (x : int32, y : int32) -> int32 yy: int32
) pub fip fun divmodstd/num/int32/divmod: (x : int32, y : int32) -> (int32, int32)(xx: int32:int32std/core/types/int32: V,yy: int32:int32std/core/types/int32: V)result: -> total (int32, int32) : (std/core/types/tuple2: (V, V) -> Vint32std/core/types/int32: V,int32std/core/types/int32: V) if (yy: int32.is-zerostd/num/int32/is-zero: (i : int32) -> bool) then returnreturn: (int32, int32) (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)zerostd/num/int32/zero: int32,xx: int32)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) if (yy: int32 ==std/num/int32/(==): (x : int32, y : int32) -> bool -1literal: int
dec = -1
hex8 = 0xFF
bit8 = 0b11111111
.int32std/num/int32/int32: (i : int) -> int32 &&std/core/types/(&&): (x : bool, y : bool) -> bool xx: int32==std/num/int32/(==): (x : int32, y : int32) -> boolmin-int32std/num/int32/min-int32: int32) returnreturn: (int32, int32) (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)xx: int32,0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) val qq: int32 = unsafe-cdivstd/num/int32/unsafe-cdiv: (int32, int32) -> int32(xx: int32,yy: int32) val rr: int32 = unsafe-cmodstd/num/int32/unsafe-cmod: (int32, int32) -> int32(xx: int32,yy: int32) if (!std/core/types/bool/(!): (b : bool) -> boolrr: int32.is-negstd/num/int32/is-neg: (i : int32) -> bool) then (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)qq: int32,rr: int32)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) elif (yy: int32.is-posstd/num/int32/is-pos: (i : int32) -> bool) then (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)qq: int32.decstd/num/int32/dec: (i : int32) -> int32, rr: int32+std/num/int32/(+): (x : int32, y : int32) -> int32yy: int32)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) else (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)qq: int32.incstd/num/int32/inc: (i : int32) -> int32,rr: int32 -std/num/int32/(-): (x : int32, y : int32) -> int32 yy: int32
)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) // Fold over the range `[start,end]` (including `end`). pub fip fun range/fold-int32std/num/int32/range/fold-int32: forall<a,e> (start : int32, end : int32, init : a, f : (int32, a) -> e a) -> e a( startstart: int32 : int32std/core/types/int32: V, endend: int32 : int32std/core/types/int32: V, initinit: $1803 : aa: V, ^ff: (int32, $1803) -> $1804 $1803 : (int32std/core/types/int32: V,aa: V) -> ee: E aa: V )result: -> 1857 1856 : ee: E aa: V if (startstart: int32 >std/num/int32/(>): (x : int32, y : int32) -> $1804 bool endend: int32) then initinit: $1803 else val xx: $1803 = ff: (int32, $1803) -> $1804 $1803(startstart: int32,initinit: $1803) range/fold-int32std/num/int32/range/fold-int32: (start : int32, end : int32, init : $1803, f : (int32, $1803) -> $1804 $1803) -> $1804 $1803(pretend-decreasingstd/core/undiv/pretend-decreasing: (x : int32) -> $1804 int32(startstart: int32.incstd/num/int32/inc: (i : int32) -> $1804 int32), endend: int32, xx: $1803, ff: (int32, $1803) -> $1804 $1803) // Fold over the 32-bit integers `0` to `n-1`. pub fip fun fold-int32std/num/int32/fold-int32: forall<a,e> (n : int32, init : a, f : (int32, a) -> e a) -> e a( nn: int32 : int32std/core/types/int32: V, initinit: $1776 : aa: V, ^ff: (int32, $1776) -> $1777 $1776 : (int32std/core/types/int32: V,aa: V) -> ee: E aa: V )result: -> 1802 1801 : ee: E aa: V range/fold-int32std/num/int32/range/fold-int32: (start : int32, end : int32, init : $1776, f : (int32, $1776) -> $1777 $1776) -> $1777 $1776(zerostd/num/int32/zero: int32, nn: int32.decstd/num/int32/dec: (i : int32) -> $1777 int32, initinit: $1776, ff: (int32, $1776) -> $1777 $1776) // Iterate over the range `[start,end]` (including `end`). pub fun range/fold-while-int32std/num/int32/range/fold-while-int32: forall<a,e> (start : int32, end : int32, init : a, f : (int32, a) -> e maybe<a>) -> e a( startstart: int32 : int32std/core/types/int32: V, endend: int32 : int32std/core/types/int32: V, initinit: $1901 : aa: V, ff: (int32, $1901) -> $1902 maybe<$1901> : (int32std/core/types/int32: V,aa: V) -> ee: E maybestd/core/types/maybe: V -> V<aa: V> )result: -> 1963 1962 : ee: E aa: V if (startstart: int32 >std/num/int32/(>): (x : int32, y : int32) -> $1902 bool endend: int32) then initinit: $1901 else match ff: (int32, $1901) -> $1902 maybe<$1901>(startstart: int32,initinit: $1901) Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $1901) -> range/fold-while-int32std/num/int32/range/fold-while-int32: (start : int32, end : int32, init : $1901, f : (int32, $1901) -> $1902 maybe<$1901>) -> $1902 $1901(pretend-decreasingstd/core/undiv/pretend-decreasing: (x : int32) -> $1902 int32(startstart: int32.incstd/num/int32/inc: (i : int32) -> $1902 int32), endend: int32, xx: $1901, ff: (int32, $1901) -> $1902 maybe<$1901>) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> initinit: $1901 // Iterate over the 32-bit integers `0` to `n-1`. pub fun fold-while-int32std/num/int32/fold-while-int32: forall<a,e> (n : int32, init : a, f : (int32, a) -> e maybe<a>) -> e a( nn: int32 : int32std/core/types/int32: V, initinit: $1874 : aa: V, ff: (int32, $1874) -> $1875 maybe<$1874> : (int32std/core/types/int32: V,aa: V) -> ee: E maybestd/core/types/maybe: V -> V<aa: V> )result: -> 1900 1899 : ee: E aa: V range/fold-while-int32std/num/int32/range/fold-while-int32: (start : int32, end : int32, init : $1874, f : (int32, $1874) -> $1875 maybe<$1874>) -> $1875 $1874(zerostd/num/int32/zero: int32, nn: int32.decstd/num/int32/dec: (i : int32) -> $1875 int32, initinit: $1874, ff: (int32, $1874) -> $1875 maybe<$1874>) // Executes `action` for each integer between `start` upto `end` (including both `start` and `end` ). // If `start > end` the function returns without any call to `action` . pub fun range/for32std/num/int32/range/for32: forall<e> (start : int32, end : int32, action : (int32) -> e ()) -> e ()( startstart: int32: int32std/core/types/int32: V, endend: int32 : int32std/core/types/int32: V, actionaction: (int32) -> $1980 () : (int32std/core/types/int32: V) -> ee: E (std/core/types/unit: V)std/core/types/unit: V )result: -> 2021 () : ee: E (std/core/types/unit: V)std/core/types/unit: V fun reprep: (i : int32) -> $1980 ()( ii: int32 : int32std/core/types/int32: V )result: -> $1980 () if ii: int32 <=std/num/int32/(<=): (x : int32, y : int32) -> $1980 bool endend: int32 then actionaction: (int32) -> $1980 ()(ii: int32) reprep: (i : int32) -> $1980 ()(pretend-decreasingstd/core/undiv/pretend-decreasing: (x : int32) -> $1980 int32(ii: int32.incstd/num/int32/inc: (i : int32) -> $1980 int32))std/core/types/Unit: () reprep: (i : int32) -> $1980 ()(startstart: int32) pub fun for32std/num/int32/for32: forall<e> (n : int32, action : (int32) -> e ()) -> e ()( nn: int32 : int32std/core/types/int32: V, actionaction: (int32) -> $2025 () : (int32std/core/types/int32: V) -> ee: E (std/core/types/unit: V)std/core/types/unit: V )result: -> 2041 () : ee: E (std/core/types/unit: V)std/core/types/unit: V range/for32std/num/int32/range/for32: (start : int32, end : int32, action : (int32) -> $2025 ()) -> $2025 ()(zerostd/num/int32/zero: int32,nn: int32.decstd/num/int32/dec: (i : int32) -> $2025 int32,actionaction: (int32) -> $2025 ()) // Executes `action` for each integer between `start` upto `end` (including both `start` and `end` ). // If `start > end` the function returns without any call to `action` . // If `action` returns `Just`, the iteration is stopped and the result returned pub fun range/for-while32std/num/int32/range/for-while32: forall<a,e> (start : int32, end : int32, action : (int32) -> e maybe<a>) -> e maybe<a>( startstart: int32: int32std/core/types/int32: V, endend: int32 : int32std/core/types/int32: V, actionaction: (int32) -> $2048 maybe<$2047> : (int32std/core/types/int32: V) -> ee: E maybestd/core/types/maybe: V -> V<aa: V> )result: -> 2110 maybe<2109> : ee: E maybestd/core/types/maybe: V -> V<aa: V> fun reprep: (i : int32) -> $2048 maybe<$2047>( ii: int32 : int32std/core/types/int32: V )result: -> $2048 maybe<$2047> if ii: int32 <=std/num/int32/(<=): (x : int32, y : int32) -> $2048 bool endend: int32 then match actionaction: (int32) -> $2048 maybe<$2047>(ii: int32) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> reprep: (i : int32) -> $2048 maybe<$2047>(pretend-decreasingstd/core/undiv/pretend-decreasing: (x : int32) -> $2048 int32(ii: int32.incstd/num/int32/inc: (i : int32) -> $2048 int32)) Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $2047) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $2047) else Nothingstd/core/types/Nothing: forall<a> maybe<a> reprep: (i : int32) -> $2048 maybe<$2047>(startstart: int32) pub fun for-while32std/num/int32/for-while32: forall<a,e> (n : int32, action : (int32) -> e maybe<a>) -> e maybe<a>( nn: int32: int32std/core/types/int32: V, actionaction: (int32) -> $2118 maybe<$2117> : (int32std/core/types/int32: V) -> ee: E maybestd/core/types/maybe: V -> V<aa: V> )result: -> 2142 maybe<2141> : ee: E maybestd/core/types/maybe: V -> V<aa: V> range/for-while32std/num/int32/range/for-while32: (start : int32, end : int32, action : (int32) -> $2118 maybe<$2117>) -> $2118 maybe<$2117>(zerostd/num/int32/zero: int32,nn: int32.decstd/num/int32/dec: (i : int32) -> $2118 int32,actionaction: (int32) -> $2118 maybe<$2117>) // Create a list with 32-bit integer elements from `lo` to `hi` (including `hi`). pub fun list32std/num/int32/list32: (lo : int32, hi : int32) -> list<int32>( lolo: int32: int32std/core/types/int32: V, hihi: int32: int32std/core/types/int32: V )result: -> total list<int32> : totalstd/core/types/total: E liststd/core/types/list: V -> V<int32std/core/types/int32: V> if lolo: int32 <=std/num/int32/(<=): (x : int32, y : int32) -> bool hihi: int32 then Consstd/core/types/Cons: forall<a> (head : a, tail : list<a>) -> list<a>( lolo: int32, list32std/num/int32/list32: (lo : int32, hi : int32) -> list<int32>( pretend-decreasingstd/core/undiv/pretend-decreasing: (x : int32) -> int32(lolo: int32.incstd/num/int32/inc: (i : int32) -> int32), hihi: int32 ) ) else Nilstd/core/types/Nil: forall<a> list<a> pub fip fun sum32std/num/int32/sum32: (xs : list<int32>) -> int32( ^xsxs: list<int32> : liststd/core/types/list: V -> V<int32std/core/types/int32: V> )result: -> total int32 : int32std/core/types/int32: V // xs.foldl( 0.int32, fn(x,y) x + y ) sumacc32std/num/int32/sumacc32: (xs : list<int32>, acc : int32) -> int32(xsxs: list<int32>,0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
.int32std/num/int32/int32: (i : int) -> int32
) fip fun sumacc32std/num/int32/sumacc32: (xs : list<int32>, acc : int32) -> int32( ^xsxs: list<int32> : liststd/core/types/list: V -> V<int32std/core/types/int32: V>, accacc: int32 : int32std/core/types/int32: V )result: -> total int32 : int32std/core/types/int32: V match xsxs: list<int32> Consstd/core/types/Cons: forall<a> (head : a, tail : list<a>) -> list<a>(xx: int32,xxxx: list<int32>) -> sumacc32std/num/int32/sumacc32: (xs : list<int32>, acc : int32) -> int32(xxxx: list<int32>,accacc: int32+std/num/int32/(+): (x : int32, y : int32) -> int32xx: int32) Nilstd/core/types/Nil: forall<a> list<a> -> accacc: int32