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

// Standard `:maybe` functions.
module std/core/maybestd/core/maybe

import std/core/typesstd/core/types
import std/core/hndstd/core/hnd
import std/core/exnstd/core/exn

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

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

// Get the value of the `Just` constructor or raise an exception
pub fun unjuststd/core/maybe/unjust: forall<a> (m : maybe<a>, @implicit/kk-file-line : string) -> exn a( mm: maybe<$151> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/kk-file-line?kk-file-line: string: stringstd/core/types/string: V)result: -> exn 188 : exnstd/core/exn/exn: (E, V) -> V aa: V
  match mm: maybe<$151>
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $151) -> xx: $151
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn $151("unexpected Nothing in "literal: string
count= 22
++std/core/types/(++): (x : string, y : string) -> exn string @implicit/kk-file-line?kk-file-line: string
) // Get the value of the `Just` constructor or raise an exception with `error-msg` pub fun expectstd/core/maybe/expect: forall<a> (m : maybe<a>, error-msg : string) -> exn a( mm: maybe<$193> : maybestd/core/types/maybe: V -> V<aa: V>, error-msgerror-msg: string: stringstd/core/types/string: V)result: -> exn 222 : exnstd/core/exn/exn: (E, V) -> V aa: V match mm: maybe<$193> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $193) -> xx: $193 Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn $193(error-msgerror-msg: string) pub fun mapstd/core/maybe/map: forall<a,b,e> (m : maybe<a>, f : (a) -> e b) -> e maybe<b>( mm: maybe<$227> : maybestd/core/types/maybe: V -> V<aa: V>, ff: ($227) -> $229 $228 : aa: V -> ee: E bb: V )result: -> 268 maybe<267> : ee: E maybestd/core/types/maybe: V -> V<bb: V> match mm: maybe<$227> 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: $227) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ff: ($227) -> $229 $228(xx: $227)) pub fun (||)std/core/maybe/(||): forall<a> (m1 : maybe<a>, m2 : maybe<a>) -> maybe<a>( m1m1: maybe<$278> : maybestd/core/types/maybe: V -> V<aa: V>, m2m2: maybe<$278>: maybestd/core/types/maybe: V -> V<aa: V> )result: -> total maybe<294> : maybestd/core/types/maybe: V -> V<aa: V> match m1m1: maybe<$278> Nothingstd/core/types/Nothing: forall<a> maybe<a> -> m2m2: maybe<$278> _ -> m1m1: maybe<$278> // Equality on `:maybe` pub fun (==)std/core/maybe/(==): forall<a,e> (mb1 : maybe<a>, mb2 : maybe<a>, @implicit/(==) : (a, a) -> e bool) -> e bool( mb1mb1: maybe<$299> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$299> : maybestd/core/types/maybe: V -> V<aa: V>, (@implicit/==)?(==): ($299, $299) -> $300 bool : (aa: V,aa: V) -> ee: E boolstd/core/types/bool: V )result: -> 352 bool : ee: E boolstd/core/types/bool: V match mb1mb1: maybe<$299> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $299) -> match mb2mb2: maybe<$299> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $299) -> xx: $299==?(==): ($299, $299) -> $300 boolyy: $299 Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Falsestd/core/types/False: bool Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$299> Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Truestd/core/types/True: bool _ -> Falsestd/core/types/False: bool // Order on `:maybe` values pub fun cmpstd/core/maybe/cmp: forall<a,e> (mb1 : maybe<a>, mb2 : maybe<a>, @implicit/cmp : (a, a) -> e order) -> e order( mb1mb1: maybe<$363> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$363> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/cmp?cmp: ($363, $363) -> $364 order : (aa: V,aa: V) -> ee: E orderstd/core/types/order: V )result: -> 416 order : ee: E orderstd/core/types/order: V match mb1mb1: maybe<$363> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $363) -> match mb2mb2: maybe<$363> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $363) -> cmp?cmp: ($363, $363) -> $364 order(xx: $363,yy: $363) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Gtstd/core/types/Gt: order Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$363> Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Eqstd/core/types/Eq: order _ -> Ltstd/core/types/Lt: order // Order two `:maybe` values in ascending order pub fip fun order2std/core/maybe/order2: forall<a,e> (mb1 : maybe<a>, mb2 : maybe<a>, @implicit/order2 : (a, a) -> e order2<a>) -> e order2<maybe<a>>( mb1mb1: maybe<$423> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$423> : maybestd/core/types/maybe: V -> V<aa: V>, ^@implicit/order2?order2: ($423, $423) -> $424 order2<$423> : (aa: V,aa: V) -> ee: E order2std/core/types/order2: V -> V<aa: V> )result: -> 569 order2<maybe<568>> : ee: E order2std/core/types/order2: V -> V<maybestd/core/types/maybe: V -> V<aa: V>> match mb1mb1: maybe<$423> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $423) -> match mb2mb2: maybe<$423> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $423) -> match order2?order2: ($423, $423) -> $424 order2<$423>(xx: $423,yy: $423) Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(zz: $423) -> Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(zz: $423)) Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $423,gg: $423) -> Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $423),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $423)) Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $423,gg: $423) -> Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $423),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $423)) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(Nothingstd/core/types/Nothing: forall<a> maybe<a>,Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $423)) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(Nothingstd/core/types/Nothing: forall<a> maybe<a>,mb2mb2: maybe<$423>) // Show a `:maybe` type pub fun showstd/core/maybe/show: forall<a,e> (mb : maybe<a>, @implicit/show : (a) -> e string) -> e string( mbmb: maybe<$576> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/show?show: ($576) -> $577 string : aa: V -> ee: E stringstd/core/types/string: V )result: -> 624 string : ee: E stringstd/core/types/string: V match mbmb: maybe<$576> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $576) -> "Just("literal: string
count= 5
++std/core/types/(++): (x : string, y : string) -> $577 string xx: $576.show?show: ($576) -> $577 string ++std/core/types/(++): (x : string, y : string) -> $577 string ")"literal: string
count= 1
Nothingstd/core/types/Nothing: forall<a> maybe<a> -> "Nothing"literal: string
count= 7
// Convert a maybe type to a boolean, equivalent to `is-just`. pub fun boolstd/core/maybe/bool: forall<a> (mb : maybe<a>) -> bool( mbmb: maybe<$638> : maybestd/core/types/maybe: V -> V<aa: V> )result: -> total bool : boolstd/core/types/bool: V match mbmb: maybe<$638> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a> -> Truestd/core/types/True: bool Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Falsestd/core/types/False: bool