Ignore:
Timestamp:
May 4, 2014, 6:57:47 PM (11 years ago)
Author:
gobi
Message:

monad for observable sharing

Location:
sizechecking/branches/macs
Files:
2 added
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • sizechecking/branches/macs/Lambda.hs

    r24 r25  
    1 {-# LANGUAGE FlexibleContexts,FlexibleInstances #-} 
     1{-# LANGUAGE TypeFamilies #-} 
     2 
    23module Lambda where 
    34 
    45import qualified Data.Supply as S 
    56import qualified Data.Char as C 
     7import Control.Monad.IO.Class (MonadIO, liftIO) 
     8import Data.IORef (newIORef, readIORef, writeIORef) 
    69 
    710{- 
     
    912 -} 
    1013class Lambda l where 
    11     lam   :: (l a -> l b) -> l (a -> b) 
    12     app   :: l (a -> b) -> l a -> l b 
     14    lam :: (l a -> l b) -> l (a -> b) 
     15    app :: l (a -> b) -> l a -> l b 
    1316    lit :: Int -> l Int 
    1417 
     
    5457printAst :: S a -> IO () 
    5558printAst l = ast l >>= (\s -> putStrLn $ s "") 
     59 
     60 
     61type family Sem (m :: * -> *) a :: * 
     62type instance Sem m Int      = Int 
     63type instance Sem m (a -> b) = m (Sem m a) -> m (Sem m b) 
     64 
     65newtype R m a = R { unR :: m (Sem m a) } 
     66 
     67share :: (MonadIO m) => m a -> m (m a) 
     68share f = do 
     69    mem <- liftIO $ newIORef (False, f) 
     70    return $ do 
     71        (evald, thunk) <- liftIO $ readIORef mem 
     72        if evald then thunk 
     73        else do 
     74          value <- thunk 
     75          liftIO $ writeIORef mem (True, return value) 
     76          return value 
     77 
     78instance (MonadIO m) => Lambda (R m) where  
     79    app x y = R $ unR x >>= ($ (unR y)) 
     80    lit = R . return 
     81    lam f   = R . return $ (\x -> share x >>= unR . f . R) 
  • sizechecking/branches/macs/Ops.hs

    r23 r25  
    1 {-# LANGUAGE FlexibleInstances #-} 
     1{-# LANGUAGE FlexibleInstances, KindSignatures #-} 
    22 
    33module Ops where 
     
    88import Lambda 
    99 
     10-- infix operatorok, ezek beagyazasa picit necces, lasd a type family kesobb 
    1011class (Lambda l) => LOps l where 
    11     infixop :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 
     12    infixop  :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 
    1213    infixopr :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 
    1314    infixopl :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 
    14     fun :: String -> a -> l a 
     15    fun ::  String -> a -> l a 
    1516 
    16     (+) :: l Int -> l Int -> l Int 
    17     (+) = infixopl "+" 6 (Prelude.+) 
     17(+) :: (LOps l) => l Int -> l Int -> l Int 
     18(+) = infixopl "+" 6 (Prelude.+) 
    1819 
    19     (-) :: l Int -> l Int -> l Int 
    20     (-) = infixopl "-" 6 (Prelude.-) 
     20(-) :: (LOps l) => l Int -> l Int -> l Int 
     21(-) = infixopl "-" 6 (Prelude.-) 
    2122 
    22     (*) :: l Int -> l Int -> l Int 
    23     (*) = infixopl "*" 7 (Prelude.*) 
     23(*) :: (LOps l) => l Int -> l Int -> l Int 
     24(*) = infixopl "*" 7 (Prelude.*) 
     25 
    2426 
    2527 
  • sizechecking/branches/macs/Size.hs

    r22 r25  
     1{-# LANGUAGE TypeFamilies #-} 
     2{-# LANGUAGE MultiParamTypeClasses #-} 
     3{-# LANGUAGE FlexibleInstances #-} 
     4 
    15module Size where 
    26 
     
    1317    unsized :: l Unsized 
    1418    bottom :: l a 
     19 
    1520 
    1621instance Size S where 
     
    4651        . showChar ' ' 
    4752        . unS e2 s3 2 
     53 
Note: See TracChangeset for help on using the changeset viewer.