implement more shorthand functions

This commit is contained in:
Allen Nelson 2016-01-28 18:09:38 -06:00
parent a5044cf2e3
commit 0a628dafbb
2 changed files with 23 additions and 0 deletions

View file

@ -100,10 +100,24 @@ mkFunction params = Fix . NAbs params
mkDot :: NExpr -> Text -> NExpr
mkDot e key = Fix $ NSelect e [StaticKey key] Nothing
-- | An `inherit` clause without an expression to pull from.
inherit :: [NKeyName e] -> Binding e
inherit = Inherit Nothing
-- | An `inherit` clause with an expression to pull from.
inheritFrom :: e -> [NKeyName e] -> Binding e
inheritFrom expr = Inherit (Just expr)
-- | Shorthand for producing a binding of a name to an expression.
bindTo :: Text -> NExpr -> Binding NExpr
bindTo name val = NamedVar (mkSelector name) val
-- | Infix version of bindTo.
($=) :: Text -> NExpr -> Binding NExpr
name $= value = bindTo name value
infixr 2 $=
-- | Append a list of bindings to a set or let expression.
-- For example, adding `[a = 1, b = 2]` to `let c = 3; in 4` produces
-- `let a = 1; b = 2; c = 3; in 4`.

View file

@ -71,6 +71,11 @@ data NExprF r
-- ^ Assert that the first returns true before evaluating the second.
deriving (Ord, Eq, Generic, Typeable, Data, Functor, Show)
-- | We make an `IsString` for expressions, where the string is interpreted
-- as an identifier. This is the most common use-case...
instance IsString NExpr where
fromString = Fix . NSym . fromString
-- | The monomorphic expression type is a fixed point of the polymorphic one.
type NExpr = Fix NExprF
@ -153,6 +158,10 @@ data NKeyName r
| StaticKey !Text
deriving (Eq, Ord, Generic, Typeable, Data, Show)
-- | Most key names are just static text, so this instance is convenient.
instance IsString (NKeyName r) where
fromString = StaticKey . fromString
-- | Deriving this instance automatically is not possible because @r@
-- occurs not only as last argument in @Antiquoted (NString r) r@
instance Functor NKeyName where