Changeset 15 for sizechecking_branches
- Timestamp:
- Nov 16, 2012, 4:26:26 PM (13 years ago)
- Location:
- sizechecking_branches
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sizechecking_branches/L.hs
r10 r15 1 {-# Language GADTs, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, ScopedTypeVariables, TypeFamilies, NoMonomorphismRestriction, OverlappingInstances #-}1 {-# Language GADTs, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, ScopedTypeVariables, TypeFamilies, NoMonomorphismRestriction, OverlappingInstances, IncoherentInstances #-} 2 2 module L where 3 3 4 import qualified Data.SBV as SBV 4 5 import Data.Char(ord, chr) 5 6 import Control.Monad.State … … 13 14 type Arr repr a b = repr a -> repr b 14 15 15 class Lambda l where 16 class (Num (LInt l)) => Lambda l where 17 type LInt l :: * 16 18 labs :: (l a -> l b) -> l (Arr l a b) 17 19 app :: l (Arr l a b) -> l a -> l b 18 lit :: Int -> l Int19 op :: l Int -> OpName -> l Int -> l Int20 lit :: (LInt l) -> l (LInt l) 21 op :: l (LInt l) -> OpName -> l (LInt l) -> l (LInt l) 20 22 21 23 class SizeExp l where … … 27 29 unsized :: l () 28 30 29 instance Lambda l => Show (l a) where 30 showsPrec _ e = error "Error: no show" 31 32 instance Lambda l => Eq (l a) where 33 (==) _ _ = error "Error: no eq" 34 35 instance Lambda l => Num (l Int) where 31 instance (Lambda l, LInt l ~ a) => Num (l a) where 36 32 fromInteger = lit . fromIntegral 37 33 lhs + rhs = op lhs PLUS rhs … … 45 41 46 42 instance Lambda LPrint where 43 type LInt LPrint = Int 47 44 lit x = LPrint $ \_ -> return $ shows x 48 45 op m opc n = LPrint $ \p -> do … … 99 96 x = aabs (\s _ -> s) `app` list (lit 0) (labs $ \_ -> undef) 100 97 y = list 0 $ labs $ \_ -> undef 101 z = (labs $ \s -> s+1) `app` (lit 1+2)102 98 conss = labs $ \x -> aabs $ \ys yf -> list (ys + 1) $ shift yf ys (labs $ \_ -> x) 103 99 concats = aabs $ \xs xf -> (aabs $ \ys yf -> list (xs+ys) $ shift xf xs yf) … … 112 108 l12 = list (lit 1) (labs $ \_ -> list (lit 2) (labs $ \_ -> unsized)) 113 109 110 v = (labs $ \s -> s+1) `app` (lit 1+2) 111 w1 = (labs $ \s -> s+ lit 1) 112 w2 = (labs $ \s -> s+ lit 2) 113 q1 :: (Lambda l, a ~ LInt l, Num (l a)) => l (Arr l b (Arr l a a)) 114 q1 = (labs $ \q -> labs $ \s -> s + lit 1) 115 q2 :: (Lambda l, a ~ LInt l, Num (l a)) => l (Arr l a (Arr l a a)) 116 q2 = (labs $ \q -> labs $ \s -> s + q) 117 q3 = (labs $ \q -> labs $ \s -> q + s) 118 114 119 {- Evaluating -} 120 eval = unR 115 121 newtype R a = R { unR :: a } 116 122 instance Lambda R where 117 labs f = R $ f 123 type LInt R = Int 124 labs f = R $ f 118 125 app e1 e2 = (unR e1) e2 119 126 lit i = R i … … 136 143 shift a s c = R $ \i -> if (unR i)<(unR s) then unR a i else unR c i 137 144 138 -- Type level int to count arity 139 data Zero 140 data Succ a 141 142 data Exp a where 143 Lit :: Int -> Exp Zero 144 Op :: Exp Zero -> OpName -> Exp Zero -> Exp Zero 145 App :: Exp (Succ a) -> Exp Zero -> Exp a 146 Var :: Int -> Exp a 147 148 instance Show (Exp a) where 149 showsPrec _ (Lit a) = shows a 150 showsPrec p (Op lhs op rhs) = 151 let (prec, lprec, rprec, c) = getPrec op 152 in showParen (p>prec) $ showsPrec lprec lhs . showChar c . showsPrec rprec rhs 153 showsPrec p (App lhs rhs) = 154 showParen (p>6) $ showsPrec 6 lhs . showChar ' ' . showsPrec 7 rhs 155 showsPrec _ (Var i) = showVar i 156 157 type IntExp = Exp Zero 158 data Condition = Eq IntExp IntExp | Gt IntExp IntExp | Lt IntExp IntExp 159 type SExp a = ([Condition], IntExp) 160 newtype Compile a = Compile { unCompile :: SExp a } 161 162 --instance Lambda Compile where 163 -- labs :: (l a -> l b) -> l (a -> b) 164 -- labs f = Compile $ f 165 166 167 --{- Create a restricted deep embedding to work with the expression -} 168 --{- sized [a] to prove -} 169 -- 170 --class Restricted a where 171 -- kind :: a -> Int 172 -- 173 --instance Restricted Int where 174 -- kind _ = 0 175 -- 176 --instance Restricted a => Restricted (Int -> a) where 177 -- kind _ = succ $ kind $ (undefined :: a) 178 -- 179 --class Var l where 180 -- var :: Restricted a => Int -> l a 181 -- 182 --showVar2 x = showChar '_' . showVar x 183 -- 184 --newtype Count a = C { count :: Int } 185 --cnv :: Restricted a => Count a -> a 186 --cnv = undefined 187 -- 188 --ckind :: (Restricted a) => Count a -> Int 189 --ckind = kind . cnv 190 -- 191 --instance Var Count where 192 -- var _ = undefined 193 -- 194 --instance Var LPrint where 195 -- var x = LPrint $ \_ -> return $ showVar2 x 196 -- 197 --class MGS a where 198 -- mgs :: Supply Int -> a -> a 199 -- mgs = mgs' [] 200 -- mgs' :: [Int] -> Supply Int -> a -> a 201 -- 202 --instance MGS () where 203 -- mgs' _ _ _ = () 204 -- 205 --instance MGS a => MGS (RList a) where 206 -- mgs' bound supp _ = RList (listsize, undefined) 207 -- where 208 -- (s1, s2, s3) = split3 supp 209 -- listsize = mgs' bound supp (undefined :: a) 145 newtype E a = E { unE :: a } 146 instance Lambda E where 147 type LInt E = SBV.SInteger 148 labs f = E $ f 149 app e1 e2 = (unE e1) e2 150 lit = E 151 op e1 op e2 = let opm = case op of 152 PLUS -> (+) 153 MINUS -> (-) 154 MUL -> (*) 155 in E $ opm (unE e1) (unE e2) 156 157 class Sizeable' a where 158 getl' :: E (Arr E a b) -> SBV.Symbolic b 159 160 instance Sizeable' (SBV.SInteger) where 161 getl' f = do 162 x <- SBV.sInteger "x" 163 return $ unE $ (unE f) $ E x 164 165 {- 166 getEq f = SBV.proveWith (SBV.z3 {SBV.verbose=True}) $ do 167 x <- getl f 168 return $ x SBV..== x 169 -} 170 data ST = ST 171 172 class Instantiateable a r | a -> r where 173 instantiate :: a -> SBV.Symbolic r 174 175 instance Instantiateable SBV.SInteger SBV.SInteger where 176 instantiate _ = do 177 liftIO $ putStrLn " INT VARIABLE x" 178 SBV.sInteger "x" 179 180 instance Instantiateable a ST where 181 instantiate _ = do 182 liftIO $ putStrLn " ? VARIABLE x" 183 return $ undefined -- SBV.uninterpret "f" 184 185 class Sizeable a where 186 type Ret a :: * 187 getl :: a -> a -> SBV.Symbolic (Ret a, Ret a) 188 189 instance (Instantiateable a r, Sizeable b) => Sizeable (Arr E a b) where 190 type Ret (Arr E a b) = Ret b 191 getl f g = do 192 liftIO $ putStrLn " -> x" 193 x <- instantiate (undefined :: a) 194 let f' = unE $ f $ E x 195 let g' = unE $ g $ E x 196 getl f' g' 197 198 instance Sizeable (SBV.SInteger) where 199 type Ret SBV.SInteger = SBV.SInteger 200 getl f g = return (f,g) 201 202 getEq f g = SBV.proveWith (SBV.z3 {SBV.verbose=True}) $ do 203 (x,y) <- getl (unE f) (unE g) 204 return $ x SBV..== y -
sizechecking_branches/LL.hs
r10 r15 1 {-# Language GADTs, 2 FlexibleInstances, 3 FlexibleContexts, 4 ScopedTypeVariables, 5 TypeFamilies, 1 {-# Language GADTs, 2 FlexibleInstances, 3 FlexibleContexts, 4 ScopedTypeVariables, 5 TypeFamilies, 6 6 NoMonomorphismRestriction, 7 7 OverlappingInstances #-} 8 module LL where 8 9 9 10 import Data.Char … … 14 15 deriving Show 15 16 16 type family Arr (repr :: * -> *) (a :: *) (b :: *) :: *17 17 18 18 class Lambda l where 19 type Arr l (a :: *) (b :: *) :: * 19 20 labs :: (l a -> l b) -> l (Arr l a b) 20 21 app :: l (Arr l a b) -> l a -> l b … … 49 50 50 51 newtype LPrint a = LPrint { unPrint :: Int -> Int -> ShowS } 51 type instance Arr LPrint a b = a -> b52 52 53 53 instance Lambda LPrint where 54 type Arr LPrint a b = a -> b 54 55 lit x = LPrint $ \_ -> return $ shows x 55 56 op m opc n = LPrint $ \p -> do … … 75 76 {- Evaluating -} 76 77 newtype R a = R { unR :: a } 77 type instance Arr R a b = R a -> R b78 78 79 79 instance Lambda R where 80 type Arr R a b = R a -> R b 80 81 labs = R 81 82 app = unR … … 94 95 eval = unR 95 96 96 z = labs ( \s -> s + 1) `app` ( 1 + lit2)97 z = labs (\s -> s + lit 1) `app` (lit 1 + 2)
Note: See TracChangeset
for help on using the changeset viewer.