Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Draft version constraint generation #5472

Merged
merged 51 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
040ef56
Draft version constraint generation
Gertjan423 Apr 7, 2020
0205507
Some code cleanup
Gertjan423 Apr 8, 2020
3476a5e
Substitution functions
Gertjan423 Apr 9, 2020
4675fdd
Template generation
Gertjan423 Apr 9, 2020
afc3766
Reformatted
Gertjan423 Apr 9, 2020
c329b37
Attempt to fix failing check
Gertjan423 Apr 9, 2020
f3e4d17
Rework generation for value definitions into 2 phase approach
Gertjan423 Apr 15, 2020
a7ef445
Various small changes
Gertjan423 Apr 16, 2020
c99827d
Rework delta to env state
Gertjan423 Apr 16, 2020
28eb1bb
Incorporate data type definitions
Gertjan423 Apr 17, 2020
80bb27a
Various constraint generation bug fixes and improvements
Gertjan423 Apr 21, 2020
220d21d
First part of constraint solving
Gertjan423 Apr 21, 2020
2bad89e
Minor bugfixes
Gertjan423 Apr 22, 2020
733f929
Added simple-smt to bazel
Gertjan423 Apr 22, 2020
5545943
Depend on z3
cocreature Apr 22, 2020
b736d85
Working pipeline
Gertjan423 Apr 23, 2020
4bc25cb
Fix overwriting value definitions, and delayed choice bindings
Gertjan423 Apr 23, 2020
310d7b0
Keep track of contract id's after fetching
Gertjan423 Apr 24, 2020
4b08356
Renaming of choice parameters
Gertjan423 Apr 24, 2020
e04944f
Cleanup
Gertjan423 Apr 24, 2020
18b0b97
Reals instead of ints
Gertjan423 Apr 27, 2020
2f3bf70
Various TODOs
Gertjan423 Apr 27, 2020
0911a90
Added function documentation
Gertjan423 Apr 27, 2020
6651d8d
Small bugfix
Gertjan423 Apr 29, 2020
a8e6c94
Conditionals and forward references
Gertjan423 Apr 29, 2020
50c56c3
Small missing arithmetic
Gertjan423 May 6, 2020
d880fa5
More small missing arithmetic
Gertjan423 May 6, 2020
aef855a
Fix daml 1.0.1 bug
Gertjan423 May 6, 2020
6c86c22
Read arguments and support fields in different templates
Gertjan423 May 7, 2020
1c21ff2
Proper verify output for testing, generate counter example, proper va…
Gertjan423 May 7, 2020
a33365c
Working testing framework, with some basic test cases
Gertjan423 May 8, 2020
64370c2
Testing: build daml instead of storing dar files
Gertjan423 May 12, 2020
f3bee74
Proper handling of update types; constraint synonyms
Gertjan423 May 11, 2020
13eb8a4
Additional equality constraints after create update
Gertjan423 May 14, 2020
16aa3e3
Make assertion print conditional
Gertjan423 May 14, 2020
642c1b9
Various improvements from PR feedback
Gertjan423 May 14, 2020
c791f31
Apply suggestions from code review
Gertjan423 May 14, 2020
ba1fad5
Small fix: Add missing import
Gertjan423 May 14, 2020
344eca8
Small fix: convert numerics
Gertjan423 May 15, 2020
1cdf6e4
Replace PRSelf with correct package reference
Gertjan423 May 15, 2020
cdbed09
Style fixes
Gertjan423 May 15, 2020
c62c276
Rework conditionals to remove When
Gertjan423 May 19, 2020
ed91ada
Cleanup test daml files
Gertjan423 May 19, 2020
b803eb9
Merge branch 'master' into daml-lf-verification
Gertjan423 May 19, 2020
a3ee542
Small style fixes
Gertjan423 May 19, 2020
91c989c
Bugfix after merge
Gertjan423 May 19, 2020
337b0ca
Attempt to fix bazel windows error
Gertjan423 May 19, 2020
49563b0
Attempt #2 to fix bazel windows error
Gertjan423 May 19, 2020
ae4a276
Attempt #3 to fix bazel windows error
Gertjan423 May 19, 2020
77ea060
Attempt #4 to fix bazel windows error
Gertjan423 May 19, 2020
2d7c840
Attempt #5 to fix bazel windows error
Gertjan423 May 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Working testing framework, with some basic test cases
  • Loading branch information
Gertjan423 committed May 8, 2020
commit a33365cb7621aff9ee6e795304a3a56c9b8189ac
68 changes: 67 additions & 1 deletion compiler/daml-lf-verify/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

load("//bazel_tools:haskell.bzl", "da_haskell_binary")
load("//bazel_tools:haskell.bzl", "da_haskell_binary", "da_haskell_library", "da_haskell_test")

da_haskell_binary(
name = "daml-lf-verify",
Expand Down Expand Up @@ -45,3 +45,69 @@ da_haskell_binary(
"//libs-haskell/da-hs-base",
],
)

