Changeset 27 for sizechecking/branches/macs/Lambda.hs
- Timestamp:
- May 5, 2014, 3:10:33 PM (12 years ago)
- File:
-
- 1 edited
-
sizechecking/branches/macs/Lambda.hs (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sizechecking/branches/macs/Lambda.hs
r26 r27 1 {-# LANGUAGE TypeFamilies, GADTs #-}1 {-# LANGUAGE TypeFamilies, GADTs, Rank2Types, TemplateHaskell #-} 2 2 3 3 module Lambda where … … 5 5 import qualified Data.Supply as S 6 6 import qualified Data.Char as C 7 import Control.Monad.IO.Class (MonadIO, liftIO) 8 import Data.IORef (newIORef, readIORef, writeIORef) 7 import Data.Lens.Light 9 8 10 9 {- … … 29 28 30 29 {- 31 - show interpreter32 -}30 - show interpreter 31 -} 33 32 showVar :: Int -> String -> String 34 33 showVar x = if x>28 … … 36 35 else showChar $ C.chr $ C.ord 'a' + x 37 36 38 newtype S a = S { unS :: S.Supply Int -> Int -> ShowS } 37 class SContext a where 38 supply :: Lens a (S.Supply Int) 39 prec :: Lens a Int 39 40 40 instance Lambda S where 41 lit a = S (\_ p -> showsPrec p a) 42 app (S fun) (S arg) = S (\s p -> 43 let (s1, s2) = S.split2 s 44 in showParen (p>6) $ fun s1 6 . showChar ' ' . arg s2 7) 45 lam fun = S (\s p -> 46 let (s1, s2) = S.split2 s 41 newtype S ctx a = S { unS :: ctx -> ShowS } 42 43 instance SContext ctx => Lambda (S ctx) where 44 lit a = S (\ctx -> showsPrec (getL prec ctx) a) 45 app (S fun) (S arg) = S (\ctx -> 46 let (s1, s2) = S.split2 (getL supply ctx) 47 p = getL prec ctx 48 in showParen (p>6) $ fun (updateCtx s1 6 ctx) . showChar ' ' . arg (updateCtx s2 7 ctx)) 49 lam fun = S (\ctx -> 50 let (s1, s2) = S.split2 (getL supply ctx) 47 51 v = S.supplyValue s1 48 showV = S $ \_ _ -> showVar v 49 in showParen (p>0) $ showChar 'λ' . showVar v . showChar '.' . unS (fun showV) s2 0) 52 p = getL prec ctx 53 showV = S $ \ctx -> showVar v 54 in showParen (p>0) $ showChar 'λ' . showVar v . showChar '.' . unS (fun showV) (updateCtx s2 0 ctx)) 50 55 51 ast :: S a -> IO ShowS 56 updateCtx :: SContext ctx => S.Supply Int -> Int -> ctx -> ctx 57 updateCtx s p = setL supply s . setL prec p 58 59 data SData = SData { _getSDataSupply :: S.Supply Int, _getSDataPrec :: Int } 60 $(makeLens ''SData) 61 62 instance SContext SData where 63 supply = getSDataSupply 64 prec = getSDataPrec 65 66 ast :: S SData a -> IO ShowS 52 67 ast a = do 53 68 s <- S.newSupply 0 (+1) 54 return $ unS a s 069 return $ unS a $ SData s 0 55 70 56 printAst :: S a -> IO ()71 printAst :: S SData a -> IO () 57 72 printAst l = ast l >>= (\s -> putStrLn $ s "") 58 73 59 60 {-61 - reduction62 -}63 data IR h t where64 Lit :: Int -> IR h Int65 App :: IR h (a -> b) -> IR h a -> IR h b66 Lam :: (IR h a -> IR h b) -> IR h (a -> b)67 68 instance Lambda (IR h) where69 lam = Lam70 app = App71 lit = Lit72 73 toFinal :: (Lambda l) => IR h t
Note: See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/trac/tamop/chrome/site/your_project_logo.png)