/*---------------------------------------------------------------------------
  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<$98> : maybestd/core/types/maybe: V -> V<aa: V>, onNothingonNothing: $99: bb: V, onJustonJust: ($98) -> $100 $99: aa: V -> ee: E bb: V )result: -> 130 129 : ee: E bb: V
  match mm: maybe<$98>
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> onNothingonNothing: $99
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $98) -> onJustonJust: ($98) -> $100 $99(xx: $98)

// 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<$140> : maybestd/core/types/maybe: V -> V<aa: V>, nothingnothing: $140 : aa: V )result: -> total 159 : astd/core/types/total: E
  match mm: maybe<$140>
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> nothingnothing: $140
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $140) -> xx: $140

// 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<$164> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/kk-file-line?kk-file-line: string: stringstd/core/types/string: V)result: -> exn 201 : exnstd/core/exn/exn: (E, V) -> V aa: V
  match mm: maybe<$164>
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $164) -> xx: $164
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn $164("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<$206> : maybestd/core/types/maybe: V -> V<aa: V>, error-msgerror-msg: string: stringstd/core/types/string: V)result: -> exn 235 : exnstd/core/exn/exn: (E, V) -> V aa: V match mm: maybe<$206> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $206) -> xx: $206 Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn $206(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<$240> : maybestd/core/types/maybe: V -> V<aa: V>, ff: ($240) -> $242 $241 : aa: V -> ee: E bb: V )result: -> 281 maybe<280> : ee: E maybestd/core/types/maybe: V -> V<bb: V> match mm: maybe<$240> 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: $240) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ff: ($240) -> $242 $241(xx: $240)) pub fun foreachstd/core/maybe/foreach: forall<a,e> (m : maybe<a>, f : (a) -> e ()) -> e ()( mm: maybe<$291> : maybestd/core/types/maybe: V -> V<aa: V>, ff: ($291) -> $292 () : aa: V -> ee: E (std/core/types/unit: V)std/core/types/unit: V )result: -> 317 () : ee: E (std/core/types/unit: V)std/core/types/unit: V match mm: maybe<$291> Nothingstd/core/types/Nothing: forall<a> maybe<a> -> (std/core/types/Unit: ())std/core/types/Unit: () Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $291) -> ff: ($291) -> $292 ()(xx: $291) pub fun (||)std/core/maybe/(||): forall<a> (m1 : maybe<a>, m2 : maybe<a>) -> maybe<a>( m1m1: maybe<$324> : maybestd/core/types/maybe: V -> V<aa: V>, m2m2: maybe<$324>: maybestd/core/types/maybe: V -> V<aa: V> )result: -> total maybe<340> : maybestd/core/types/maybe: V -> V<aa: V> match m1m1: maybe<$324> Nothingstd/core/types/Nothing: forall<a> maybe<a> -> m2m2: maybe<$324> _ -> m1m1: maybe<$324> pub fun flattenstd/core/maybe/flatten: forall<a> (mm : maybe<maybe<a>>) -> maybe<a>( mmmm: maybe<maybe<$345>> : maybestd/core/types/maybe: V -> V<maybestd/core/types/maybe: V -> V<aa: V>> )result: -> total maybe<373> : maybestd/core/types/maybe: V -> V<aa: V> match mmmm: maybe<maybe<$345>> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $345)) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $345) _ -> Nothingstd/core/types/Nothing: forall<a> maybe<a> // 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<$378> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$378> : maybestd/core/types/maybe: V -> V<aa: V>, (@implicit/==)?(==): ($378, $378) -> $379 bool : (aa: V,aa: V) -> ee: E boolstd/core/types/bool: V )result: -> 431 bool : ee: E boolstd/core/types/bool: V match mb1mb1: maybe<$378> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $378) -> match mb2mb2: maybe<$378> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $378) -> xx: $378==?(==): ($378, $378) -> $379 boolyy: $378 Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Falsestd/core/types/False: bool Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$378> 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<$442> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$442> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/cmp?cmp: ($442, $442) -> $443 order : (aa: V,aa: V) -> ee: E orderstd/core/types/order: V )result: -> 501 order : ee: E orderstd/core/types/order: V match mb1mb1: maybe<$442> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $442) -> match mb2mb2: maybe<$442> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $442) -> cmp?cmp: ($442, $442) -> $443 order(xx: $442,yy: $442) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Gtstd/core/types/Gt: order Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$442> 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<$508> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$508> : maybestd/core/types/maybe: V -> V<aa: V>, ^@implicit/order2?order2: ($508, $508) -> $509 order2<$508> : (aa: V,aa: V) -> ee: E order2std/core/types/order2: V -> V<aa: V> )result: -> 660 order2<maybe<659>> : ee: E order2std/core/types/order2: V -> V<maybestd/core/types/maybe: V -> V<aa: V>> match mb1mb1: maybe<$508> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $508) -> match mb2mb2: maybe<$508> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $508) -> match order2?order2: ($508, $508) -> $509 order2<$508>(xx: $508,yy: $508) Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(zz: $508) -> Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(zz: $508)) Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $508,gg: $508) -> Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $508),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $508)) Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $508,gg: $508) -> Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $508),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $508)) 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: $508)) 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<$508>) // 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<$667> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/show?show: ($667) -> $668 string : aa: V -> ee: E stringstd/core/types/string: V )result: -> 715 string : ee: E stringstd/core/types/string: V match mbmb: maybe<$667> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $667) -> "Just("literal: string
count= 5
++std/core/types/(++): (x : string, y : string) -> $668 string xx: $667.show?show: ($667) -> $668 string ++std/core/types/(++): (x : string, y : string) -> $668 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<$729> : maybestd/core/types/maybe: V -> V<aa: V> )result: -> total bool : boolstd/core/types/bool: V match mbmb: maybe<$729> 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