-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
437 implement #if_#then_#else hook (#442)
* Evaluation now proceeds bottom-up (but still discovers work top-down) * New module `Booster.Builtin` as a home for all hook implementations * If a hook implementation is provided, it will be tried before function and simplification equations * A hook is allowed to return no result, in which case function and simplification equations will be tried * Sort errors or arity errors (currently) abort the evaluation (should be caught during internalisation) FIxes #437 --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Georgy Lukyanov <georgiylukjanov@gmail.com> Co-authored-by: Samuel Balco <goodlyrottenapple@gmail.com>
- Loading branch information
1 parent
c3436ba
commit a673bb5
Showing
19 changed files
with
1,132 additions
and
2,890 deletions.
There are no files selected for viewing
This file contains 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,74 @@ | ||
{- | | ||
Copyright : (c) Runtime Verification, 2023 | ||
License : BSD-3-Clause | ||
Builtin functions. Only a select few functions are implemented. | ||
Built-in functions are looked up by their symbol attribute 'hook' from | ||
the definition's symbol map. | ||
-} | ||
module Booster.Builtin ( | ||
hooks, | ||
) where | ||
|
||
import Control.Monad | ||
import Control.Monad.Trans.Except | ||
import Data.Map (Map) | ||
import Data.Map qualified as Map | ||
import Data.Text (Text) | ||
import Prettyprinter (pretty, vsep) | ||
|
||
import Booster.Pattern.Base | ||
import Booster.Pattern.Bool | ||
import Booster.Pattern.Util | ||
import Booster.Prettyprinter | ||
|
||
-- built-in functions may fail on arity or sort errors, and may be | ||
-- partial (returning a Maybe type) | ||
type BuiltinFunction = [Term] -> Except Text (Maybe Term) | ||
|
||
hooks :: Map Text BuiltinFunction | ||
hooks = | ||
Map.unions | ||
[ builtinsKEQUAL | ||
] | ||
|
||
------------------------------------------------------------ | ||
(~~>) :: Text -> BuiltinFunction -> (Text, BuiltinFunction) | ||
(~~>) = (,) | ||
|
||
------------------------------------------------------------ | ||
-- KEQUAL hooks | ||
builtinsKEQUAL :: Map Text BuiltinFunction | ||
builtinsKEQUAL = | ||
Map.fromList | ||
[ "KEQUAL.ite" ~~> iteHook | ||
] | ||
|
||
iteHook :: BuiltinFunction | ||
iteHook args | ||
| [cond, thenVal, elseVal] <- args = do | ||
cond `shouldHaveSort` "SortBool" | ||
unless (sortOfTerm thenVal == sortOfTerm elseVal) $ | ||
throwE . renderText . vsep $ | ||
[ "Different sorts in alternatives:" | ||
, pretty thenVal | ||
, pretty elseVal | ||
] | ||
case cond of | ||
TrueBool -> pure $ Just thenVal | ||
FalseBool -> pure $ Just elseVal | ||
_other -> pure Nothing | ||
| otherwise = | ||
throwE . renderText $ "KEQUAL.ite: wrong arity " <> pretty (length args) | ||
|
||
-- check for simple (parameter-less) sorts | ||
shouldHaveSort :: Term -> SortName -> Except Text () | ||
t `shouldHaveSort` s | ||
| sortOfTerm t == SortApp s [] = | ||
pure () | ||
| otherwise = | ||
throwE . renderText . vsep $ | ||
[ pretty $ "Argument term has unexpected sort (expected " <> show s <> "):" | ||
, pretty t | ||
] |
This file contains 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 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
echo "kompiling simplify.k" | ||
kompile --backend haskell simplify.k | ||
kompile --backend haskell simplify.k --syntax-module SIMPLIFY | ||
cp simplify-kompiled/definition.kore simplify.kore | ||
rm -r simplify-kompiled |
Oops, something went wrong.