File: Fix.hs

package info (click to toggle)
bali-phy 4.0~beta16%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,192 kB
  • sloc: cpp: 119,288; xml: 13,482; haskell: 9,722; python: 2,930; yacc: 1,329; perl: 1,169; lex: 904; sh: 343; makefile: 26
file content (44 lines) | stat: -rw-r--r-- 1,068 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{-# LANGUAGE NoImplicitPrelude #-}
module Control.Monad.Fix (
        MonadFix(mfix),
        fix
   ) where

import Control.Monad
import Data.Function (fix,(.))
import Data.Maybe
import Compiler.Error (error)
import Data.OldList (head, tail)
import Compiler.IO
import Compiler.ST

--Laws:
--
-- Purity
--     mfix (return . h) = return (fix h)
-- Left shrinking (or Tightening)
--     mfix (\x -> a >>= \y -> f x y) = a >>= \y -> mfix (\x -> f x y)
-- Sliding
--     mfix (liftM h . f) = liftM h (mfix (f . h)), for strict h.
-- Nesting
--     mfix (\x -> mfix (\y -> f x y)) = mfix (\x -> f x x)

class Monad m => MonadFix m where
    mfix :: (a -> m a) -> m a 

instance MonadFix [] where
    mfix f = case fix (f . head) of
               []    -> []
               (x:_) -> x : mfix (tail . f)


instance MonadFix Maybe where
    mfix f = let a = f (unJust a) in a
             where unJust (Just x) = x
                   unJust Nothing  = error "mfix Maybe: Nothing"

instance MonadFix IO where
    mfix = fixIO

instance MonadFix (ST s) where
    mfix = fixST