This short example shows how floating-point numbers behave in C and why simple expressions like 0.1 + 0.2
don't always behave as you'd expect.
#include
type 'a state = Unfilled of task list | Filled of 'a | |
type 'a t = 'a state ref | |
let create () = ref (Unfilled []) | |
let rec try_fill promise value = | |
let old_state = !promise in | |
match old_state with | |
| Filled _ -> false | |
| Unfilled tasklist -> |
d.file "" | |
// Assembly output for sum.ml | |
.data | |
.globl _camlSum__data_begin | |
_camlSum__data_begin: | |
.text | |
.globl _camlSum__code_begin | |
_camlSum__code_begin: | |
.data | |
.align 3 |
(* Reverse-mode Algorithmic differentiation using effect handlers. | |
Adapted from https://twitter.com/tiarkrompf/status/963314799521222656. | |
See https://openreview.net/forum?id=SJxJtYkPG for more information. *) | |
module F : sig | |
type t | |
val mk : float code -> t | |
val (+.) : t -> t -> t | |
val ( *. ) : t -> t -> t | |
val grad : (t -> t) -> float code -> float code |
(* Reverse-mode Algorithmic differentiation using effect handlers. | |
Adapted from https://twitter.com/tiarkrompf/status/963314799521222656. | |
See https://openreview.net/forum?id=SJxJtYkPG for more information. *) | |
module F : sig | |
type t | |
val mk : float code -> t | |
val (+.) : t -> t -> t | |
val ( *. ) : t -> t -> t | |
val grad : (t -> t) -> float code -> float code |
(* deep_state.ml *) | |
open Effect | |
open Effect.Shallow | |
module type State = sig | |
type a | |
type _ Effect.t += Get : a Effect.t | |
type _ Effect.t += Set : a -> unit Effect.t | |
end |
(* The Computer Language Benchmarks Game | |
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ | |
* | |
* Contributed by Troestler Christophe | |
* Modified by Fabrice Le Fessant | |
* *reset* | |
*) | |
type 'a tree = Empty | Node of 'a tree * 'a * 'a tree |
type tree = Leaf of int | Node of tree * tree | |
let gensym = ref 0 | |
let rec build_tree n = | |
if n <= 0 | |
then (incr gensym; Leaf !gensym) | |
else Node(build_tree (n-1), build_tree (n-1)) | |
let test n repet = |
open Printf | |
open EffectHandlers | |
open EffectHandlers.Deep | |
module type STATE = sig | |
type t | |
val put : t -> unit | |
val get : unit -> t | |
val run : (unit -> 'a) -> init:t -> t * 'a | |
end |
effect E : unit | |
let foo () = | |
let r = Atomic.make 0 in | |
perform E; | |
Atomic.set r (Atomic.get r + 1); | |
Atomic.get r | |
let bar () = | |
let r = ref 0 in |