(module main)

(fn maybe_value ((value i32)) -> (option i32)
  (some i32 value))

(fn maybe_empty () -> (option i32)
  (none i32))

(fn maybe_wide_value ((value i64)) -> (option i64)
  (some i64 value))

(fn maybe_wide_empty () -> (option i64)
  (none i64))

(fn maybe_float_value ((value f64)) -> (option f64)
  (some f64 value))

(fn maybe_float_empty () -> (option f64)
  (none f64))

(fn maybe_flag_value ((value bool)) -> (option bool)
  (some bool value))

(fn maybe_flag_empty () -> (option bool)
  (none bool))

(fn maybe_string_value ((value string)) -> (option string)
  (some string value))

(fn maybe_string_empty () -> (option string)
  (none string))

(fn result_ok_value ((value i32)) -> (result i32 i32)
  (ok i32 i32 value))

(fn result_err_value ((code i32)) -> (result i32 i32)
  (err i32 i32 code))

(fn option_direct_payload () -> i32
  (unwrap_some (some i32 42)))

(fn option_param_payload ((value (option i32))) -> i32
  (unwrap_some value))

(fn option_local_payload () -> i32
  (let value (option i32) (maybe_value 21))
  (unwrap_some value))

(fn option_call_payload () -> i32
  (unwrap_some (maybe_value 22)))

(fn option_guarded_payload ((value (option i32))) -> i32
  (if (is_some value)
    (unwrap_some value)
    0))

(fn option_wide_direct_payload () -> i64
  (unwrap_some (some i64 2147483648i64)))

(fn option_wide_param_payload ((value (option i64))) -> i64
  (unwrap_some value))

(fn option_wide_local_payload () -> i64
  (let value (option i64) (maybe_wide_value 21i64))
  (unwrap_some value))

(fn option_wide_call_payload () -> i64
  (unwrap_some (maybe_wide_value 22i64)))

(fn option_wide_guarded_payload ((value (option i64))) -> i64
  (if (is_some value)
    (unwrap_some value)
    0i64))

(fn option_float_direct_payload () -> f64
  (unwrap_some (some f64 42.5)))

(fn option_float_param_payload ((value (option f64))) -> f64
  (unwrap_some value))

(fn option_float_local_payload () -> f64
  (let value (option f64) (maybe_float_value 21.5))
  (unwrap_some value))

(fn option_float_call_payload () -> f64
  (unwrap_some (maybe_float_value 22.5)))

(fn option_float_guarded_payload ((value (option f64))) -> f64
  (if (is_some value)
    (unwrap_some value)
    0.0))

(fn option_flag_direct_payload () -> bool
  (unwrap_some (some bool true)))

(fn option_flag_param_payload ((value (option bool))) -> bool
  (unwrap_some value))

(fn option_flag_local_payload () -> bool
  (let value (option bool) (maybe_flag_value true))
  (unwrap_some value))

(fn option_flag_call_payload () -> bool
  (unwrap_some (maybe_flag_value true)))

(fn option_flag_guarded_payload ((value (option bool))) -> bool
  (if (is_some value)
    (unwrap_some value)
    false))

(fn option_string_direct_payload () -> string
  (unwrap_some (some string "slovo")))

(fn option_string_param_payload ((value (option string))) -> string
  (unwrap_some value))

(fn option_string_local_payload () -> string
  (let value (option string) (maybe_string_value "oak"))
  (unwrap_some value))

(fn option_string_call_payload () -> string
  (unwrap_some (maybe_string_value "pine")))

(fn option_string_guarded_payload ((value (option string))) -> string
  (if (is_some value)
    (unwrap_some value)
    "fallback"))

(fn result_ok_direct_payload () -> i32
  (unwrap_ok (ok i32 i32 30)))

(fn result_err_direct_payload () -> i32
  (unwrap_err (err i32 i32 7)))

