/*---------------------------------------------------------------------------
  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<$75> : maybestd/core/types/maybe: V -> V<aa: V>, onNothingonNothing: $76: bb: V, onJustonJust: ($75) -> $77 $76: aa: V -> ee: E bb: V )result: -> 104 103 : ee: E bb: V
  match mm: maybe<$75>
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> onNothingonNothing: $76
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $75) -> onJustonJust: ($75) -> $77 $76(xx: $75)

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

// Get the value of the `Just` constructor or raise an exception
pub fun unjuststd/core/maybe/unjust: forall<a> (m : maybe<a>) -> exn a( mm: maybe<$136> : maybestd/core/types/maybe: V -> V<aa: V> )result: -> exn 162 : exnstd/core/exn/exn: (E, V) -> V aa: V
  match mm: maybe<$136>
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $136) -> xx: $136
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> throwstd/core/exn/throw: (message : string, info : ? exception-info) -> exn $136("unexpected Nothing in std/core/maybe/unjust"literal: string
count= 43
) pub fun mapstd/core/maybe/map: forall<a,b,e> (m : maybe<a>, f : (a) -> e b) -> e maybe<b>( mm: maybe<$167> : maybestd/core/types/maybe: V -> V<aa: V>, ff: ($167) -> $169 $168 : aa: V -> ee: E bb: V )result: -> 204 maybe<203> : ee: E maybestd/core/types/maybe: V -> V<bb: V> match mm: maybe<$167> 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: $167) -> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ff: ($167) -> $169 $168(xx: $167)) pub fun (||)std/core/maybe/(||): forall<a> (m1 : maybe<a>, m2 : maybe<a>) -> maybe<a>( m1m1: maybe<$214> : maybestd/core/types/maybe: V -> V<aa: V>, m2m2: maybe<$214>: maybestd/core/types/maybe: V -> V<aa: V> )result: -> total maybe<228> : maybestd/core/types/maybe: V -> V<aa: V> match m1m1: maybe<$214> Nothingstd/core/types/Nothing: forall<a> maybe<a> -> m2m2: maybe<$214> _ -> m1m1: maybe<$214> // Equality on `:maybe` pub fun (==)std/core/maybe/(==): forall<a> (mb1 : maybe<a>, mb2 : maybe<a>, @implicit/(==) : (a, a) -> bool) -> bool( mb1mb1: maybe<$233> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$233> : maybestd/core/types/maybe: V -> V<aa: V>, (@implicit/==)?(==): ($233, $233) -> bool : (aa: V,aa: V) -> boolstd/core/types/bool: V )result: -> total bool : boolstd/core/types/bool: V match mb1mb1: maybe<$233> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $233) -> match mb2mb2: maybe<$233> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $233) -> xx: $233==?(==): ($233, $233) -> boolyy: $233 Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Falsestd/core/types/False: bool Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$233> 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<$282> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$282> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/cmp?cmp: ($282, $282) -> order : (aa: V,aa: V) -> orderstd/core/types/order: V )result: -> total order : orderstd/core/types/order: V match mb1mb1: maybe<$282> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $282) -> match mb2mb2: maybe<$282> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $282) -> cmp?cmp: ($282, $282) -> order(xx: $282,yy: $282) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> Gtstd/core/types/Gt: order Nothingstd/core/types/Nothing: forall<a> maybe<a> -> match mb2mb2: maybe<$282> 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<$327> : maybestd/core/types/maybe: V -> V<aa: V>, mb2mb2: maybe<$327> : maybestd/core/types/maybe: V -> V<aa: V>, ^@implicit/order2?order2: ($327, $327) -> order2<$327> : (aa: V,aa: V) -> order2std/core/types/order2: V -> V<aa: V> )result: -> total order2<maybe<451>> : order2std/core/types/order2: V -> V<maybestd/core/types/maybe: V -> V<aa: V>> match mb1mb1: maybe<$327> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $327) -> match mb2mb2: maybe<$327> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(yy: $327) -> match order2?order2: ($327, $327) -> order2<$327>(xx: $327,yy: $327) Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(zz: $327) -> Eq2std/core/types/Eq2: forall<a> (eq : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(zz: $327)) Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $327,gg: $327) -> Lt2std/core/types/Lt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $327),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $327)) Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(ll: $327,gg: $327) -> Gt2std/core/types/Gt2: forall<a> (lt : a, gt : a) -> order2<a>(Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(ll: $327),Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(gg: $327)) 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: $327)) 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<$327>) // 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<$456> : maybestd/core/types/maybe: V -> V<aa: V>, @implicit/show?show: ($456) -> $457 string : aa: V -> ee: E stringstd/core/types/string: V )result: -> 499 string : ee: E stringstd/core/types/string: V match mbmb: maybe<$456> Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(xx: $456) -> "Just("literal: string
count= 5
++std/core/types/(++): (x : string, y : string) -> $457 string xx: $456.show?show: ($456) -> $457 string ++std/core/types/(++): (x : string, y : string) -> $457 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<$513> : maybestd/core/types/maybe: V -> V<aa: V> )result: -> total bool : boolstd/core/types/bool: V match mbmb: maybe<$513> 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