[Haskell-cafe] Monads with "The" contexts?
muranushi at gmail.com
Wed Jul 18 16:53:37 CEST 2012
Done with some exercises on Gaussian distribution as a monad!
What do you think? Will this be a good approach or bad?
Also this is the first page in my attempt to create runnable, and even
testable wiki pages. To run the tests, please use
2012/7/18 Takayuki Muranushi <muranushi at gmail.com>:
> Thank you Oleg, for your detailed instructions!
> First, let me clarify my problem here (in sacrifice of physical accuracy.)
> c.f. Wrong.hs .
>> earthMass, sunMass, marsMass :: [Double]
>> earthMass = [1,10,100]
>> sunMass = (*) <$> [9,10,11] <*> earthMass
>> marsMass = (*) <$> [0.09,0.1,0.11] <*> earthMass
>> sunPerMars = (/) <$> sunMass <*> marsMass
>> sunPerMars_range = (minimum sunPerMars, maximum sunPerMars)
> gives> (0.8181818181818182,12222.222222222223)
> These extreme answers close to 1 or 10000 are inconsistent in sense
> that they used different Earth mass value for calculating Sun and Mars
> mass. Factoring out Earth mass is perfect and efficient solution in
> this case, but is not always viable when more complicated functions
> are involved.
> We want to remove such inconsistency.
>> -- Exercise: why do we need the seemingly redundant EarthMass
>> -- and deriving Typeable?
>> -- Could we use TemplateHaskell instead?
> Aha! you use the Types as unique keys that resides in "The" context.
> Smart! To understand this, I have made MassStr.hs, which
> essentially does the same thing with more familiar type Strings. Of
> course using Strings are naive and collision-prone approach. Printing
> `stateAfter` shows pretty much what have happened.
> I'll remember that we can use Types as global identifiers.
>> -- The following is essentially Control.Monad.Sharing.Memoization
>> -- with one important addition
>> -- Can you spot the important addition?
>> type NonDet a = StateT FirstClassStore  a
>> data Key = KeyDyn Int | KeySta TypeRep
>> deriving (Show, Ord, Eq)
> Hmm, I don't see what Control.Monad.Sharing.Memoization is; googling
> gives our conversation at the top.
> If it's Memo in chapter 4.2 of your JFP paper, the difference I see is
> that you used Data.Set here instead of list of pairs for better
>> Exercise: how does the approach in the code relate to the approaches
>> to sharing explained in
> Chapter 3 introduces an implicit impure counter, and Chapter 4 uses a
> database that is passed around.
> let_ in Chapter 5 of sharing.pdf realizes the sharing with sort of
> continuation-passing style.The unsafe counter works across the module
> (c.f. counter.zip) but is generally unpredictable...
> Now I'm on to the next task; how we represent continuous probability
> distributions? The existing libraries:
> Seemingly have restricted themselves to discrete distributions, or at
> least providing Random support for Monte-Carlo simulations. There's
> some hope; I guess Gaussian distributions form a Monad provided that
> 1. the standard deviations you are dealing with are small compared to
> the scale you deal with, and 2. the monadic functions are
> Maybe I can use non-standard analysis and automatic differentiation;
> maybe I can resort to numerical differentiation; maybe I just give up
> and be satisfied with random sampling. I have to try first; then
> finally we can abstract upon different approaches.
> Also, I can start writing my Knowledge libraries from the part our
> knowledge is so accurate enough that the deviations are negligible
> (such as Earth mass!)
> P.S. extra spaces may have annoyed you. I'm sorry for that. My
> keyboard is chattering badly now; I have to update him soon.
> Best wishes,
More information about the Haskell-Cafe