Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add records to the Inferno language #103

Merged
merged 24 commits into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c43effa
Add Records to Inferno
siddharth-krishna Feb 7, 2024
b69f4b7
Use row variables for type checking records
siddharth-krishna Feb 26, 2024
6055707
Add some debug tracing and fix some bugs
siddharth-krishna Feb 27, 2024
09d3bbf
Fix unifyRecords base case and cleanup
siddharth-krishna Feb 27, 2024
0a270b3
Cleanup
siddharth-krishna Feb 27, 2024
bd3def1
Add eval support
siddharth-krishna Feb 27, 2024
e5f2755
Add references
siddharth-krishna Feb 28, 2024
4a394ab
Comment out trace
siddharth-krishna Feb 28, 2024
94e60d8
Use {x = 2; y = 3} and record.field syntax
siddharth-krishna Mar 5, 2024
699d328
Add CLI options to inferno exe
siddharth-krishna Mar 5, 2024
2939d25
Cleanup and add unifyRecords tests
siddharth-krishna Mar 5, 2024
9c9b4b0
Add golden file for new TRecord constructor
siddharth-krishna Mar 5, 2024
ec5a7c1
Some more cleanup and tests
siddharth-krishna Mar 5, 2024
126abe3
Ormulu with the correct ormolu
siddharth-krishna Mar 5, 2024
bb4eb52
Merge branch 'main' into sidk-records
Daniel-Diaz Mar 5, 2024
41be8dc
Add Arbitrary instances for record types and exprs
siddharth-krishna Mar 12, 2024
3752b35
Add parser for record types
siddharth-krishna Mar 12, 2024
40acf42
Fix parser tests
siddharth-krishna Mar 12, 2024
374081b
Add tests for inferTypeReps
siddharth-krishna Mar 12, 2024
e310b14
Fix tests; better fresh variable counter
siddharth-krishna Mar 12, 2024
32f009e
Format
siddharth-krishna Mar 12, 2024
dde2789
Review comments
siddharth-krishna Mar 12, 2024
f8a0690
Redundant imports
siddharth-krishna Mar 12, 2024
b7c522a
Bump
siddharth-krishna Mar 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions inferno-core/CHANGELOG.md
@@ -1,6 +1,9 @@
# Revision History for inferno-core
*Note*: we use https://pvp.haskell.org/ (MAJOR.MAJOR.MINOR.PATCH)

## 0.11.0.0 -- 2024-03-12
* Add records to the Inferno language

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

Expand Down
66 changes: 53 additions & 13 deletions inferno-core/app/Main.hs
Expand Up @@ -3,28 +3,68 @@

module Main where

import Data.Bifunctor (bimap)
import qualified Data.Map as Map
import qualified Data.Text.IO as Text
import Inferno.Core (Interpreter (..), mkInferno)
import Inferno.Module.Prelude (builtinModules)
import Inferno.Types.VersionControl (pinnedToMaybe)
import Inferno.Utils.Prettyprinter (showPretty)
import System.Environment (getArgs)
import Options.Applicative
( Parser,
argument,
execParser,
fullDesc,
header,
help,
helper,
info,
long,
metavar,
progDesc,
short,
str,
switch,
(<**>),
)
import System.Exit (exitFailure)
import System.IO (hPutStrLn, stderr)
import System.IO (hPrint, stderr)

data CliArgs = CliArgs {file :: String, typecheck :: Bool}

cliargs :: Parser CliArgs
cliargs =
CliArgs
<$> argument str (metavar "FILE" <> help "Input file path")
<*> switch (long "typecheck" <> short 't' <> help "Only run type inference")

main :: IO ()
main = do
file <- head <$> getArgs
src <- Text.readFile file
Interpreter {evalExpr, defaultEnv, parseAndInferTypeReps} <-
let opts =
info
(cliargs <**> helper)
( fullDesc
<> progDesc "Run Inferno on FILE"
<> header "inferno - a functional scripting language"
)
args <- execParser opts

