Skip to content

Instantly share code, notes, and snippets.

@Octachron
Created April 21, 2021 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Octachron/77b6cf086d8e636100a08d517f5ba3a4 to your computer and use it in GitHub Desktop.
Save Octachron/77b6cf086d8e636100a08d517f5ba3a4 to your computer and use it in GitHub Desktop.
type void = |
module Make(T:sig type 'a t end) = struct
type 'a t =
| []: void t
| (::): 'a T.t * 'b t -> ('a -> 'b) t
end
module HL = Make(struct type 'a t = 'a end)
type 'a ty =
| Int: int ty
| Float: float ty
module HLT =Make(struct type 'a t = 'a ty end)
type dyn = Typed: 'a HL.t * 'a HLT.t -> dyn
let rec gen n =
if n = 0 then Typed (HL.[], HLT.[]) else
let Typed(v,t) = gen (n - 1) in
if Random.float 1. < 0.5 then
Typed(HL.(Random.float 1.::v),
HLT.(Float::t)
)
else
Typed(HL.(Random.int 10::v),
HLT.(Int::t)
)
let rec add : type a. a HLT.t -> a HL.t -> float = fun ts xs ->
match ts, xs with
| [], [] -> 0.
| Int :: ts, n :: xs -> float n +. add ts xs
| Float :: ts, x :: xs -> x +. add ts xs
let test =
let Typed(xs,ts) = gen 100 in
add ts xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment