From a6bc806799f64fdd24c1a9d6aa33d2380e92848d Mon Sep 17 00:00:00 2001 From: Lukasz Czajka Date: Fri, 29 Nov 2024 18:13:29 +0100 Subject: [PATCH 1/2] fix aliases --- .../FromParsed/Analysis/Scoping.hs | 46 ++++++++++--------- tests/positive/Alias.juvix | 17 ++++++- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs index 9ac311641e..8642c37632 100644 --- a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs +++ b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs @@ -1696,26 +1696,8 @@ checkSections sec = topBindings helper -- checks the definitions in a section goDefsSection :: NonEmpty (Definition 'Parsed) -> Sem r' (NonEmpty (Definition 'Scoped)) goDefsSection defs = do - mapM_ scanAlias (defs ^.. each . _DefinitionSyntax . _SyntaxAlias) mapM goDefinition defs - scanAlias :: AliasDef 'Parsed -> Sem r' () - scanAlias a = do - aliasId <- gets (^?! scopeLocalSymbols . at (a ^. aliasDefName) . _Just . S.nameId) - asName <- checkName (a ^. aliasDefAsName) - modify' (set (scoperAlias . at aliasId) (Just asName)) - checkLoop aliasId - registerAlias aliasId asName - where - checkLoop :: NameId -> Sem r' () - checkLoop = evalState (mempty :: HashSet NameId) . go - where - go :: (Members '[State (HashSet NameId), Error ScoperError, State ScoperState] s) => NameId -> Sem s () - go i = do - whenM (gets (HashSet.member i)) (throw (ErrAliasCycle (AliasCycle a))) - modify' (HashSet.insert i) - whenJustM (gets (^? scoperAlias . at i . _Just . preSymbolName . S.nameId)) go - reserveDefinition :: Definition 'Parsed -> Sem r' (Maybe (Module 'Parsed 'ModuleLocal)) reserveDefinition = \case DefinitionSyntax s -> resolveSyntaxDef s $> Nothing @@ -3253,7 +3235,7 @@ checkParsePatternAtom' :: checkParsePatternAtom' = localBindings . ignoreSyntax . runReader PatternNamesKindVariables . checkParsePatternAtom checkSyntaxDef :: - (Members '[Error ScoperError, Reader ScopeParameters, State Scope, State ScoperState, InfoTableBuilder, Reader InfoTable, NameIdGen, Reader PackageId, State ScoperSyntax] r) => + (Members '[Error ScoperError, Reader ScopeParameters, State Scope, State ScoperState, Reader BindingStrategy, InfoTableBuilder, Reader InfoTable, NameIdGen, Reader PackageId, State ScoperSyntax] r) => SyntaxDef 'Parsed -> Sem r (SyntaxDef 'Scoped) checkSyntaxDef = \case @@ -3263,10 +3245,12 @@ checkSyntaxDef = \case SyntaxIterator iterDef -> return $ SyntaxIterator iterDef checkAliasDef :: - (Members '[Error ScoperError, Reader ScopeParameters, State Scope, State ScoperState, InfoTableBuilder, Reader InfoTable, NameIdGen, State ScoperSyntax] r) => + forall r. + (Members '[Error ScoperError, Reader ScopeParameters, State Scope, State ScoperState, Reader BindingStrategy, InfoTableBuilder, Reader InfoTable, NameIdGen, State ScoperSyntax] r) => AliasDef 'Parsed -> Sem r (AliasDef 'Scoped) -checkAliasDef AliasDef {..} = do +checkAliasDef def@AliasDef {..} = do + scanAlias def aliasName' :: S.Symbol <- gets (^?! scopeLocalSymbols . at _aliasDefName . _Just) asName' <- checkScopedIden _aliasDefAsName return @@ -3275,6 +3259,24 @@ checkAliasDef AliasDef {..} = do _aliasDefAsName = asName', .. } + where + scanAlias :: AliasDef 'Parsed -> Sem r () + scanAlias a = do + reserveAliasDef a + aliasId <- gets (^?! scopeLocalSymbols . at (a ^. aliasDefName) . _Just . S.nameId) + asName <- checkName (a ^. aliasDefAsName) + modify' (set (scoperAlias . at aliasId) (Just asName)) + checkLoop aliasId + registerAlias aliasId asName + where + checkLoop :: NameId -> Sem r () + checkLoop = evalState (mempty :: HashSet NameId) . go + where + go :: (Members '[State (HashSet NameId), Error ScoperError, State ScoperState] s) => NameId -> Sem s () + go i = do + whenM (gets (HashSet.member i)) (throw (ErrAliasCycle (AliasCycle a))) + modify' (HashSet.insert i) + whenJustM (gets (^? scoperAlias . at i . _Just . preSymbolName . S.nameId)) go reserveAliasDef :: (Members '[Error ScoperError, Reader ScopeParameters, State Scope, State ScoperState, InfoTableBuilder, Reader InfoTable, NameIdGen, State ScoperSyntax, Reader BindingStrategy] r) => @@ -3290,7 +3292,7 @@ resolveSyntaxDef = \case SyntaxFixity fixDef -> resolveFixitySyntaxDef fixDef SyntaxOperator opDef -> resolveOperatorSyntaxDef opDef SyntaxIterator iterDef -> resolveIteratorSyntaxDef iterDef - SyntaxAlias a -> reserveAliasDef a + SyntaxAlias {} -> return () ------------------------------------------------------------------------------- -- Check precedences are comparable diff --git a/tests/positive/Alias.juvix b/tests/positive/Alias.juvix index 86b5392224..ff7b27e01e 100644 --- a/tests/positive/Alias.juvix +++ b/tests/positive/Alias.juvix @@ -2,7 +2,8 @@ module Alias; import Stdlib.Data.Fixity open; --- aliases are allowed to forward reference +-- aliases are allowed to forward reference, but they themselves cannot be +-- forward referenced syntax alias Boolean := Bool; syntax alias ⊥ := false; syntax alias ⊤ := true; @@ -53,8 +54,22 @@ syntax alias , := mkPair; myPair : Pair := one, ⊥; +syntax alias myPair' := myPair; + localAlias : Binary -> Binary | b := let syntax alias b' := b; in b'; + +syntax alias g' := g; + +terminating +f (b : Binary) : Binary := case b of + | zero := g' one + | one := zero; + +terminating +g (b : Binary) : Binary := case b of + | zero := f one + | one := zero; From 64e3813d365746dd7475aef6310161d08c2f4fd7 Mon Sep 17 00:00:00 2001 From: Lukasz Czajka Date: Fri, 29 Nov 2024 18:53:01 +0100 Subject: [PATCH 2/2] remove alias cycle detection (not needed anymore) --- .../FromParsed/Analysis/Scoping.hs | 10 --------- .../FromParsed/Analysis/Scoping/Error.hs | 2 -- .../Analysis/Scoping/Error/Types.hs | 22 ------------------- .../Core/Transformation/Optimize/Inlining.hs | 4 ++-- test/Scope/Negative.hs | 5 ----- tests/negative/AliasCycle.juvix | 5 ----- 6 files changed, 2 insertions(+), 46 deletions(-) delete mode 100644 tests/negative/AliasCycle.juvix diff --git a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs index 8642c37632..c26b0fdf05 100644 --- a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs +++ b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping.hs @@ -3266,17 +3266,7 @@ checkAliasDef def@AliasDef {..} = do aliasId <- gets (^?! scopeLocalSymbols . at (a ^. aliasDefName) . _Just . S.nameId) asName <- checkName (a ^. aliasDefAsName) modify' (set (scoperAlias . at aliasId) (Just asName)) - checkLoop aliasId registerAlias aliasId asName - where - checkLoop :: NameId -> Sem r () - checkLoop = evalState (mempty :: HashSet NameId) . go - where - go :: (Members '[State (HashSet NameId), Error ScoperError, State ScoperState] s) => NameId -> Sem s () - go i = do - whenM (gets (HashSet.member i)) (throw (ErrAliasCycle (AliasCycle a))) - modify' (HashSet.insert i) - whenJustM (gets (^? scoperAlias . at i . _Just . preSymbolName . S.nameId)) go reserveAliasDef :: (Members '[Error ScoperError, Reader ScopeParameters, State Scope, State ScoperState, InfoTableBuilder, Reader InfoTable, NameIdGen, State ScoperSyntax, Reader BindingStrategy] r) => diff --git a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error.hs b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error.hs index c6ad481c77..39b89433d9 100644 --- a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error.hs +++ b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error.hs @@ -51,7 +51,6 @@ data ScoperError | ErrMissingArgs MissingArgs | ErrPrecedenceInconsistency PrecedenceInconsistencyError | ErrIncomparablePrecedences IncomaprablePrecedences - | ErrAliasCycle AliasCycle | ErrInvalidRangeNumber InvalidRangeNumber | ErrWrongDefaultValue WrongDefaultValue | ErrUnsupported Unsupported @@ -104,7 +103,6 @@ instance ToGenericError ScoperError where ErrMissingArgs e -> genericError e ErrPrecedenceInconsistency e -> genericError e ErrIncomparablePrecedences e -> genericError e - ErrAliasCycle e -> genericError e ErrInvalidRangeNumber e -> genericError e ErrWrongDefaultValue e -> genericError e ErrUnsupported e -> genericError e diff --git a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error/Types.hs b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error/Types.hs index fcf0b17bf4..ac71598a84 100644 --- a/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error/Types.hs +++ b/src/Juvix/Compiler/Concrete/Translation/FromParsed/Analysis/Scoping/Error/Types.hs @@ -1019,28 +1019,6 @@ instance ToGenericError IncomaprablePrecedences where i :: Interval i = getLoc _incomparablePrecedencesName1 -newtype AliasCycle = AliasCycle - { _aliasCycleDef :: AliasDef 'Parsed - } - deriving stock (Show) - -instance ToGenericError AliasCycle where - genericError AliasCycle {..} = do - opts <- fromGenericOptions <$> ask - let msg = - "The definition of" - <+> ppCode opts (_aliasCycleDef ^. aliasDefName) - <+> "creates an alias cycle." - return - GenericError - { _genericErrorLoc = i, - _genericErrorMessage = mkAnsiText msg, - _genericErrorIntervals = [i] - } - where - i :: Interval - i = getLoc _aliasCycleDef - newtype WrongDefaultValue = WrongDefaultValue { _wrongDefaultValue :: SigArg 'Parsed } diff --git a/src/Juvix/Compiler/Core/Transformation/Optimize/Inlining.hs b/src/Juvix/Compiler/Core/Transformation/Optimize/Inlining.hs index 0e4697af17..32ef13fee1 100644 --- a/src/Juvix/Compiler/Core/Transformation/Optimize/Inlining.hs +++ b/src/Juvix/Compiler/Core/Transformation/Optimize/Inlining.hs @@ -38,8 +38,8 @@ convertNode inlineDepth nonRecSyms md = dmapL go node _ | HashSet.member _identSymbol nonRecSyms - && isInlineableLambda inlineDepth md bl def - && length args >= argsNum -> + && length args >= argsNum + && isInlineableLambda inlineDepth md bl def -> mkApps def args _ -> node diff --git a/test/Scope/Negative.hs b/test/Scope/Negative.hs index f97949aecc..b87950a646 100644 --- a/test/Scope/Negative.hs +++ b/test/Scope/Negative.hs @@ -269,11 +269,6 @@ scoperErrorTests = $(mkRelDir ".") $(mkRelFile "PrecedenceInconsistency.juvix") $ wantsError ErrPrecedenceInconsistency, - negTest - "Alias cycle" - $(mkRelDir ".") - $(mkRelFile "AliasCycle.juvix") - $ wantsError ErrAliasCycle, negTest "Invalid range number in iterator definition" $(mkRelDir ".") diff --git a/tests/negative/AliasCycle.juvix b/tests/negative/AliasCycle.juvix deleted file mode 100644 index 2e6ce0838a..0000000000 --- a/tests/negative/AliasCycle.juvix +++ /dev/null @@ -1,5 +0,0 @@ -module AliasCycle; - -syntax alias x1 := x2; -syntax alias x2 := x3; -syntax alias x3 := x1;