[Haskell-cafe] Controlling scope using monadic classes

Daniel McAllansmith dagda at xtra.co.nz
Tue May 16 21:43:50 EDT 2006


I'm trying to control the scope within which functions can be used by putting 
them in a type class.
Unfortunately I can't seem to figure out how to get it done.  Any advice would 
be much appreciated.

What I want is to start out in a certain scope, which restricts me to using 
functions in that scope or opening up a subsidiary scope at which point I'm 
restricted to functions in that scope or opening up an even deeper scope.

Hopefully a failed attempt will help explain what I'm trying to achieve... the 
following has trouble with the inScope{B,C} functions.

type AInfo = String
type BInfo = String
type CInfo = String

type BResult = Int
type CResult = Char

class (Monad m) => WithinA m where
    askAInfo :: m AInfo

class (WithinA m) => WithinB m where
    askBInfo :: m BInfo

class (WithinB m) => WithinC m where
    askCInfo :: m CInfo

class (WithinA m) => ScopeA m where
    getAInfo    :: m AInfo
    putAInfo    :: AInfo -> m ()
    updateAInfo :: BResult -> m ()
    inScopeB    :: (ScopeB m2) => m2 BResult -> m BResult

class (WithinB m) => ScopeB m where
    getBInfo :: m BInfo
    putBInfo :: BInfo -> m ()
    inScopeC :: (ScopeC m2) => m2 CResult -> m CResult

class (WithinC m) => ScopeC m where
    getCInfo :: m CInfo
    putCInfo :: CInfo -> m ()

aScoped :: (ScopeA m) => m String
aScoped = do
    bResult <- inScopeB bScoped
    updateAInfo bResult
    return "done"

bScoped :: (ScopeB m) => m BResult
bScoped = do
    i1 <- b1
    i2 <- b2
    return (i1 + i2)

b1 :: (ScopeB m) => m Int
b1 = return 2

b2 :: (ScopeB m) => m Int
b2 = inScopeC cScoped >>= return . fromEnum

cScoped :: (ScopeC m) => m Char
cScoped = return '('


More information about the Haskell-Cafe mailing list