import Base # Entropy: random bytes from the kernel (entropy's job in the Haskell # backend): n bytes read from /dev/urandom, which never blocks once the # kernel's pool is seeded (getrandom(2)'s source). Only IO: no laws, only # what the kernel promises. # # Every def is prefixed `en.` (a program's defs share one namespace). def en.got(r: File & Result<&1, &1, U32 & String, List<&2, U32>>) -> IO(Maybe<&2, List<&2, U32>>): match r: case (f, Fail{e}): do IO>>: File.close(f) return None{} case (f, Done{bs}): do IO>>: File.close(f) return Some{bs} def en.opened(+n: U32, r: Result<&1, &1, U32 & String, File>) -> IO(Maybe<&2, List<&2, U32>>): match r: case Fail{e}: IO.pure(Maybe<&2, List<&2, U32>>, None{}) case Done{f}: do IO>>: r2 : File & Result<&1, &1, U32 & String, List<&2, U32>> <- File.read_bytes(f, n) en.got(r2) # n random bytes (None: the kernel would not give them) def en.bytes(+n: U32) -> IO(Maybe<&2, List<&2, U32>>): do IO>>: r : Result<&1, &1, U32 & String, File> <- File.open("/dev/urandom", "r") en.opened(n, r)