Changeset 8


Ignore:
Timestamp:
Nov 13, 2012, 11:27:26 PM (13 years ago)
Author:
gobi
Message:

new arrow types

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sizechecking/L.hs

    r7 r8  
    33import Data.Char(ord, chr) 
    44import Control.Monad.State 
    5 import Data.Supply 
     5--import Data.Supply 
    66 
    77data OpName = PLUS | MINUS | MUL 
     
    1010 
    1111{- Lambda calculus without free variables -} 
     12type Arr repr a b = repr a -> repr b 
     13 
    1214class Lambda l where 
    13     labs    :: (l a -> l b) -> l (a -> b) 
    14     app     :: l (a -> b) -> l a -> l b 
     15    labs    :: (l a -> l b) -> l (Arr l a b) 
     16    app     :: l (Arr l a b) -> l a -> l b 
    1517    lit     :: Int -> l Int 
    1618    op      :: l Int -> OpName -> l Int -> l Int 
     
    1820class SizeExp l where 
    1921    type List l :: * -> * 
    20     list    :: l Int -> l (Int -> b) -> l (List l b) 
    21     aabs    :: (l Int -> l (Int -> a) -> l b) -> l (List l a -> b) 
    22     shift   :: l (Int -> a) -> l Int -> l (Int -> a) -> l (Int -> a) 
     22    list    :: l Int -> l (Arr l Int b) -> l (List l b) 
     23    aabs    :: (l Int -> l (Arr l Int a) -> l b) -> l (Arr l (List l a) b) 
     24    shift   :: l (Arr l Int a) -> l Int -> l (Arr l Int a) -> l (Arr l Int a) 
    2325    undef   :: l Bottom 
    2426    unsized :: l () 
     
    3335 
    3436{- Printing -} 
    35 newtype LPrint a = LPrint { unPrint :: Int -> State Int ShowS } 
     37newtype LPrint a = LPrint { unPrint :: Int -> Int -> ShowS } 
    3638 
    3739instance Lambda LPrint where 
    38   lit x      = LPrint $ \p -> return $ shows x 
     40  lit x      = LPrint $ \_ -> return $ shows x 
    3941  op m opc n = LPrint $ \p -> do 
    4042    let (prec, lprec, rprec, c) = getPrec opc 
     
    4648    l2 <- unPrint v 7 
    4749    return $ showParen (p>6) $ l1 . showChar ' ' . l2 
    48   labs e      = LPrint $ \p -> do 
    49     v <- getVar 
    50     let var = LPrint $ \_ -> return $ showVar v 
    51     l <- unPrint (e var) 0  
    52     return $ showParen (p>0) $ showChar 'λ' . showVar v . showChar '.' . l 
     50  labs e      = LPrint $ \p v -> let 
     51    var = LPrint $ \_ -> return $ showVar v 
     52    l = unPrint (e var) 0 $ succ v 
     53    in showParen (p>0) $ showChar 'λ' . showVar v . showChar '.' . l 
    5354 
    5455instance SizeExp LPrint where 
    5556  type List LPrint = [] 
    56   aabs f   = LPrint $ \p -> do 
    57     v1 <- getVar 
    58     v2 <- getVar 
    59     let var1 = LPrint $ \_ -> return $ showVar v1 
    60         var2 = LPrint $ \_ -> return $ showVar v2 
    61     l <- unPrint (f var1 var2) 0 
    62     return $ showParen (p>0) $ showChar 'Λ' . showVar v1 . showChar ',' . showVar v2 . showChar '.' . l 
     57  aabs f   = LPrint $ \p v -> let  
     58    v2 = succ v 
     59    var1 = LPrint $ \_ _ -> showVar v 
     60    var2 = LPrint $ \_ _ -> showVar v2 
     61    l = unPrint (f var1 var2) 0 (v+2) 
     62    in showParen (p>0) $ showChar 'Λ' . showVar v . showChar ',' . showVar v2 . showChar '.' . l 
    6363  list s f = LPrint $ \p -> do 
    6464    l1 <- unPrint s 9 
     
    7373  unsized = LPrint $ \_ -> return $ showChar 'U' 
    7474 
    75 mprint x = putStrLn $ (fst $ runState (unPrint x 0) 0) "" 
     75view :: LPrint a -> LPrint a 
     76view = id 
     77instance Show (LPrint a) where 
     78    showsPrec _ e = unPrint e 0 0 
    7679 
    7780showVar x = if x>28  
     
    105108newtype R a = R { unR :: a } 
    106109instance Lambda R where 
    107   labs f      = R $ unR . f . R 
    108   app e1 e2   = R $ (unR e1) (unR e2) 
     110  labs f      = R $ f  
     111  app e1 e2   = (unR e1) e2 
    109112  lit i       = R i 
    110113  op e1 op e2 = let opm = case op of 
     
    115118 
    116119newtype RList a = RList { unList :: (Int, Int -> a)  } 
    117 instance (Show a) => Show (RList a) where 
    118   show (RList (s,f)) = show (map f [0..s-1]) 
     120--instance (Show a) => Show (RList a) where 
     121--  show (RList (s,f)) = show (map f [0..s-1]) 
    119122 
    120123instance SizeExp R where 
     
    122125  undef       = R $ Bottom 
    123126  unsized     = R $ () 
    124   list s f    = R $ RList (unR s, unR f) 
    125   aabs e      = R $ \(RList (s,f)) -> unR (e (R s) (R f)) 
    126   shift a s c = R $ \i -> if i<(unR s) then unR a i else unR c i 
    127  
    128 {- Create a restricted deep embedding to work with the expression -} 
    129  
    130 {- sized [a] to prove -} 
    131 data Nil 
    132 data Cons a 
    133 data SLInt a where 
    134   SLNil  :: SLInt Nil 
    135   SLCons :: Int -> SLInt a -> SLInt (Cons a) 
    136  
    137 class MGS a where 
    138   mgs :: Supply Int -> a -> a 
    139   mgs = mgs' [] 
    140   mgs' :: [Int] -> Supply Int -> a -> a 
    141  
    142  
    143 instance MGS () where 
    144   mgs' _ _ _ = () 
    145 instance MGS a => MGS (RList a) where 
    146   mgs' bound supp  _ = RList undefined 
    147     where 
    148     (s1, s2, s3) = split3 supp 
    149     listsize = mgs' bound supp (undefined :: a) 
     127  list s f    = R $ RList (unR s, unR . (unR f) . R ) 
     128--aabs    :: Arr l Int (Arr l (Arr l Int a) b) -> l (Arr l (List l a) b) 
     129--  aabs e      = R $ \(RList (s,f)) -> unR (e (R s) (R f)) 
     130--  shift a s c = R $ \i -> if i<(unR s) then unR a i else unR c i 
     131 
     132-- Type level int to count arity 
     133data Zero 
     134data Succ a 
     135 
     136data Exp a where 
     137    Lit :: Int -> Exp Zero 
     138    Op  :: Exp Zero -> OpName -> Exp Zero -> Exp Zero 
     139    App :: Exp (Succ a) -> Exp Zero -> Exp a 
     140    Var :: Int -> Exp a 
     141 
     142instance Show (Exp a) where 
     143    showsPrec _ (Lit a)         = shows a 
     144    showsPrec p (Op lhs op rhs) = 
     145        let (prec, lprec, rprec, c) = getPrec op 
     146        in showParen (p>prec) $  showsPrec lprec lhs . showChar c . showsPrec rprec rhs 
     147    showsPrec p (App lhs rhs)   = 
     148        showParen (p>6) $ showsPrec 6 lhs . showChar ' ' . showsPrec 7 rhs 
     149    showsPrec _ (Var i)         = showVar i 
     150 
     151type IntExp = Exp Zero 
     152data Condition = Eq IntExp IntExp | Gt IntExp IntExp | Lt IntExp IntExp 
     153type SExp = ([Condition], IntExp) 
     154newtype Compile a = Compile { unCompile :: SExp } 
     155 
     156--instance Lambda Compile where 
     157--  labs :: (l a -> l b) -> l (a -> b) 
     158--    labs f = Compile $ f 
     159 
     160 
     161--{- Create a restricted deep embedding to work with the expression -} 
     162--{- sized [a] to prove -} 
     163-- 
     164--class Restricted a where 
     165--    kind :: a -> Int 
     166-- 
     167--instance Restricted Int where 
     168--    kind _ = 0 
     169-- 
     170--instance Restricted a => Restricted (Int -> a) where 
     171--    kind _ = succ $ kind $ (undefined :: a) 
     172-- 
     173--class Var l where 
     174--    var :: Restricted a => Int -> l a 
     175-- 
     176--showVar2 x = showChar '_' . showVar x 
     177-- 
     178--newtype Count a = C { count :: Int } 
     179--cnv :: Restricted a => Count a -> a 
     180--cnv = undefined 
     181-- 
     182--ckind :: (Restricted a) => Count a -> Int 
     183--ckind = kind . cnv 
     184-- 
     185--instance Var Count where 
     186--    var _ = undefined 
     187-- 
     188--instance Var LPrint where 
     189--    var x = LPrint $ \_ -> return $ showVar2 x 
     190-- 
     191--class MGS a where 
     192--  mgs :: Supply Int -> a -> a 
     193--  mgs = mgs' [] 
     194--  mgs' :: [Int] -> Supply Int -> a -> a 
     195-- 
     196--instance MGS () where 
     197--  mgs' _ _ _ = () 
     198-- 
     199--instance MGS a => MGS (RList a) where 
     200--  mgs' bound supp  _ = RList (listsize, undefined) 
     201--    where 
     202--    (s1, s2, s3) = split3 supp 
     203--    listsize = mgs' bound supp (undefined :: a) 
Note: See TracChangeset for help on using the changeset viewer.