Skip to content

Commit

Permalink
OK, fix one at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Dec 4, 2024
1 parent b5ffdc3 commit 033729a
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 415 deletions.
4 changes: 2 additions & 2 deletions wasm-calc12/src/Calc/Ability/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module Calc.Ability.Check
)
where

import Calc.Types.Identifier
import Calc.Ability.Error
import Calc.ExprUtils
import Calc.Types.Ability
import Calc.Types.Expr
import Calc.Types.Function
import Calc.Types.Identifier
import Calc.Types.Import
import Calc.Types.Module
import Calc.Types.ModuleAnnotations
Expand Down Expand Up @@ -162,7 +162,7 @@ abilityExpr (EConstructor ann constructor as) = do
EConstructor ann constructor <$> traverse abilityExpr as
abilityExpr (EApply ann fn@(EVar _ (Identifier fnVar)) args) = do
let functionName = FunctionName fnVar
isImport <- asks (S.member functionName . aeImportNames)
isImport <- asks (S.member functionName . aeImportNames)
if isImport
then tell (S.singleton $ CallImportedFunction ann functionName)
else do
Expand Down
12 changes: 3 additions & 9 deletions wasm-calc12/src/Calc/Linearity/Decorate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import Calc.ExprUtils
import Calc.Linearity.Types
import Calc.TypeUtils
import Calc.Types.Expr
import Calc.Types.FunctionName
import Calc.Types.Identifier
import Calc.Types.Pattern
import Calc.Types.Type
import Control.Monad (unless, when)
import Control.Monad (unless)
import Control.Monad.State
import Control.Monad.Writer
import Data.Bifunctor (second)
Expand Down Expand Up @@ -265,13 +264,8 @@ decorate (EIf ty predExpr thenExpr elseExpr) = do
<$> decorate predExpr
<*> pure (mapOuterExprAnnotation (second (const uniqueToElse)) decoratedThen)
<*> pure (mapOuterExprAnnotation (second (const uniqueToThen)) decoratedElse)
decorate (EApply ty fnName@(FunctionName inner) args) = do
-- if we know about the var, assume it's a lambda not a built in function
let identifier = Identifier inner
isVar <- gets (M.member (UserDefined identifier) . lsVars)
when isVar $
recordUse (Identifier inner) ty
EApply (ty, Nothing) fnName <$> traverse decorate args
decorate (EApply ty fn args) = do
EApply (ty, Nothing) <$> decorate fn <*> traverse decorate args
decorate (ETuple ty a as) =
ETuple (ty, Nothing) <$> decorate a <*> traverse decorate as
decorate (EBox ty a) =
Expand Down
9 changes: 4 additions & 5 deletions wasm-calc12/src/Calc/Parser/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,15 @@ applyParser :: Parser (Expr Annotation)
applyParser = addLocation $ do
func <- applyFuncParser
let argParser = do
stringLiteral "("
args <- sepEndBy exprParserInternal (stringLiteral ",")
stringLiteral ")"
pure args
stringLiteral "("
args <- sepEndBy exprParserInternal (stringLiteral ",")
stringLiteral ")"
pure args
let argParser' :: Parser [[ParserExpr]]
argParser' = (: []) <$> argParser
args <- chainl1 argParser' (pure (<>))
pure $ foldl (EApply mempty) func args


tupleParser :: Parser (Expr Annotation)
tupleParser = label "tuple" $
addLocation $ do
Expand Down
5 changes: 2 additions & 3 deletions wasm-calc12/src/Calc/Parser/Shared.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}

module Calc.Parser.Shared
( chainl1,inBrackets,
( chainl1,
inBrackets,
myLexeme,
withLocation,
stringLiteral,
Expand Down Expand Up @@ -60,7 +61,6 @@ maybePred parser predicate' = try $ do
Just b -> pure b
_ -> fail $ T.unpack $ "Predicate did not hold for " <> T.pack (show a)


-- | stolen from Parsec, allows parsing infix expressions without recursion
-- death
chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a
Expand All @@ -72,4 +72,3 @@ chainl1 p op = do x <- p; rest x
y <- p
rest (f x y)
<|> return x

31 changes: 19 additions & 12 deletions wasm-calc12/src/Calc/Typecheck/Elaborate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module Calc.Typecheck.Elaborate
)
where

import qualified Data.Map.Strict as M
import Calc.ExprUtils
import Calc.Typecheck.Error
import Calc.Typecheck.Helpers
Expand All @@ -25,6 +24,7 @@ import Calc.Types.Test
import Calc.Types.Type
import Control.Monad.State
import Data.Functor
import qualified Data.Map.Strict as M
import qualified Data.Set as S

elaborateModule ::
Expand Down Expand Up @@ -53,14 +53,20 @@ elaborateModule
}

-- statically provide types of all functions in scope
let functionsInScope = foldMap (\(Function {fnFunctionName,fnAnn,fnArgs,fnReturnType}) ->

M.singleton fnFunctionName (

TFunction fnAnn (faType <$> fnArgs) fnReturnType

)) mdFunctions

let functionsInScope =
foldMap
( \(Function {fnFunctionName, fnAnn, fnArgs, fnReturnType}) ->
M.singleton
fnFunctionName
( TFunction fnAnn (faType <$> fnArgs) fnReturnType
)
)
mdFunctions

let importsInScope =
foldMap
(\(Import {impImportName, impAnn, impArgs, impReturnType}) -> M.singleton impImportName (TFunction impAnn (iaType <$> impArgs) impReturnType))
mdImports

