Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4576840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:15:45+00:00 2026-05-21T20:15:45+00:00

I was trying to implement the state monad in OCaml (as an exercise). My

  • 0

I was trying to implement the state monad in OCaml (as an exercise). My implementation looks like this:

module type MONAD_BUILDER =
sig
  type 'a t
  val return : 'a -> 'a t
  val bind : 'a t -> ('a -> 'b t) -> 'b t
end;;

module MonadBuilder = functor (M: MONAD_BUILDER) ->
struct
  let ( >>= ) = M.bind
  let return = M.return
end;;

module StateM = 
struct
  type 'a state = { state: 's . 's -> ('a * 's) }
  type 'a t = 'a state
  let return x = { state = fun s -> (x, s) }
  let bind m f = 
    let aux s = 
      let (x, s') = m.state s in
      (f x).state s'
    in { state = aux }
  let run m x = fst (m.state x)
end;;

I chose the existential type for the record field since I don’t like the idea of using a functor and wrapping the state type in a module. The above implementation works, but I
ran into a problem while implementing getState and setState. I tried to implement them like:

let getState = { state = fun s -> (s, s) }

let setState s = { state = fun _ -> ((), s) }

This doesn’t work since the inferred field types, e.g. 'a -> ('a * 'a) and 'a -> (unit * 'a), are less generic than the declared type 's . 's -> ('a * 's). I understand why this is happening, but I was wondering if there is another way of making it work using the record approach?

Thanks.

Cheers,
Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:15:46+00:00Added an answer on May 21, 2026 at 8:15 pm

    's. 's -> ('a * 's) is an universal type. You’re going to have a hard time implementing a state with universal types…

    There’s no clean way of encapsulating an existential type there without using a module (because existential types is what abstract types are for).

    You could, of course, publish the state type instead:

    type ('a,'s) state = { state : 's -> ('a * 's) } 
    

    Or even shorter,

    type ('a,'s) state = 's -> 'a * 's
    

    With the additional parameter, your code runs. However, you run into the fact that your parameter must be handled by the monad. So, you can either hide it when building the monad:

    module Monad = MonadBuilder(struct
      include StateM
      type 'a t = ('a,myStateType) state
    end)
    

    Or alter your monad design to incorporate an additional type parameter that is to be used for existential types:

    module type MONAD_BUILDER =
    sig
      type ('a,'param) t
      val return : 'a -> ('a,'param) t
      val bind : ('a,'param) t -> ('a -> ('b,'param) t) -> ('b,'param) t
    end;;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all
Am trying to implement a generic way for reading sections from a config file.
I am trying to implement string unescaping with Python regex and backreferences, and it
I'm trying to implement a data compression idea I've had, and since I'm imagining
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
We are trying to implement a REST API for an application we have now.
I am trying to implement a software Null Modem . Any suggestion how to
I am trying to implement AJAX in my Google App Engine application, and so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.