Skip to content

Commit

Permalink
Fix ToValue instances for functions and ImplicitCast (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharth-krishna authored Jan 30, 2024
1 parent ee41975 commit f229ad9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions inferno-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Revision History for inferno-core
*Note*: we use https://pvp.haskell.org/ (MAJOR.MAJOR.MINOR.PATCH)

## 0.10.1.0 -- 2024-01-30
* Fix `ToValue` instances for functions and `ImplicitCast`

## 0.10.0.0 -- 2024-01-29
* Modify `TermEnv` to defer evaluating prelude `Expr` definitions till runtime. Should reduce memory consumption

Expand Down
2 changes: 1 addition & 1 deletion inferno-core/inferno-core.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: inferno-core
version: 0.10.0.0
version: 0.10.1.0
synopsis: A statically-typed functional scripting language
description: Parser, type inference, and interpreter for a statically-typed functional scripting language
category: DSL,Scripting
Expand Down
27 changes: 20 additions & 7 deletions inferno-core/src/Inferno/Module/Cast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ couldNotCast v =
<> " to "
<> (show $ typeRep (Proxy :: Proxy a))

-- TODO not possible anymore
-- instance ToValue c m (m (Value c m)) where
-- toValue = pure

instance ToValue c m (Value c m) where
toValue = id

Expand Down Expand Up @@ -211,16 +207,33 @@ instance (MonadThrow m, FromValue c m a, ToValue c m b) => ToValue c m (a -> b)
x <- fromValue v
pure $ toValue $ f x

instance (MonadThrow m, FromValue c (ImplEnvM m c) a1, FromValue c (ImplEnvM m c) a2, ToValue c (ImplEnvM m c) a3, KnownSymbol lbl) => ToValue c (ImplEnvM m c) (ImplicitCast lbl a1 a2 a3) where
-- We have a separate instance for functions that run in the monad m.
-- This is because we cannot `toValue` the result, as it is impossible to have
-- toValue :: ImplEnvM m c (Value c (ImplEnvM m c)) -> Value c (ImplEnvM m c)
-- as that would entail running the ImplEnvM monad.
instance {-# OVERLAPPING #-} (MonadThrow m, FromValue c m a) => ToValue c m (a -> m (Value c m)) where
toValue f =
VFun $ \v -> do
x <- fromValue v
f x

instance (MonadThrow m, FromValue c (ImplEnvM m c) a1, ToValue c (ImplEnvM m c) (a2 -> a3), KnownSymbol lbl) => ToValue c (ImplEnvM m c) (ImplicitCast lbl a1 a2 a3) where
toValue (ImplicitCast f) =
VFun $ \b' -> do
impl <- ask
let i = ExtIdent $ Right $ pack $ symbolVal (Proxy :: Proxy lbl)
case Map.lookup i impl of
Just v -> do
x <- fromValue v
b <- fromValue b'
pure $ toValue $ f x b
let f' = f x
-- f' :: a2 -> a3, and a3 might be ImplEnvM m c (Value c (ImplEnvM m c)),
-- so we cannot apply f' to (fromValue b') and toValue the result, see the above
-- instance. Instead, we convert f' to a value and let the compiler pick the
-- appropriate instance of ToValue c m (a -> b) from the 2 choices above.
case toValue f' of
VFun f'' ->
f'' b'
_ -> error ""
Nothing -> throwM $ NotFoundInImplicitEnv i

-- | In this instance, the 'IO' in the type is ignored.
Expand Down

0 comments on commit f229ad9

Please sign in to comment.