Changeset 26
- Timestamp:
- May 4, 2014, 8:06:39 PM (11 years ago)
- Location:
- sizechecking/branches/macs
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sizechecking/branches/macs/Lambda.hs
r25 r26 1 {-# LANGUAGE TypeFamilies #-}1 {-# LANGUAGE TypeFamilies, GADTs #-} 2 2 3 3 module Lambda where … … 36 36 else showChar $ C.chr $ C.ord 'a' + x 37 37 38 -- unS :: Value supply -> Precedence -> ShowS39 38 newtype S a = S { unS :: S.Supply Int -> Int -> ShowS } 40 39 … … 59 58 60 59 61 type family Sem (m :: * -> *) a :: * 62 type instance Sem m Int = Int 63 type instance Sem m (a -> b) = m (Sem m a) -> m (Sem m b) 60 {- 61 - reduction 62 -} 63 data IR h t where 64 Lit :: Int -> IR h Int 65 App :: IR h (a -> b) -> IR h a -> IR h b 66 Lam :: (IR h a -> IR h b) -> IR h (a -> b) 64 67 65 newtype R m a = R { unR :: m (Sem m a) } 68 instance Lambda (IR h) where 69 lam = Lam 70 app = App 71 lit = Lit 66 72 67 share :: (MonadIO m) => m a -> m (m a) 68 share 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 78 instance (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) 73 toFinal :: (Lambda l) => IR h t -
sizechecking/branches/macs/Tests/LambdaTest.hs
r24 r26 9 9 test1 :: (Lambda l) => l Int 10 10 test1 = app (lam $ \_ -> lit 3) (lit 2) 11 12 const2 :: (Lambda l) => l ( Int -> Int ) 13 const2 = lam $ \_ -> lit 2 14 15 t3 :: (Lambda l) => l ( (a -> a) -> (a -> a) ) 16 t3 = (lam $ \f -> lam $ \v -> f `app` (f `app` (f `app` v))) 17 11 18 12 19 test1ast :: IO Bool
Note: See TracChangeset
for help on using the changeset viewer.