Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
zehnpaard / effectful_split_mutual_recursion.ml
Last active January 8, 2023 03:56
Implementation of mutually recursive is_odd and is_even, splitting the definition across modules using OCaml 5.0 effect handlers
module Base = struct
type _ Effect.t +=
| IsOdd: int -> bool Effect.t
| IsEven: int -> bool Effect.t
end
module Odd = struct
let is_odd n =
if n=0 then false
else Effect.perform (Base.IsEven (n-1))
@zehnpaard
zehnpaard / composable_interpreter_and_print_with_ocaml_effect_handlers.ml
Last active January 5, 2023 03:21
Extensible and composable interpreter using OCaml 5.0's effect handlers + print function demonstrating solution to Expression Problem, based on https://gist.github.com/takahisa/e5d3b012a11081302489d29bf417575c
module D = Effect.Deep
type 'a expr = ..
type _ Effect.t +=
| Extension : 'a expr -> 'a Effect.t
| Evaluate : 'a expr -> 'a Effect.t
let eval_effect e = Effect.perform (Evaluate e)
(* Extension 1 *)
@zehnpaard
zehnpaard / composable_interpreter_with_ocaml_effect_handlers.ml
Last active January 4, 2023 01:12
Extensible and composable interpreter using OCaml 5.0's effect handlers, based on https://gist.github.com/takahisa/e5d3b012a11081302489d29bf417575c
module D = Effect.Deep
type 'a expr = ..
type _ Effect.t +=
| Extension : 'a expr -> 'a Effect.t
| Evaluate : 'a expr -> 'a Effect.t
let eval_effect e = Effect.perform (Evaluate e)
(* Extension 1 *)
@zehnpaard
zehnpaard / extensible_interpreter_with_ocaml_effect_handlers2.ml
Created January 3, 2023 01:46
Extensible interpreter using OCaml 5.0's effect handlers with some isolation between handlers and eval functions, based on https://gist.github.com/takahisa/e5d3b012a11081302489d29bf417575c
module D = Effect.Deep
type 'a expr = ..
type _ Effect.t += Extension : 'a expr -> 'a Effect.t
(* Base interpreter *)
let eval_base e = Effect.perform (Extension e)
(* Extension 1 *)
type 'a expr +=
module D = Effect.Deep
type 'a expr = ..
type _ Effect.t += Extension : 'a expr -> 'a Effect.t
(* Base Interpreter *)
type 'a expr +=
| Int : int -> int expr
| Add : int expr * int expr -> int expr
| Sub : int expr * int expr -> int expr
@zehnpaard
zehnpaard / hm9_no_match_skip_occurs_bad.ml
Last active November 20, 2022 13:45
Hindley Milner Type Inference with Unit, Bool, Int, Tuple, Record, Variant, Fix, Ref, List without match_..._ty, with unify_skip (buggy)
type ty =
| TVar of tvar ref
| TArrow of ty * ty
| TUnit
| TBool
| TInt
| TTuple of ty list
| TRecord of (string * ty) list
| TVariant of (string * ty) list
| TRef of ty
@zehnpaard
zehnpaard / hm9_no_match.ml
Created November 17, 2022 05:28
Hindley Milner Type Inference with Unit, Bool, Int, Tuple, Record, Variant, Fix, Ref, List without match_..._ty
type ty =
| TVar of tvar ref
| TArrow of ty * ty
| TUnit
| TBool
| TInt
| TTuple of ty list
| TRecord of (string * ty) list
| TVariant of (string * ty) list
| TRef of ty
@zehnpaard
zehnpaard / hm9.ml
Last active November 15, 2022 02:05
Hindley Milner Type Inference with Unit, Bool, Int, Tuple, Record, Variant, Fix, Ref, List
type ty =
| TVar of tvar ref
| TArrow of ty * ty
| TUnit
| TBool
| TInt
| TTuple of ty list
| TRecord of (string * ty) list
| TVariant of (string * ty) list
| TRef of ty
@zehnpaard
zehnpaard / hm8.ml
Last active November 13, 2022 03:07
Hindley Milner Type Inference with Unit, Bool, Int, Tuple, Record, Variant, Fix, Ref
type ty =
| TVar of tvar ref
| TArrow of ty * ty
| TUnit
| TBool
| TInt
| TTuple of ty list
| TRecord of (string * ty) list
| TVariant of (string * ty) list
| TRef of ty
@zehnpaard
zehnpaard / hm8bad.ml
Last active November 12, 2022 22:30
Hindley Milner Type Inference with Unit, Bool, Int, Tuple, Record, Variant, Fix, Ref without Value Restriction
type ty =
| TVar of tvar ref
| TArrow of ty * ty
| TUnit
| TBool
| TInt
| TTuple of ty list
| TRecord of (string * ty) list
| TVariant of (string * ty) list
| TRef of ty