
gittech. site
for different kinds of informations and explorations.
Porting PFP from Haskell to OCaml
Published at
Jan 18, 2025
Main Article
PFPCaml
A port of the library described in this 2006 paper from Haskell to OCaml. This was ill-advised, because I didn't know Haskell or OCaml. I thought this would be a good opportunity to get comfortable with both. Also, the actual library has got some pretty interesting concepts.
I've translated the core of the module, Probability.hs (from the original 2006 version) to OCaml.
A lot of the time, this felt like trying to fit a round peg into a square hole:
Type Definitions:
- Haskell uses
newtype
forProbability
, while OCaml usestype
. - Haskell defines
Dist
as anewtype
, whereas OCaml uses a regular type definition.
- Haskell uses
Monad and Functor Instances:
- Haskell defines
Monad
andMonadPlus
instances forDist
. - OCaml uses functions like
bind
andmplus
to achieve similar functionality.
- Haskell defines
Random Values:
- Haskell uses the
IO
monad for random values (R
type). - OCaml uses a function type
unit -> 'a
for random values (random
type).
- Haskell uses the
Pretty Printing:
- Haskell uses the
Show
typeclass for pretty printing. - OCaml uses functions like
show_dist
for pretty printing.
- Haskell uses the
Expected Value Calculation:
- Haskell uses the
Expected
typeclass. - OCaml uses a module type
Expected
and functors to achieve similar functionality.
- Haskell uses the
Iteration and Simulation:
- Haskell uses the
Iterate
andSim
typeclasses. - OCaml uses modules and functors to define similar behavior.
- Haskell uses the
Conditional Probability:
- Haskell uses the
|||
operator for filtering distributions. - OCaml uses the
|||
operator for similar functionality but implemented differently.
- Haskell uses the
Randomized Generators:
- Haskell defines
RChange
andRTrans
types for random changes and transitions. - OCaml defines similar types but uses different naming conventions and implementations.
- Haskell defines