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; actual = 1; expected = 1 } .rs output: { name = __assert_eq; actual = 1; expected = 1 } .ts output: { name = __assert_eq; actual = 1; expected = 1 } .py output: { name = __assert_eq; actual = 1; expected = 1 } .gleam output (Gleam): { name = __assert_eq; actual = 1; expected = 1 }
.fsx output: { name = __assert_eq; actual = 1; 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; actual = 1; expected = 1 } .rs output: { name = __assert_eq; actual = 1; expected = 1 } .ts output: { name = __assert_eq; actual = 1; expected = 1 } .py output: { name = __assert_eq; actual = 1; expected = 1 }
.fsx output: { name = __assert_eq; actual = 1; 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; actual = 15; expected = 15 } .rs output: { name = __assert_eq; actual = 15; expected = 15 } .ts output: { name = __assert_eq; actual = 15; expected = 15 } .py output: { name = __assert_eq; actual = 15; expected = 15 } .gleam output (Gleam): { name = __assert_eq; actual = 15; 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, v2 : Int) -> String { let v3 = method4() let v4 = Mut0(l0 : v3) let v5 = "{ " let v6 = v5 let v10 = v4.l0 let v11 = v10 <> v6 let v4 = Mut0(l0: v11) let v23 = "name" let v24 = v23 let v28 = v4.l0 let v29 = v28 <> v24 let v4 = Mut0(l0: v29) let v41 = " = " let v42 = v41 let v46 = v4.l0 let v47 = v46 <> v42 let v4 = Mut0(l0: v47) let v59 = v0 let v63 = v4.l0 let v64 = v63 <> v59 let v4 = Mut0(l0: v64) let v76 = "; " let v77 = v76 let v81 = v4.l0 let v82 = v81 <> v77 let v4 = Mut0(l0: v82) let v94 = "actual" let v95 = v94 let v99 = v4.l0 let v100 = v99 <> v95 let v4 = Mut0(l0: v100) let v112 = v41 let v116 = v4.l0 let v117 = v116 <> v112 let v4 = Mut0(l0: v117) let v129 = string.inspect(v1) let v136 = v4.l0 let v137 = v136 <> v129 let v4 = Mut0(l0: v137) let v149 = v76 let v153 = v4.l0 let v154 = v153 <> v149 let v4 = Mut0(l0: v154) let v166 = "expected" let v167 = v166 let v171 = v4.l0 let v172 = v171 <> v167 let v4 = Mut0(l0: v172) let v184 = v41 let v188 = v4.l0 let v189 = v188 <> v184 let v4 = Mut0(l0: v189) let v201 = string.inspect(v2) let v208 = v4.l0 let v209 = v208 <> v201 let v4 = Mut0(l0: v209) let v221 = " }" let v222 = v221 let v226 = v4.l0 let v227 = v226 <> v222 let v4 = Mut0(l0: v227) let v239 = v4.l0 v239 } 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, v3, 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, v2 : int32) : string = let v3 : string = method4() let v4 : Mut0 = {l0 = v3} : Mut0 let v7 : string = "{ " let v8 : string = $"{v7}" let v16 : unit = () let v17 : (unit -> unit) = closure0(v4, v8) let v18 : unit = (fun () -> v17 (); v16) () let v26 : string = "name" let v27 : string = $"{v26}" let v35 : unit = () let v36 : (unit -> unit) = closure0(v4, v27) let v37 : unit = (fun () -> v36 (); v35) () let v45 : string = " = " let v46 : string = $"{v45}" let v54 : unit = () let v55 : (unit -> unit) = closure0(v4, v46) let v56 : unit = (fun () -> v55 (); v54) () let v63 : string = $"{v0}" let v71 : unit = () let v72 : (unit -> unit) = closure0(v4, v63) let v73 : unit = (fun () -> v72 (); v71) () let v81 : string = "; " let v82 : string = $"{v81}" let v90 : unit = () let v91 : (unit -> unit) = closure0(v4, v82) let v92 : unit = (fun () -> v91 (); v90) () let v100 : string = "actual" let v101 : string = $"{v100}" let v109 : unit = () let v110 : (unit -> unit) = closure0(v4, v101) let v111 : unit = (fun () -> v110 (); v109) () let v118 : string = $"{v45}" let v126 : unit = () let v127 : (unit -> unit) = closure0(v4, v118) let v128 : unit = (fun () -> v127 (); v126) () let v138 : string = $"{v1}" let v146 : unit = () let v147 : (unit -> unit) = closure0(v4, v138) let v148 : unit = (fun () -> v147 (); v146) () let v155 : string = $"{v81}" let v163 : unit = () let v164 : (unit -> unit) = closure0(v4, v155) let v165 : unit = (fun () -> v164 (); v163) () let v173 : string = "expected" let v174 : string = $"{v173}" let v182 : unit = () let v183 : (unit -> unit) = closure0(v4, v174) let v184 : unit = (fun () -> v183 (); v182) () let v191 : string = $"{v45}" let v199 : unit = () let v200 : (unit -> unit) = closure0(v4, v191) let v201 : unit = (fun () -> v200 (); v199) () let v211 : string = $"{v2}" let v219 : unit = () let v220 : (unit -> unit) = closure0(v4, v211) let v221 : unit = (fun () -> v220 (); v219) () let v229 : string = " }" let v230 : string = $"{v229}" let v238 : unit = () let v239 : (unit -> unit) = closure0(v4, v230) let v240 : unit = (fun () -> v239 (); v238) () let v246 : string = v4.l0 v246 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, v3, 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, v2: i32) -> string { let v4: LrcPtr<Spiral::Mut0> = LrcPtr::new(Spiral::Mut0 { l0: MutCell::new(Spiral::method4()), }); let v18: () = { Spiral::closure0(v4.clone(), string("{ "), ()); () }; let v37: () = { Spiral::closure0(v4.clone(), string("name"), ()); () }; let v56: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v73: () = { Spiral::closure0(v4.clone(), v0, ()); () }; let v92: () = { Spiral::closure0(v4.clone(), string("; "), ()); () }; let v111: () = { Spiral::closure0(v4.clone(), string("actual"), ()); () }; let v128: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v148: () = { Spiral::closure0(v4.clone(), sprintf!("{}", v1), ()); () }; let v165: () = { Spiral::closure0(v4.clone(), string("; "), ()); () }; let v184: () = { Spiral::closure0(v4.clone(), string("expected"), ()); () }; let v201: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v221: () = { Spiral::closure0(v4.clone(), sprintf!("{}", v2), ()); () }; let v240: () = { Spiral::closure0(v4.clone(), string(" }"), ()); () }; v4.l0.get().clone() } pub fn closure1(v0: string, unitVar: ()) { printfn!("{0}", v0); } pub fn method0() { let v3: i32 = ((9_i32 + (Spiral::method1())) + 2_i32) + 1_i32; let v4: bool = (v3) == 15_i32; let v6: bool = if v4 { true } else { Spiral::method2(v4) }; let v9: string = Spiral::method3(string("__assert_eq"), v3, 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, v2: int32): string { const v4: Mut0 = new Mut0(method4()); let v18: any; closure0(v4, "{ ", undefined); v18 = undefined; let v37: any; closure0(v4, "name", undefined); v37 = undefined; let v56: any; closure0(v4, " = ", undefined); v56 = undefined; let v73: any; closure0(v4, v0, undefined); v73 = undefined; let v92: any; closure0(v4, "; ", undefined); v92 = undefined; let v111: any; closure0(v4, "actual", undefined); v111 = undefined; let v128: any; closure0(v4, " = ", undefined); v128 = undefined; let v148: any; closure0(v4, `${v1}`, undefined); v148 = undefined; let v165: any; closure0(v4, "; ", undefined); v165 = undefined; let v184: any; closure0(v4, "expected", undefined); v184 = undefined; let v201: any; closure0(v4, " = ", undefined); v201 = undefined; let v221: any; closure0(v4, `${v2}`, undefined); v221 = undefined; let v240: any; closure0(v4, " }", undefined); v240 = undefined; return v4.l0; } export function closure1(v0: string, unitVar: void): void { console.log(v0); } export function method0(): void { const v3: int32 = (((9 + method1()) + 2) + 1) | 0; const v4: boolean = v3 === 15; const v6: boolean = v4 ? true : method2(v4); const v9: string = method3("__assert_eq", v3, 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, v2: int) -> str: v4: Mut0 = Mut0(method4()) v18: None closure0(v4, "{ ", None) v18 = None v37: None closure0(v4, "name", None) v37 = None v56: None closure0(v4, " = ", None) v56 = None v73: None closure0(v4, v0, None) v73 = None v92: None closure0(v4, "; ", None) v92 = None v111: None closure0(v4, "actual", None) v111 = None v128: None closure0(v4, " = ", None) v128 = None v148: None closure0(v4, ("" + str(v1)) + "", None) v148 = None v165: None closure0(v4, "; ", None) v165 = None v184: None closure0(v4, "expected", None) v184 = None v201: None closure0(v4, " = ", None) v201 = None v221: None closure0(v4, ("" + str(v2)) + "", None) v221 = None v240: None closure0(v4, " }", None) v240 = None return v4.l0 def closure1(v0: str, unit_var: None) -> None: print(v0) def method0(__unit: None=None) -> None: v3: int = (((9 + method1()) + 2) + 1) or 0 v4: bool = v3 == 15 v6: bool = True if v4 else method2(v4) v9: str = method3("__assert_eq", v3, 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, v2 : i32) -> string: v3 = method4() v4 = Mut0(v3) del v3 v9 = "{ " v10 = f"{v9}" del v9 v20 = v4.v0 v23 = v20 + v10 del v10, v20 v4.v0 = v23 del v23 v29 = "name" v30 = f"{v29}" del v29 v40 = v4.v0 v43 = v40 + v30 del v30, v40 v4.v0 = v43 del v43 v49 = " = " v50 = f"{v49}" v60 = v4.v0 v63 = v60 + v50 del v50, v60 v4.v0 = v63 del v63 v67 = f"{v0}" del v0 v77 = v4.v0 v80 = v77 + v67 del v67, v77 v4.v0 = v80 del v80 v86 = "; " v87 = f"{v86}" v97 = v4.v0 v100 = v97 + v87 del v87, v97 v4.v0 = v100 del v100 v106 = "actual" v107 = f"{v106}" del v106 v117 = v4.v0 v120 = v117 + v107 del v107, v117 v4.v0 = v120 del v120 v124 = f"{v49}" v134 = v4.v0 v137 = v134 + v124 del v124, v134 v4.v0 = v137 del v137 v144 = f"{v1}" del v1 v154 = v4.v0 v157 = v154 + v144 del v144, v154 v4.v0 = v157 del v157 v161 = f"{v86}" del v86 v171 = v4.v0 v174 = v171 + v161 del v161, v171 v4.v0 = v174 del v174 v180 = "expected" v181 = f"{v180}" del v180 v191 = v4.v0 v194 = v191 + v181 del v181, v191 v4.v0 = v194 del v194 v198 = f"{v49}" del v49 v208 = v4.v0 v211 = v208 + v198 del v198, v208 v4.v0 = v211 del v211 v218 = f"{v2}" del v2 v228 = v4.v0 v231 = v228 + v218 del v218, v228 v4.v0 = v231 del v231 v237 = " }" v238 = f"{v237}" del v237 v248 = v4.v0 v251 = v248 + v238 del v238, v248 v4.v0 = v251 del v251 v253 = v4.v0 del v4 return v253 def method0() -> None: v0 = method1() v1 = 9 + v0 del v0 v2 = v1 + 2 del v1 v3 = v2 + 1 del v2 v4 = v3 == 15 if v4: v6 = True else: v6 = method2(v4) del v4 v7 = "__assert_eq" v8 = 15 v9 = method3(v7, v3, v8) del v3, 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; actual = 15; 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; actual = 15; expected = 15 } .rs output: { name = __assert_eq; actual = 15; expected = 15 } .ts output: { name = __assert_eq; actual = 15; expected = 15 } .py output: { name = __assert_eq; actual = 15; expected = 15 } .gleam output (Gleam): { name = __assert_eq; actual = 15; 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, v2 : Int) -> String { let v3 = method5() let v4 = Mut0(l0 : v3) let v5 = "{ " let v6 = v5 let v10 = v4.l0 let v11 = v10 <> v6 let v4 = Mut0(l0: v11) let v23 = "name" let v24 = v23 let v28 = v4.l0 let v29 = v28 <> v24 let v4 = Mut0(l0: v29) let v41 = " = " let v42 = v41 let v46 = v4.l0 let v47 = v46 <> v42 let v4 = Mut0(l0: v47) let v59 = v0 let v63 = v4.l0 let v64 = v63 <> v59 let v4 = Mut0(l0: v64) let v76 = "; " let v77 = v76 let v81 = v4.l0 let v82 = v81 <> v77 let v4 = Mut0(l0: v82) let v94 = "actual" let v95 = v94 let v99 = v4.l0 let v100 = v99 <> v95 let v4 = Mut0(l0: v100) let v112 = v41 let v116 = v4.l0 let v117 = v116 <> v112 let v4 = Mut0(l0: v117) let v129 = string.inspect(v1) let v136 = v4.l0 let v137 = v136 <> v129 let v4 = Mut0(l0: v137) let v149 = v76 let v153 = v4.l0 let v154 = v153 <> v149 let v4 = Mut0(l0: v154) let v166 = "expected" let v167 = v166 let v171 = v4.l0 let v172 = v171 <> v167 let v4 = Mut0(l0: v172) let v184 = v41 let v188 = v4.l0 let v189 = v188 <> v184 let v4 = Mut0(l0: v189) let v201 = string.inspect(v2) let v208 = v4.l0 let v209 = v208 <> v201 let v4 = Mut0(l0: v209) let v221 = " }" let v222 = v221 let v226 = v4.l0 let v227 = v226 <> v222 let v4 = Mut0(l0: v227) let v239 = v4.l0 v239 } 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, v4, 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, v2 : int32) : string = let v3 : string = method5() let v4 : Mut0 = {l0 = v3} : Mut0 let v7 : string = "{ " let v8 : string = $"{v7}" let v16 : unit = () let v17 : (unit -> unit) = closure0(v4, v8) let v18 : unit = (fun () -> v17 (); v16) () let v26 : string = "name" let v27 : string = $"{v26}" let v35 : unit = () let v36 : (unit -> unit) = closure0(v4, v27) let v37 : unit = (fun () -> v36 (); v35) () let v45 : string = " = " let v46 : string = $"{v45}" let v54 : unit = () let v55 : (unit -> unit) = closure0(v4, v46) let v56 : unit = (fun () -> v55 (); v54) () let v63 : string = $"{v0}" let v71 : unit = () let v72 : (unit -> unit) = closure0(v4, v63) let v73 : unit = (fun () -> v72 (); v71) () let v81 : string = "; " let v82 : string = $"{v81}" let v90 : unit = () let v91 : (unit -> unit) = closure0(v4, v82) let v92 : unit = (fun () -> v91 (); v90) () let v100 : string = "actual" let v101 : string = $"{v100}" let v109 : unit = () let v110 : (unit -> unit) = closure0(v4, v101) let v111 : unit = (fun () -> v110 (); v109) () let v118 : string = $"{v45}" let v126 : unit = () let v127 : (unit -> unit) = closure0(v4, v118) let v128 : unit = (fun () -> v127 (); v126) () let v138 : string = $"{v1}" let v146 : unit = () let v147 : (unit -> unit) = closure0(v4, v138) let v148 : unit = (fun () -> v147 (); v146) () let v155 : string = $"{v81}" let v163 : unit = () let v164 : (unit -> unit) = closure0(v4, v155) let v165 : unit = (fun () -> v164 (); v163) () let v173 : string = "expected" let v174 : string = $"{v173}" let v182 : unit = () let v183 : (unit -> unit) = closure0(v4, v174) let v184 : unit = (fun () -> v183 (); v182) () let v191 : string = $"{v45}" let v199 : unit = () let v200 : (unit -> unit) = closure0(v4, v191) let v201 : unit = (fun () -> v200 (); v199) () let v211 : string = $"{v2}" let v219 : unit = () let v220 : (unit -> unit) = closure0(v4, v211) let v221 : unit = (fun () -> v220 (); v219) () let v229 : string = " }" let v230 : string = $"{v229}" let v238 : unit = () let v239 : (unit -> unit) = closure0(v4, v230) let v240 : unit = (fun () -> v239 (); v238) () let v246 : string = v4.l0 v246 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, v4, 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, v2: i32) -> string { let v4: LrcPtr<Spiral::Mut0> = LrcPtr::new(Spiral::Mut0 { l0: MutCell::new(Spiral::method5()), }); let v18: () = { Spiral::closure0(v4.clone(), string("{ "), ()); () }; let v37: () = { Spiral::closure0(v4.clone(), string("name"), ()); () }; let v56: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v73: () = { Spiral::closure0(v4.clone(), v0, ()); () }; let v92: () = { Spiral::closure0(v4.clone(), string("; "), ()); () }; let v111: () = { Spiral::closure0(v4.clone(), string("actual"), ()); () }; let v128: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v148: () = { Spiral::closure0(v4.clone(), sprintf!("{}", v1), ()); () }; let v165: () = { Spiral::closure0(v4.clone(), string("; "), ()); () }; let v184: () = { Spiral::closure0(v4.clone(), string("expected"), ()); () }; let v201: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v221: () = { Spiral::closure0(v4.clone(), sprintf!("{}", v2), ()); () }; let v240: () = { Spiral::closure0(v4.clone(), string(" }"), ()); () }; v4.l0.get().clone() } pub fn closure1(v0: string, unitVar: ()) { printfn!("{0}", v0); } pub fn method0() { let v4: i32 = ((Spiral::method2(Spiral::method1(), 9_i32)) + 2_i32) + 1_i32; let v5: bool = (v4) == 15_i32; let v7: bool = if v5 { true } else { Spiral::method3(v5) }; let v10: string = Spiral::method4(string("__assert_eq"), v4, 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, v2: int32): string { const v4: Mut0 = new Mut0(method5()); let v18: any; closure0(v4, "{ ", undefined); v18 = undefined; let v37: any; closure0(v4, "name", undefined); v37 = undefined; let v56: any; closure0(v4, " = ", undefined); v56 = undefined; let v73: any; closure0(v4, v0, undefined); v73 = undefined; let v92: any; closure0(v4, "; ", undefined); v92 = undefined; let v111: any; closure0(v4, "actual", undefined); v111 = undefined; let v128: any; closure0(v4, " = ", undefined); v128 = undefined; let v148: any; closure0(v4, `${v1}`, undefined); v148 = undefined; let v165: any; closure0(v4, "; ", undefined); v165 = undefined; let v184: any; closure0(v4, "expected", undefined); v184 = undefined; let v201: any; closure0(v4, " = ", undefined); v201 = undefined; let v221: any; closure0(v4, `${v2}`, undefined); v221 = undefined; let v240: any; closure0(v4, " }", undefined); v240 = undefined; return v4.l0; } export function closure1(v0: string, unitVar: void): void { console.log(v0); } export function method0(): void { const v4: int32 = ((method2(method1(), 9) + 2) + 1) | 0; const v5: boolean = v4 === 15; const v7: boolean = v5 ? true : method3(v5); const v10: string = method4("__assert_eq", v4, 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, v2: int) -> str: v4: Mut0 = Mut0(method5()) v18: None closure0(v4, "{ ", None) v18 = None v37: None closure0(v4, "name", None) v37 = None v56: None closure0(v4, " = ", None) v56 = None v73: None closure0(v4, v0, None) v73 = None v92: None closure0(v4, "; ", None) v92 = None v111: None closure0(v4, "actual", None) v111 = None v128: None closure0(v4, " = ", None) v128 = None v148: None closure0(v4, ("" + str(v1)) + "", None) v148 = None v165: None closure0(v4, "; ", None) v165 = None v184: None closure0(v4, "expected", None) v184 = None v201: None closure0(v4, " = ", None) v201 = None v221: None closure0(v4, ("" + str(v2)) + "", None) v221 = None v240: None closure0(v4, " }", None) v240 = None return v4.l0 def closure1(v0: str, unit_var: None) -> None: print(v0) def method0(__unit: None=None) -> None: v4: int = ((method2(method1(), 9) + 2) + 1) or 0 v5: bool = v4 == 15 v7: bool = True if v5 else method3(v5) v10: str = method4("__assert_eq", v4, 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, v2 : i32) -> string: v3 = method5() v4 = Mut0(v3) del v3 v9 = "{ " v10 = f"{v9}" del v9 v20 = v4.v0 v23 = v20 + v10 del v10, v20 v4.v0 = v23 del v23 v29 = "name" v30 = f"{v29}" del v29 v40 = v4.v0 v43 = v40 + v30 del v30, v40 v4.v0 = v43 del v43 v49 = " = " v50 = f"{v49}" v60 = v4.v0 v63 = v60 + v50 del v50, v60 v4.v0 = v63 del v63 v67 = f"{v0}" del v0 v77 = v4.v0 v80 = v77 + v67 del v67, v77 v4.v0 = v80 del v80 v86 = "; " v87 = f"{v86}" v97 = v4.v0 v100 = v97 + v87 del v87, v97 v4.v0 = v100 del v100 v106 = "actual" v107 = f"{v106}" del v106 v117 = v4.v0 v120 = v117 + v107 del v107, v117 v4.v0 = v120 del v120 v124 = f"{v49}" v134 = v4.v0 v137 = v134 + v124 del v124, v134 v4.v0 = v137 del v137 v144 = f"{v1}" del v1 v154 = v4.v0 v157 = v154 + v144 del v144, v154 v4.v0 = v157 del v157 v161 = f"{v86}" del v86 v171 = v4.v0 v174 = v171 + v161 del v161, v171 v4.v0 = v174 del v174 v180 = "expected" v181 = f"{v180}" del v180 v191 = v4.v0 v194 = v191 + v181 del v181, v191 v4.v0 = v194 del v194 v198 = f"{v49}" del v49 v208 = v4.v0 v211 = v208 + v198 del v198, v208 v4.v0 = v211 del v211 v218 = f"{v2}" del v2 v228 = v4.v0 v231 = v228 + v218 del v218, v228 v4.v0 = v231 del v231 v237 = " }" v238 = f"{v237}" del v237 v248 = v4.v0 v251 = v248 + v238 del v238, v248 v4.v0 = v251 del v251 v253 = v4.v0 del v4 return v253 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 if v5: v7 = True else: v7 = method3(v5) del v5 v8 = "__assert_eq" v9 = 15 v10 = method4(v8, v4, v9) del v4, 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; actual = 15; 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; actual = 15; expected = 15 } .rs output: { name = __assert_eq; actual = 15; expected = 15 } .ts output: { name = __assert_eq; actual = 15; expected = 15 } .py output: { name = __assert_eq; actual = 15; expected = 15 } .gleam output (Gleam): { name = __assert_eq; actual = 15; 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, v2 : Int) -> String { let v3 = method5() let v4 = Mut0(l0 : v3) let v5 = "{ " let v6 = v5 let v10 = v4.l0 let v11 = v10 <> v6 let v4 = Mut0(l0: v11) let v23 = "name" let v24 = v23 let v28 = v4.l0 let v29 = v28 <> v24 let v4 = Mut0(l0: v29) let v41 = " = " let v42 = v41 let v46 = v4.l0 let v47 = v46 <> v42 let v4 = Mut0(l0: v47) let v59 = v0 let v63 = v4.l0 let v64 = v63 <> v59 let v4 = Mut0(l0: v64) let v76 = "; " let v77 = v76 let v81 = v4.l0 let v82 = v81 <> v77 let v4 = Mut0(l0: v82) let v94 = "actual" let v95 = v94 let v99 = v4.l0 let v100 = v99 <> v95 let v4 = Mut0(l0: v100) let v112 = v41 let v116 = v4.l0 let v117 = v116 <> v112 let v4 = Mut0(l0: v117) let v129 = string.inspect(v1) let v136 = v4.l0 let v137 = v136 <> v129 let v4 = Mut0(l0: v137) let v149 = v76 let v153 = v4.l0 let v154 = v153 <> v149 let v4 = Mut0(l0: v154) let v166 = "expected" let v167 = v166 let v171 = v4.l0 let v172 = v171 <> v167 let v4 = Mut0(l0: v172) let v184 = v41 let v188 = v4.l0 let v189 = v188 <> v184 let v4 = Mut0(l0: v189) let v201 = string.inspect(v2) let v208 = v4.l0 let v209 = v208 <> v201 let v4 = Mut0(l0: v209) let v221 = " }" let v222 = v221 let v226 = v4.l0 let v227 = v226 <> v222 let v4 = Mut0(l0: v227) let v239 = v4.l0 v239 } 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, v3, 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, v2 : int32) : string = let v3 : string = method5() let v4 : Mut0 = {l0 = v3} : Mut0 let v7 : string = "{ " let v8 : string = $"{v7}" let v16 : unit = () let v17 : (unit -> unit) = closure0(v4, v8) let v18 : unit = (fun () -> v17 (); v16) () let v26 : string = "name" let v27 : string = $"{v26}" let v35 : unit = () let v36 : (unit -> unit) = closure0(v4, v27) let v37 : unit = (fun () -> v36 (); v35) () let v45 : string = " = " let v46 : string = $"{v45}" let v54 : unit = () let v55 : (unit -> unit) = closure0(v4, v46) let v56 : unit = (fun () -> v55 (); v54) () let v63 : string = $"{v0}" let v71 : unit = () let v72 : (unit -> unit) = closure0(v4, v63) let v73 : unit = (fun () -> v72 (); v71) () let v81 : string = "; " let v82 : string = $"{v81}" let v90 : unit = () let v91 : (unit -> unit) = closure0(v4, v82) let v92 : unit = (fun () -> v91 (); v90) () let v100 : string = "actual" let v101 : string = $"{v100}" let v109 : unit = () let v110 : (unit -> unit) = closure0(v4, v101) let v111 : unit = (fun () -> v110 (); v109) () let v118 : string = $"{v45}" let v126 : unit = () let v127 : (unit -> unit) = closure0(v4, v118) let v128 : unit = (fun () -> v127 (); v126) () let v138 : string = $"{v1}" let v146 : unit = () let v147 : (unit -> unit) = closure0(v4, v138) let v148 : unit = (fun () -> v147 (); v146) () let v155 : string = $"{v81}" let v163 : unit = () let v164 : (unit -> unit) = closure0(v4, v155) let v165 : unit = (fun () -> v164 (); v163) () let v173 : string = "expected" let v174 : string = $"{v173}" let v182 : unit = () let v183 : (unit -> unit) = closure0(v4, v174) let v184 : unit = (fun () -> v183 (); v182) () let v191 : string = $"{v45}" let v199 : unit = () let v200 : (unit -> unit) = closure0(v4, v191) let v201 : unit = (fun () -> v200 (); v199) () let v211 : string = $"{v2}" let v219 : unit = () let v220 : (unit -> unit) = closure0(v4, v211) let v221 : unit = (fun () -> v220 (); v219) () let v229 : string = " }" let v230 : string = $"{v229}" let v238 : unit = () let v239 : (unit -> unit) = closure0(v4, v230) let v240 : unit = (fun () -> v239 (); v238) () let v246 : string = v4.l0 v246 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, v3, 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, v2: i32) -> string { let v4: LrcPtr<Spiral::Mut0> = LrcPtr::new(Spiral::Mut0 { l0: MutCell::new(Spiral::method5()), }); let v18: () = { Spiral::closure0(v4.clone(), string("{ "), ()); () }; let v37: () = { Spiral::closure0(v4.clone(), string("name"), ()); () }; let v56: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v73: () = { Spiral::closure0(v4.clone(), v0, ()); () }; let v92: () = { Spiral::closure0(v4.clone(), string("; "), ()); () }; let v111: () = { Spiral::closure0(v4.clone(), string("actual"), ()); () }; let v128: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v148: () = { Spiral::closure0(v4.clone(), sprintf!("{}", v1), ()); () }; let v165: () = { Spiral::closure0(v4.clone(), string("; "), ()); () }; let v184: () = { Spiral::closure0(v4.clone(), string("expected"), ()); () }; let v201: () = { Spiral::closure0(v4.clone(), string(" = "), ()); () }; let v221: () = { Spiral::closure0(v4.clone(), sprintf!("{}", v2), ()); () }; let v240: () = { Spiral::closure0(v4.clone(), string(" }"), ()); () }; v4.l0.get().clone() } pub fn closure1(v0: string, unitVar: ()) { printfn!("{0}", v0); } pub fn method0() { let v3: i32 = ((Spiral::method2(Spiral::method1())) + 2_i32) + 1_i32; let v4: bool = (v3) == 15_i32; let v6: bool = if v4 { true } else { Spiral::method3(v4) }; let v9: string = Spiral::method4(string("__assert_eq"), v3, 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, v2: int32): string { const v4: Mut0 = new Mut0(method5()); let v18: any; closure0(v4, "{ ", undefined); v18 = undefined; let v37: any; closure0(v4, "name", undefined); v37 = undefined; let v56: any; closure0(v4, " = ", undefined); v56 = undefined; let v73: any; closure0(v4, v0, undefined); v73 = undefined; let v92: any; closure0(v4, "; ", undefined); v92 = undefined; let v111: any; closure0(v4, "actual", undefined); v111 = undefined; let v128: any; closure0(v4, " = ", undefined); v128 = undefined; let v148: any; closure0(v4, `${v1}`, undefined); v148 = undefined; let v165: any; closure0(v4, "; ", undefined); v165 = undefined; let v184: any; closure0(v4, "expected", undefined); v184 = undefined; let v201: any; closure0(v4, " = ", undefined); v201 = undefined; let v221: any; closure0(v4, `${v2}`, undefined); v221 = undefined; let v240: any; closure0(v4, " }", undefined); v240 = undefined; return v4.l0; } export function closure1(v0: string, unitVar: void): void { console.log(v0); } export function method0(): void { const v3: int32 = ((method2(method1()) + 2) + 1) | 0; const v4: boolean = v3 === 15; const v6: boolean = v4 ? true : method3(v4); const v9: string = method4("__assert_eq", v3, 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, v2: int) -> str: v4: Mut0 = Mut0(method5()) v18: None closure0(v4, "{ ", None) v18 = None v37: None closure0(v4, "name", None) v37 = None v56: None closure0(v4, " = ", None) v56 = None v73: None closure0(v4, v0, None) v73 = None v92: None closure0(v4, "; ", None) v92 = None v111: None closure0(v4, "actual", None) v111 = None v128: None closure0(v4, " = ", None) v128 = None v148: None closure0(v4, ("" + str(v1)) + "", None) v148 = None v165: None closure0(v4, "; ", None) v165 = None v184: None closure0(v4, "expected", None) v184 = None v201: None closure0(v4, " = ", None) v201 = None v221: None closure0(v4, ("" + str(v2)) + "", None) v221 = None v240: None closure0(v4, " }", None) v240 = None return v4.l0 def closure1(v0: str, unit_var: None) -> None: print(v0) def method0(__unit: None=None) -> None: v3: int = ((method2(method1()) + 2) + 1) or 0 v4: bool = v3 == 15 v6: bool = True if v4 else method3(v4) v9: str = method4("__assert_eq", v3, 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, v2 : i32) -> string: v3 = method5() v4 = Mut0(v3) del v3 v9 = "{ " v10 = f"{v9}" del v9 v20 = v4.v0 v23 = v20 + v10 del v10, v20 v4.v0 = v23 del v23 v29 = "name" v30 = f"{v29}" del v29 v40 = v4.v0 v43 = v40 + v30 del v30, v40 v4.v0 = v43 del v43 v49 = " = " v50 = f"{v49}" v60 = v4.v0 v63 = v60 + v50 del v50, v60 v4.v0 = v63 del v63 v67 = f"{v0}" del v0 v77 = v4.v0 v80 = v77 + v67 del v67, v77 v4.v0 = v80 del v80 v86 = "; " v87 = f"{v86}" v97 = v4.v0 v100 = v97 + v87 del v87, v97 v4.v0 = v100 del v100 v106 = "actual" v107 = f"{v106}" del v106 v117 = v4.v0 v120 = v117 + v107 del v107, v117 v4.v0 = v120 del v120 v124 = f"{v49}" v134 = v4.v0 v137 = v134 + v124 del v124, v134 v4.v0 = v137 del v137 v144 = f"{v1}" del v1 v154 = v4.v0 v157 = v154 + v144 del v144, v154 v4.v0 = v157 del v157 v161 = f"{v86}" del v86 v171 = v4.v0 v174 = v171 + v161 del v161, v171 v4.v0 = v174 del v174 v180 = "expected" v181 = f"{v180}" del v180 v191 = v4.v0 v194 = v191 + v181 del v181, v191 v4.v0 = v194 del v194 v198 = f"{v49}" del v49 v208 = v4.v0 v211 = v208 + v198 del v198, v208 v4.v0 = v211 del v211 v218 = f"{v2}" del v2 v228 = v4.v0 v231 = v228 + v218 del v218, v228 v4.v0 = v231 del v231 v237 = " }" v238 = f"{v237}" del v237 v248 = v4.v0 v251 = v248 + v238 del v238, v248 v4.v0 = v251 del v251 v253 = v4.v0 del v4 return v253 def method0() -> None: v0 = method1() v1 = method2(v0) del v0 v2 = v1 + 2 del v1 v3 = v2 + 1 del v2 v4 = v3 == 15 if v4: v6 = True else: v6 = method3(v4) del v4 v7 = "__assert_eq" v8 = 15 v9 = method4(v7, v3, v8) del v3, 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; actual = 15; 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; actual = -3; expected = -3 } .rs output: { name = __assert_eq; actual = -3; expected = -3 } .ts output: { name = __assert_eq; actual = -3; expected = -3 } .py output: { name = __assert_eq; actual = -3; expected = -3 } .gleam output (Gleam): { name = __assert_eq; actual = -3; expected = -3 }
.fsx output: { name = __assert_eq; actual = -3; 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; actual = -3; expected = -3 } .rs output: { name = __assert_eq; actual = -3; expected = -3 } .ts output: { name = __assert_eq; actual = -3; expected = -3 } .py output: { name = __assert_eq; actual = -3; expected = -3 } .gleam output (Gleam): { name = __assert_eq; actual = -3; expected = -3 }
.fsx output: { name = __assert_eq; actual = -3; 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; actual = -3; expected = -3 } .rs output: { name = __assert_eq; actual = -3; expected = -3 } .ts output: { name = __assert_eq; actual = -3; expected = -3 } .py output: { name = __assert_eq; actual = -3; expected = -3 } .gleam output (Gleam): { name = __assert_eq; actual = -3; expected = -3 }
.fsx output: { name = __assert_eq; actual = -3; 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; actual = -3.0; expected = -3 } .rs output: { name = __assert_eq; actual = -3; expected = -3 } .ts output: { name = __assert_eq; actual = -3; expected = -3 } .py output: { name = __assert_eq; actual = -3; expected = -3 } .gleam output (Gleam): { name = __assert_eq; actual = -3; expected = -3 }
.fsx output: { name = __assert_eq; actual = -3; 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; actual = true; expected = true } .rs output: { name = __assert_eq; actual = true; expected = true } .ts output: { name = __assert_eq; actual = true; expected = true } .py output: { name = __assert_eq; actual = true; expected = true } .gleam output (Gleam): { name = __assert_eq; actual = true; expected = true }
.fsx output: { name = __assert_eq; actual = true; 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; actual = true; expected = true } .rs output: { name = __assert_eq; actual = true; expected = true } .ts output: { name = __assert_eq; actual = true; expected = true } .py output: { name = __assert_eq; actual = true; expected = true } .gleam output (Gleam): { name = __assert_eq; actual = true; expected = true }
.fsx output: { name = __assert_eq; actual = true; 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; actual = true; expected = true } .rs output: { name = __assert_eq; actual = true; expected = true } .ts output: { name = __assert_eq; actual = true; expected = true } .py output: { name = __assert_eq; actual = true; expected = true } .gleam output (Gleam): { name = __assert_eq; actual = true; expected = true }
.fsx output: { name = __assert_eq; actual = true; 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; actual = 1, 2; expected = 1, 2 } .rs output: { name = __assert_eq; actual = 1, 2; expected = 1, 2 } .ts output: { name = __assert_eq; actual = 1, 2; expected = 1, 2 } .py output: { name = __assert_eq; actual = 1, 2; expected = 1, 2 } .gleam output (Gleam): { name = __assert_eq; actual = 1, 2; expected = 1, 2 }
.fsx output: { name = __assert_eq; actual = 1, 2; 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'; actual = a, (1, 'b'); expected = a, (1, 'b') } .rs output: { name = __assert_eq'; actual = a, ( 1, "b", ); expected = a, ( 1, "b", ) } .ts output: { name = __assert_eq'; actual = a, 1,b; expected = a, 1,b } .py output: { name = __assert_eq'; actual = a, (1, 'b'); expected = a, (1, 'b') } .gleam output (Gleam): { name = __assert_eq'; actual = a, #(1, "b"); expected = a, #(1, "b") }
.fsx output: { name = __assert_eq'; actual = a, (1, "b"); 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; actual = 1; expected = 1 } .rs output: { name = __assert_eq; actual = 1; expected = 1 } .ts output: { name = __assert_eq; actual = 1; expected = 1 } .py output: { name = __assert_eq; actual = 1; expected = 1 } .gleam output (Gleam): { name = __assert_eq; actual = 1; expected = 1 }
.fsx output: { name = __assert_eq; actual = 1; expected = 1 }
In [ ]:
//// test
///! gleam
///! fsharp
///! cuda
///! rust
///! typescript
///! python
(1i32, 2i32)
||> flip pair
|> _assert_eq (2, 1)
.py output (Python): { name = __assert_eq; actual = 2, 1; expected = 2, 1 } .rs output: { name = __assert_eq; actual = 2, 1; expected = 2, 1 } .ts output: { name = __assert_eq; actual = 2, 1; expected = 2, 1 } .py output: { name = __assert_eq; actual = 2, 1; expected = 2, 1 } .gleam output (Gleam): { name = __assert_eq; actual = 2, 1; expected = 2, 1 }
.fsx output: { name = __assert_eq; actual = 2, 1; 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; actual = ; expected = } { name = __assert_eq; actual = 0; expected = 0 } { name = __assert_eq; actual = Us0i1; expected = Us0i1 }
.fsx output: { name = __assert_eq; actual = ; expected = } { name = __assert_eq; actual = 0; expected = 0 } { name = __assert_eq; actual = US0_1; 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; actual = index 1 is out of bounds for axis 0 with size 1; expected = index 1 is out of bounds for axis 0 with size 1 } .rs output: { name = __assert_eq; actual = Exception { message: "index out of bounds: the len is 1 but the index is 1" }; expected = Exception { message: "index out of bounds: the len is 1 but the index is 1" } } .ts output: { name = __assert_eq; actual = Error: Index was outside the bounds of the array.\nParameter name: index; expected = Error: Index was outside the bounds of the array.\nParameter name: index } .py output: { name = __assert_eq; actual = array index out of range; expected = array index out of range }
.fsx output: { name = __assert_eq; actual = System.IndexOutOfRangeException: Index was outside the bounds of the array.; 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; actual = 0; expected = 0 } .rs output: { name = __assert_eq; actual = 0; expected = 0 } .ts output: { name = __assert_eq; actual = 0; expected = 0 } .py output: { name = __assert_eq; actual = 0; expected = 0 } .gleam output (Gleam): { name = __assert_eq; actual = 0; expected = 0 }
.fsx output: { name = __assert_eq; actual = 0; 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 () => ()
}