Skip to content

Commit

Permalink
Merge pull request #1986 from GaloisInc/T1985
Browse files Browse the repository at this point in the history
Allow type synonyms inside other type synonyms
  • Loading branch information
mergify[bot] committed Dec 5, 2023
2 parents 6ff9711 + 6782359 commit 2adef40
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
12 changes: 12 additions & 0 deletions intTests/test1985/test.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Test top level typedefs
typedef Foo = Int;
typedef Bar = Foo;
let thing : Bar = 2;

// Test local typedefs
let local = do {
typedef LocalFoo = Bar;
typedef LocalBar = LocalFoo;
let local_thing : LocalBar = 3;
return ();
};
3 changes: 3 additions & 0 deletions intTests/test1985/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set -e

$SAW test.saw
18 changes: 12 additions & 6 deletions src/SAWScript/MGU.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Stability : provisional
module SAWScript.MGU
( checkDecl
, checkDeclGroup
, instantiate
) where

import SAWScript.AST
Expand Down Expand Up @@ -264,8 +265,12 @@ bindPatternSchema pat s@(Forall vs t) m =

bindTypedef :: LName -> Type -> TI a -> TI a
bindTypedef n t m =
TI $ local (\ro -> ro { typedefEnv = M.insert (getVal n) t $ typedefEnv ro })
$ unTI m
TI $
local
(\ro ->
let t' = instantiate (typedefEnv ro) t
in ro { typedefEnv = M.insert (getVal n) t' $ typedefEnv ro })
$ unTI m

-- FIXME: This function may miss type variables that occur in the type
-- of a binding that has been shadowed by another value with the same
Expand Down Expand Up @@ -365,7 +370,8 @@ instance AppSubst Decl where
-- Instantiate {{{

class Instantiate t where
instantiate :: [(Name, Type)] -> t -> t
-- | @instantiate m x@ applies the map @m@ to type variables in @x@.
instantiate :: Map Name Type -> t -> t

instance (Instantiate a) => Instantiate (Maybe a) where
instantiate nts = fmap (instantiate nts)
Expand All @@ -377,15 +383,15 @@ instance Instantiate Type where
instantiate nts ty = case ty of
TyCon tc ts -> TyCon tc (instantiate nts ts)
TyRecord fs -> TyRecord (fmap (instantiate nts) fs)
TyVar n -> maybe ty id (lookup n nts)
TyVar n -> M.findWithDefault ty n nts
TyUnifyVar _ -> ty
TySkolemVar _ _ -> ty
LType pos ty' -> LType pos (instantiate nts ty')

instantiateM :: Instantiate t => t -> TI t
instantiateM t = do
s <- TI $ asks typedefEnv
return $ instantiate (M.assocs s) t
return $ instantiate s t

-- }}}

Expand Down Expand Up @@ -484,7 +490,7 @@ inferE (ln, expr) = case expr of
return (Var x, t)
Just (Forall as t) -> do
ts <- mapM (const newType) as
return (Var x, instantiate (zip as ts) t)
return (Var x, instantiate (M.fromList (zip as ts)) t)

Function pat body ->
do (pt, pat') <- newTypePattern pat
Expand Down
4 changes: 3 additions & 1 deletion src/SAWScript/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import qualified Text.LLVM.AST as LLVM (Type)
import qualified Text.LLVM.PP as LLVM (ppType)
import SAWScript.JavaExpr (JavaType(..))
import SAWScript.JavaPretty (prettyClass)
import SAWScript.MGU (instantiate)
import SAWScript.Options (Options(printOutFn),printOutLn,Verbosity(..))
import SAWScript.Proof
import SAWScript.Prover.SolverStats
Expand Down Expand Up @@ -504,7 +505,8 @@ extendLocal :: SS.LName -> Maybe SS.Schema -> Maybe String -> Value -> LocalEnv
extendLocal x mt md v env = LocalLet x mt md v : env

addTypedef :: SS.Name -> SS.Type -> TopLevelRW -> TopLevelRW
addTypedef name ty rw = rw { rwTypedef = M.insert name ty (rwTypedef rw) }
addTypedef name ty rw = rw { rwTypedef = M.insert name ty' (rwTypedef rw) }
where ty' = instantiate (rwTypedef rw) ty

mergeLocalEnv :: SharedContext -> LocalEnv -> TopLevelRW -> IO TopLevelRW
mergeLocalEnv sc env rw = foldrM addBinding rw env
Expand Down

0 comments on commit 2adef40

Please sign in to comment.