da_haskell_library(
name = "daml-lf-verify-lib",
srcs = glob(["src/**/*.hs"]),
data = [
"@z3_nix//:bin/z3",
],
hackage_deps = [
"aeson",
"base",
"bytestring",
"containers",
"deepseq",
"Decimal",
"extra",
"filepath",
"ghc-lib-parser",
"hashable",
"lens",
"mtl",
"optparse-applicative",
"recursion-schemes",
"safe",
"scientific",
"simple-smt",
"template-haskell",
"text",
"time",
"unordered-containers",
"zip-archive",
],
src_strip_prefix = "src",
visibility = ["//visibility:public"],
deps = [
"//compiler/daml-lf-ast",
"//compiler/daml-lf-proto",
"//compiler/daml-lf-reader",
"//compiler/daml-lf-tools",
"//libs-haskell/bazel-runfiles",
"//libs-haskell/da-hs-base",
],
)

da_haskell_test(
name = "verify-tests",
srcs = glob(["tests/**/*.hs"]),
data = glob(["data/*.dar"]),
Gertjan423 marked this conversation as resolved.
Show resolved Hide resolved
hackage_deps = [
"base",
"containers",
"filepath",
"tasty",
"tasty-hunit",
],
main_function = "DA.Daml.LF.Verify.Tests.mainTest",
src_strip_prefix = "tests",
visibility = ["//visibility:public"],
deps = [
":daml-lf-verify-lib",
"//compiler/daml-lf-ast",
"//libs-haskell/bazel-runfiles",
"//libs-haskell/da-hs-base",
"//libs-haskell/test-utils",
],
)

Binary file added compiler/daml-lf-verify/data/conditionals.dar
Binary file not shown.
Binary file added compiler/daml-lf-verify/data/quickstart.dar
Binary file not shown.
35 changes: 20 additions & 15 deletions compiler/daml-lf-verify/src/DA/Daml/LF/Verify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module DA.Daml.LF.Verify
, verify
) where

import Control.Monad (when)
import Data.Text
import Options.Applicative

Expand All @@ -19,22 +20,24 @@ import DA.Daml.LF.Verify.Read
import DA.Daml.LF.Verify.Context
import DA.Bazel.Runfiles

getSolver :: IO FilePath
getSolver = locateRunfiles "z3_nix/bin/z3"

main :: IO ()
main = do
Options{..} <- execParser optionsParserInfo
let choiceTmpl = TypeConName [pack optChoiceTmpl]
choiceName = ChoiceName (pack optChoiceName)
fieldTmpl = TypeConName [pack optFieldTmpl]
fieldName = FieldName (pack optFieldName)
solver <- locateRunfiles "z3_nix/bin/z3"
pkgs <- readPackages [optInputDar]
verify pkgs solver choiceTmpl choiceName fieldTmpl fieldName >>= print
result <- verify optInputDar True choiceTmpl choiceName fieldTmpl fieldName
print result

