Ignore:
Timestamp:
May 5, 2014, 3:10:33 PM (12 years ago)
Author:
gobi
Message:

new files

File:
1 edited

Legend:

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

    r26 r27  
    1 {-# LANGUAGE TypeFamilies, GADTs #-} 
     1{-# LANGUAGE TypeFamilies, GADTs, Rank2Types, TemplateHaskell #-} 
    22 
    33module Lambda where 
     
    55import qualified Data.Supply as S 
    66import qualified Data.Char as C 
    7 import Control.Monad.IO.Class (MonadIO, liftIO) 
    8 import Data.IORef (newIORef, readIORef, writeIORef) 
     7import Data.Lens.Light  
    98 
    109{- 
     
    2928 
    3029{- 
    31  - show interpreter 
    32  -} 
     30- show interpreter 
     31-} 
    3332showVar :: Int -> String -> String 
    3433showVar x = if x>28  
     
    3635    else showChar $ C.chr $ C.ord 'a' + x 
    3736 
    38 newtype S a = S { unS :: S.Supply Int -> Int -> ShowS } 
     37class SContext a where 
     38  supply :: Lens a (S.Supply Int) 
     39  prec :: Lens a Int 
    3940 
    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 
     41newtype S ctx a = S { unS :: ctx -> ShowS } 
     42 
     43instance 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) 
    4751            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)) 
    5055 
    51 ast :: S a -> IO ShowS 
     56updateCtx :: SContext ctx => S.Supply Int -> Int -> ctx -> ctx 
     57updateCtx s p = setL supply s . setL prec p 
     58 
     59data SData = SData { _getSDataSupply :: S.Supply Int, _getSDataPrec :: Int } 
     60$(makeLens ''SData) 
     61 
     62instance SContext SData where 
     63  supply = getSDataSupply 
     64  prec = getSDataPrec 
     65 
     66ast :: S SData a -> IO ShowS 
    5267ast a = do 
    5368    s <- S.newSupply 0 (+1) 
    54     return $ unS a s 0 
     69    return $ unS a $ SData s 0 
    5570 
    56 printAst :: S a -> IO () 
     71printAst :: S SData a -> IO () 
    5772printAst l = ast l >>= (\s -> putStrLn $ s "") 
    5873 
    59  
    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) 
    67  
    68 instance Lambda (IR h) where 
    69     lam = Lam 
    70     app = App 
    71     lit = Lit 
    72  
    73 toFinal :: (Lambda l) => IR h t 
Note: See TracChangeset for help on using the changeset viewer.