Changeset 15 for sizechecking


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

new solver

Location:
sizechecking
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sizechecking/Constraints.hs

    r14 r15  
    11{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances, 
    22             IncoherentInstances, RankNTypes, ScopedTypeVariables, 
    3              FlexibleContexts,UndecidableInstances #-} 
     3             FlexibleContexts,UndecidableInstances, DeriveDataTypeable, 
     4             ImpredicativeTypes #-} 
    45module Constraints where 
    56 
     
    1213import Data.SBV ( (.==), (.<), (.>=)) 
    1314import Control.Monad 
     15import Control.Monad.IO.Class 
    1416import Data.IORef() 
    1517import Data.Supply as S 
     18import Data.Data 
     19import Data.IORef 
     20import Data.Dynamic 
    1621 
    1722asType :: a -> a -> a 
     
    3742appall :: Int -> [L] -> L 
    3843appall var = foldl App (Var var) 
    39  
    4044 
    4145data Condition = Condition [Constraint] L L 
     
    253257 
    254258    solve1' supp c@(Condition d a b) 
    255         | checkConds d = do 
    256             putStrLn "Contradiction in preconditions" 
    257             return [] 
     259--        | checkConds d = do 
     260--            putStrLn "Contradiction in preconditions" 
     261--            return [] 
    258262        | a==b = do 
    259263            putStrLn "Equals" 
    260264            return [] 
    261         | Just (var, nl) <- searchzero d = do 
    262             let x = Condition (Prelude.map (normalizec.substc var (Num 0)) nl) 
    263                         (normalize$subst var (Num 0) a) 
    264                         (normalize$subst var (Num 0) b) 
    265  
    266             putStrLn $ show (Var var) ++ " is zero:\n" ++ show x 
    267             solve1 supp x  
    268         | Just (var, exp, nl) <- searcheq d [] = do 
    269             putStrLn $ "Found equation " ++ show (Var var) ++ " = " ++ show exp 
    270             let x = Condition (Prelude.map (normalizec.substc var exp) nl ) 
    271                         (normalize$subst var exp a) 
    272                         (normalize$subst var exp b) 
    273             putStrLn $ "New equations:\n" ++ show x 
    274             solve1 supp x  
     265--        | Just (var, nl) <- searchzero d = do 
     266--            let x = Condition (Prelude.map (normalizec.substc var (Num 0)) nl) 
     267--                        (normalize$subst var (Num 0) a) 
     268--                        (normalize$subst var (Num 0) b) 
     269-- 
     270--            putStrLn $ show (Var var) ++ " is zero:\n" ++ show x 
     271--            solve1 supp x  
     272--        | Just (var, exp, nl) <- searcheq d [] = do 
     273--            putStrLn $ "Found equation " ++ show (Var var) ++ " = " ++ show exp 
     274--            let x = Condition (Prelude.map (normalizec.substc var exp) nl ) 
     275--                        (normalize$subst var exp a) 
     276--                        (normalize$subst var exp b) 
     277--            putStrLn $ "New equations:\n" ++ show x 
     278--            solve1 supp x  
    275279        | App p q <- a, App r s <- b = do 
    276280            putStrLn "Branching!" 
    277281            nc <- checkCond supp [Condition d p r, Condition d q s]  
    278282            solve nc supp 
    279  
    280283 
    281284--        | Abs _ _ <- a, Abs _ _ <- b = do 
     
    288291--        | Abs  _ _   <- a, AAbs _ _ _ <- b = applyList a b d supp 
    289292--        | AAbs _ _ _ <- a, AAbs _ _ _ <- b = applyList a b d supp 
    290         | Just dd <- reorder d = do 
    291             putStrLn $ "Reorder " ++ show dd 
    292             solve1 supp $ Condition dd a b 
     293 
     294--        | Just dd <- reorder d = do 
     295--            putStrLn $ "Reorder " ++ show dd 
     296--            solve1 supp $ Condition dd a b 
    293297        | otherwise = do 
    294298            putStrLn "Tying to call solver" 
     
    300304                otherwise -> return [c] 
    301305 
     306data LU = LU deriving (Eq, Ord, Data, Typeable) 
     307instance SBV.SymWord LU 
     308instance SBV.HasKind LU 
     309 
    302310fvc (Zero a)  = fv a 
    303311fvc (LTC a b) = fv a `Set.union` fv b 
     
    306314compiletosolver :: L -> L -> [Constraint] -> SBV.Symbolic SBV.SBool 
    307315compiletosolver a b d = do 
    308     let fvs = Set.toList $ Set.unions $ fv a : fv b : map fvc d 
    309     (vars::[SBV.SInteger]) <- mapM (SBV.free . flip showVar "") fvs 
    310     let varmap = Map.fromList $ zip fvs vars 
    311     mapM_ (SBV.constrain . compilec varmap) d 
    312     return $ compilel varmap a .== compilel varmap b 
    313  
    314 compilec v (Zero a)  = compilel v a .== (0::SBV.SInteger) 
    315 compilec v (LTC a b) = compilel v a .<  compilel v b 
    316 compilec v (GEC a b) = compilel v a .>= compilel v b 
    317  
    318 compilel :: Map.Map Int SBV.SInteger -> L -> SBV.SInteger 
    319 compilel v (Op a c b) = case c of  
     316    varmap <- liftIO createVarPool 
     317    expmap <- liftIO createExpPool 
     318    supply  <- liftIO $ newNumSupply 
     319    let (s1,s2,s3,s4) = split4 supply 
     320    cs <- mapM (\(s,x) -> compilec varmap expmap s x) $ zip (split s1) d 
     321    mapM_ (SBV.constrain) cs 
     322    lhs <- compilel varmap expmap s3 a 
     323    rhs <- compilel varmap expmap s4 b 
     324    return $ lhs .== rhs 
     325 
     326data VarT = VarI | VarF VarT VarT 
     327type VarPool = IORef (Map.Map Int Dynamic) 
     328type ExpPool = IORef (Map.Map L SBV.SInteger) 
     329 
     330createVarPool :: IO (VarPool) 
     331createVarPool = newIORef $ Map.empty 
     332createExpPool :: IO (ExpPool) 
     333createExpPool = newIORef $ Map.empty 
     334 
     335 
     336sbvTc :: TyCon 
     337sbvTc = mkTyCon3 "Data" "SBV" "SBV" 
     338 
     339instance (Typeable t) => Typeable (SBV.SBV t) where 
     340  typeOf x = mkTyConApp sbvTc [typeOf (get x)] 
     341    where 
     342      get :: SBV.SBV a -> a 
     343      get = undefined 
     344 
     345 
     346getVarSymbol :: VarPool -> (SBV.Symbolic Dynamic) -> Int -> SBV.Symbolic Dynamic 
     347getVarSymbol pool factory a = do 
     348  v <- liftIO $ readIORef pool 
     349  case Map.lookup a v of 
     350    Just q -> return $ q 
     351    Nothing -> do 
     352      dx <- factory 
     353      let newmap = Map.insert a dx v 
     354      liftIO $ writeIORef pool newmap 
     355      return dx 
     356 
     357getExpSymbol :: ExpPool -> (SBV.Symbolic SBV.SInteger) -> L -> SBV.Symbolic SBV.SInteger 
     358getExpSymbol pool factory a = do 
     359  v <- liftIO $ readIORef pool 
     360  case Map.lookup a v of 
     361    Just q -> return $ q 
     362    Nothing -> do 
     363      dx <- factory 
     364      let newmap = Map.insert a dx v 
     365      liftIO $ writeIORef pool newmap 
     366      return dx 
     367 
     368 
     369compilec :: VarPool -> ExpPool -> Supply Int -> Constraint -> SBV.Symbolic (SBV.SBool) 
     370compilec v e s (Zero a)  = do 
     371  lhs <- compilel v e s a 
     372  return $ lhs .== (0::SBV.SInteger) 
     373 
     374compilec v e s (LTC a b) = do 
     375  let (s1,s2) = split2 s  
     376  lhs <- compilel v e s1 a 
     377  rhs <- compilel v e s2 b 
     378  return $ lhs .< rhs 
     379 
     380compilec v e s (GEC a b) = do 
     381  let (s1,s2) = split2 s  
     382  lhs <- compilel v e s1 a 
     383  rhs <- compilel v e s2 b 
     384  return $ lhs .>= rhs 
     385 
     386class Typeable a => VarFactory a where 
     387  createDyn :: String -> a -> SBV.Symbolic Dynamic 
     388  createDyn a n = do 
     389    x <- createVar a n 
     390    return $ toDyn x 
     391  createVar :: String -> a -> SBV.Symbolic a 
     392 
     393instance VarFactory SBV.SInteger where 
     394  createVar n _ = SBV.free n 
     395 
     396compilel :: VarPool -> ExpPool -> Supply Int -> L -> SBV.Symbolic (SBV.SInteger) 
     397compilel v e s (Op a c b) = do 
     398  let (s1, s2) = split2 s 
     399  al <- compilel v e s1 a 
     400  bl <- compilel v e s1 b 
     401  return $ case c of 
    320402    '+' -> al + bl 
    321403    '-' -> al - bl 
    322404    '*' -> al * bl 
    323405    '/' -> al `SBV.sDiv` bl 
    324     where 
    325     al = compilel v a 
    326     bl = compilel v b 
    327 compilel v (Var a) | Just x<-Map.lookup a v = x 
    328 compilel v (Num a) = SBV.literal $ fromIntegral a 
    329 compilel v a = error $ "Cannot compile " ++ show a 
     406compilel v e s (Var a)   = do 
     407  let itype = (undefined ::SBV.SInteger) 
     408  sym <- getVarSymbol v (createDyn (showVar a "") itype) a 
     409  let var = (fromDynamic sym) :: Maybe SBV.SInteger 
     410  case var of 
     411      Just x -> return x 
     412      Nothing -> do 
     413        error "Type Error" 
     414compilel v e s (Num a)   = return $ SBV.literal $ fromIntegral a 
     415compilel v e s (Bottom)  = SBV.free_ 
     416compilel v e s l = do 
     417    sym <- getExpSymbol e (return $ SBV.uninterpret ("unint" ++ showVar (supplyValue s) "") ) l 
     418    return sym 
     419--compilel v e x         = error $  "Cannot compile "++ show x 
     420 
     421gettype :: ((b -> t) -> t) -> a -> b -> a 
     422gettype = error "???" 
     423 
     424{- 
     425compileapp :: (VarFactory b, SBV.Uninterpreted (b -> SBV.SInteger)) => VarPool -> L -> ((b -> t) -> t) -> SBV.Symbolic (SBV.SInteger) 
     426compileapp v (Var a) f = do 
     427  let itype = (error :: SBV.SInteger) 
     428  sym <- getVarSymbol v (createDyn (showVar a "") (gettype f itype)) a 
     429  let var = (fromDynamic sym) :: Maybe SBV.SInteger 
     430  case var of 
     431      Just x -> return x 
     432      Nothing -> do 
     433        error "Type Error" 
     434-} 
     435compileapp v (App x y) t = error $ "Not yet implemented." 
     436 
  • sizechecking/Lambda.hs

    r12 r15  
    1313data L = Abs Int L | App L L | Var Int | Num Int | Op L Char L | List L L | AAbs Int Int L 
    1414    | Shift L L L | Unsized | Bottom  
    15     deriving Eq 
     15    deriving (Eq, Ord) 
    1616 
    1717showVar x = if x>28  
Note: See TracChangeset for help on using the changeset viewer.