Changeset 15 for sizechecking
- Timestamp:
- Nov 16, 2012, 4:26:26 PM (13 years ago)
- Location:
- sizechecking
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sizechecking/Constraints.hs
r14 r15 1 1 {-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances, 2 2 IncoherentInstances, RankNTypes, ScopedTypeVariables, 3 FlexibleContexts,UndecidableInstances #-} 3 FlexibleContexts,UndecidableInstances, DeriveDataTypeable, 4 ImpredicativeTypes #-} 4 5 module Constraints where 5 6 … … 12 13 import Data.SBV ( (.==), (.<), (.>=)) 13 14 import Control.Monad 15 import Control.Monad.IO.Class 14 16 import Data.IORef() 15 17 import Data.Supply as S 18 import Data.Data 19 import Data.IORef 20 import Data.Dynamic 16 21 17 22 asType :: a -> a -> a … … 37 42 appall :: Int -> [L] -> L 38 43 appall var = foldl App (Var var) 39 40 44 41 45 data Condition = Condition [Constraint] L L … … 253 257 254 258 solve1' supp c@(Condition d a b) 255 | checkConds d = do256 putStrLn "Contradiction in preconditions"257 return []259 -- | checkConds d = do 260 -- putStrLn "Contradiction in preconditions" 261 -- return [] 258 262 | a==b = do 259 263 putStrLn "Equals" 260 264 return [] 261 | Just (var, nl) <- searchzero d = do262 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 x267 solve1 supp x268 | Just (var, exp, nl) <- searcheq d [] = do269 putStrLn $ "Found equation " ++ show (Var var) ++ " = " ++ show exp270 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 x274 solve1 supp x265 -- | 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 275 279 | App p q <- a, App r s <- b = do 276 280 putStrLn "Branching!" 277 281 nc <- checkCond supp [Condition d p r, Condition d q s] 278 282 solve nc supp 279 280 283 281 284 -- | Abs _ _ <- a, Abs _ _ <- b = do … … 288 291 -- | Abs _ _ <- a, AAbs _ _ _ <- b = applyList a b d supp 289 292 -- | 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 293 297 | otherwise = do 294 298 putStrLn "Tying to call solver" … … 300 304 otherwise -> return [c] 301 305 306 data LU = LU deriving (Eq, Ord, Data, Typeable) 307 instance SBV.SymWord LU 308 instance SBV.HasKind LU 309 302 310 fvc (Zero a) = fv a 303 311 fvc (LTC a b) = fv a `Set.union` fv b … … 306 314 compiletosolver :: L -> L -> [Constraint] -> SBV.Symbolic SBV.SBool 307 315 compiletosolver 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 326 data VarT = VarI | VarF VarT VarT 327 type VarPool = IORef (Map.Map Int Dynamic) 328 type ExpPool = IORef (Map.Map L SBV.SInteger) 329 330 createVarPool :: IO (VarPool) 331 createVarPool = newIORef $ Map.empty 332 createExpPool :: IO (ExpPool) 333 createExpPool = newIORef $ Map.empty 334 335 336 sbvTc :: TyCon 337 sbvTc = mkTyCon3 "Data" "SBV" "SBV" 338 339 instance (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 346 getVarSymbol :: VarPool -> (SBV.Symbolic Dynamic) -> Int -> SBV.Symbolic Dynamic 347 getVarSymbol 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 357 getExpSymbol :: ExpPool -> (SBV.Symbolic SBV.SInteger) -> L -> SBV.Symbolic SBV.SInteger 358 getExpSymbol 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 369 compilec :: VarPool -> ExpPool -> Supply Int -> Constraint -> SBV.Symbolic (SBV.SBool) 370 compilec v e s (Zero a) = do 371 lhs <- compilel v e s a 372 return $ lhs .== (0::SBV.SInteger) 373 374 compilec 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 380 compilec 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 386 class 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 393 instance VarFactory SBV.SInteger where 394 createVar n _ = SBV.free n 395 396 compilel :: VarPool -> ExpPool -> Supply Int -> L -> SBV.Symbolic (SBV.SInteger) 397 compilel 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 320 402 '+' -> al + bl 321 403 '-' -> al - bl 322 404 '*' -> al * bl 323 405 '/' -> 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 406 compilel 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" 414 compilel v e s (Num a) = return $ SBV.literal $ fromIntegral a 415 compilel v e s (Bottom) = SBV.free_ 416 compilel 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 421 gettype :: ((b -> t) -> t) -> a -> b -> a 422 gettype = error "???" 423 424 {- 425 compileapp :: (VarFactory b, SBV.Uninterpreted (b -> SBV.SInteger)) => VarPool -> L -> ((b -> t) -> t) -> SBV.Symbolic (SBV.SInteger) 426 compileapp 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 -} 435 compileapp v (App x y) t = error $ "Not yet implemented." 436 -
sizechecking/Lambda.hs
r12 r15 13 13 data L = Abs Int L | App L L | Var Int | Num Int | Op L Char L | List L L | AAbs Int Int L 14 14 | Shift L L L | Unsized | Bottom 15 deriving Eq15 deriving (Eq, Ord) 16 16 17 17 showVar x = if x>28
Note: See TracChangeset
for help on using the changeset viewer.