(fn result_ok_param_payload ((value (result i32 i32))) -> i32
  (unwrap_ok value))

(fn result_err_param_payload ((value (result i32 i32))) -> i32
  (unwrap_err value))

(fn result_ok_local_payload () -> i32
  (let value (result i32 i32) (result_ok_value 31))
  (unwrap_ok value))

(fn result_err_call_payload () -> i32
  (unwrap_err (result_err_value 9)))

(test "unwrap some direct constructor"
  (= (option_direct_payload) 42))

(test "unwrap some local"
  (= (option_local_payload) 21))

(test "unwrap some call"
  (= (option_call_payload) 22))

(test "unwrap some parameter"
  (= (option_param_payload (maybe_value 23)) 23))

(test "guarded unwrap some present"
  (= (option_guarded_payload (maybe_value 24)) 24))

(test "guarded unwrap some absent"
  (= (option_guarded_payload (maybe_empty)) 0))

(test "unwrap some i64 direct constructor"
  (= (option_wide_direct_payload) 2147483648i64))

(test "unwrap some i64 local"
  (= (option_wide_local_payload) 21i64))

(test "unwrap some i64 call"
  (= (option_wide_call_payload) 22i64))

(test "unwrap some i64 parameter"
  (= (option_wide_param_payload (maybe_wide_value 23i64)) 23i64))

(test "guarded unwrap some i64 present"
  (= (option_wide_guarded_payload (maybe_wide_value 24i64)) 24i64))

(test "guarded unwrap some i64 absent"
  (= (option_wide_guarded_payload (maybe_wide_empty)) 0i64))

(test "unwrap some f64 direct constructor"
  (= (option_float_direct_payload) 42.5))

(test "unwrap some f64 local"
  (= (option_float_local_payload) 21.5))

(test "unwrap some f64 call"
  (= (option_float_call_payload) 22.5))

(test "unwrap some f64 parameter"
  (= (option_float_param_payload (maybe_float_value 23.5)) 23.5))

(test "guarded unwrap some f64 present"
  (= (option_float_guarded_payload (maybe_float_value 24.5)) 24.5))

(test "guarded unwrap some f64 absent"
  (= (option_float_guarded_payload (maybe_float_empty)) 0.0))

(test "unwrap some bool direct constructor"
  (option_flag_direct_payload))

(test "unwrap some bool local"
  (option_flag_local_payload))

(test "unwrap some bool call"
  (option_flag_call_payload))

(test "unwrap some bool parameter"
  (option_flag_param_payload (maybe_flag_value true)))

(test "guarded unwrap some bool present"
  (option_flag_guarded_payload (maybe_flag_value true)))

(test "guarded unwrap some bool absent"
  (if (option_flag_guarded_payload (maybe_flag_empty))
    false
    true))

(test "unwrap some string direct constructor"
  (= (option_string_direct_payload) "slovo"))

(test "unwrap some string local"
  (= (option_string_local_payload) "oak"))

(test "unwrap some string call"
  (= (option_string_call_payload) "pine"))

(test "unwrap some string parameter"
  (= (option_string_param_payload (maybe_string_value "birch")) "birch"))

(test "guarded unwrap some string present"
  (= (option_string_guarded_payload (maybe_string_value "cedar")) "cedar"))

(test "guarded unwrap some string absent"
  (= (option_string_guarded_payload (maybe_string_empty)) "fallback"))

(test "unwrap ok direct constructor"
  (= (result_ok_direct_payload) 30))

(test "unwrap err direct constructor"
  (= (result_err_direct_payload) 7))

(test "unwrap ok parameter"
  (= (result_ok_param_payload (result_ok_value 32)) 32))

(test "unwrap err parameter"
  (= (result_err_param_payload (result_err_value 8)) 8))

(test "unwrap ok local"
  (= (result_ok_local_payload) 31))

(test "unwrap err call"
  (= (result_err_call_payload) 9))

(fn main () -> i32
  0)
