-
Notifications
You must be signed in to change notification settings - Fork 44
Validate functions #2585
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
Merged
Merged
Validate functions #2585
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
148782e
Replay "Validate function definitions (#2168)"
ttuegel 619f6f3
Trim source paths from regression tests
ttuegel 73a6f3d
Replay "Issue 2100 follow up (#2574)"
ttuegel 07b614f
Added another case for \or patterns
MirceaS 1d90960
Merge branch 'master' into validate-functions
MirceaS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
{- | | ||
Copyright : (c) Runtime Verification, 2021 | ||
License : NCSA | ||
-} | ||
module Kore.Equation.Validate ( | ||
validateAxiom, | ||
) where | ||
|
||
import Control.Monad.State.Strict ( | ||
StateT, | ||
) | ||
import qualified Data.Functor.Foldable as Recursive | ||
import Data.Text ( | ||
pack, | ||
) | ||
import Kore.AST.Error | ||
import Kore.ASTVerifier.Verifier | ||
import Kore.Attribute.Axiom ( | ||
Assoc (..), | ||
Comm (..), | ||
Idem (..), | ||
Overload (..), | ||
Unit (..), | ||
) | ||
import qualified Kore.Attribute.Axiom as Attribute ( | ||
Axiom (..), | ||
) | ||
import qualified Kore.Builtin.List.List as List | ||
import qualified Kore.Builtin.Map.Map as Map | ||
import qualified Kore.Builtin.Set.Set as Set | ||
import Kore.Equation.Equation ( | ||
Equation (..), | ||
isSimplificationRule, | ||
) | ||
import Kore.Equation.Sentence ( | ||
MatchEquationError (..), | ||
fromSentenceAxiom, | ||
) | ||
import Kore.Internal.Predicate ( | ||
pattern PredicateCeil, | ||
pattern PredicateIn, | ||
) | ||
import qualified Kore.Internal.Predicate as Predicate | ||
import qualified Kore.Internal.Symbol as Symbol | ||
import qualified Kore.Internal.TermLike as TermLike | ||
import Kore.Syntax.Definition | ||
import Kore.Syntax.Variable | ||
import Kore.Unparser ( | ||
unparse, | ||
) | ||
import qualified Kore.Verified as Verified | ||
import Prelude.Kore | ||
import qualified Pretty | ||
|
||
validateAxiom :: | ||
Attribute.Axiom TermLike.Symbol VariableName -> | ||
Verified.SentenceAxiom -> | ||
StateT VerifiedModule' Verifier () | ||
validateAxiom attrs verified = | ||
case fromSentenceAxiom (attrs, verified) of | ||
Right eq@Equation{left, argument} -> | ||
when (needsVerification eq) $ | ||
checkLHS eq left >> checkArg eq argument | ||
Left err@(RequiresError _) -> failWithBadEquation err | ||
Left err@(ArgumentError _) -> failWithBadEquation err | ||
Left err@(AntiLeftError _) -> failWithBadEquation err | ||
Left err@(EnsuresError _) -> failWithBadEquation err | ||
Left (NotEquation _) -> return () | ||
Left FunctionalAxiom -> return () | ||
Left ConstructorAxiom -> return () | ||
Left SubsortAxiom -> return () | ||
where | ||
failWithBadEquation = | ||
koreFailWithLocations [sentenceAxiomPattern verified] | ||
. pack | ||
. show | ||
. Pretty.pretty | ||
|
||
needsVerification eq@Equation{attributes} = | ||
not | ||
( isSimplificationRule eq | ||
|| isAssoc | ||
|| isComm | ||
|| isUnit | ||
|| isIdem | ||
|| isJust getOverload | ||
) | ||
where | ||
Assoc{isAssoc} = Attribute.assoc attributes | ||
Comm{isComm} = Attribute.comm attributes | ||
Unit{isUnit} = Attribute.unit attributes | ||
Idem{isIdem} = Attribute.idem attributes | ||
Overload{getOverload} = Attribute.overload attributes | ||
|
||
checkLHS eq termLike = do | ||
checkAllowedFunctionSymbol | ||
checkVarFunctionArguments | ||
where | ||
_ :< termLikeF = Recursive.project termLike | ||
|
||
checkAllowedFunctionSymbol | ||
| TermLike.App_ sym _ <- termLike = | ||
unless | ||
(isAllowedFunctionSymbol sym) | ||
(throwIllegalFunctionSymbol sym) | ||
| otherwise = return () | ||
where | ||
isAllowedFunctionSymbol sym = | ||
Symbol.isFunction sym && not (Symbol.isConstructorLike sym) | ||
|
||
throwIllegalFunctionSymbol sym = | ||
koreFailWithLocations [eq] $ | ||
pack $ | ||
show $ | ||
Pretty.vsep | ||
[ "Expected function symbol, but found constructor symbol:" | ||
, unparse sym | ||
] | ||
|
||
checkVarFunctionArguments = | ||
failOnJust | ||
eq | ||
"Expected variable, but found:" | ||
$ asum $ getNotVar <$> termLikeF | ||
|
||
getNotVar (TermLike.Var_ _) = Nothing | ||
getNotVar term = Just term | ||
|
||
checkArg _ Nothing = return () | ||
checkArg eq (Just arg) = | ||
traverse_ | ||
( failOnJust eq "Found invalid subterm in argument of function equation:" | ||
. checkArgIn | ||
) | ||
$ Predicate.getMultiAndPredicate arg | ||
where | ||
checkArgIn (PredicateIn (TermLike.Var_ _) term) = | ||
findBadArgSubterm term | ||
checkArgIn (PredicateCeil (TermLike.And_ _ (TermLike.Var_ _) term)) = | ||
findBadArgSubterm term | ||
checkArgIn badArg = Just $ Predicate.fromPredicate_ badArg | ||
|
||
findBadArgSubterm term = case term of | ||
_ | ||
| TermLike.isConstructorLike term -> descend | ||
TermLike.App_ sym _ | ||
| isGoodSymbol sym -> descend | ||
| otherwise -> Just term | ||
TermLike.InternalBytes_ _ _ -> Nothing | ||
TermLike.InternalBool_ _ -> Nothing | ||
TermLike.InternalInt_ _ -> Nothing | ||
TermLike.InternalString_ _ -> Nothing | ||
TermLike.DV_ _ (TermLike.StringLiteral_ _) -> Nothing | ||
TermLike.And_ _ _ _ -> descend | ||
TermLike.Or_ _ _ _ -> descend | ||
TermLike.Var_ _ -> Nothing | ||
TermLike.Inj_ _ -> descend | ||
_ -> Just term | ||
where | ||
_ :< termF = Recursive.project term | ||
isGoodSymbol sym = | ||
or | ||
[ Symbol.isConstructorLike sym | ||
, Symbol.isAnywhere sym && Symbol.isInjective sym | ||
, Map.isSymbolConcat sym | ||
, Map.isSymbolElement sym | ||
, Map.isSymbolUnit sym | ||
, Set.isSymbolConcat sym | ||
, Set.isSymbolElement sym | ||
, Set.isSymbolUnit sym | ||
, List.isSymbolConcat sym | ||
, List.isSymbolElement sym | ||
, List.isSymbolUnit sym | ||
] | ||
descend = asum $ findBadArgSubterm <$> termF | ||
|
||
failOnJust _ _ Nothing = return () | ||
failOnJust eq errorMessage (Just term) = | ||
koreFailWithLocations | ||
[term] | ||
( pack $ | ||
show $ | ||
Pretty.vsep | ||
[ errorMessage | ||
, Pretty.indent 4 $ unparse term | ||
, "The equation that the above occurs in is:" | ||
, Pretty.indent 4 $ Pretty.pretty eq | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.