Skip to content
Snippets Groups Projects
Verified Commit d95ed55e authored by Ogilvie, D.H. (Duncan)'s avatar Ogilvie, D.H. (Duncan)
Browse files

implement array length operator

parent 56f44b11
No related branches found
No related tags found
No related merge requests found
...@@ -44,10 +44,10 @@ prettyNUnop NNeg = "-" ...@@ -44,10 +44,10 @@ prettyNUnop NNeg = "-"
prettyNUnop NNot = "~" prettyNUnop NNot = "~"
prettyVar :: Var -> String prettyVar :: Var -> String
prettyVar (Var t n) = '(' : prettyType t ++ ")" ++ n prettyVar (Var t n) = prettyType t ++ ":" ++ n
prettyLExprAlgebra :: LExprAlgebra String prettyLExprAlgebra :: LExprAlgebra String
prettyLExprAlgebra = (flConst, prettyVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, prettyVar, fnUnop, fnBinop, fnArray, fnIf) where prettyLExprAlgebra = (flConst, prettyVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, prettyVar, fnUnop, fnBinop, fnArray, fnIf, fnLen) where
flConst b = if b then "true" else "false" flConst b = if b then "true" else "false"
flNot a = '!' : a flNot a = '!' : a
flBinop a o b = a ++ " " ++ prettyLBinop o ++ " " ++ b flBinop a o b = a ++ " " ++ prettyLBinop o ++ " " ++ b
...@@ -58,4 +58,5 @@ prettyLExprAlgebra = (flConst, prettyVar, flNot, flBinop, flComp, flQuant, flArr ...@@ -58,4 +58,5 @@ prettyLExprAlgebra = (flConst, prettyVar, flNot, flBinop, flComp, flQuant, flArr
fnUnop o a = prettyNUnop o ++ a fnUnop o a = prettyNUnop o ++ a
fnBinop a o b = a ++ " " ++ prettyNBinop o ++ " " ++ b fnBinop a o b = a ++ " " ++ prettyNBinop o ++ " " ++ b
fnArray = flArray fnArray = flArray
fnIf c a b = "(" ++ c ++ ") ? (" ++ a ++ ") : (" ++ b ++ ")" fnIf c a b = "(" ++ c ++ ") ? (" ++ a ++ ") : (" ++ b ++ ")"
\ No newline at end of file fnLen v = "len(" ++ prettyVar v ++ ")"
\ No newline at end of file
...@@ -9,7 +9,7 @@ lExprToZ3Ast :: LExpr -> Z3 AST ...@@ -9,7 +9,7 @@ lExprToZ3Ast :: LExpr -> Z3 AST
lExprToZ3Ast = foldLExpr lExprToZ3AstAlgebra lExprToZ3Ast = foldLExpr lExprToZ3AstAlgebra
lExprToZ3AstAlgebra :: LExprAlgebra (Z3 AST) lExprToZ3AstAlgebra :: LExprAlgebra (Z3 AST)
lExprToZ3AstAlgebra = (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, fnVar, fnUnop, fnBinop, fnArray, fnIf) where lExprToZ3AstAlgebra = (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, fnVar, fnUnop, fnBinop, fnArray, fnIf, fnLen) where
flConst = undefined flConst = undefined
flVar = undefined flVar = undefined
flNot = undefined flNot = undefined
...@@ -22,4 +22,5 @@ lExprToZ3AstAlgebra = (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray, ...@@ -22,4 +22,5 @@ lExprToZ3AstAlgebra = (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray,
fnUnop = undefined fnUnop = undefined
fnBinop = undefined fnBinop = undefined
fnArray = undefined fnArray = undefined
fnIf = undefined fnIf = undefined
\ No newline at end of file fnLen = undefined
\ No newline at end of file
...@@ -57,7 +57,7 @@ data COp = CEqual -- a == b ...@@ -57,7 +57,7 @@ data COp = CEqual -- a == b
type NExpr = LExpr type NExpr = LExpr
-- Logical expressions -- Logical expressions (TODO: clean up duplicates)
data LExpr = LConst Bool -- True/False data LExpr = LConst Bool -- True/False
| LVar Var -- Variable | LVar Var -- Variable
| LNot LExpr -- Logical negation/not | LNot LExpr -- Logical negation/not
...@@ -72,4 +72,5 @@ data LExpr = LConst Bool -- True/False ...@@ -72,4 +72,5 @@ data LExpr = LConst Bool -- True/False
| NBinop NExpr NBinop NExpr -- Binary operators | NBinop NExpr NBinop NExpr -- Binary operators
| NArray Var [NExpr] -- Integer array access | NArray Var [NExpr] -- Integer array access
| NIf LExpr NExpr NExpr -- if c then a else b | NIf LExpr NExpr NExpr -- if c then a else b
| NLen Var -- len(array)
deriving (Show, Eq, Read) deriving (Show, Eq, Read)
\ No newline at end of file
...@@ -15,12 +15,13 @@ type LExprAlgebra r = (Bool -> r, -- LConst ...@@ -15,12 +15,13 @@ type LExprAlgebra r = (Bool -> r, -- LConst
NUnop -> r -> r, -- NUnop NUnop -> r -> r, -- NUnop
r -> NBinop -> r -> r, -- NBinop r -> NBinop -> r -> r, -- NBinop
Var -> [NExpr] -> r, -- NArray Var -> [NExpr] -> r, -- NArray
r -> r -> r -> r -- NIf r -> r -> r -> r, -- NIf
Var -> r -- NLen
) )
-- Fold for logical expressions -- Fold for logical expressions
foldLExpr :: LExprAlgebra r -> LExpr -> r foldLExpr :: LExprAlgebra r -> LExpr -> r
foldLExpr (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, fnVar, fnUnop, fnBinop, fnArray, fnIf) = fold where foldLExpr (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, fnVar, fnUnop, fnBinop, fnArray, fnIf, fnLen) = fold where
fold e = case e of fold e = case e of
LConst c -> flConst c LConst c -> flConst c
LVar n -> flVar n LVar n -> flVar n
...@@ -34,4 +35,5 @@ foldLExpr (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, fn ...@@ -34,4 +35,5 @@ foldLExpr (flConst, flVar, flNot, flBinop, flComp, flQuant, flArray, fnConst, fn
NUnop o e -> fnUnop o (fold e) NUnop o e -> fnUnop o (fold e)
NBinop a o b -> fnBinop (fold a) o (fold b) NBinop a o b -> fnBinop (fold a) o (fold b)
NArray n e -> fnArray n e NArray n e -> fnArray n e
NIf c a b -> fnIf (fold c) (fold a) (fold b) NIf c a b -> fnIf (fold c) (fold a) (fold b)
\ No newline at end of file NLen v -> fnLen v
\ No newline at end of file
...@@ -39,12 +39,17 @@ javaExpToLExprAlgebra = (fLit, fClassLit, fThis, fThisClass, fInstanceCreation, ...@@ -39,12 +39,17 @@ javaExpToLExprAlgebra = (fLit, fClassLit, fThis, fThisClass, fInstanceCreation,
PrimType IntT -> NArray (Var (TArray (TPrim PInt)) (prettyPrint name)) [NVar (Var (TPrim PInt) (prettyPrint index))] PrimType IntT -> NArray (Var (TArray (TPrim PInt)) (prettyPrint name)) [NVar (Var (TPrim PInt) (prettyPrint index))]
_ -> error $ show (arrayIndex, indexType) _ -> error $ show (arrayIndex, indexType)
_ -> error $ show (arrayIndex, arrayType) _ -> error $ show (arrayIndex, arrayType)
fExpName name env decls = let symbol = prettyPrint name in let t = lookupType decls env name in fExpName name env decls = case name of -- TODO: better type checking + multiple dimension arrays + better abstractions
-- If we're not dealing with library methods, we should be able to get the type from the type environment Name [Ident a, Ident "length"] -> let arrayType = lookupType decls env (Name [Ident a]) in
case t of case arrayType of
PrimType BooleanT -> LVar (Var (TPrim PBool) symbol) (RefType (ArrayType (PrimType IntT))) -> NLen (Var (TArray (TPrim PInt)) a)
PrimType IntT -> NVar (Var (TPrim PInt) symbol) _ -> error $ show (name, arrayType)
t -> error ("Verifier: Type of " ++ prettyPrint name ++ " unknown or not implemented: " ++ show t) _ -> let symbol = prettyPrint name in let t = lookupType decls env name in
-- If we're not dealing with library methods, we should be able to get the type from the type environment
case t of
PrimType BooleanT -> LVar (Var (TPrim PBool) symbol)
PrimType IntT -> NVar (Var (TPrim PInt) symbol)
t -> error ("Verifier: Type of " ++ prettyPrint name ++ " unknown or not implemented: " ++ show t)
fPostIncrement = error "fPostIncrement has side effects..." fPostIncrement = error "fPostIncrement has side effects..."
fPostDecrement = error "fPostDecrement has side effects..." fPostDecrement = error "fPostDecrement has side effects..."
fPreIncrement = error "fPreIncrement has side effects..." fPreIncrement = error "fPreIncrement has side effects..."
......
...@@ -149,4 +149,4 @@ edslSrc = "javawlp_edsl/src/nl/uu/javawlp_edsl/Main.java" ...@@ -149,4 +149,4 @@ edslSrc = "javawlp_edsl/src/nl/uu/javawlp_edsl/Main.java"
testEq = compareSpec (edslSrc, "swap_spec1") (edslSrc, "swap_spec1") testEq = compareSpec (edslSrc, "swap_spec1") (edslSrc, "swap_spec1")
testNeq = compareSpec (edslSrc, "swap_spec1") (edslSrc, "swap_spec2") testNeq = compareSpec (edslSrc, "swap_spec1") (edslSrc, "swap_spec2")
blub = compareSpec (edslSrc, "simple1") (edslSrc, "simple1") blub = compareSpec (edslSrc, "simple2") (edslSrc, "simple2")
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment