Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
Last active September 20, 2024 04:46
Show Gist options
  • Save ClarkeRemy/fcda36f82310adde6a530d5c861f811c to your computer and use it in GitHub Desktop.
Save ClarkeRemy/fcda36f82310adde6a530d5c861f811c to your computer and use it in GitHub Desktop.
Dictionary Passing style
structure Plus = struct
datatype 'a t = T of 'a -> 'a -> 'a
end
structure Mul = struct
datatype 'a t = T of 'a -> 'a -> 'a
end
structure FromInt = struct
datatype 'a t = T of int -> 'a
end
fun uses_dict (Plus.T plus, Mul.T mul, FromInt.T from_num) =
let val z = from_num 15
fun compiled x y = mul (plus x y) z
in compiled end
fun uses_dict2 (Plus.T plus, FromInt.T fromInt) =
let val z = fromInt 15
fun compiled x = plus x z
in compiled end
fun uses_dict3 (plus, mul, fromNum as FromInt.T frN) =
let
val f = uses_dict (plus, mul, fromNum)
val g = uses_dict2 (plus, fromNum)
val z = frN 5
fun compiled x = f (g x) z
in
compiled
end
local fun curry f x y = f(x,y) in
structure Int_ =
struct
val plus = Plus.T (curry Int.+)
val mul = Mul.T (curry Int.*)
val fromInt = FromInt.T (fn x => x)
end
structure Real_ =
struct
val plus = Plus.T (curry Real.+)
val mul = Mul.T (curry Real.*)
val fromInt = FromInt.T Real.fromInt
end
end
local open Int_ in
val _ = uses_dict (plus, mul, fromInt) 5 6
val _ = uses_dict2 (plus, fromInt) 5
end
local open Real_ in
val _ = uses_dict (plus, mul, fromInt) 5.0 6.0
val _ = uses_dict2 (plus, fromInt) 5.0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment