Changeset 27 for sizechecking/branches
- Timestamp:
- May 5, 2014, 3:10:33 PM (11 years ago)
- Location:
- sizechecking/branches/macs
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
sizechecking/branches/macs/Exp.hs
r24 r27 4 4 import Ops 5 5 import Data.Supply as S 6 import Data.Lens.Light 6 7 7 8 class LOps l => Exp l where … … 21 22 cond c tbranch fbranch = if unQ c then tbranch else fbranch 22 23 undefined = Prelude.undefined 23 instance Exp S where 24 nil = S $ \_ _ -> showString "[]" 25 undefined = S $ \_ _ -> showString "undefined" 26 match list nbranch cbranch = S $ \s p -> 27 let (s1, s2, ss) = S.split3 s 24 25 instance SContext s => Exp (S s) where 26 nil = S $ \_ -> showString "[]" 27 undefined = S $ \_ -> showString "undefined" 28 match list nbranch cbranch = S $ \ctx -> 29 let (s1, s2, ss) = S.split3 (getL supply ctx) 28 30 (s3, s4, s5) = S.split3 ss 29 31 v1 = S.supplyValue s4 30 showV1 = S $ \_ _-> showVar v132 showV1 = S $ \_ -> showVar v1 31 33 v2 = S.supplyValue s5 32 showV2 = S $ \_ _ -> showVar v2 34 showV2 = S $ \_ -> showVar v2 35 p = getL prec ctx 33 36 in showParen (p>0) $ 34 37 showString "case ". 35 unS list s1 0.38 unS list (updateCtx s1 0 ctx) . 36 39 showString " of [] => ". 37 unS nbranch s2 0.40 unS nbranch (updateCtx s2 0 ctx) . 38 41 showString "; (" . showVar v1 . showChar ':' . showVar v2 . showString ") => " . 39 unS (cbranch showV1 showV2) s3 0 40 cond c tbranch fbranch = S $ \s p -> 41 let (s1, s2, s3) = S.split3 s 42 unS (cbranch showV1 showV2) (updateCtx s3 0 ctx) 43 cond c tbranch fbranch = S $ \ctx -> 44 let (s1, s2, s3) = S.split3 (getL supply ctx) 45 p = getL prec ctx 42 46 in showParen (p>0) $ 43 47 showString "if ". 44 unS c s1 0.48 unS c (updateCtx s1 0 ctx). 45 49 showString " then " . 46 unS tbranch s2 0.50 unS tbranch (updateCtx s2 0 ctx). 47 51 showString " else " . 48 unS fbranch s3 052 unS fbranch (updateCtx s3 0 ctx) -
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 32 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 -
sizechecking/branches/macs/Ops.hs
r25 r27 7 7 import qualified Prelude 8 8 import Lambda 9 import Data.Lens.Light 9 10 10 11 -- infix operatorok, ezek beagyazasa picit necces, lasd a type family kesobb … … 32 33 fun _ = Q 33 34 34 instance LOps Swhere35 fun name _ = S (\ _ p -> Prelude.showsPrec pname)35 instance (SContext s) => LOps (S s) where 36 fun name _ = S (\ctx -> Prelude.showsPrec (getL prec ctx) name) 36 37 37 infixopl name p rec _ lhs rhs = S(\s p->38 let (s1, s2) = S.split2 s39 in Prelude.showParen ( p Prelude.> prec) $40 unS lhs s1 prec.38 infixopl name p _ lhs rhs = S(\ctx -> 39 let (s1, s2) = S.split2 (getL supply ctx) 40 in Prelude.showParen ((getL prec ctx) Prelude.> p) $ 41 unS lhs (setL supply s1 $ setL prec p ctx) . 41 42 Prelude.showString name . 42 unS rhs s2 (Prelude.succ prec)43 unS rhs (setL supply s2 $ setL prec (Prelude.succ p) ctx) 43 44 ) 44 infixop name prec _ lhs rhs = S(\s p -> 45 let (s1, s2) = S.split2 s 46 in Prelude.showParen (p Prelude.> prec) $ 47 unS lhs s1 (Prelude.succ prec) . 45 46 infixop name p _ lhs rhs = S(\ctx -> 47 let (s1, s2) = S.split2 (getL supply ctx) 48 in Prelude.showParen ((getL prec ctx) Prelude.> p) $ 49 unS lhs (setL supply s1 $ setL prec (Prelude.succ p) ctx) . 48 50 Prelude.showString name . 49 unS rhs s2 (Prelude.succ prec)51 unS rhs (setL supply s2 $ setL prec (Prelude.succ p) ctx) 50 52 ) 51 infixopr name prec _ lhs rhs = S(\s p -> 52 let (s1, s2) = S.split2 s 53 in Prelude.showParen (p Prelude.> prec) $ 54 unS lhs s1 (Prelude.succ prec) . 53 54 infixopr name p _ lhs rhs = S(\ctx -> 55 let (s1, s2) = S.split2 (getL supply ctx) 56 in Prelude.showParen ((getL prec ctx) Prelude.> p) $ 57 unS lhs (setL supply s1 $ setL prec (Prelude.succ p) ctx) . 55 58 Prelude.showString name . 56 unS rhs s2 prec59 unS rhs (setL supply s2 $ setL prec p ctx) 57 60 ) -
sizechecking/branches/macs/Size.hs
r25 r27 8 8 import Ops 9 9 import Data.Supply as S 10 import Data.Lens.Light 10 11 11 12 data Unsized … … 19 20 20 21 21 instance Size S where 22 unsized = S $ \_ _ -> showChar 'U' 23 bottom = S $ \_ _ -> showChar 'âŽ' 24 list size sexp = S $ \s p -> 25 let (s1, s2) = S.split2 s 22 instance SContext s => Size (S s) where 23 unsized = S $ \_ -> showChar 'U' 24 bottom = S $ \_ -> showChar 'âŽ' 25 list size sexp = S $ \ctx -> 26 let (s1, s2) = S.split2 (getL supply ctx) 27 p = getL prec ctx 26 28 in showParen (p>0) $ 27 29 showString "List " . 28 unS size s1 9.30 unS size (updateCtx s1 9 ctx) . 29 31 showChar ' ' . 30 unS sexp s2 931 slam f = S $ \ s p->32 let (s1, s2, s3) = S.split3 s32 unS sexp (updateCtx s2 9 ctx) 33 slam f = S $ \ctx -> 34 let (s1, s2, s3) = S.split3 (getL supply ctx) 33 35 v1 = S.supplyValue s1 34 showV1 = S $ \_ _-> showVar v136 showV1 = S $ \_ -> showVar v1 35 37 v2 = S.supplyValue s2 36 showV2 = S $ \_ _ -> showVar v2 38 showV2 = S $ \_ -> showVar v2 39 p = getL prec ctx 37 40 in showParen (p>0) $ 38 41 showChar 'Î' . … … 41 44 showVar v2 . 42 45 showChar '.' . 43 unS (f showV1 showV2) s3 0 44 shift e1 ss e2 = S $ \s p -> 45 let (s1, s2, s3) = S.split3 s 46 unS (f showV1 showV2) (updateCtx s3 0 ctx) 47 shift e1 ss e2 = S $ \ctx -> 48 let (s1, s2, s3) = S.split3 (getL supply ctx) 49 p = getL prec ctx 46 50 in showParen (p>0) 47 51 $ showString "Shift " 48 . unS e1 s1 252 . unS e1 (updateCtx s1 2 ctx) 49 53 . showChar ' ' 50 . unS ss s2 254 . unS ss (updateCtx s2 2 ctx) 51 55 . showChar ' ' 52 . unS e2 s3 256 . unS e2 (updateCtx s3 2 ctx) 53 57 -
sizechecking/branches/macs/Tests/ExpTest.hs
r24 r27 30 30 testD2Cons = cons (cons (lit 1) nil) nil 31 31 32 checkAST :: S a -> String -> IO Bool32 checkAST :: S SData a -> String -> IO Bool 33 33 checkAST exp repr = ast exp >>= (\t -> return $ t "" == repr ) 34 34 -
sizechecking/branches/macs/Tests/SizeTest.hs
r24 r27 21 21 testTail = slam $ \s f -> list (s - lit 1) f 22 22 23 testAddOne :: (Size l) => l ([Unsized] -> [Unsized]) 24 testAddOne = slam $ \s f -> list (s + lit 1) (lam $ const unsized) 25 23 26 testCons :: Size l => l (a -> [a] -> [a]) 24 27 testCons = lam $ \x -> slam $ \s f -> 25 28 list (s + lit 1) $ shift f s (lam $ const x) 26 29 27 checkAst :: S a -> String -> IO Bool 30 testConcat :: Size l => l ([a] -> [a] -> [a]) 31 testConcat = slam $ \s1 f1 -> slam $ \s2 f2 -> 32 list (s1 + s2) $ shift f1 s1 f2 33 34 35 checkAst :: S SData a -> String -> IO Bool 28 36 checkAst exp repr = ast exp >>= (\t -> return $ t "" == repr) 29 37 -
sizechecking/branches/macs/sizechecking.cabal
r25 r27 55 55 -- Other library packages from which modules are imported. 56 56 build-depends: base ==4.*, containers ==0.5.*, sbv, value-supply, 57 transformers 57 transformers, data-lens-light
Note: See TracChangeset
for help on using the changeset viewer.