-- | Execute the full verification pipeline.
verify :: [(PackageId, (Package, Maybe PackageName))]
-- ^ The packages to load.
-> FilePath
-- ^ The solver to use.
verify :: FilePath
-- ^ The DAR file to load.
-> Bool
-- ^ Enable print outs.
-> TypeConName
-- ^ The template in which the given choice is defined.
-> ChoiceName
Expand All @@ -44,27 +47,29 @@ verify :: [(PackageId, (Package, Maybe PackageName))]
-> FieldName
-- ^ The field to be verified.
-> IO Result
verify pkgs solver choiceTmpl choiceName fieldTmpl fieldName = do
putStrLn "Start value gathering"
verify dar verbose choiceTmpl choiceName fieldTmpl fieldName = do
pkgs <- readPackages [dar]
solver <- getSolver
when verbose $ putStrLn "Start value gathering"
case runEnv (genPackages pkgs) (emptyEnv :: Env 'ValueGathering) of
Gertjan423 marked this conversation as resolved.
Show resolved Hide resolved
Left err-> do
putStrLn "Value phase finished with error: "
print err
return Unknown
Right env1 -> do
putStrLn "Start value solving"
when verbose $ putStrLn "Start value solving"
Gertjan423 marked this conversation as resolved.
Show resolved Hide resolved
let env2 = solveValueReferences env1
putStrLn "Start choice gathering"
when verbose $ putStrLn "Start choice gathering"
case runEnv (genPackages pkgs) env2 of
Left err -> do
putStrLn "Choice phase finished with error: "
print err
return Unknown
Right env3 -> do
putStrLn "Start choice solving"
when verbose $ putStrLn "Start choice solving"
let env4 = solveChoiceReferences env3
putStrLn "Start constraint solving phase"
when verbose $ putStrLn "Start constraint solving phase"
let cset = constructConstr env4 choiceTmpl choiceName fieldTmpl fieldName
putStr "Create: " >> print (_cCres cset)
putStr "Archive: " >> print (_cArcs cset)
when verbose $ putStr "Create: " >> print (_cCres cset)
when verbose $ putStr "Archive: " >> print (_cArcs cset)
solveConstr solver cset
6 changes: 3 additions & 3 deletions compiler/daml-lf-verify/src/DA/Daml/LF/Verify/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module DA.Daml.LF.Verify.Generate
import Control.Monad.Error.Class (catchError, throwError)
import Data.Maybe (listToMaybe)
import qualified Data.NameMap as NM
import Debug.Trace

import DA.Daml.LF.Ast hiding (lookupChoice)
import DA.Daml.LF.Verify.Context
Expand Down Expand Up @@ -377,9 +376,10 @@ genForExercise tem ch cid par arg = do
cidOut <- genExpr cid
arout <- genExpr arg
updSubst <- lookupChoice tem ch
-- TODO: Temporary solution
-- TODO: Temporary solution. To be fixed urgently.
this <- lookupCid (_oExpr cidOut) `catchError`
(\_ -> trace ("Not found: " ++ show (_oExpr cidOut)) $ return $ ExprVarName "this")
-- (\_ -> trace ("Not found: " ++ show (_oExpr cidOut)) $ return $ ExprVarName "this")
(\_ -> return $ ExprVarName "this")
-- TODO: Should we further eval after subst? But how to eval an update set?
let updSet = updSubst (_oExpr cidOut) (EVar this) (_oExpr arout)
return (Output (EUpdate (UExercise tem ch (_oExpr cidOut) par (_oExpr arout))) updSet)
Expand Down
1 change: 1 addition & 0 deletions compiler/daml-lf-verify/src/DA/Daml/LF/Verify/Solve.hs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ data Result
-- provided.
| Unknown
-- ^ The result is inconclusive.
deriving Eq

instance Show Result where
show Success = "Success!"
Expand Down
97 changes: 97 additions & 0 deletions compiler/daml-lf-verify/tests/DA/Daml/LF/Verify/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
-- Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0

module DA.Daml.LF.Verify.Tests
( mainTest
) where

import System.FilePath
import Test.Tasty
import Test.Tasty.HUnit

import DA.Daml.LF.Ast.Base
import DA.Daml.LF.Verify
import DA.Daml.LF.Verify.Solve
import DA.Bazel.Runfiles

-- TODO: Build the dar files from within bazel, instead of passing them in manually.

mainTest :: IO ()
mainTest = defaultMain $ testGroup "DA.Daml.LF.Verify"
[ quickstartTests
, conditionalTests
]

quickstartTests :: TestTree
quickstartTests = testGroup "Quickstart"
[ testCase "Iou_Split" $ do
quickstartDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/quickstart.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "Iou_Split"
field = FieldName "amount"
result <- verify quickstartDar False tmpl choice tmpl field
assertEqual "Verification failed for Iou_Split - amount"
Success result
, testCase "Iou_Merge" $ do
quickstartDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/quickstart.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "Iou_Merge"
field = FieldName "amount"
result <- verify quickstartDar False tmpl choice tmpl field
assertEqual "Verification failed for Iou_Merge - amount"
Success result
]

conditionalTests :: TestTree
conditionalTests = testGroup "Conditionals"
[ testCase "Success A" $ do
condDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/conditionals.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "SuccA"
field = FieldName "content"
result <- verify condDar False tmpl choice tmpl field
assertEqual "Verification failed for SuccA - content"
Success result
, testCase "Success B" $ do
condDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/conditionals.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "SuccB"
field = FieldName "content"
result <- verify condDar False tmpl choice tmpl field
assertEqual "Verification failed for SuccB - content"
Success result
, testCase "Success C" $ do
condDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/conditionals.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "SuccC"
field = FieldName "content"
result <- verify condDar False tmpl choice tmpl field
assertEqual "Verification failed for SuccC - content"
Success result
, testCase "Success D" $ do
condDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/conditionals.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "SuccD"
field = FieldName "content"
result <- verify condDar False tmpl choice tmpl field
assertEqual "Verification failed for SuccD - content"
Success result
, testCase "Fail A" $ do
condDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/conditionals.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "FailA"
field = FieldName "content"
verify condDar False tmpl choice tmpl field >>= \case
Success -> assertFailure "Verification wrongfully passed for FailA - content"
Unknown -> assertFailure "Verification inconclusive for FailA - content"
Fail _ -> return ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ar the results not deterministic or why are you not just using assertEqual on the full Fail expression?

, testCase "Fail B" $ do
condDar <- locateRunfiles (mainWorkspace </> "compiler/daml-lf-verify/data/conditionals.dar")
let tmpl = TypeConName ["Iou"]
choice = ChoiceName "FailB"
field = FieldName "content"
verify condDar False tmpl choice tmpl field >>= \case
Success -> assertFailure "Verification wrongfully passed for FailB - content"
Unknown -> assertFailure "Verification inconclusive for FailB - content"
Fail _ -> return ()
]