source: sizechecking/L.hs @ 8

Last change on this file since 8 was 8, checked in by gobi, 13 years ago

new arrow types

File size: 6.5 KB
Line 
1{-# Language GADTs, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, ScopedTypeVariables, TypeFamilies, NoMonomorphismRestriction #-}
2
3import Data.Char(ord, chr)
4import Control.Monad.State
5--import Data.Supply
6
7data OpName = PLUS | MINUS | MUL
8data Bottom = Bottom
9  deriving Show
10
11{- Lambda calculus without free variables -}
12type Arr repr a b = repr a -> repr b
13
14class Lambda l where
15    labs    :: (l a -> l b) -> l (Arr l a b)
16    app     :: l (Arr l a b) -> l a -> l b
17    lit     :: Int -> l Int
18    op      :: l Int -> OpName -> l Int -> l Int
19
20class SizeExp l where
21    type List l :: * -> *
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)
25    undef   :: l Bottom
26    unsized :: l ()
27
28instance Lambda l => Num (l Int) where
29    fromInteger = lit . fromIntegral
30    lhs + rhs   = op lhs PLUS rhs
31    lhs - rhs   = op lhs MINUS rhs
32    lhs * rhs   = op lhs MUL rhs
33    abs = error "abs is not implemented"
34    signum = error "signum is not implemented"
35
36{- Printing -}
37newtype LPrint a = LPrint { unPrint :: Int -> Int -> ShowS }
38
39instance Lambda LPrint where
40  lit x      = LPrint $ \_ -> return $ shows x
41  op m opc n = LPrint $ \p -> do
42    let (prec, lprec, rprec, c) = getPrec opc
43    l1 <- (unPrint m lprec)
44    l2 <- (unPrint n rprec)
45    return $ showParen (p>prec) $  l1 . showChar c . l2
46  app f v    = LPrint $ \p -> do
47    l1 <- unPrint f 6
48    l2 <- unPrint v 7
49    return $ showParen (p>6) $ l1 . showChar ' ' . l2
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
54
55instance SizeExp LPrint where
56  type List LPrint = []
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
63  list s f = LPrint $ \p -> do
64    l1 <- unPrint s 9
65    l2 <- unPrint f 9
66    return $ showParen (p>0) $ showString "List " . l1 . showChar ' ' . l2
67  shift e1 s e2 = LPrint $ \p -> do
68    l1 <- unPrint e1 2
69    l2 <- unPrint s 2
70    l3 <- unPrint e2 2
71    return $ showParen (p>0) $ showString "Shift " . l1 .  showChar ' ' . l2 . showChar ' ' . l3
72  undef = LPrint $ \_ -> return $ showChar '┮'
73  unsized = LPrint $ \_ -> return $ showChar 'U'
74
75view :: LPrint a -> LPrint a
76view = id
77instance Show (LPrint a) where
78    showsPrec _ e = unPrint e 0 0
79
80showVar x = if x>28 
81    then showVar (x `div` 29) . showChar (chr $ ord 'a' + (x `mod` 29))
82    else showChar $ chr $ ord 'a' + x
83
84getPrec :: OpName -> (Int,Int,Int,Char)
85getPrec PLUS = (4,4,5,'+')
86getPrec MINUS = (4,4,5,'-')
87getPrec MUL = (5,5,6,'*')
88
89getVar :: State Int Int
90getVar = do { x <- get; put (x+1); return x }
91
92x = aabs (\s _ -> s) `app` list (lit 0) (labs $ \_ -> undef)
93y = list 0 $ labs $ \_ -> undef
94z = (labs $ \s -> s+1) `app` (lit 1+2)
95conss = labs $ \x -> aabs $ \ys yf -> list (ys + 1) $ shift yf ys (labs $ \_ -> x)
96concats = aabs $ \xs xf -> (aabs $ \ys yf -> list (xs+ys) $ shift xf xs yf)
97maps = labs $ \x -> aabs $ \ys yf -> list ys ( x `app` yf )
98l0 = list (lit 0) (labs $ \_ -> unsized)
99l1 = list (lit 1) (labs $ \_ -> unsized)
100l2 = list (lit 2) (labs $ \_ -> unsized)
101l00 = list (lit 0) (labs $ \_ -> list (lit 0) (labs $ \_ -> unsized))
102l10 = list (lit 1) (labs $ \_ -> list (lit 0) (labs $ \_ -> unsized))
103l01 = list (lit 0) (labs $ \_ -> list (lit 1) (labs $ \_ -> unsized))
104l11 = list (lit 1) (labs $ \_ -> list (lit 1) (labs $ \_ -> unsized))
105l12 = list (lit 1) (labs $ \_ -> list (lit 2) (labs $ \_ -> unsized))
106
107{- Evaluating -}
108newtype R a = R { unR :: a }
109instance Lambda R where
110  labs f      = R $ f
111  app e1 e2   = (unR e1) e2
112  lit i       = R i
113  op e1 op e2 = let opm = case op of
114                                  PLUS  -> (+)
115                                  MINUS -> (-)
116                                  MUL   -> (*)
117                in R $ opm (unR e1) (unR e2)
118
119newtype RList a = RList { unList :: (Int, Int -> a)  }
120--instance (Show a) => Show (RList a) where
121--  show (RList (s,f)) = show (map f [0..s-1])
122
123instance SizeExp R where
124  type List R = RList
125  undef       = R $ Bottom
126  unsized     = R $ ()
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 TracBrowser for help on using the repository browser.