1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Merge pull request #1563 from github/array-literals

Evaluatable instance for Array literals.
This commit is contained in:
Patrick Thomson 2018-03-15 14:18:29 -04:00 committed by GitHub
commit 601e99d7a6
4 changed files with 24 additions and 9 deletions

View File

@ -67,6 +67,9 @@ class (MonadAnalysis term value m, Show value) => MonadValue term value m where
-- | Construct an N-ary tuple of multiple (possibly-disjoint) values -- | Construct an N-ary tuple of multiple (possibly-disjoint) values
multiple :: [value] -> m value multiple :: [value] -> m value
-- | Construct an array of zero or more values.
array :: [value] -> m value
-- | Eliminate boolean values. TODO: s/boolean/truthy -- | Eliminate boolean values. TODO: s/boolean/truthy
ifthenelse :: value -> m a -> m a -> m a ifthenelse :: value -> m a -> m a -> m a
@ -132,6 +135,8 @@ instance ( FreeVariables term
multiple = pure . injValue . Value.Tuple multiple = pure . injValue . Value.Tuple
array = pure . injValue . Value.Array
ifthenelse cond if' else' ifthenelse cond if' else'
| Just (Boolean b) <- prjValue cond = if b then if' else else' | Just (Boolean b) <- prjValue cond = if b then if' else else'
| otherwise = fail ("not defined for non-boolean conditions: " <> show cond) | otherwise = fail ("not defined for non-boolean conditions: " <> show cond)
@ -217,6 +222,7 @@ instance (Alternative m, MonadAnalysis term Type m, MonadFresh m) => MonadValue
symbol _ = pure Type.Symbol symbol _ = pure Type.Symbol
rational _ = pure Type.Rational rational _ = pure Type.Rational
multiple = pure . Type.Product multiple = pure . Type.Product
array = pure . Type.Array
ifthenelse cond if' else' = unify cond Bool *> (if' <|> else') ifthenelse cond if' else' = unify cond Bool *> (if' <|> else')

View File

@ -19,6 +19,7 @@ data Type
| Type :-> Type -- ^ Binary function types. | Type :-> Type -- ^ Binary function types.
| Var TName -- ^ A type variable. | Var TName -- ^ A type variable.
| Product [Type] -- ^ N-ary products. | Product [Type] -- ^ N-ary products.
| Array [Type] -- ^ Arrays. Note that this is heterogenous.
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
-- TODO: À la carte representation of types. -- TODO: À la carte representation of types.

View File

@ -14,15 +14,16 @@ import Prelude hiding (Float, Integer, String, Rational, fail)
import qualified Prelude import qualified Prelude
type ValueConstructors location term type ValueConstructors location term
= '[Closure location term = '[Array
, Unit
, Boolean , Boolean
, Closure location term
, Float , Float
, Integer , Integer
, String , String
, Rational , Rational
, Symbol , Symbol
, Tuple , Tuple
, Unit
] ]
-- | Open union of primitive values that terms can be evaluated to. -- | Open union of primitive values that terms can be evaluated to.
@ -111,10 +112,9 @@ instance Eq1 Float where liftEq = genericLiftEq
instance Ord1 Float where liftCompare = genericLiftCompare instance Ord1 Float where liftCompare = genericLiftCompare
instance Show1 Float where liftShowsPrec = genericLiftShowsPrec instance Show1 Float where liftShowsPrec = genericLiftShowsPrec
-- Zero or more values. -- | Zero or more values. Fixed-size at interpretation time.
-- TODO: Investigate whether we should use Vector for this. -- TODO: Investigate whether we should use Vector for this.
-- TODO: Should we have a Some type over a nonemmpty list? Or does this merit one? -- TODO: Should we have a Some type over a nonemmpty list? Or does this merit one?
newtype Tuple value = Tuple [value] newtype Tuple value = Tuple [value]
deriving (Eq, Generic1, Ord, Show) deriving (Eq, Generic1, Ord, Show)
@ -122,6 +122,15 @@ instance Eq1 Tuple where liftEq = genericLiftEq
instance Ord1 Tuple where liftCompare = genericLiftCompare instance Ord1 Tuple where liftCompare = genericLiftCompare
instance Show1 Tuple where liftShowsPrec = genericLiftShowsPrec instance Show1 Tuple where liftShowsPrec = genericLiftShowsPrec
-- | Zero or more values. Dynamically resized as needed at interpretation time.
-- TODO: Vector? Seq?
newtype Array value = Array [value]
deriving (Eq, Generic1, Ord, Show)
instance Eq1 Array where liftEq = genericLiftEq
instance Ord1 Array where liftCompare = genericLiftCompare
instance Show1 Array where liftShowsPrec = genericLiftShowsPrec
-- | The environment for an abstract value type. -- | The environment for an abstract value type.
type EnvironmentFor v = Environment (LocationFor v) v type EnvironmentFor v = Environment (LocationFor v) v

View File

@ -205,9 +205,8 @@ instance Eq1 Array where liftEq = genericLiftEq
instance Ord1 Array where liftCompare = genericLiftCompare instance Ord1 Array where liftCompare = genericLiftCompare
instance Show1 Array where liftShowsPrec = genericLiftShowsPrec instance Show1 Array where liftShowsPrec = genericLiftShowsPrec
-- TODO: Implement Eval instance for Array instance Evaluatable Array where
instance Evaluatable Array eval (Array a) = array =<< traverse subtermValue a
newtype Hash a = Hash { hashElements :: [a] } newtype Hash a = Hash { hashElements :: [a] }
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable, FreeVariables1) deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable, FreeVariables1)