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

new solver

File:
1 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 #-} 
    22module L where 
    33 
     4import qualified Data.SBV as SBV 
    45import Data.Char(ord, chr) 
    56import Control.Monad.State 
     
    1314type Arr repr a b = repr a -> repr b 
    1415 
    15 class Lambda l where 
     16class (Num (LInt l)) => Lambda l where 
     17    type LInt l :: * 
    1618    labs    :: (l a -> l b) -> l (Arr l a b) 
    1719    app     :: l (Arr l a b) -> l a -> l b 
    18     lit     :: Int -> l Int 
    19     op      :: l Int -> OpName -> l Int -> l Int 
     20    lit     :: (LInt l) -> l (LInt l) 
     21    op      :: l (LInt l) -> OpName -> l (LInt l) -> l (LInt l) 
    2022 
    2123class SizeExp l where 
     
    2729    unsized :: l () 
    2830 
    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 
     31instance (Lambda l, LInt l ~ a) => Num (l a) where 
    3632    fromInteger = lit . fromIntegral 
    3733    lhs + rhs   = op lhs PLUS rhs 
     
    4541 
    4642instance Lambda LPrint where 
     43  type LInt LPrint = Int 
    4744  lit x      = LPrint $ \_ -> return $ shows x 
    4845  op m opc n = LPrint $ \p -> do 
     
    9996x = aabs (\s _ -> s) `app` list (lit 0) (labs $ \_ -> undef) 
    10097y = list 0 $ labs $ \_ -> undef 
    101 z = (labs $ \s -> s+1) `app` (lit 1+2) 
    10298conss = labs $ \x -> aabs $ \ys yf -> list (ys + 1) $ shift yf ys (labs $ \_ -> x) 
    10399concats = aabs $ \xs xf -> (aabs $ \ys yf -> list (xs+ys) $ shift xf xs yf) 
     
    112108l12 = list (lit 1) (labs $ \_ -> list (lit 2) (labs $ \_ -> unsized)) 
    113109 
     110v = (labs $ \s -> s+1) `app` (lit 1+2) 
     111w1 = (labs $ \s -> s+ lit 1) 
     112w2 = (labs $ \s -> s+ lit 2) 
     113q1 :: (Lambda l, a ~ LInt l, Num (l a)) => l (Arr l b (Arr l a a)) 
     114q1 = (labs $ \q -> labs $ \s -> s + lit 1) 
     115q2 :: (Lambda l, a ~ LInt l, Num (l a)) => l (Arr l a (Arr l a a)) 
     116q2 = (labs $ \q -> labs $ \s -> s + q) 
     117q3 = (labs $ \q -> labs $ \s -> q + s) 
     118 
    114119{- Evaluating -} 
     120eval = unR 
    115121newtype R a = R { unR :: a } 
    116122instance Lambda R where 
    117   labs f      = R $ f  
     123  type LInt R = Int 
     124  labs f      = R $ f 
    118125  app e1 e2   = (unR e1) e2 
    119126  lit i       = R i 
     
    136143  shift a s c = R $ \i -> if (unR i)<(unR s) then unR a i else unR c i 
    137144 
    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) 
     145newtype E a = E { unE :: a } 
     146instance 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 
     157class Sizeable' a where 
     158  getl' :: E (Arr E a b) -> SBV.Symbolic b 
     159 
     160instance Sizeable' (SBV.SInteger) where 
     161  getl' f = do 
     162    x <- SBV.sInteger "x" 
     163    return $ unE $ (unE f) $ E x 
     164 
     165{- 
     166getEq f = SBV.proveWith (SBV.z3 {SBV.verbose=True}) $ do 
     167  x <- getl f 
     168  return $ x SBV..== x 
     169-} 
     170data ST  = ST 
     171 
     172class Instantiateable a r | a -> r where 
     173  instantiate :: a -> SBV.Symbolic r 
     174 
     175instance Instantiateable SBV.SInteger SBV.SInteger where 
     176  instantiate _ = do 
     177    liftIO $ putStrLn " INT VARIABLE x" 
     178    SBV.sInteger "x" 
     179 
     180instance Instantiateable a ST where 
     181  instantiate _ = do 
     182    liftIO $ putStrLn " ? VARIABLE x" 
     183    return $ undefined -- SBV.uninterpret "f" 
     184 
     185class Sizeable a where 
     186  type Ret a :: * 
     187  getl :: a -> a -> SBV.Symbolic (Ret a, Ret a) 
     188 
     189instance (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 
     198instance Sizeable (SBV.SInteger) where 
     199  type Ret SBV.SInteger = SBV.SInteger 
     200  getl f g = return (f,g) 
     201 
     202getEq f g = SBV.proveWith (SBV.z3 {SBV.verbose=True}) $ do 
     203  (x,y) <- getl (unE f) (unE g) 
     204  return $ x SBV..== y 
Note: See TracChangeset for help on using the changeset viewer.