runTypecheckM typecheckEnv $ do
globals <-
Expand All @@ -84,7 +90,7 @@ elaborateModule
functions <-
traverse
( \fn -> do
elabFn <- elaborateFunction functionsInScope fn
elabFn <- elaborateFunction (functionsInScope <> importsInScope) fn
storeFunction
(fnFunctionName elabFn)
(S.fromList $ fnGenerics fn)
Expand Down Expand Up @@ -187,7 +193,8 @@ elaborateFunction ::
M.Map FunctionName (Type ann) ->
Function ann ->
TypecheckM ann (Function (Type ann))
elaborateFunction functionsInScope
elaborateFunction
functionsInScope
( Function
{ fnPublic,
fnAnn,
Expand All @@ -201,7 +208,7 @@ elaborateFunction functionsInScope
) = do
-- include current function with arguments so we can recursively call ourselves
let tyCurrentFunction =
TFunction fnAnn (faType <$> fnArgs) fnReturnType
TFunction fnAnn (faType <$> fnArgs) fnReturnType

exprA <-
withFunctionEnv
Expand Down
6 changes: 4 additions & 2 deletions wasm-calc12/src/Calc/Typecheck/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ withFunctionEnv args functionsInScope generics =
in local
( \tce ->
tce
{ tceVars = tceVars tce <> HM.fromList identifiersFromFunctions <>
HM.fromList identifiersFromArgs,
{ tceVars =
tceVars tce
<> HM.fromList identifiersFromFunctions
<> HM.fromList identifiersFromArgs,
tceGenerics = generics
}
)
Expand Down
10 changes: 5 additions & 5 deletions wasm-calc12/src/Calc/Typecheck/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ checkReturnType (TUnificationVar {}) p@(TPrim ann _) =
checkReturnType _ ty = pure ty

freeVars :: Type ann -> S.Set TypeVar
freeVars ty
= go ty
where
go (TVar _ var) = S.singleton var
go other = monoidType go other
freeVars ty =

Check warning on line 286 in wasm-calc12/src/Calc/Typecheck/Infer.hs

View workflow job for this annotation

GitHub Actions / hlint

Warning in freeVars in module Calc.Typecheck.Infer: Eta reduce ▫︎ Found: "freeVars ty = go ty" ▫︎ Perhaps: "freeVars = go"
go ty
where
go (TVar _ var) = S.singleton var
go other = monoidType go other

checkApply ::
Maybe (Type ann) ->
Expand Down
32 changes: 17 additions & 15 deletions wasm-calc12/src/Calc/Wasm/FromExpr/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import Calc.Types
import Calc.Wasm.FromExpr.Drops
( addDropsFromPath,
addDropsToWasmExpr,
--dropFunctionForType,
dropFunctionForType,
)
import Calc.Wasm.FromExpr.Helpers
import Calc.Wasm.FromExpr.Patterns
import Calc.Wasm.FromExpr.Types
import Calc.Wasm.ToWasm.Types
-- import Control.Monad (void)
import Control.Monad (void)
import Control.Monad.Except
import Control.Monad.State
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -181,7 +181,7 @@ fromExprWithDrops expr = do

addDropsToWasmExpr drops wasmExpr

fromFunctionApply ::
_fromFunctionApply ::
( MonadState FromExprState m,
MonadError FromWasmError m,
Show ann,
Expand All @@ -190,7 +190,7 @@ fromFunctionApply ::
FunctionName ->
[Expr (Type ann, Maybe (Drops ann))] ->
m WasmExpr
fromFunctionApply funcName args = do
_fromFunctionApply funcName args = do
(fIndex, fGenerics, fArgTypes) <- lookupFunction funcName
let types =
monomorphiseTypes
Expand All @@ -201,6 +201,7 @@ fromFunctionApply funcName args = do
wasmArgs <- traverse fromExpr args
pure $ WApply fIndex (wasmArgs <> dropArgs)

{-
fromLambdaApply ::
( MonadState FromExprState m,
MonadError FromWasmError m,
Expand All @@ -216,6 +217,7 @@ fromLambdaApply (FunctionName inner) args = do
wasmArgs <- traverse fromExpr args
pure $ WApplyIndirect (WVar fIndex) wasmArgs
-}

fromExpr ::
( MonadError FromWasmError m,
Expand Down Expand Up @@ -306,17 +308,17 @@ fromExpr (EVar _ ident) = do
`catchError` \_ -> WGlobal <$> lookupGlobal ident
fromExpr (EApply _ fnExpr _args) = do
error ("fromExpr " <> show fnExpr)
{-
(fIndex, fGenerics, fArgTypes) <- lookupFunction funcName
let types =
monomorphiseTypes
fGenerics
fArgTypes
(void . fst . getOuterAnnotation <$> args)
dropArgs <- traverse (dropFunctionForType . snd) types
wasmArgs <- traverse fromExpr args
pure $ WApply fIndex (wasmArgs <> dropArgs)
-}
{-
(fIndex, fGenerics, fArgTypes) <- lookupFunction funcName
let types =
monomorphiseTypes
fGenerics
fArgTypes
(void . fst . getOuterAnnotation <$> args)
dropArgs <- traverse (dropFunctionForType . snd) types
wasmArgs <- traverse fromExpr args
pure $ WApply fIndex (wasmArgs <> dropArgs)
-}
fromExpr (ETuple (ty, _) a as) = do
wasmType <- liftEither $ scalarFromType ty
index <- addLocal Nothing wasmType
Expand Down
2 changes: 1 addition & 1 deletion wasm-calc12/test/Test/Typecheck/TypecheckSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ testInputs =

spec :: Spec
spec = do
fdescribe "TypecheckSpec" $ do
describe "TypecheckSpec" $ do
describe "Function" $ do
let succeeding =
[ ("function one () -> Int64 { 1 }", TFunction () [] tyInt64),
Expand Down
Loading

0 comments on commit 033729a

Please sign in to comment.