https://wiki.haskell.org/api.php?action=feedcontributions&user=Arcatan&feedformat=atomHaskellWiki - User contributions [en]2021-06-15T23:52:51ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Monad/ST&diff=45783Monad/ST2012-05-25T19:51:07Z<p>Arcatan: /* A few simple examples */ Make fibST more strict</p>
<hr />
<div>{{Standard class|ST|module=Control.Monad.ST|module-doc=Control-Monad-ST|package=base}}<br />
<br />
The ST monad provides support for ''strict'' state threads.<br />
<br />
<br />
==A discussion on the Haskell irc ==<br />
From #haskell (see 13:05:37 in the [http://tunes.org/~nef/logs/haskell/07.02.07 log] ):<br />
<br />
* TuringTest: ST lets you implement algorithms that are much more efficient with mutable memory used internally. But the whole "thread" of computation cannot exchange mutable state with the outside world, it can only exchange immutable state.<br />
<br />
* TuringTest: chessguy: You pass in normal Haskell values and then use ST to allocate mutable memory, then you initialize and play with it, then you put it away and return a normal Haskell value.<br />
<br />
* sjanssen: a monad that has mutable references and arrays, but has a "run" function that is referentially transparent<br />
<br />
* DapperDan2: it strikes me that ST is like a lexical scope, where all the variables/state disappear when the function returns.<br />
[[Category:Standard classes]] [[Category:Monad]]<br />
<br />
<br />
==An explanation in Haskell-Cafe==<br />
<br />
The ST monad lets you use update-in-place, but is escapable (unlike IO). <br />
ST actions have the form:<br />
<br />
<haskell><br />
ST s α<br />
</haskell><br />
<br />
Meaning that they return a value of type α, and execute in "thread" s.<br />
All reference types are tagged with the thread, so that actions can only<br />
affect references in their own "thread".<br />
<br />
Now, the type of the function used to escape ST is:<br />
<br />
<haskell><br />
runST :: forall α. (forall s. ST s α) -> α<br />
</haskell><br />
<br />
The action you pass must be universal in s, so inside your action you don't know what thread, thus you cannot access any other threads, thus <hask>runST</hask> is pure. This is very useful, since it allows you to implement externally pure things like in-place quicksort, and present them as pure functions ∀ e. Ord e ⇒ Array e → Array e; without using any unsafe functions.<br />
<br />
But that type of <hask>runST</hask> is illegal in Haskell-98, because it needs a universal quantifier *inside* the function-arrow! In the jargon, that type has rank 2; haskell 98 types may have rank at most 1.<br />
<br />
See http://www.haskell.org/pipermail/haskell-cafe/2007-July/028233.html<br />
<br />
Could we *please* see an example.<br />
<br />
Sure thing...<br />
<br />
== A few simple examples ==<br />
<br />
In this example, we define a version of the function sum, but do it in a way which more like how it would be done in imperative languages, where a variable is updated, rather than a new value is formed and passed to the next iteration of the function. While in place modifications of the STRef n are occurring, something that would usually be considered a side effect, it is all done in a safe way which is deterministic. The result is that we get the benefits of being able to modify memory in place, while still producing a pure function with the use of runST.<br />
<br />
<haskell><br />
import Control.Monad.ST<br />
import Data.STRef<br />
import Control.Monad<br />
<br />
<br />
sumST :: Num a => [a] -> a<br />
sumST xs = runST $ do -- runST takes out stateful code and makes it pure again.<br />
<br />
n <- newSTRef 0 -- Create an STRef (place in memory to store values)<br />
<br />
forM_ xs $ \x -> do -- For each element of xs ..<br />
modifySTRef n (+x) -- add it to what we have in n.<br />
<br />
readSTRef n -- read the value of n, and return it.<br />
<br />
<br />
</haskell><br />
<br />
An implementation of foldl using the ST monad (a lot like sum, and in fact sum can be defined in terms of foldlST):<br />
<br />
<haskell><br />
foldlST :: (a -> b -> a) -> a -> [b] -> a<br />
foldlST f acc xs = runST $ do<br />
acc' <- newSTRef acc -- Create a variable for the accumulator<br />
<br />
forM_ xs $ \x -> do -- For each x in xs...<br />
<br />
a <- readSTRef acc' -- read the accumulator<br />
writeSTRef acc' (f a x) -- apply f to the accumulator and x<br />
<br />
readSTRef acc' -- and finally read the result<br />
</haskell><br />
<br />
An example of the Fibonacci function running in constant¹ space:<br />
<br />
<haskell><br />
fibST :: Integer -> Integer<br />
fibST n = <br />
if n < 2<br />
then n<br />
else runST $ do<br />
x <- newSTRef 0<br />
y <- newSTRef 1<br />
fibST' n x y<br />
<br />
where fibST' 0 x _ = readSTRef x<br />
fibST' n x y = do<br />
x' <- readSTRef x<br />
y' <- readSTRef y<br />
writeSTRef x y'<br />
writeSTRef y $! x'+y'<br />
fibST' (n-1) x y<br />
</haskell><br />
<br />
[1] Since we're using Integers, technically it's not constant space, as they grow in size when they get bigger, but we can ignore this.</div>Arcatanhttps://wiki.haskell.org/index.php?title=Hackathon&diff=40907Hackathon2011-07-11T18:28:50Z<p>Arcatan: Added CamHac</p>
<hr />
<div>[[Image:Hac-axe-icon.png|Hac icon]]<br />
<br />
'''Hac: The Haskell Hackathon'''<br />
<br />
The Haskell developer community holds regular hackathons, to get<br />
together, collaborate, and work on key tools and infrastructure.<br />
<br />
* [[CamHac|Cambridge, Aug 2011]]<br />
* [[Hac φ|Philadelphia, Jul 2011]]<br />
* [[BayHac2011|SF Bay Area, Feb 2011]]<br />
* [[Ghent Functional Programming Group/BelHac|Ghent, Nov 2010]]<br />
* [[LtU-Kiev/Hackathon|Kiev, Oct 2010]]<br />
* [[AusHac2010|Australian Hackathon, Jul 2010]]<br />
* [[Hac φ|Philadelphia, May 2010]]<br />
* [[ZuriHac|Zurich, Mar 2010]]<br />
* [[HacPDX|Portland, OR, Sep 2009]]<br />
* [[Hac7|Edinburgh, Aug 2009]]<br />
* [[Hac φ|Philadelphia, Jul 2009]]<br />
* [[Hac5|Utrecht, Apr 2009]]<br />
* [[HaL3|Leipzig, Apr 2008]]<br />
* [[Hac 2008|Göteborg, Apr 2008]]<br />
* [[Hac 2007 II|Freiburg, Oct 2007]]<br />
* [[Hac 2007|Oxford, Jan 2007]]<br />
* [http://hackage.haskell.org/trac/ghc/wiki/Hackathon Portland, Sep 2006]<br />
<br />
[[Category:Events]]<br />
[[Category:Hackathon]]</div>Arcatan