base¶
In [ ]:
//// test
open testing
base¶
emit¶
In [ ]:
inl emit forall t. (x : t) : t =
$'!x '
emit_unit¶
In [ ]:
inl emit_unit forall t. (x : t) : () =
$'!x '
use¶
In [ ]:
inl use forall t. (x : t) : t =
$'use !x = !x ' : ()
$'!x '
unit¶
In [ ]:
nominal unit = $'unit'
backend_switch¶
In [ ]:
inl backend_switch forall t. x : t =
real
inl backend (key : string) : t =
inl s = real_core.string_lit_to_symbol key
real_core.record_type_try_find `(`x) s
(forall v'. => (x s) ())
(fun () => $'' : t)
inl x' = {
Gleam = fun () => backend "Gleam"
Fsharp = fun () => backend "Fsharp"
Python = fun () => backend "Python"
Cpp = fun () => backend "Cpp"
}
!!!!BackendSwitch (
x'
)
target_runtime¶
In [ ]:
union target_runtime =
| Native
| Wasm
| Contract
target¶
In [ ]:
union target =
| Gleam : target_runtime
| Fsharp : target_runtime
| Cuda : target_runtime
| Cpp : target_runtime
| Rust : target_runtime
| TypeScript : target_runtime
| Python : target_runtime
run_target_args'¶
In [ ]:
inl run_target_args' forall t u. (args : u) (fn : target -> (u -> t)) : t =
backend_switch {
Gleam = fun () =>
inl target = Gleam Native
fn target args
Fsharp = fun () =>
inl is_unit : bool =
real
typecase t with
| () => true
| _ => false
$'(* run_target_args\''
inl result = $'()' : unit
$'run_target_args\' *)'
inl emit_result x : () =
if is_unit |> not
then $'let _run_target_args\'_!result = !x '
$'\n\#if FABLE_COMPILER || WASM || CONTRACT'
$'\n\#if FABLE_COMPILER_RUST && \!WASM && \!CONTRACT'
inl target = Rust Native
fn target args |> emit_result
$'\#endif\n\#if FABLE_COMPILER_RUST && WASM'
inl target = Rust Wasm
fn target args |> emit_result
$'\#endif\n\#if FABLE_COMPILER_RUST && CONTRACT'
inl target = Rust Contract
fn target args |> emit_result
$'\#endif\n\#if FABLE_COMPILER_TYPESCRIPT'
inl target = TypeScript Native
fn target args |> emit_result
$'\#endif\n\#if FABLE_COMPILER_PYTHON'
inl target = Python Native
fn target args |> emit_result
$'\#endif\n\#if \!FABLE_COMPILER_RUST && \!FABLE_COMPILER_TYPESCRIPT && \!FABLE_COMPILER_PYTHON'
inl target = Fsharp Wasm
fn target args |> emit_result
$'\#endif\n\#else'
inl target = Fsharp Native
fn target args |> emit_result
$'\#endif'
if is_unit
then $'// run_target_args\' is_unit'
else $'_run_target_args\'_!result ' : t
Python = fun () =>
inl target = Cuda Native
fn target args
Cpp = fun () =>
inl target = Cpp Native
fn target args
}
run_target_args¶
In [ ]:
inl run_target_args forall t u. (args : () -> u) (fn : target -> (u -> t)) : t =
inl args = args () |> dyn
fn |> run_target_args' args
run_target¶
In [ ]:
inl run_target forall t. (fn : target -> (() -> t)) : t =
run_target_args id fn
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
run_target function
| Gleam (Native) => fun () => $'1'
| Fsharp (Native) => fun () => $'1uy'
| Cuda (Native) => fun () => $'1'
| Rust (Native) => fun () => $'1uy'
| TypeScript (Native) => fun () => $'1uy'
| Python (Native) => fun () => $'1uy'
| _ => fun () => $'2uy'
|> _assert_eq 1u8
.py output (Python):
{ name = __assert_eq; expected = 1 }
.rs output:
{ name = __assert_eq; expected = 1 }
.ts output:
{ name = __assert_eq; expected = 1 }
.py output:
{ name = __assert_eq; expected = 1 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 1 }
.fsx output:
{ name = __assert_eq; expected = 1 }
eval¶
In [ ]:
inl eval fn =
fn ()
flip¶
In [ ]:
inl flip fn a b =
fn b a
do¶
In [ ]:
inl do (body : () -> ()) : () =
!!!!Do (body())
indent¶
In [ ]:
inl indent (body : () -> ()) : () =
backend_switch {
Gleam = fun () =>
!!!!Indent (body())
()
Fsharp = fun () =>
inl body () =
body ()
$'(* indent' : ()
!!!!Indent (body())
$'indent *)' : ()
Python = fun () =>
!!!!Indent (body())
()
}
let'¶
In [ ]:
inl let' fn =
inl result : unit =
backend_switch {
Gleam = fun () => $'Nil' : unit
Fsharp = fun () => $'()' : unit
Python = fun () => $'None' : unit
}
backend_switch {
Gleam = fun () =>
$'let _let\'_!result = {' : ()
fn |> indent
$'}' : ()
Fsharp = fun () =>
$'let _let\'_!result =' : ()
fn |> indent
Python = fun () =>
$'def _let\'_!result():' : ()
fn |> indent
}
$'_let\'_!result '
exec_unit¶
In [ ]:
inl exec_unit (fn : () -> ()) : () =
backend_switch {
Gleam = fun () => fn ()
Fsharp = fun () =>
inl unit = $'()' : $'unit'
($'(fun () -> !fn (); !unit) ()' : $'unit') |> ignore
Python = fun () => fn ()
}
lazy¶
In [ ]:
nominal lazy t = $'Lazy<`t>'
memoize¶
In [ ]:
nominal lazy t = $'Lazy<`t>'
inl memoize forall t. (fn : () -> t) : () -> t =
inl body () =
inl result = mut None
inl computed = mut false
fun () =>
if *computed
then *result
else
result <- fn () |> Some
computed <- true
*result
|> optionm.value
: () -> t
backend_switch {
Gleam = fun () => body ()
Fsharp = fun () =>
inl result : lazy t = $'lazy !fn ()'
fun () => $'!result.Value' : t
Python = fun () => body ()
}
In [ ]:
//// test
///! fsharp
///! cuda
///! rust
///! typescript
///! python
inl count = mut 0i32
inl add =
fun () =>
count <- *count + 1
count
|> memoize
add () |> ignore
add () |> ignore
add () |> ignore
*count
|> _assert_eq 1
.py output (Python):
{ name = __assert_eq; expected = 1 }
.rs output:
{ name = __assert_eq; expected = 1 }
.ts output:
{ name = __assert_eq; expected = 1 }
.py output:
{ name = __assert_eq; expected = 1 }
.fsx output:
{ name = __assert_eq; expected = 1 }
capture¶
In [ ]:
inl capture forall t. (fn : () -> t) : t =
backend_switch {
Fsharp = fun () =>
inl result = dyn true
$'let mutable _capture_!result : `t option = None '
$'('
$'(fun () ->'
$'(fun () ->'
fn () |> emit_unit
$')'
$'|> fun x -> x ()'
$') () )'
$'|> fun x -> _capture_!result <- Some x'
$'match _capture_!result with Some x -> x | None -> failwith "base.capture / _capture_!result=None"' : t
Python = fun () =>
fn ()
}
yield_from¶
In [ ]:
inl yield_from forall (t : * -> *) u. (a : t u) : () =
backend_switch {
Fsharp = fun () => $'yield\! !a ' : ()
Python = fun () => $'asyncio.run(!a())' : ()
}
join_body¶
In [ ]:
inl join_body body acc x =
if var_is x |> not
then body acc x
else
inl acc = dyn acc
join body acc x
In [ ]:
//// test
inl rec fold_list f s = function
| Cons (x, x') => fold_list f (f s x) x'
| Nil => s
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
//// print_code
[ 5i32; 4; join 3; 2; 1 ]
|> fold_list (+) 0
|> _assert_eq 15
.py output (Python):
{ name = __assert_eq; expected = 15 }
.rs output:
{ name = __assert_eq; expected = 15 }
.ts output:
{ name = __assert_eq; expected = 15 }
.py output:
{ name = __assert_eq; expected = 15 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 15 }
.gleam (Gleam):
import gleam/string
import gleam/io
pub type Mut0 { Mut0(l0 : String) }
pub fn method1 () -> Int {
3
}
pub fn method2 (v0 : Bool) -> Bool {
v0
}
pub fn method4 () -> String {
let v0 = ""
v0
}
pub fn method3 (v0 : String, v1 : Int) -> String {
let v2 = method4()
let v3 = Mut0(l0 : v2)
let v4 = "{ "
let v5 = v4
let v9 = v3.l0
let v10 = v9 <> v5
let v3 = Mut0(l0: v10)
let v22 = "name"
let v23 = v22
let v27 = v3.l0
let v28 = v27 <> v23
let v3 = Mut0(l0: v28)
let v40 = " = "
let v41 = v40
let v45 = v3.l0
let v46 = v45 <> v41
let v3 = Mut0(l0: v46)
let v58 = v0
let v62 = v3.l0
let v63 = v62 <> v58
let v3 = Mut0(l0: v63)
let v75 = "; "
let v76 = v75
let v80 = v3.l0
let v81 = v80 <> v76
let v3 = Mut0(l0: v81)
let v93 = "expected"
let v94 = v93
let v98 = v3.l0
let v99 = v98 <> v94
let v3 = Mut0(l0: v99)
let v111 = v40
let v115 = v3.l0
let v116 = v115 <> v111
let v3 = Mut0(l0: v116)
let v128 = string.inspect(v1)
let v135 = v3.l0
let v136 = v135 <> v128
let v3 = Mut0(l0: v136)
let v148 = " }"
let v149 = v148
let v153 = v3.l0
let v154 = v153 <> v149
let v3 = Mut0(l0: v154)
let v166 = v3.l0
v166
}
pub fn method0 () -> Nil {
let v0 = method1()
let v1 = 9 + v0
let v2 = v1 + 2
let v3 = v2 + 1
let v4 = v3 == 15
let v6 =
case v4 {
True -> {
True
}
False -> {
method2(v4)
}
}
let v7 = "__assert_eq"
let v8 = 15
let v9 = method3(v7, v8)
io.println(v9)
let v15 = v6 == False
case v15 {
True -> {
panic as v9
}
False -> {
Nil
}
}
}
pub fn main () { method0()
}
.fsx:
type Mut0 = {mutable l0 : string}
let rec method1 () : int32 =
3
and method2 (v0 : bool) : bool =
v0
and method4 () : string =
let v0 : string = ""
v0
and closure0 (v0 : Mut0, v1 : string) () : unit =
let v2 : string = v0.l0
let v4 : string = v2 + v1
v0.l0 <- v4
()
and method3 (v0 : string, v1 : int32) : string =
let v2 : string = method4()
let v3 : Mut0 = {l0 = v2} : Mut0
let v6 : string = "{ "
let v7 : string = $"{v6}"
let v15 : unit = ()
let v16 : (unit -> unit) = closure0(v3, v7)
let v17 : unit = (fun () -> v16 (); v15) ()
let v25 : string = "name"
let v26 : string = $"{v25}"
let v34 : unit = ()
let v35 : (unit -> unit) = closure0(v3, v26)
let v36 : unit = (fun () -> v35 (); v34) ()
let v44 : string = " = "
let v45 : string = $"{v44}"
let v53 : unit = ()
let v54 : (unit -> unit) = closure0(v3, v45)
let v55 : unit = (fun () -> v54 (); v53) ()
let v62 : string = $"{v0}"
let v70 : unit = ()
let v71 : (unit -> unit) = closure0(v3, v62)
let v72 : unit = (fun () -> v71 (); v70) ()
let v80 : string = "; "
let v81 : string = $"{v80}"
let v89 : unit = ()
let v90 : (unit -> unit) = closure0(v3, v81)
let v91 : unit = (fun () -> v90 (); v89) ()
let v99 : string = "expected"
let v100 : string = $"{v99}"
let v108 : unit = ()
let v109 : (unit -> unit) = closure0(v3, v100)
let v110 : unit = (fun () -> v109 (); v108) ()
let v117 : string = $"{v44}"
let v125 : unit = ()
let v126 : (unit -> unit) = closure0(v3, v117)
let v127 : unit = (fun () -> v126 (); v125) ()
let v137 : string = $"{v1}"
let v145 : unit = ()
let v146 : (unit -> unit) = closure0(v3, v137)
let v147 : unit = (fun () -> v146 (); v145) ()
let v155 : string = " }"
let v156 : string = $"{v155}"
let v164 : unit = ()
let v165 : (unit -> unit) = closure0(v3, v156)
let v166 : unit = (fun () -> v165 (); v164) ()
let v172 : string = v3.l0
v172
and closure1 (v0 : string) () : unit =
let v1 : (string -> unit) = System.Console.WriteLine
v1 v0
and method0 () : unit =
let v0 : int32 = method1()
let v1 : int32 = 9 + v0
let v2 : int32 = v1 + 2
let v3 : int32 = v2 + 1
let v4 : bool = v3 = 15
let v6 : bool =
if v4 then
true
else
method2(v4)
let v7 : string = "__assert_eq"
let v8 : int32 = 15
let v9 : string = method3(v7, v8)
let v11 : unit = ()
let v12 : (unit -> unit) = closure1(v9)
let v13 : unit = (fun () -> v12 (); v11) ()
let v15 : bool = v6 = false
if v15 then
failwith<unit> v9
method0()
.rs:
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(unreachable_code)]
#![allow(unused_attributes)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(unused_parens)]
#![allow(unused_variables)]
#![allow(unused_assignments)]
mod module_6ff740fe {
pub mod Spiral {
use super::*;
use fable_library_rust::Native_::LrcPtr;
use fable_library_rust::Native_::MutCell;
use fable_library_rust::Native_::on_startup;
use fable_library_rust::String_::append;
use fable_library_rust::String_::printfn;
use fable_library_rust::String_::sprintf;
use fable_library_rust::String_::string;
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd)]
pub struct Mut0 {
pub l0: MutCell<string>,
}
impl core::fmt::Display for Mut0 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", core::any::type_name::<Self>())
}
}
pub fn method1() -> i32 {
3_i32
}
pub fn method2(v0: bool) -> bool {
v0
}
pub fn method4() -> string {
string("")
}
pub fn closure0(v0: LrcPtr<Spiral::Mut0>, v1: string, unitVar: ()) {
let v4: string = append((v0.l0.get().clone()), (v1));
v0.l0.set(v4);
()
}
pub fn method3(v0: string, v1: i32) -> string {
let v3: LrcPtr<Spiral::Mut0> = LrcPtr::new(Spiral::Mut0 {
l0: MutCell::new(Spiral::method4()),
});
let v17: () = {
Spiral::closure0(v3.clone(), string("{ "), ());
()
};
let v36: () = {
Spiral::closure0(v3.clone(), string("name"), ());
()
};
let v55: () = {
Spiral::closure0(v3.clone(), string(" = "), ());
()
};
let v72: () = {
Spiral::closure0(v3.clone(), v0, ());
()
};
let v91: () = {
Spiral::closure0(v3.clone(), string("; "), ());
()
};
let v110: () = {
Spiral::closure0(v3.clone(), string("expected"), ());
()
};
let v127: () = {
Spiral::closure0(v3.clone(), string(" = "), ());
()
};
let v147: () = {
Spiral::closure0(v3.clone(), sprintf!("{}", v1), ());
()
};
let v166: () = {
Spiral::closure0(v3.clone(), string(" }"), ());
()
};
v3.l0.get().clone()
}
pub fn closure1(v0: string, unitVar: ()) {
printfn!("{0}", v0);
}
pub fn method0() {
let v4: bool = (((9_i32 + (Spiral::method1())) + 2_i32) + 1_i32) == 15_i32;
let v6: bool = if v4 { true } else { Spiral::method2(v4) };
let v9: string = Spiral::method3(string("__assert_eq"), 15_i32);
let v13: () = {
Spiral::closure1(v9.clone(), ());
()
};
if (v6) == false {
panic!("{}", v9,);
}
}
// on_startup!(Spiral::method0());
}
}
pub use module_6ff740fe::*;
pub fn main() -> Result<(), String> { Ok(Spiral::method0()) }
.ts:
import { Record } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Types.js";
import { IComparable, IEquatable } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Util.js";
import { record_type, string_type, TypeInfo } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Reflection.js";
import { int32 } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Int32.js";
export class Mut0 extends Record implements IEquatable<Mut0>, IComparable<Mut0> {
l0: string;
constructor(l0: string) {
super();
this.l0 = l0;
}
}
export function Mut0_$reflection(): TypeInfo {
return record_type("Spiral.Mut0", [], Mut0, () => [["l0", string_type]]);
}
export function method1(): int32 {
return 3;
}
export function method2(v0: boolean): boolean {
return v0;
}
export function method4(): string {
return "";
}
export function closure0(v0: Mut0, v1: string, unitVar: void): void {
const v4: string = v0.l0 + v1;
v0.l0 = v4;
}
export function method3(v0: string, v1: int32): string {
const v3: Mut0 = new Mut0(method4());
let v17: any;
closure0(v3, "{ ", undefined);
v17 = undefined;
let v36: any;
closure0(v3, "name", undefined);
v36 = undefined;
let v55: any;
closure0(v3, " = ", undefined);
v55 = undefined;
let v72: any;
closure0(v3, v0, undefined);
v72 = undefined;
let v91: any;
closure0(v3, "; ", undefined);
v91 = undefined;
let v110: any;
closure0(v3, "expected", undefined);
v110 = undefined;
let v127: any;
closure0(v3, " = ", undefined);
v127 = undefined;
let v147: any;
closure0(v3, `${v1}`, undefined);
v147 = undefined;
let v166: any;
closure0(v3, " }", undefined);
v166 = undefined;
return v3.l0;
}
export function closure1(v0: string, unitVar: void): void {
console.log(v0);
}
export function method0(): void {
const v4: boolean = (((9 + method1()) + 2) + 1) === 15;
const v6: boolean = v4 ? true : method2(v4);
const v9: string = method3("__assert_eq", 15);
let v13: any;
closure1(v9, undefined);
v13 = undefined;
if (v6 === false) {
throw new Error(v9);
}
}
method0();
.py:
from __future__ import annotations
from dataclasses import dataclass
from fable_modules.fable_library.reflection import (TypeInfo, string_type, record_type)
from fable_modules.fable_library.types import Record
def _expr0() -> TypeInfo:
return record_type("Spiral.Mut0", [], Mut0, lambda: [("l0", string_type)])
@dataclass(eq = False, repr = False, slots = True)
class Mut0(Record):
l0: str
Mut0_reflection = _expr0
def method1(__unit: None=None) -> int:
return 3
def method2(v0: bool) -> bool:
return v0
def method4(__unit: None=None) -> str:
return ""
def closure0(v0: Mut0, v1: str, unit_var: None) -> None:
v4: str = v0.l0 + v1
v0.l0 = v4
def method3(v0: str, v1: int) -> str:
v3: Mut0 = Mut0(method4())
v17: None
closure0(v3, "{ ", None)
v17 = None
v36: None
closure0(v3, "name", None)
v36 = None
v55: None
closure0(v3, " = ", None)
v55 = None
v72: None
closure0(v3, v0, None)
v72 = None
v91: None
closure0(v3, "; ", None)
v91 = None
v110: None
closure0(v3, "expected", None)
v110 = None
v127: None
closure0(v3, " = ", None)
v127 = None
v147: None
closure0(v3, ("" + str(v1)) + "", None)
v147 = None
v166: None
closure0(v3, " }", None)
v166 = None
return v3.l0
def closure1(v0: str, unit_var: None) -> None:
print(v0)
def method0(__unit: None=None) -> None:
v4: bool = (((9 + method1()) + 2) + 1) == 15
v6: bool = True if v4 else method2(v4)
v9: str = method3("__assert_eq", 15)
v13: None
closure1(v9, None)
v13 = None
if v6 == False:
raise Exception(v9)
method0()
.py (Python):
kernels_main = r"""
"""
from main_auto import *
kernels = kernels_aux + kernels_main
import cupy as cp
import numpy as np
from dataclasses import dataclass
from typing import NamedTuple, Union, Callable, Tuple
i8 = int; i16 = int; i32 = int; i64 = int; u8 = int; u16 = int; u32 = int; u64 = int; f32 = float; f64 = float; char = str; string = str
cuda = False
# fwd_dcls
# types
@dataclass
class Mut0:
v0 : string
# functions
def method1() -> i32:
return 3
def method2(v0 : bool) -> bool:
return v0
def method4() -> string:
v0 = ""
return v0
def method3(v0 : string, v1 : i32) -> string:
v2 = method4()
v3 = Mut0(v2)
del v2
v8 = "{ "
v9 = f"{v8}"
del v8
v19 = v3.v0
v22 = v19 + v9
del v9, v19
v3.v0 = v22
del v22
v28 = "name"
v29 = f"{v28}"
del v28
v39 = v3.v0
v42 = v39 + v29
del v29, v39
v3.v0 = v42
del v42
v48 = " = "
v49 = f"{v48}"
v59 = v3.v0
v62 = v59 + v49
del v49, v59
v3.v0 = v62
del v62
v66 = f"{v0}"
del v0
v76 = v3.v0
v79 = v76 + v66
del v66, v76
v3.v0 = v79
del v79
v85 = "; "
v86 = f"{v85}"
del v85
v96 = v3.v0
v99 = v96 + v86
del v86, v96
v3.v0 = v99
del v99
v105 = "expected"
v106 = f"{v105}"
del v105
v116 = v3.v0
v119 = v116 + v106
del v106, v116
v3.v0 = v119
del v119
v123 = f"{v48}"
del v48
v133 = v3.v0
v136 = v133 + v123
del v123, v133
v3.v0 = v136
del v136
v143 = f"{v1}"
del v1
v153 = v3.v0
v156 = v153 + v143
del v143, v153
v3.v0 = v156
del v156
v162 = " }"
v163 = f"{v162}"
del v162
v173 = v3.v0
v176 = v173 + v163
del v163, v173
v3.v0 = v176
del v176
v178 = v3.v0
del v3
return v178
def method0() -> None:
v0 = method1()
v1 = 9 + v0
del v0
v2 = v1 + 2
del v1
v3 = v2 + 1
del v2
v4 = v3 == 15
del v3
if v4:
v6 = True
else:
v6 = method2(v4)
del v4
v7 = "__assert_eq"
v8 = 15
v9 = method3(v7, v8)
del v7, v8
print(v9)
v15 = v6 == False
del v6
if v15:
del v15
raise Exception(v9)
else:
del v9, v15
return
# main_defs
def main_body():
return method0()
def main():
r = main_body()
if cuda: cp.cuda.get_current_stream().synchronize() # This line is here so the `__trap()` calls on the kernel aren't missed.
return r
if __name__ == '__main__': result = main(); None if result is None else print(result)
.fsx output:
{ name = __assert_eq; expected = 15 }
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
//// print_code
[ 5i32; 4; join 3; 2; 1 ]
|> fold_list (join_body (+)) 0
|> _assert_eq 15
.py output (Python):
{ name = __assert_eq; expected = 15 }
.rs output:
{ name = __assert_eq; expected = 15 }
.ts output:
{ name = __assert_eq; expected = 15 }
.py output:
{ name = __assert_eq; expected = 15 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 15 }
.gleam (Gleam):
import gleam/string
import gleam/io
pub type Mut0 { Mut0(l0 : String) }
pub fn method1 () -> Int {
3
}
pub fn method2 (v0 : Int, v1 : Int) -> Int {
let v2 = v1 + v0
v2
}
pub fn method3 (v0 : Bool) -> Bool {
v0
}
pub fn method5 () -> String {
let v0 = ""
v0
}
pub fn method4 (v0 : String, v1 : Int) -> String {
let v2 = method5()
let v3 = Mut0(l0 : v2)
let v4 = "{ "
let v5 = v4
let v9 = v3.l0
let v10 = v9 <> v5
let v3 = Mut0(l0: v10)
let v22 = "name"
let v23 = v22
let v27 = v3.l0
let v28 = v27 <> v23
let v3 = Mut0(l0: v28)
let v40 = " = "
let v41 = v40
let v45 = v3.l0
let v46 = v45 <> v41
let v3 = Mut0(l0: v46)
let v58 = v0
let v62 = v3.l0
let v63 = v62 <> v58
let v3 = Mut0(l0: v63)
let v75 = "; "
let v76 = v75
let v80 = v3.l0
let v81 = v80 <> v76
let v3 = Mut0(l0: v81)
let v93 = "expected"
let v94 = v93
let v98 = v3.l0
let v99 = v98 <> v94
let v3 = Mut0(l0: v99)
let v111 = v40
let v115 = v3.l0
let v116 = v115 <> v111
let v3 = Mut0(l0: v116)
let v128 = string.inspect(v1)
let v135 = v3.l0
let v136 = v135 <> v128
let v3 = Mut0(l0: v136)
let v148 = " }"
let v149 = v148
let v153 = v3.l0
let v154 = v153 <> v149
let v3 = Mut0(l0: v154)
let v166 = v3.l0
v166
}
pub fn method0 () -> Nil {
let v0 = method1()
let v1 = 9
let v2 = method2(v0, v1)
let v3 = v2 + 2
let v4 = v3 + 1
let v5 = v4 == 15
let v7 =
case v5 {
True -> {
True
}
False -> {
method3(v5)
}
}
let v8 = "__assert_eq"
let v9 = 15
let v10 = method4(v8, v9)
io.println(v10)
let v16 = v7 == False
case v16 {
True -> {
panic as v10
}
False -> {
Nil
}
}
}
pub fn main () { method0()
}
.fsx:
type Mut0 = {mutable l0 : string}
let rec method1 () : int32 =
3
and method2 (v0 : int32, v1 : int32) : int32 =
let v2 : int32 = v1 + v0
v2
and method3 (v0 : bool) : bool =
v0
and method5 () : string =
let v0 : string = ""
v0
and closure0 (v0 : Mut0, v1 : string) () : unit =
let v2 : string = v0.l0
let v4 : string = v2 + v1
v0.l0 <- v4
()
and method4 (v0 : string, v1 : int32) : string =
let v2 : string = method5()
let v3 : Mut0 = {l0 = v2} : Mut0
let v6 : string = "{ "
let v7 : string = $"{v6}"
let v15 : unit = ()
let v16 : (unit -> unit) = closure0(v3, v7)
let v17 : unit = (fun () -> v16 (); v15) ()
let v25 : string = "name"
let v26 : string = $"{v25}"
let v34 : unit = ()
let v35 : (unit -> unit) = closure0(v3, v26)
let v36 : unit = (fun () -> v35 (); v34) ()
let v44 : string = " = "
let v45 : string = $"{v44}"
let v53 : unit = ()
let v54 : (unit -> unit) = closure0(v3, v45)
let v55 : unit = (fun () -> v54 (); v53) ()
let v62 : string = $"{v0}"
let v70 : unit = ()
let v71 : (unit -> unit) = closure0(v3, v62)
let v72 : unit = (fun () -> v71 (); v70) ()
let v80 : string = "; "
let v81 : string = $"{v80}"
let v89 : unit = ()
let v90 : (unit -> unit) = closure0(v3, v81)
let v91 : unit = (fun () -> v90 (); v89) ()
let v99 : string = "expected"
let v100 : string = $"{v99}"
let v108 : unit = ()
let v109 : (unit -> unit) = closure0(v3, v100)
let v110 : unit = (fun () -> v109 (); v108) ()
let v117 : string = $"{v44}"
let v125 : unit = ()
let v126 : (unit -> unit) = closure0(v3, v117)
let v127 : unit = (fun () -> v126 (); v125) ()
let v137 : string = $"{v1}"
let v145 : unit = ()
let v146 : (unit -> unit) = closure0(v3, v137)
let v147 : unit = (fun () -> v146 (); v145) ()
let v155 : string = " }"
let v156 : string = $"{v155}"
let v164 : unit = ()
let v165 : (unit -> unit) = closure0(v3, v156)
let v166 : unit = (fun () -> v165 (); v164) ()
let v172 : string = v3.l0
v172
and closure1 (v0 : string) () : unit =
let v1 : (string -> unit) = System.Console.WriteLine
v1 v0
and method0 () : unit =
let v0 : int32 = method1()
let v1 : int32 = 9
let v2 : int32 = method2(v0, v1)
let v3 : int32 = v2 + 2
let v4 : int32 = v3 + 1
let v5 : bool = v4 = 15
let v7 : bool =
if v5 then
true
else
method3(v5)
let v8 : string = "__assert_eq"
let v9 : int32 = 15
let v10 : string = method4(v8, v9)
let v12 : unit = ()
let v13 : (unit -> unit) = closure1(v10)
let v14 : unit = (fun () -> v13 (); v12) ()
let v16 : bool = v7 = false
if v16 then
failwith<unit> v10
method0()
.rs:
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(unreachable_code)]
#![allow(unused_attributes)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(unused_parens)]
#![allow(unused_variables)]
#![allow(unused_assignments)]
mod module_6ff740fe {
pub mod Spiral {
use super::*;
use fable_library_rust::Native_::LrcPtr;
use fable_library_rust::Native_::MutCell;
use fable_library_rust::Native_::on_startup;
use fable_library_rust::String_::append;
use fable_library_rust::String_::printfn;
use fable_library_rust::String_::sprintf;
use fable_library_rust::String_::string;
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd)]
pub struct Mut0 {
pub l0: MutCell<string>,
}
impl core::fmt::Display for Mut0 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", core::any::type_name::<Self>())
}
}
pub fn method1() -> i32 {
3_i32
}
pub fn method2(v0: i32, v1: i32) -> i32 {
(v1) + (v0)
}
pub fn method3(v0: bool) -> bool {
v0
}
pub fn method5() -> string {
string("")
}
pub fn closure0(v0: LrcPtr<Spiral::Mut0>, v1: string, unitVar: ()) {
let v4: string = append((v0.l0.get().clone()), (v1));
v0.l0.set(v4);
()
}
pub fn method4(v0: string, v1: i32) -> string {
let v3: LrcPtr<Spiral::Mut0> = LrcPtr::new(Spiral::Mut0 {
l0: MutCell::new(Spiral::method5()),
});
let v17: () = {
Spiral::closure0(v3.clone(), string("{ "), ());
()
};
let v36: () = {
Spiral::closure0(v3.clone(), string("name"), ());
()
};
let v55: () = {
Spiral::closure0(v3.clone(), string(" = "), ());
()
};
let v72: () = {
Spiral::closure0(v3.clone(), v0, ());
()
};
let v91: () = {
Spiral::closure0(v3.clone(), string("; "), ());
()
};
let v110: () = {
Spiral::closure0(v3.clone(), string("expected"), ());
()
};
let v127: () = {
Spiral::closure0(v3.clone(), string(" = "), ());
()
};
let v147: () = {
Spiral::closure0(v3.clone(), sprintf!("{}", v1), ());
()
};
let v166: () = {
Spiral::closure0(v3.clone(), string(" }"), ());
()
};
v3.l0.get().clone()
}
pub fn closure1(v0: string, unitVar: ()) {
printfn!("{0}", v0);
}
pub fn method0() {
let v5: bool =
(((Spiral::method2(Spiral::method1(), 9_i32)) + 2_i32) + 1_i32) == 15_i32;
let v7: bool = if v5 { true } else { Spiral::method3(v5) };
let v10: string = Spiral::method4(string("__assert_eq"), 15_i32);
let v14: () = {
Spiral::closure1(v10.clone(), ());
()
};
if (v7) == false {
panic!("{}", v10,);
}
}
// on_startup!(Spiral::method0());
}
}
pub use module_6ff740fe::*;
pub fn main() -> Result<(), String> { Ok(Spiral::method0()) }
.ts:
import { Record } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Types.js";
import { IComparable, IEquatable } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Util.js";
import { record_type, string_type, TypeInfo } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Reflection.js";
import { int32 } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Int32.js";
export class Mut0 extends Record implements IEquatable<Mut0>, IComparable<Mut0> {
l0: string;
constructor(l0: string) {
super();
this.l0 = l0;
}
}
export function Mut0_$reflection(): TypeInfo {
return record_type("Spiral.Mut0", [], Mut0, () => [["l0", string_type]]);
}
export function method1(): int32 {
return 3;
}
export function method2(v0: int32, v1: int32): int32 {
return v1 + v0;
}
export function method3(v0: boolean): boolean {
return v0;
}
export function method5(): string {
return "";
}
export function closure0(v0: Mut0, v1: string, unitVar: void): void {
const v4: string = v0.l0 + v1;
v0.l0 = v4;
}
export function method4(v0: string, v1: int32): string {
const v3: Mut0 = new Mut0(method5());
let v17: any;
closure0(v3, "{ ", undefined);
v17 = undefined;
let v36: any;
closure0(v3, "name", undefined);
v36 = undefined;
let v55: any;
closure0(v3, " = ", undefined);
v55 = undefined;
let v72: any;
closure0(v3, v0, undefined);
v72 = undefined;
let v91: any;
closure0(v3, "; ", undefined);
v91 = undefined;
let v110: any;
closure0(v3, "expected", undefined);
v110 = undefined;
let v127: any;
closure0(v3, " = ", undefined);
v127 = undefined;
let v147: any;
closure0(v3, `${v1}`, undefined);
v147 = undefined;
let v166: any;
closure0(v3, " }", undefined);
v166 = undefined;
return v3.l0;
}
export function closure1(v0: string, unitVar: void): void {
console.log(v0);
}
export function method0(): void {
const v5: boolean = ((method2(method1(), 9) + 2) + 1) === 15;
const v7: boolean = v5 ? true : method3(v5);
const v10: string = method4("__assert_eq", 15);
let v14: any;
closure1(v10, undefined);
v14 = undefined;
if (v7 === false) {
throw new Error(v10);
}
}
method0();
.py:
from __future__ import annotations
from dataclasses import dataclass
from fable_modules.fable_library.reflection import (TypeInfo, string_type, record_type)
from fable_modules.fable_library.types import Record
def _expr0() -> TypeInfo:
return record_type("Spiral.Mut0", [], Mut0, lambda: [("l0", string_type)])
@dataclass(eq = False, repr = False, slots = True)
class Mut0(Record):
l0: str
Mut0_reflection = _expr0
def method1(__unit: None=None) -> int:
return 3
def method2(v0: int, v1: int) -> int:
return v1 + v0
def method3(v0: bool) -> bool:
return v0
def method5(__unit: None=None) -> str:
return ""
def closure0(v0: Mut0, v1: str, unit_var: None) -> None:
v4: str = v0.l0 + v1
v0.l0 = v4
def method4(v0: str, v1: int) -> str:
v3: Mut0 = Mut0(method5())
v17: None
closure0(v3, "{ ", None)
v17 = None
v36: None
closure0(v3, "name", None)
v36 = None
v55: None
closure0(v3, " = ", None)
v55 = None
v72: None
closure0(v3, v0, None)
v72 = None
v91: None
closure0(v3, "; ", None)
v91 = None
v110: None
closure0(v3, "expected", None)
v110 = None
v127: None
closure0(v3, " = ", None)
v127 = None
v147: None
closure0(v3, ("" + str(v1)) + "", None)
v147 = None
v166: None
closure0(v3, " }", None)
v166 = None
return v3.l0
def closure1(v0: str, unit_var: None) -> None:
print(v0)
def method0(__unit: None=None) -> None:
v5: bool = ((method2(method1(), 9) + 2) + 1) == 15
v7: bool = True if v5 else method3(v5)
v10: str = method4("__assert_eq", 15)
v14: None
closure1(v10, None)
v14 = None
if v7 == False:
raise Exception(v10)
method0()
.py (Python):
kernels_main = r"""
"""
from main_auto import *
kernels = kernels_aux + kernels_main
import cupy as cp
import numpy as np
from dataclasses import dataclass
from typing import NamedTuple, Union, Callable, Tuple
i8 = int; i16 = int; i32 = int; i64 = int; u8 = int; u16 = int; u32 = int; u64 = int; f32 = float; f64 = float; char = str; string = str
cuda = False
# fwd_dcls
# types
@dataclass
class Mut0:
v0 : string
# functions
def method1() -> i32:
return 3
def method2(v0 : i32, v1 : i32) -> i32:
v2 = v1 + v0
del v0, v1
return v2
def method3(v0 : bool) -> bool:
return v0
def method5() -> string:
v0 = ""
return v0
def method4(v0 : string, v1 : i32) -> string:
v2 = method5()
v3 = Mut0(v2)
del v2
v8 = "{ "
v9 = f"{v8}"
del v8
v19 = v3.v0
v22 = v19 + v9
del v9, v19
v3.v0 = v22
del v22
v28 = "name"
v29 = f"{v28}"
del v28
v39 = v3.v0
v42 = v39 + v29
del v29, v39
v3.v0 = v42
del v42
v48 = " = "
v49 = f"{v48}"
v59 = v3.v0
v62 = v59 + v49
del v49, v59
v3.v0 = v62
del v62
v66 = f"{v0}"
del v0
v76 = v3.v0
v79 = v76 + v66
del v66, v76
v3.v0 = v79
del v79
v85 = "; "
v86 = f"{v85}"
del v85
v96 = v3.v0
v99 = v96 + v86
del v86, v96
v3.v0 = v99
del v99
v105 = "expected"
v106 = f"{v105}"
del v105
v116 = v3.v0
v119 = v116 + v106
del v106, v116
v3.v0 = v119
del v119
v123 = f"{v48}"
del v48
v133 = v3.v0
v136 = v133 + v123
del v123, v133
v3.v0 = v136
del v136
v143 = f"{v1}"
del v1
v153 = v3.v0
v156 = v153 + v143
del v143, v153
v3.v0 = v156
del v156
v162 = " }"
v163 = f"{v162}"
del v162
v173 = v3.v0
v176 = v173 + v163
del v163, v173
v3.v0 = v176
del v176
v178 = v3.v0
del v3
return v178
def method0() -> None:
v0 = method1()
v1 = 9
v2 = method2(v0, v1)
del v0, v1
v3 = v2 + 2
del v2
v4 = v3 + 1
del v3
v5 = v4 == 15
del v4
if v5:
v7 = True
else:
v7 = method3(v5)
del v5
v8 = "__assert_eq"
v9 = 15
v10 = method4(v8, v9)
del v8, v9
print(v10)
v16 = v7 == False
del v7
if v16:
del v16
raise Exception(v10)
else:
del v10, v16
return
# main_defs
def main_body():
return method0()
def main():
r = main_body()
if cuda: cp.cuda.get_current_stream().synchronize() # This line is here so the `__trap()` calls on the kernel aren't missed.
return r
if __name__ == '__main__': result = main(); None if result is None else print(result)
.fsx output:
{ name = __assert_eq; expected = 15 }
join_body_unit¶
In [ ]:
inl join_body_unit body d x =
if var_is d |> not
then body x
else
inl x = dyn x
join body x
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
//// print_code
[ 5i32; 4; join 3; 2; 1 ]
|> fold_list (fun acc n => join_body_unit ((+) acc) n n) 0
|> _assert_eq 15
.py output (Python):
{ name = __assert_eq; expected = 15 }
.rs output:
{ name = __assert_eq; expected = 15 }
.ts output:
{ name = __assert_eq; expected = 15 }
.py output:
{ name = __assert_eq; expected = 15 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 15 }
.gleam (Gleam):
import gleam/string
import gleam/io
pub type Mut0 { Mut0(l0 : String) }
pub fn method1 () -> Int {
3
}
pub fn method2 (v0 : Int) -> Int {
let v1 = 9 + v0
v1
}
pub fn method3 (v0 : Bool) -> Bool {
v0
}
pub fn method5 () -> String {
let v0 = ""
v0
}
pub fn method4 (v0 : String, v1 : Int) -> String {
let v2 = method5()
let v3 = Mut0(l0 : v2)
let v4 = "{ "
let v5 = v4
let v9 = v3.l0
let v10 = v9 <> v5
let v3 = Mut0(l0: v10)
let v22 = "name"
let v23 = v22
let v27 = v3.l0
let v28 = v27 <> v23
let v3 = Mut0(l0: v28)
let v40 = " = "
let v41 = v40
let v45 = v3.l0
let v46 = v45 <> v41
let v3 = Mut0(l0: v46)
let v58 = v0
let v62 = v3.l0
let v63 = v62 <> v58
let v3 = Mut0(l0: v63)
let v75 = "; "
let v76 = v75
let v80 = v3.l0
let v81 = v80 <> v76
let v3 = Mut0(l0: v81)
let v93 = "expected"
let v94 = v93
let v98 = v3.l0
let v99 = v98 <> v94
let v3 = Mut0(l0: v99)
let v111 = v40
let v115 = v3.l0
let v116 = v115 <> v111
let v3 = Mut0(l0: v116)
let v128 = string.inspect(v1)
let v135 = v3.l0
let v136 = v135 <> v128
let v3 = Mut0(l0: v136)
let v148 = " }"
let v149 = v148
let v153 = v3.l0
let v154 = v153 <> v149
let v3 = Mut0(l0: v154)
let v166 = v3.l0
v166
}
pub fn method0 () -> Nil {
let v0 = method1()
let v1 = method2(v0)
let v2 = v1 + 2
let v3 = v2 + 1
let v4 = v3 == 15
let v6 =
case v4 {
True -> {
True
}
False -> {
method3(v4)
}
}
let v7 = "__assert_eq"
let v8 = 15
let v9 = method4(v7, v8)
io.println(v9)
let v15 = v6 == False
case v15 {
True -> {
panic as v9
}
False -> {
Nil
}
}
}
pub fn main () { method0()
}
.fsx:
type Mut0 = {mutable l0 : string}
let rec method1 () : int32 =
3
and method2 (v0 : int32) : int32 =
let v1 : int32 = 9 + v0
v1
and method3 (v0 : bool) : bool =
v0
and method5 () : string =
let v0 : string = ""
v0
and closure0 (v0 : Mut0, v1 : string) () : unit =
let v2 : string = v0.l0
let v4 : string = v2 + v1
v0.l0 <- v4
()
and method4 (v0 : string, v1 : int32) : string =
let v2 : string = method5()
let v3 : Mut0 = {l0 = v2} : Mut0
let v6 : string = "{ "
let v7 : string = $"{v6}"
let v15 : unit = ()
let v16 : (unit -> unit) = closure0(v3, v7)
let v17 : unit = (fun () -> v16 (); v15) ()
let v25 : string = "name"
let v26 : string = $"{v25}"
let v34 : unit = ()
let v35 : (unit -> unit) = closure0(v3, v26)
let v36 : unit = (fun () -> v35 (); v34) ()
let v44 : string = " = "
let v45 : string = $"{v44}"
let v53 : unit = ()
let v54 : (unit -> unit) = closure0(v3, v45)
let v55 : unit = (fun () -> v54 (); v53) ()
let v62 : string = $"{v0}"
let v70 : unit = ()
let v71 : (unit -> unit) = closure0(v3, v62)
let v72 : unit = (fun () -> v71 (); v70) ()
let v80 : string = "; "
let v81 : string = $"{v80}"
let v89 : unit = ()
let v90 : (unit -> unit) = closure0(v3, v81)
let v91 : unit = (fun () -> v90 (); v89) ()
let v99 : string = "expected"
let v100 : string = $"{v99}"
let v108 : unit = ()
let v109 : (unit -> unit) = closure0(v3, v100)
let v110 : unit = (fun () -> v109 (); v108) ()
let v117 : string = $"{v44}"
let v125 : unit = ()
let v126 : (unit -> unit) = closure0(v3, v117)
let v127 : unit = (fun () -> v126 (); v125) ()
let v137 : string = $"{v1}"
let v145 : unit = ()
let v146 : (unit -> unit) = closure0(v3, v137)
let v147 : unit = (fun () -> v146 (); v145) ()
let v155 : string = " }"
let v156 : string = $"{v155}"
let v164 : unit = ()
let v165 : (unit -> unit) = closure0(v3, v156)
let v166 : unit = (fun () -> v165 (); v164) ()
let v172 : string = v3.l0
v172
and closure1 (v0 : string) () : unit =
let v1 : (string -> unit) = System.Console.WriteLine
v1 v0
and method0 () : unit =
let v0 : int32 = method1()
let v1 : int32 = method2(v0)
let v2 : int32 = v1 + 2
let v3 : int32 = v2 + 1
let v4 : bool = v3 = 15
let v6 : bool =
if v4 then
true
else
method3(v4)
let v7 : string = "__assert_eq"
let v8 : int32 = 15
let v9 : string = method4(v7, v8)
let v11 : unit = ()
let v12 : (unit -> unit) = closure1(v9)
let v13 : unit = (fun () -> v12 (); v11) ()
let v15 : bool = v6 = false
if v15 then
failwith<unit> v9
method0()
.rs:
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(unreachable_code)]
#![allow(unused_attributes)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(unused_parens)]
#![allow(unused_variables)]
#![allow(unused_assignments)]
mod module_6ff740fe {
pub mod Spiral {
use super::*;
use fable_library_rust::Native_::LrcPtr;
use fable_library_rust::Native_::MutCell;
use fable_library_rust::Native_::on_startup;
use fable_library_rust::String_::append;
use fable_library_rust::String_::printfn;
use fable_library_rust::String_::sprintf;
use fable_library_rust::String_::string;
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd)]
pub struct Mut0 {
pub l0: MutCell<string>,
}
impl core::fmt::Display for Mut0 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", core::any::type_name::<Self>())
}
}
pub fn method1() -> i32 {
3_i32
}
pub fn method2(v0: i32) -> i32 {
9_i32 + (v0)
}
pub fn method3(v0: bool) -> bool {
v0
}
pub fn method5() -> string {
string("")
}
pub fn closure0(v0: LrcPtr<Spiral::Mut0>, v1: string, unitVar: ()) {
let v4: string = append((v0.l0.get().clone()), (v1));
v0.l0.set(v4);
()
}
pub fn method4(v0: string, v1: i32) -> string {
let v3: LrcPtr<Spiral::Mut0> = LrcPtr::new(Spiral::Mut0 {
l0: MutCell::new(Spiral::method5()),
});
let v17: () = {
Spiral::closure0(v3.clone(), string("{ "), ());
()
};
let v36: () = {
Spiral::closure0(v3.clone(), string("name"), ());
()
};
let v55: () = {
Spiral::closure0(v3.clone(), string(" = "), ());
()
};
let v72: () = {
Spiral::closure0(v3.clone(), v0, ());
()
};
let v91: () = {
Spiral::closure0(v3.clone(), string("; "), ());
()
};
let v110: () = {
Spiral::closure0(v3.clone(), string("expected"), ());
()
};
let v127: () = {
Spiral::closure0(v3.clone(), string(" = "), ());
()
};
let v147: () = {
Spiral::closure0(v3.clone(), sprintf!("{}", v1), ());
()
};
let v166: () = {
Spiral::closure0(v3.clone(), string(" }"), ());
()
};
v3.l0.get().clone()
}
pub fn closure1(v0: string, unitVar: ()) {
printfn!("{0}", v0);
}
pub fn method0() {
let v4: bool = (((Spiral::method2(Spiral::method1())) + 2_i32) + 1_i32) == 15_i32;
let v6: bool = if v4 { true } else { Spiral::method3(v4) };
let v9: string = Spiral::method4(string("__assert_eq"), 15_i32);
let v13: () = {
Spiral::closure1(v9.clone(), ());
()
};
if (v6) == false {
panic!("{}", v9,);
}
}
// on_startup!(Spiral::method0());
}
}
pub use module_6ff740fe::*;
pub fn main() -> Result<(), String> { Ok(Spiral::method0()) }
.ts:
import { Record } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Types.js";
import { IComparable, IEquatable } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Util.js";
import { record_type, string_type, TypeInfo } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Reflection.js";
import { int32 } from "./fable_modules/fable-library-ts.5.0.0-alpha.9/Int32.js";
export class Mut0 extends Record implements IEquatable<Mut0>, IComparable<Mut0> {
l0: string;
constructor(l0: string) {
super();
this.l0 = l0;
}
}
export function Mut0_$reflection(): TypeInfo {
return record_type("Spiral.Mut0", [], Mut0, () => [["l0", string_type]]);
}
export function method1(): int32 {
return 3;
}
export function method2(v0: int32): int32 {
return 9 + v0;
}
export function method3(v0: boolean): boolean {
return v0;
}
export function method5(): string {
return "";
}
export function closure0(v0: Mut0, v1: string, unitVar: void): void {
const v4: string = v0.l0 + v1;
v0.l0 = v4;
}
export function method4(v0: string, v1: int32): string {
const v3: Mut0 = new Mut0(method5());
let v17: any;
closure0(v3, "{ ", undefined);
v17 = undefined;
let v36: any;
closure0(v3, "name", undefined);
v36 = undefined;
let v55: any;
closure0(v3, " = ", undefined);
v55 = undefined;
let v72: any;
closure0(v3, v0, undefined);
v72 = undefined;
let v91: any;
closure0(v3, "; ", undefined);
v91 = undefined;
let v110: any;
closure0(v3, "expected", undefined);
v110 = undefined;
let v127: any;
closure0(v3, " = ", undefined);
v127 = undefined;
let v147: any;
closure0(v3, `${v1}`, undefined);
v147 = undefined;
let v166: any;
closure0(v3, " }", undefined);
v166 = undefined;
return v3.l0;
}
export function closure1(v0: string, unitVar: void): void {
console.log(v0);
}
export function method0(): void {
const v4: boolean = ((method2(method1()) + 2) + 1) === 15;
const v6: boolean = v4 ? true : method3(v4);
const v9: string = method4("__assert_eq", 15);
let v13: any;
closure1(v9, undefined);
v13 = undefined;
if (v6 === false) {
throw new Error(v9);
}
}
method0();
.py:
from __future__ import annotations
from dataclasses import dataclass
from fable_modules.fable_library.reflection import (TypeInfo, string_type, record_type)
from fable_modules.fable_library.types import Record
def _expr0() -> TypeInfo:
return record_type("Spiral.Mut0", [], Mut0, lambda: [("l0", string_type)])
@dataclass(eq = False, repr = False, slots = True)
class Mut0(Record):
l0: str
Mut0_reflection = _expr0
def method1(__unit: None=None) -> int:
return 3
def method2(v0: int) -> int:
return 9 + v0
def method3(v0: bool) -> bool:
return v0
def method5(__unit: None=None) -> str:
return ""
def closure0(v0: Mut0, v1: str, unit_var: None) -> None:
v4: str = v0.l0 + v1
v0.l0 = v4
def method4(v0: str, v1: int) -> str:
v3: Mut0 = Mut0(method5())
v17: None
closure0(v3, "{ ", None)
v17 = None
v36: None
closure0(v3, "name", None)
v36 = None
v55: None
closure0(v3, " = ", None)
v55 = None
v72: None
closure0(v3, v0, None)
v72 = None
v91: None
closure0(v3, "; ", None)
v91 = None
v110: None
closure0(v3, "expected", None)
v110 = None
v127: None
closure0(v3, " = ", None)
v127 = None
v147: None
closure0(v3, ("" + str(v1)) + "", None)
v147 = None
v166: None
closure0(v3, " }", None)
v166 = None
return v3.l0
def closure1(v0: str, unit_var: None) -> None:
print(v0)
def method0(__unit: None=None) -> None:
v4: bool = ((method2(method1()) + 2) + 1) == 15
v6: bool = True if v4 else method3(v4)
v9: str = method4("__assert_eq", 15)
v13: None
closure1(v9, None)
v13 = None
if v6 == False:
raise Exception(v9)
method0()
.py (Python):
kernels_main = r"""
"""
from main_auto import *
kernels = kernels_aux + kernels_main
import cupy as cp
import numpy as np
from dataclasses import dataclass
from typing import NamedTuple, Union, Callable, Tuple
i8 = int; i16 = int; i32 = int; i64 = int; u8 = int; u16 = int; u32 = int; u64 = int; f32 = float; f64 = float; char = str; string = str
cuda = False
# fwd_dcls
# types
@dataclass
class Mut0:
v0 : string
# functions
def method1() -> i32:
return 3
def method2(v0 : i32) -> i32:
v1 = 9 + v0
del v0
return v1
def method3(v0 : bool) -> bool:
return v0
def method5() -> string:
v0 = ""
return v0
def method4(v0 : string, v1 : i32) -> string:
v2 = method5()
v3 = Mut0(v2)
del v2
v8 = "{ "
v9 = f"{v8}"
del v8
v19 = v3.v0
v22 = v19 + v9
del v9, v19
v3.v0 = v22
del v22
v28 = "name"
v29 = f"{v28}"
del v28
v39 = v3.v0
v42 = v39 + v29
del v29, v39
v3.v0 = v42
del v42
v48 = " = "
v49 = f"{v48}"
v59 = v3.v0
v62 = v59 + v49
del v49, v59
v3.v0 = v62
del v62
v66 = f"{v0}"
del v0
v76 = v3.v0
v79 = v76 + v66
del v66, v76
v3.v0 = v79
del v79
v85 = "; "
v86 = f"{v85}"
del v85
v96 = v3.v0
v99 = v96 + v86
del v86, v96
v3.v0 = v99
del v99
v105 = "expected"
v106 = f"{v105}"
del v105
v116 = v3.v0
v119 = v116 + v106
del v106, v116
v3.v0 = v119
del v119
v123 = f"{v48}"
del v48
v133 = v3.v0
v136 = v133 + v123
del v123, v133
v3.v0 = v136
del v136
v143 = f"{v1}"
del v1
v153 = v3.v0
v156 = v153 + v143
del v143, v153
v3.v0 = v156
del v156
v162 = " }"
v163 = f"{v162}"
del v162
v173 = v3.v0
v176 = v173 + v163
del v163, v173
v3.v0 = v176
del v176
v178 = v3.v0
del v3
return v178
def method0() -> None:
v0 = method1()
v1 = method2(v0)
del v0
v2 = v1 + 2
del v1
v3 = v2 + 1
del v2
v4 = v3 == 15
del v3
if v4:
v6 = True
else:
v6 = method3(v4)
del v4
v7 = "__assert_eq"
v8 = 15
v9 = method4(v7, v8)
del v7, v8
print(v9)
v15 = v6 == False
del v6
if v15:
del v15
raise Exception(v9)
else:
del v9, v15
return
# main_defs
def main_body():
return method0()
def main():
r = main_body()
if cuda: cp.cuda.get_current_stream().synchronize() # This line is here so the `__trap()` calls on the kernel aren't missed.
return r
if __name__ == '__main__': result = main(); None if result is None else print(result)
.fsx output:
{ name = __assert_eq; expected = 15 }
(+.)¶
In [ ]:
inl (+.) forall t. (a : t) (b : t) : t =
$'!a + !b '
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
($'3' : i32) +. ($'-6' : i32)
|> _assert_eq -3i32
.py output (Python):
{ name = __assert_eq; expected = -3 }
.rs output:
{ name = __assert_eq; expected = -3 }
.ts output:
{ name = __assert_eq; expected = -3 }
.py output:
{ name = __assert_eq; expected = -3 }
.gleam output (Gleam):
{ name = __assert_eq; expected = -3 }
.fsx output:
{ name = __assert_eq; expected = -3 }
(-.)¶
In [ ]:
inl (-.) forall t. (a : t) (b : t) : t =
$'!a - !b '
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
($'3' : i32) -. ($'6' : i32)
|> _assert_eq -3i32
.py output (Python):
{ name = __assert_eq; expected = -3 }
.rs output:
{ name = __assert_eq; expected = -3 }
.ts output:
{ name = __assert_eq; expected = -3 }
.py output:
{ name = __assert_eq; expected = -3 }
.gleam output (Gleam):
{ name = __assert_eq; expected = -3 }
.fsx output:
{ name = __assert_eq; expected = -3 }
(*.)¶
In [ ]:
inl (*.) forall t. (a : t) (b : t) : t =
$'!a * !b '
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
($'3' : i32) *. ($'-1' : i32)
|> _assert_eq -3i32
.py output (Python):
{ name = __assert_eq; expected = -3 }
.rs output:
{ name = __assert_eq; expected = -3 }
.ts output:
{ name = __assert_eq; expected = -3 }
.py output:
{ name = __assert_eq; expected = -3 }
.gleam output (Gleam):
{ name = __assert_eq; expected = -3 }
.fsx output:
{ name = __assert_eq; expected = -3 }
(/.)¶
In [ ]:
inl (/.) forall t. (a : t) (b : t) : t =
$'!a / !b '
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
($'-3' : i32) /. ($'1' : i32)
|> _assert_eq -3i32
.py output (Python):
{ name = __assert_eq; expected = -3 }
.rs output:
{ name = __assert_eq; expected = -3 }
.ts output:
{ name = __assert_eq; expected = -3 }
.py output:
{ name = __assert_eq; expected = -3 }
.gleam output (Gleam):
{ name = __assert_eq; expected = -3 }
.fsx output:
{ name = __assert_eq; expected = -3 }
(=.)¶
In [ ]:
inl (=.) forall t. (a : t) (b : t) : bool =
backend_switch {
Gleam = fun () => $'!a == !b ' : bool
Fsharp = fun () => $'!a = !b ' : bool
Python = fun () => $'!a == !b ' : bool
}
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
($'-3' : i32) =. ($'-3' : i32)
|> _assert_eq true
.py output (Python):
{ name = __assert_eq; expected = true }
.rs output:
{ name = __assert_eq; expected = true }
.ts output:
{ name = __assert_eq; expected = true }
.py output:
{ name = __assert_eq; expected = true }
.gleam output (Gleam):
{ name = __assert_eq; expected = true }
.fsx output:
{ name = __assert_eq; expected = true }
(<>.)¶
In [ ]:
inl (<>.) forall t. (a : t) (b : t) : bool =
backend_switch {
Gleam = fun () => $'!a \!= !b ' : bool
Fsharp = fun () => $'!a <> !b ' : bool
Python = fun () => $'!a \!= !b ' : bool
}
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
($'-3' : i32) <>. ($'3' : i32)
|> _assert_eq true
.py output (Python):
{ name = __assert_eq; expected = true }
.rs output:
{ name = __assert_eq; expected = true }
.ts output:
{ name = __assert_eq; expected = true }
.py output:
{ name = __assert_eq; expected = true }
.gleam output (Gleam):
{ name = __assert_eq; expected = true }
.fsx output:
{ name = __assert_eq; expected = true }
(<>..)¶
In [ ]:
inl (<>..) a b =
fun () => a = b
|> dyn
|> eval
|> not
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
($'-3' : i32) <>.. ($'3' : i32)
|> _assert_eq true
.py output (Python):
{ name = __assert_eq; expected = true }
.rs output:
{ name = __assert_eq; expected = true }
.ts output:
{ name = __assert_eq; expected = true }
.py output:
{ name = __assert_eq; expected = true }
.gleam output (Gleam):
{ name = __assert_eq; expected = true }
.fsx output:
{ name = __assert_eq; expected = true }
append¶
In [ ]:
prototype append t : t -> t -> t
(++)¶
In [ ]:
inl (++) a b =
b |> append a
pair¶
In [ ]:
type pair_switch a b =
{
Gleam : $'\#(`a, `b )'
Fsharp : $'(`a * `b)'
Python : $'(`a, `b)'
}
nominal pair a b = $'backend_switch `(pair_switch a b)'
inl pair x y =
x, y
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
pair 1i32 2i32
|> _assert_eq (1, 2)
.py output (Python):
{ name = __assert_eq; expected = 1, 2 }
.rs output:
{ name = __assert_eq; expected = 1, 2 }
.ts output:
{ name = __assert_eq; expected = 1, 2 }
.py output:
{ name = __assert_eq; expected = 1, 2 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 1, 2 }
.fsx output:
{ name = __assert_eq; expected = 1, 2 }
new_pair¶
In [ ]:
inl new_pair forall a b. (a : a) (b : b) : pair a b =
backend_switch {
Gleam = fun () =>
$'\#(!a, !b )' : pair a b
Fsharp = fun () =>
$'!a, !b ' : pair a b
Python = fun () =>
$'!a, !b ' : pair a b
}
from_pair¶
In [ ]:
inl from_pair forall a b. (pair : pair a b) : a * b =
backend_switch {
Gleam = fun () =>
$'let \#(a, b) = !pair '
($'a' : a), ($'b' : b)
Fsharp = fun () =>
$'let (a, b) = !pair '
($'a' : a), ($'b' : b)
Python = fun () =>
$'a, b = !pair '
($'a' : a), ($'b' : b)
}
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
new_pair "a" (new_pair 1i32 "b")
|> from_pair
|> _assert_eq' ("a", (new_pair 1i32 "b"))
.py output (Python):
{ name = __assert_eq'; expected = a, (1, 'b') }
.rs output:
{ name = __assert_eq'; expected = a, (
1,
"b",
) }
.ts output:
{ name = __assert_eq'; expected = a, 1,b }
.py output:
{ name = __assert_eq'; expected = a, (1, 'b') }
.gleam output (Gleam):
{ name = __assert_eq'; expected = a, #(1, "b") }
.fsx output:
{ name = __assert_eq'; expected = a, (1, "b") }
(||>)¶
In [ ]:
inl (||>) (arg1, arg2) fn =
arg2 |> fn arg1
(||>)¶
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
(3i32, 2i32)
||> fun a b => a - b
|> _assert_eq 1
.py output (Python):
{ name = __assert_eq; expected = 1 }
.rs output:
{ name = __assert_eq; expected = 1 }
.ts output:
{ name = __assert_eq; expected = 1 }
.py output:
{ name = __assert_eq; expected = 1 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 1 }
.fsx output:
{ name = __assert_eq; expected = 1 }
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
(1i32, 2i32)
||> flip pair
|> _assert_eq (2, 1)
.py output (Python):
{ name = __assert_eq; expected = 2, 1 }
.rs output:
{ name = __assert_eq; expected = 2, 1 }
.ts output:
{ name = __assert_eq; expected = 2, 1 }
.py output:
{ name = __assert_eq; expected = 2, 1 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 2, 1 }
.fsx output:
{ name = __assert_eq; expected = 2, 1 }
fix_condition¶
In [ ]:
inl fix_condition x a b =
if x ()
then a () |> fun x => $'(* fix_condition then' : ()
else
$'fix_condition then *) else' : ()
b () |> fun x => $'(* fix_condition else' : ()
|> fun x => $'fix_condition else *)' : ()
infer¶
In [ ]:
nominal infer = $'_'
infer'¶
In [ ]:
nominal infer' t = $'_'
any¶
In [ ]:
nominal any = $'obj'
null¶
In [ ]:
inl null forall t. () : t =
backend_switch {
Gleam = fun () => $'Nil' : t
Fsharp = fun () => $'null |> unbox<`t>' : t
Python = fun () => $'None' : t
}
defaultof¶
In [ ]:
inl defaultof forall t. () : t =
inl run default =
real
typecase t with
| string => ""
| i32 => 0
| option ~u => None `u
| _ => default ()
backend_switch {
Gleam = fun () => run (fun () => (real failwith `t "base.defaultof / invalid type") : t) : t
Fsharp = fun () => run (fun () => $'Unchecked.defaultof<`t>' : t) : t
}
In [ ]:
//// test
///! gleam
///! fsharp
real defaultof `string ()
|> _assert_eq ""
real defaultof `i32 ()
|> _assert_eq 0i32
real defaultof `(option string) ()
|> _assert_eq (None : option string)
.gleam output (Gleam):
{ name = __assert_eq; expected = }
{ name = __assert_eq; expected = 0 }
{ name = __assert_eq; expected = Us0i1 }
.fsx output:
{ name = __assert_eq; expected = }
{ name = __assert_eq; expected = 0 }
{ name = __assert_eq; expected = US0_1 }
choice2'¶
In [ ]:
nominal choice2' a b = $'Choice<`a, `b>'
choice2_unbox¶
In [ ]:
inl choice2_unbox forall t1 t2. (choice : choice2' t1 t2) : choice2 t1 t2 =
run_target_args (fun () => choice) function
| Fsharp _ => fun choice =>
inl c1of2 (x : t1) : _ _ t2 = C1of2 x
inl c2of2 (x : t2) : _ t1 _ = C2of2 x
inl c1of2 = join c1of2
inl c2of2 = join c2of2
$'match !choice with Choice1Of2 x -> !c1of2 x | Choice2Of2 x -> !c2of2 x'
| _ => fun _ => null ()
ref¶
In [ ]:
nominal ref t = $'`t ref'
new_ref¶
In [ ]:
inl new_ref forall t. (x : t) : ref t =
$'ref !x '
ref_value¶
In [ ]:
inl ref_value forall t. (x : ref t) : t =
$'!x.Value'
ref_set_value¶
In [ ]:
inl ref_set_value forall t. (value : t) (ref : ref t) : ref t =
$'!ref.Value <- !value '
ref
to¶
In [ ]:
inl to forall t u. (x : t) : u =
$'!x ' : u
convert¶
In [ ]:
inl convert forall t u. (x : t) : u =
backend_switch {
Gleam = fun () => $'!x ' : u
Fsharp = fun () => $'!x |> `u ' : u
Python = fun () => $'`u(!x)' : u
}
unbox¶
In [ ]:
inl unbox forall t u. (x : t) : u =
backend_switch {
Gleam = fun () => x |> to : u
Fsharp = fun () => $'!x |> unbox<`u>' : u
Python = fun () => x |> to : u
}
u8¶
In [ ]:
inl u8 forall t. (x : t) : u8 =
backend_switch {
Gleam = fun () => x |> to : u8
Fsharp = fun () => x |> $'uint8' : u8
Python = fun () => x |> to : u8
}
u16¶
In [ ]:
inl u16 forall t. (x : t) : u16 =
backend_switch {
Gleam = fun () => x |> to : u16
Fsharp = fun () => x |> $'uint16' : u16
Python = fun () => $'!x & 0xFFFF' : u16
}
u64¶
In [ ]:
inl u64 forall t. (x : t) : u64 =
backend_switch {
Gleam = fun () => x |> to : u64
Fsharp = fun () => x |> $'uint64' : u64
Python = fun () => x |> to : u64
}
i32¶
In [ ]:
inl i32 forall t. (x : t) : i32 =
backend_switch {
Gleam = fun () => x |> convert : i32
Fsharp = fun () => x |> convert : i32
Python = fun () => x |> convert : i32
}
i64¶
In [ ]:
inl i64 forall t. (x : t) : i64 =
backend_switch {
Gleam = fun () => x |> to : i64
Fsharp = fun () => x |> $'int64' : i64
Python = fun () => x |> to : i64
}
f32¶
In [ ]:
inl f32 forall t. (x : t) : f32 =
backend_switch {
Gleam = fun () => x |> to : f32
Fsharp = fun () => x |> $'float32' : f32
Python = fun () => x |> to : f32
}
f64¶
In [ ]:
inl f64 forall t. (x : t) : f64 =
backend_switch {
Gleam = fun () => x |> to : f64
Fsharp = fun () => x |> $'float' : f64
Python = fun () => x |> to : f64
}
unativeint¶
In [ ]:
nominal unativeint = $'unativeint'
convert_i32¶
In [ ]:
inl convert_i32 forall t. (x : t) : i32 =
backend_switch {
Gleam = fun () => x |> to : i32
Fsharp = fun () => x |> $'System.Convert.ToInt32' : i32
Python = fun () => x |> to : i32
}
convert_i32_base¶
In [ ]:
inl convert_i32_base forall t. (base : i32) (x : t) : i32 =
backend_switch {
Fsharp = fun () => $'System.Convert.ToInt32 (!x, !base)' : i32
Python = fun () => $'int (!x, !base)' : i32
}
(:>)¶
In [ ]:
prototype (~:>) r : forall t. t -> r
to_any¶
In [ ]:
inl to_any forall t. (obj : t) : any =
obj |> to
(~:>) any¶
In [ ]:
instance (~:>) any = to_any
exn¶
In [ ]:
type exn_switch =
{
Gleam : $'Nil'
Fsharp : $'exn'
Python : $'BaseException'
}
nominal exn = $'backend_switch `(exn_switch)'
inl exn x =
x |> $'`exn '
try¶
In [ ]:
inl try forall t. (fn : () -> t) (ex_fn : exn -> option t) : option t =
inl some x : option t = Some x
inl some = dyn some
inl fn = dyn fn
inl ex_fn = dyn ex_fn
backend_switch {
Gleam = fun () =>
fn () |> some
Fsharp = fun () =>
$'let result = ref !(None : option t)'
$'try'
$' result.Value <- !fn () |> !some '
$'with ex ->'
$' result.Value <- !ex_fn ex '
$'result.Value' : option t
Python = fun () =>
$'result = !(None : option t)'
inl fn = dyn fn
inl ex_fn = dyn ex_fn
$'try:'
$' result = !some(!fn())\n \'\'\''
$'\'\'\''
$'except Exception as e:'
$' result = !ex_fn(e)'
$'result' : option t
}
In [ ]:
//// test
///! fsharp
///! cuda
///! rust
///! typescript
///! python
try
fun () => a ;[ 0i32 ] |> am'.index 1i32 |> sm'.format
(fun ex => $'!ex ' |> sm'.format_exception |> Some)
|> optionm.value
|> _assert_eq (run_target function
| Gleam => fun () => null ()
| Fsharp => fun () => join "System.IndexOutOfRangeException: Index was outside the bounds of the array."
| Cuda => fun () => "index 1 is out of bounds for axis 0 with size 1"
| Cpp => fun () => null ()
| Rust => fun () => "Exception { message: \"index out of bounds: the len is 1 but the index is 1\" }"
| TypeScript => fun () => "Error: Index was outside the bounds of the array.\\nParameter name: index"
| Python => fun () => "array index out of range"
)
.py output (Python):
{ name = __assert_eq; expected = index 1 is out of bounds for axis 0 with size 1 }
.rs output:
{ name = __assert_eq; expected = Exception { message: "index out of bounds: the len is 1 but the index is 1" } }
.ts output:
{ name = __assert_eq; expected = Error: Index was outside the bounds of the array.\nParameter name: index }
.py output:
{ name = __assert_eq; expected = array index out of range }
.fsx output:
{ name = __assert_eq; expected = System.IndexOutOfRangeException: Index was outside the bounds of the array. }
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
try
fun () => a ;[ 0i32 ] |> am'.index 0i32 |> sm'.format
(fun ex => $'!ex ' |> sm'.format_exception |> Some)
|> optionm.value
|> _assert_eq "0"
.py output (Python):
{ name = __assert_eq; expected = 0 }
.rs output:
{ name = __assert_eq; expected = 0 }
.ts output:
{ name = __assert_eq; expected = 0 }
.py output:
{ name = __assert_eq; expected = 0 }
.gleam output (Gleam):
{ name = __assert_eq; expected = 0 }
.fsx output:
{ name = __assert_eq; expected = 0 }
try_unit¶
In [ ]:
inl try_unit forall t. (fn : () -> ()) (ex_fn : (() -> exn) -> ()) : t =
backend_switch {
Fsharp = fun () => $'try' : ()
Python = fun () => $'try:' : ()
}
fn |> indent
backend_switch {
Fsharp = fun () => $'with ex ->' : ()
Python = fun () => $'except Exception as ex:' : ()
}
fun () =>
inl ex = $'ex'
inl ex () =
ex
ex_fn ex
|> indent
backend_switch {
Fsharp = fun () =>
$'(* try_unit'
$'try_unit *)' : t
Python = fun () => $'' : t
}
try_unit'¶
In [ ]:
inl try_unit' forall t. (ex_fn : (() -> exn) -> ()) (fn : () -> ()) : t =
try_unit fn ex_fn
try_finally¶
In [ ]:
inl try_finally forall t. (fn : () -> ()) (finally : () -> ()) : t =
backend_switch {
Fsharp = fun () => $'try' : ()
Python = fun () => $'try:' : ()
}
fn |> indent
backend_switch {
Fsharp = fun () => $'finally' : ()
Python = fun () => $'finally:' : ()
}
finally |> indent
backend_switch {
Fsharp = fun () =>
$'(* try_finally'
$'try_finally *)'
()
Python = fun () => ()
}