src <- Text.readFile $ file args
Interpreter {evalExpr, defaultEnv, parseAndInfer} <-
mkInferno builtinModules [] :: IO (Interpreter IO ())
case parseAndInferTypeReps src of
case parseAndInfer src of
Left err -> do
hPutStrLn stderr $ show err
hPrint stderr err
exitFailure
Right ast -> do
evalExpr defaultEnv Map.empty ast >>= \case
Left err -> do
hPutStrLn stderr $ show err
exitFailure
Right res -> showPretty res
Right (ast, ty, _, _) -> do
if typecheck args
then do
putStrLn "Inferred type:"
showPretty ty
else do
let ast' = bimap pinnedToMaybe (const ()) ast
evalExpr defaultEnv Map.empty ast' >>= \case
Left err -> do
hPrint stderr err
exitFailure
Right res -> showPretty res
5,833 changes: 5,833 additions & 0 deletions inferno-core/golden/InfernoType/TRecord.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions inferno-core/inferno-core.cabal
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: inferno-core
version: 0.10.1.0
version: 0.11.0.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 Expand Up @@ -46,7 +46,7 @@ library
, Inferno.Utils.QQ.Common
hs-source-dirs:
src
ghc-options: -Wall -Wunused-packages -Wincomplete-uni-patterns -Wincomplete-record-updates
ghc-options: -Wall -Wunused-packages -Wincomplete-uni-patterns -Wincomplete-record-updates -Wincomplete-patterns
build-depends:
base >= 4.13 && < 4.17
, bimap >= 0.5.0 && < 0.6
Expand All @@ -59,7 +59,7 @@ library
, extra >= 1.7.12 && < 1.8
, generic-lens >= 2.2.1 && < 2.3
, inferno-vc >= 0.3.0 && < 0.4
, inferno-types >= 0.3.0 && < 0.4
, inferno-types >= 0.4.0 && < 0.5
, megaparsec >= 9.2.1 && < 9.3
, memory >= 0.18.0 && < 0.19
, mtl >= 2.2.2 && < 2.3
Expand Down Expand Up @@ -137,5 +137,6 @@ executable inferno
, containers
, inferno-core
, inferno-types
, optparse-applicative
, text
default-language: Haskell2010
25 changes: 12 additions & 13 deletions inferno-core/src/Inferno/Eval.hs
Expand Up @@ -29,19 +29,7 @@ import Inferno.Types.Syntax
)
import Inferno.Types.Value
( ImplEnvM,
Value
( VArray,
VDouble,
VEmpty,
VEnum,
VFun,
VInt,
VOne,
VText,
VTuple,
VTypeRep,
VWord64
),
Value (..),
runImplEnvM,
)
import Inferno.Types.VersionControl (VCObjectHash)
Expand Down Expand Up @@ -204,6 +192,17 @@ eval env@(localEnv, pinnedEnv) expr = case expr of
_ -> throwM $ RuntimeError "failed to match with a bool"
Tuple_ es ->
foldrM (\(e, _) vs -> eval env e >>= return . (: vs)) [] (tListToList es) >>= return . VTuple
Record_ fs -> do
valMap <- foldrM (\(f, e, _) vs -> eval env e >>= \v -> return ((f, v) : vs)) [] fs
return $ VRecord $ Map.fromList valMap
RecordField_ (Ident r) f -> do
case Map.lookup (ExtIdent $ Right r) localEnv of
Just (VRecord fs) -> do
case Map.lookup f fs of
Just v -> return v
Nothing -> throwM $ RuntimeError "record field not found"
Just _ -> throwM $ RuntimeError "failed to match with a record"
Nothing -> throwM $ RuntimeError $ show (ExtIdent $ Right r) <> " not found in the unpinned env"
One_ e -> eval env e >>= return . VOne
Empty_ -> return $ VEmpty
Assert_ cond e ->
Expand Down