/*---------------------------------------------------------------------------
  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<$79> : maybestd/core/types/maybe: V -> V<aa: V>, onNothingonNothing: $80: bb: V, onJustonJust: ($79) -> $81 $80: aa: V -> ee: E bb: V )result: -> 110 109 : ee: E bb: V
  match mm: maybe<$79>
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> onNothingonNothing: $80
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $79) -> onJustonJust: ($79) -> $81 $80(xx: $79)

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

// 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<$144> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/kk-file-line?kk-file-line: string: stringstd/core/types/string: V)result: -> exn 179 : exnstd/core/exn/exn: (E, V) -> V aa: V
  match mm: maybe<$144>
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $144) -> xx: $144
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn $144("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<$184> : maybestd/core/types/maybe: V -> V<aa: V>, error-msgerror-msg: string: stringstd/core/types/string: V)result: -> exn 212 : exnstd/core/exn/exn: (E, V) -> V aa: V match mm: maybe<$184> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $184) -> xx: $184 Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn $184(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<$217> : maybestd/core/types/maybe: V -> V<aa: V>, ff: ($217) -> $219 $218 : aa: V -> ee: E bb: V )result: -> 256 maybe<255> : ee: E maybestd/core/types/maybe: V -> V<bb: V> match mm: maybe<$217> 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: $217) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ff: ($217) -> $219 $218(xx: $217)) pub fun (||)std/core/maybe/(||): forall<a> (m1 : maybe<a>, m2 : maybe<a>) -> maybe<a>( m1m1: maybe<$266> : maybestd/core/types/maybe: V -> V<aa: V>, m2m2: maybe<$266>: maybestd/core/types/maybe: V -> V<aa: V> )result: -> total maybe<282> : maybestd/core/types/maybe: V -> V<aa: V> match m1m1: maybe<$266> Nothingstd/core/types/Nothing: forall<a> maybe<a> -> m2m2: maybe<$266> _ -> m1m1: maybe<$266> // Equality on `:maybe` pub fun (==)std/core/maybe/(==): forall<a> (mb1 : maybe<a>, mb2 : maybe<a>, @implicit/(==) : (a, a) -> bool) -> bool( mb1mb1: maybe<$287> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$287> : maybestd/core/types/maybe: V -> V<aa: V>, (@implicit/==)?(==): ($287, $287) -> bool : (aa: V,aa: V) -> boolstd/core/types/bool: V )result: -> total bool : boolstd/core/types/bool: V match mb1mb1: maybe<$287> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $287) -> match mb2mb2: maybe<$287> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $287) -> xx: $287==?(==): ($287, $287) -> boolyy: $287 Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Falsestd/core/types/False: bool Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$287> 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> (mb1 : maybe<a>, mb2 : maybe<a>, @implicit/cmp : (a, a) -> order) -> order( mb1mb1: maybe<$342> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$342> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/cmp?cmp: ($342, $342) -> order : (aa: V,aa: V) -> orderstd/core/types/order: V )result: -> total order : orderstd/core/types/order: V match mb1mb1: maybe<$342> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $342) -> match mb2mb2: maybe<$342> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $342) -> cmp?cmp: ($342, $342) -> order(xx: $342,yy: $342) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Gtstd/core/types/Gt: order Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$342> 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> (mb1 : maybe<a>, mb2 : maybe<a>, @implicit/order2 : (a, a) -> order2<a>) -> order2<maybe<a>>( mb1mb1: maybe<$393> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$393> : maybestd/core/types/maybe: V -> V<aa: V>, ^@implicit/order2?order2: ($393, $393) -> order2<$393> : (aa: V,aa: V) -> order2std/core/types/order2: V -> V<aa: V> )result: -> total order2<maybe<521>> : order2std/core/types/order2: V -> V<maybestd/core/types/maybe: V -> V<aa: V>> match mb1mb1: maybe<$393> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $393) -> match mb2mb2: maybe<$393> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $393) -> match order2?order2: ($393, $393) -> order2<$393>(xx: $393,yy: $393) Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(zz: $393) -> Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(zz: $393)) Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $393,gg: $393) -> Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $393),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $393)) Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $393,gg: $393) -> Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $393),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $393)) 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: $393)) 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<$393>) // 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<$526> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/show?show: ($526) -> $527 string : aa: V -> ee: E stringstd/core/types/string: V )result: -> 571 string : ee: E stringstd/core/types/string: V match mbmb: maybe<$526> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $526) -> "Just("literal: string
count= 5
++std/core/types/(++): (x : string, y : string) -> $527 string xx: $526.show?show: ($526) -> $527 string ++std/core/types/(++): (x : string, y : string) -> $527 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<$585> : maybestd/core/types/maybe: V -> V<aa: V> )result: -> total bool : boolstd/core/types/bool: V match mbmb: maybe<$585> 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