|
1 | 1 | module Syntax.ConstExpr where |
2 | 2 |
|
3 | | -import Data.Bifunctor ( bimap ) |
| 3 | +import Data.Char ( isLower ) |
4 | 4 | import Data.List ( partition ) |
5 | | -import Data.Maybe ( mapMaybe ) |
| 5 | +import Data.Maybe ( listToMaybe |
| 6 | + , mapMaybe |
| 7 | + ) |
6 | 8 | import qualified Data.Set as Set |
| 9 | +import qualified Data.Text as Text |
7 | 10 | import GCL.Common ( fv ) |
8 | 11 | import Syntax.Abstract |
9 | | -import Syntax.Common ( Name ) |
| 12 | +import Syntax.Common ( Name |
| 13 | + , nameToText |
| 14 | + ) |
10 | 15 |
|
11 | 16 | pickGlobals :: [Declaration] -> ([Expr], [Expr]) |
12 | | -pickGlobals = |
13 | | - bimap (map snd) (map snd) |
14 | | - . partition isGlobalProp |
15 | | - . mapMaybe extractAssertion |
| 17 | +pickGlobals = partition isGlobalProp . mapMaybe extractAssertion |
16 | 18 | where |
17 | 19 | -- An assertion is a global prop |
18 | | - -- if it has no free variables other than the accompanied declared names |
19 | | - isGlobalProp :: ([Name], Expr) -> Bool |
20 | | - isGlobalProp (names, assertion) = |
21 | | - fv assertion `Set.isSubsetOf` Set.fromList names |
| 20 | + -- if all of its free variables are of CONSTANTS |
| 21 | + isGlobalProp :: Expr -> Bool |
| 22 | + isGlobalProp assertion = Set.null $ Set.filter nameIsVar (fv assertion) |
| 23 | + |
| 24 | + nameIsVar :: Name -> Bool |
| 25 | + nameIsVar name = |
| 26 | + maybe False isLower (listToMaybe (Text.unpack (nameToText name))) |
22 | 27 |
|
23 | 28 | -- Extracts both the assertion and those declared names |
24 | | - extractAssertion :: Declaration -> Maybe ([Name], Expr) |
25 | | - extractAssertion (ConstDecl _ _ Nothing _) = Nothing |
26 | | - extractAssertion (ConstDecl names _ (Just e) _) = Just (names, e) |
27 | | - extractAssertion (VarDecl _ _ Nothing _) = Nothing |
28 | | - extractAssertion (VarDecl names _ (Just e) _) = Just (names, e) |
| 29 | + extractAssertion :: Declaration -> Maybe Expr |
| 30 | + extractAssertion (ConstDecl _ _ e _) = e |
| 31 | + extractAssertion (VarDecl _ _ e _) = e |
0 commit comments