Skip to content

Commit 04c45c1

Browse files
Allow Identifiers to store a list of Modules rather than a single one (#839)
* Allow Identifiers to store a list of Modules rather than a single one * Fix rogue print statements * Fix pattern synonym * Fix formatting error * Fix nothunks
1 parent 1b90d91 commit 04c45c1

File tree

81 files changed

+233
-202
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+233
-202
lines changed

tasty-golden-executable/src/Test/Tasty/Golden/Executable/Error.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ instance Exception ProducedFilesError
8888

8989
handleProducedFilesError :: ProducedFilesError -> IO Result
9090
handleProducedFilesError ProducedFilesError {..} = do
91-
print stdoutDiff
9291
let message =
9392
unlines . catMaybes $
9493
[ case stderrDiff of

tasty-golden-executable/src/Test/Tasty/Golden/Executable/Runner.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,6 @@ acceptTestProduced testProduces (IgnoreFiles testIgnores) = do
412412
NonEmpty.prependList
413413
otherTestSpecsBefore
414414
$ NonEmpty.appendList (NonEmpty.singleton acceptTestSpec) otherTestSpecsAfter
415-
liftIO $ putStrLn "Hi2"
416-
liftIO $ print acceptTestSpecsList
417415
let acceptTestSpecs = TestSpecs acceptTestSpecsList
418416
lift $ writeTestSpecsFile (testDirectory </> testSpecsFileName) acceptTestSpecs
419417
-- Remove the outdated .golden files:

vehicle-syntax/src/Vehicle/Syntax/AST/Instances/NoThunks.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ instance NoThunks MetaID
7575

7676
-- Vehicle.Syntax.AST.Name
7777
instance NoThunks Module
78+
instance NoThunks ModulePath
7879
instance NoThunks Identifier
7980

8081
-- Vehicle.Syntax.AST.Prog

vehicle-syntax/src/Vehicle/Syntax/AST/Name.hs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Data.Serialize (Serialize)
77
import Data.Serialize.Text ()
88
import Data.Text (Text)
99
import GHC.Generics (Generic)
10-
import Prettyprinter (Pretty (..))
10+
import Prettyprinter (Pretty (..), concatWith, dot, surround)
1111

1212
--------------------------------------------------------------------------------
1313
-- Definition
@@ -20,6 +20,7 @@ type Name = Text
2020
data Module
2121
= User
2222
| StdLib
23+
| Record Name
2324
deriving (Eq, Ord, Show, Generic)
2425

2526
instance NFData Module
@@ -36,15 +37,37 @@ instance Pretty Module where
3637
pretty = \case
3738
User -> "User"
3839
StdLib -> "StdLib"
40+
Record name -> pretty name
41+
42+
newtype ModulePath = ModulePath
43+
{ modules :: [Module]
44+
}
45+
deriving (Eq, Ord, Show, Generic)
46+
47+
instance NFData ModulePath
48+
49+
instance Hashable ModulePath
50+
51+
instance ToJSON ModulePath
52+
53+
instance FromJSON ModulePath
54+
55+
instance Serialize ModulePath
56+
57+
instance Pretty ModulePath where
58+
pretty (ModulePath m) = concatWith (surround dot) (fmap pretty m)
3959

4060
--------------------------------------------------------------------------------
4161
-- Identifiers
4262

43-
data Identifier = Identifier Module Name
63+
data Identifier = Identifier
64+
{ modulePath :: ModulePath,
65+
identifierName :: Name
66+
}
4467
deriving (Eq, Ord, Show, Generic)
4568

4669
instance Pretty Identifier where
47-
pretty (Identifier m s) = pretty m <> "." <> pretty s
70+
pretty (Identifier m s) = pretty m <> dot <> pretty s
4871

4972
instance NFData Identifier
5073

@@ -61,11 +84,14 @@ instance Serialize Identifier
6184
class HasIdentifier a where
6285
identifierOf :: a -> Identifier
6386

64-
moduleOf :: Identifier -> Module
65-
moduleOf (Identifier m _) = m
87+
stdlibIdentifier :: Name -> Identifier
88+
stdlibIdentifier = Identifier (ModulePath [StdLib])
89+
90+
isUserIdent :: Identifier -> Bool
91+
isUserIdent ident = User `elem` modules (modulePath ident)
6692

67-
identifierName :: Identifier -> Name
68-
identifierName (Identifier _ n) = n
93+
changeName :: Identifier -> Name -> Identifier
94+
changeName Identifier {..} newName = Identifier {identifierName = newName, ..}
6995

7096
--------------------------------------------------------------------------------
7197
-- Names

vehicle-syntax/src/Vehicle/Syntax/AST/Provenance.hs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import Data.List.NonEmpty (NonEmpty)
1818
import Data.List.NonEmpty qualified as NonEmpty
1919
import Data.Serialize (Serialize)
2020
import GHC.Generics (Generic (..))
21-
import Prettyprinter (Pretty (..), (<+>))
22-
import Vehicle.Syntax.AST.Name (Module (..))
21+
import Prettyprinter (Pretty (..), squotes, (<+>))
22+
import System.FilePath (takeFileName)
2323
import Vehicle.Syntax.Parse.Token
2424

2525
--------------------------------------------------------------------------------
@@ -96,10 +96,13 @@ expandRange (l, r) (Range {..}) =
9696

9797
data Provenance = Provenance
9898
{ range :: Range,
99-
modul :: Module
99+
file :: FilePath
100100
}
101101
deriving (Generic)
102102

103+
noProvenance :: Provenance
104+
noProvenance = Provenance mempty "unknown"
105+
103106
instance Show Provenance where
104107
show = const ""
105108

@@ -114,9 +117,19 @@ instance Hashable Provenance where
114117

115118
instance Serialize Provenance
116119

120+
instance Pretty Provenance where
121+
pretty (Provenance origin file) = "file" <+> squotes (pretty (takeFileName file)) <+> "at" <+> pretty origin
122+
123+
instance Semigroup Provenance where
124+
Provenance origin1 owner1 <> Provenance origin2 _owner2 =
125+
Provenance (origin1 <> origin2) owner1
126+
127+
instance Monoid Provenance where
128+
mempty = noProvenance
129+
117130
-- | Get the provenance for a single token.
118-
tkProvenance :: (IsToken a) => Module -> a -> Provenance
119-
tkProvenance modl tk = Provenance (Range start end) modl
131+
tkProvenance :: (IsToken a) => a -> FilePath -> Provenance
132+
tkProvenance tk = Provenance (Range start end)
120133
where
121134
start = tkPosition tk
122135
end = Position (posLine start) (posColumn start + tkLength tk)
@@ -126,29 +139,14 @@ fillInProvenance provenances = do
126139
let (starts, ends) = NonEmpty.unzip (fmap getPositions provenances)
127140
let start = minimum starts
128141
let end = maximum ends
129-
Provenance (Range start end) (modul $ NonEmpty.head provenances)
142+
Provenance (Range start end) (file $ NonEmpty.head provenances)
130143
where
131144
getPositions :: Provenance -> (Position, Position)
132145
getPositions (Provenance (Range start end) _) = (start, end)
133146

134147
expandProvenance :: (Int, Int) -> Provenance -> Provenance
135148
expandProvenance w (Provenance range o) = Provenance (expandRange w range) o
136149

137-
instance Pretty Provenance where
138-
pretty (Provenance origin modl) = case modl of
139-
User -> pretty origin
140-
StdLib -> pretty modl <> "," <+> pretty origin
141-
142-
instance Semigroup Provenance where
143-
Provenance origin1 owner1 <> Provenance origin2 _owner2 =
144-
Provenance (origin1 <> origin2) owner1
145-
146-
noProvenance :: Provenance
147-
noProvenance = Provenance mempty User
148-
149-
instance Monoid Provenance where
150-
mempty = Provenance mempty User
151-
152150
--------------------------------------------------------------------------------
153151
-- Type-classes
154152

vehicle-syntax/src/Vehicle/Syntax/BNFC/Elaborate/External.hs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ where
1212

1313
import Control.Monad (foldM_)
1414
import Control.Monad.Except (MonadError (..), throwError)
15-
import Control.Monad.Reader (MonadReader (..), runReaderT)
15+
import Control.Monad.Reader (runReaderT)
1616
import Data.Bitraversable (bitraverse)
1717
import Data.Either (partitionEithers)
1818
import Data.List (find)
@@ -28,6 +28,8 @@ import Prettyprinter
2828
import Vehicle.Syntax.AST qualified as V
2929
import Vehicle.Syntax.BNFC.Utils
3030
( MonadElab,
31+
ParseLocation,
32+
getModule,
3133
mkProvenance,
3234
tokArrow,
3335
tokDot,
@@ -61,10 +63,10 @@ newtype UnparsedExpr = UnparsedExpr B.Expr
6163
-- types and definitions.
6264
partiallyElabProg ::
6365
(MonadError ParseError m) =>
64-
V.Module ->
66+
ParseLocation ->
6567
B.Prog ->
6668
m PartiallyParsedProg
67-
partiallyElabProg modl (B.Main decls) = flip runReaderT modl $ do
69+
partiallyElabProg file (B.Main decls) = flip runReaderT file $ do
6870
V.Main <$> partiallyElabDecls decls
6971

7072
partiallyElabDecls :: (MonadElab m) => [B.Decl] -> m [PartiallyParsedDecl]
@@ -303,10 +305,10 @@ getTypeOption = \case
303305

304306
elaborateDecl ::
305307
(MonadError ParseError m) =>
306-
V.Module ->
308+
ParseLocation ->
307309
PartiallyParsedDecl ->
308310
m (V.Decl V.Name V.Builtin)
309-
elaborateDecl modl decl = flip runReaderT modl $ case decl of
311+
elaborateDecl file decl = flip runReaderT file $ case decl of
310312
V.DefAbstract p n r t -> V.DefAbstract p n r <$> elabDeclType t
311313
V.DefFunction p n b t e -> V.DefFunction p n b <$> elabDeclType t <*> elabDeclBody e
312314

@@ -330,10 +332,10 @@ elabDeclBody (UnparsedExpr expr) = case expr of
330332

331333
elaborateExpr ::
332334
(MonadError ParseError m) =>
333-
V.Module ->
335+
ParseLocation ->
334336
UnparsedExpr ->
335337
m (V.Expr V.Name V.Builtin)
336-
elaborateExpr modl (UnparsedExpr expr) = runReaderT (elabExpr expr) modl
338+
elaborateExpr file (UnparsedExpr expr) = runReaderT (elabExpr expr) file
337339

338340
elabExpr :: (MonadElab m) => B.Expr -> m (V.Expr V.Name V.Builtin)
339341
elabExpr = \case
@@ -391,7 +393,7 @@ elabExpr = \case
391393
B.HasMap tk -> builtinTypeClass V.HasMap tk []
392394
B.HasFold tk -> builtinTypeClass V.HasFold tk []
393395
-- NOTE: we reverse the arguments to make it well-typed.
394-
B.Ann e tk t -> freeVar (V.Identifier V.StdLib "typeAnn") tk [t, e]
396+
B.Ann e tk t -> elabExpr (B.App (B.App (B.Var (B.Name (tkLocation tk, "typeAnn"))) (B.ExplicitArg t)) (B.ExplicitArg e))
395397

396398
elabArg :: (MonadElab m) => B.Arg -> m (V.Arg V.Name V.Builtin)
397399
elabArg = \case
@@ -401,7 +403,7 @@ elabArg = \case
401403

402404
elabName :: (MonadElab m) => B.Name -> m V.Identifier
403405
elabName n = do
404-
modl <- ask
406+
modl <- getModule
405407
return $ V.Identifier modl $ tkSymbol n
406408

407409
elabBasicBinder :: (MonadElab m) => Bool -> B.BasicBinder -> m (V.Binder V.Name V.Builtin)
@@ -493,11 +495,6 @@ op2 mk t e1 e2 = do
493495
let p = V.fillInProvenance (tProv :| [V.provenanceOf ce1, V.provenanceOf ce2])
494496
return $ mk p ce1 ce2
495497

496-
freeVar :: (MonadElab m, IsToken token) => V.Identifier -> token -> [B.Expr] -> m (V.Expr V.Name V.Builtin)
497-
freeVar b t args = do
498-
tProv <- mkProvenance t
499-
app (V.FreeVar tProv b) <$> traverse elabExpr args
500-
501498
builtin :: (MonadElab m, IsToken token) => V.Builtin -> token -> [B.Expr] -> m (V.Expr V.Name V.Builtin)
502499
builtin b t args = do
503500
tProv <- mkProvenance t
@@ -603,9 +600,9 @@ elabQuantifierIn ::
603600
m (V.Expr V.Name V.Builtin)
604601
elabQuantifierIn tk q binder container body = do
605602
p <- mkProvenance tk
606-
let quantBuiltin = V.FreeVar p $ case q of
607-
V.Forall -> V.Identifier V.StdLib "forallIn"
608-
V.Exists -> V.Identifier V.StdLib "existsIn"
603+
let quantBuiltin = V.BoundVar p $ case q of
604+
V.Forall -> "forallIn"
605+
V.Exists -> "existsIn"
609606

610607
binder' <- elabNameBinder False binder
611608
container' <- elabExpr container
@@ -627,7 +624,7 @@ elabForeach ::
627624
m (V.Expr V.Name V.Builtin)
628625
elabForeach tk binder body = do
629626
p <- mkProvenance tk
630-
let foreachBuiltin = V.FreeVar p (V.Identifier V.StdLib "foreachIndex")
627+
let foreachBuiltin = V.BoundVar p "foreachIndex"
631628

632629
binder' <- elabNameBinder False binder
633630
body' <- elabExpr body

vehicle-syntax/src/Vehicle/Syntax/BNFC/Utils.hs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,32 @@
33
module Vehicle.Syntax.BNFC.Utils where
44

55
import Control.Monad.Except (MonadError)
6-
import Control.Monad.Reader (MonadReader (..))
6+
import Control.Monad.Reader (MonadReader (..), asks)
77
import Data.Text (Text, pack)
8-
import Vehicle.Syntax.AST.Name (Module)
8+
import Vehicle.Syntax.AST.Name (ModulePath)
99
import Vehicle.Syntax.AST.Provenance
1010
import Vehicle.Syntax.External.Abs qualified as B
1111
import Vehicle.Syntax.Parse.Error (ParseError (..))
1212
import Vehicle.Syntax.Parse.Token (IsToken, mkToken)
1313

1414
type MonadElab m =
1515
( MonadError ParseError m,
16-
MonadReader Module m
16+
MonadReader ParseLocation m
1717
)
1818

1919
pattern InferableOption :: Text
2020
pattern InferableOption = "infer"
2121

22+
type ParseLocation = (ModulePath, FilePath)
23+
24+
getModule :: (MonadElab m) => m ModulePath
25+
getModule = asks fst
26+
27+
getFile :: (MonadElab m) => m FilePath
28+
getFile = asks snd
29+
2230
mkProvenance :: (MonadElab m, IsToken tk) => tk -> m Provenance
23-
mkProvenance tk = do
24-
modl <- ask
25-
return $ tkProvenance modl tk
31+
mkProvenance tk = tkProvenance tk <$> getFile
2632

2733
tokType :: Int -> B.Expr
2834
tokType l = B.Type (mkToken B.TokType ("Type" <> pack (show l)))

vehicle-syntax/src/Vehicle/Syntax/Parse.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Vehicle.Syntax.Parse
33
UnparsedExpr,
44
PartiallyParsedProg,
55
PartiallyParsedDecl,
6+
ParseLocation,
67
readAndParseProg,
78
parseDecl,
89
parseExpr,
@@ -14,6 +15,7 @@ import Control.Monad.Except (MonadError (..))
1415
import Data.Text (Text)
1516
import Vehicle.Syntax.AST
1617
import Vehicle.Syntax.BNFC.Elaborate.External
18+
import Vehicle.Syntax.BNFC.Utils (ParseLocation)
1719
import Vehicle.Syntax.Builtin
1820
import Vehicle.Syntax.External.Abs qualified as External (Expr, Prog)
1921
import Vehicle.Syntax.External.Layout as External (resolveLayout)
@@ -24,14 +26,14 @@ import Vehicle.Syntax.Parse.Error (ParseError (..))
2426
--------------------------------------------------------------------------------
2527
-- Interface
2628

27-
readAndParseProg :: (MonadError ParseError m) => Module -> Text -> m PartiallyParsedProg
29+
readAndParseProg :: (MonadError ParseError m) => ParseLocation -> Text -> m PartiallyParsedProg
2830
readAndParseProg modul txt =
2931
castBNFCError (partiallyElabProg modul) (parseExternalProg txt)
3032

31-
parseDecl :: (MonadError ParseError m) => Module -> PartiallyParsedDecl -> m (Decl Name Builtin)
33+
parseDecl :: (MonadError ParseError m) => ParseLocation -> PartiallyParsedDecl -> m (Decl Name Builtin)
3234
parseDecl = elaborateDecl
3335

34-
parseExpr :: (MonadError ParseError m) => Module -> UnparsedExpr -> m (Expr Name Builtin)
36+
parseExpr :: (MonadError ParseError m) => ParseLocation -> UnparsedExpr -> m (Expr Name Builtin)
3537
parseExpr = elaborateExpr
3638

3739
readExpr :: (MonadError ParseError m) => Text -> m UnparsedExpr

vehicle-syntax/vehicle-syntax.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ library
166166
, cereal-text >=0.1 && <1
167167
, containers >=0.5 && <1
168168
, deepseq >=1.4 && <2
169+
, filepath >=1.4 && <2
169170
, hashable >=1.3 && <2
170171
, mtl >=2.2 && <3
171172
, prettyprinter >=1.7 && <2

vehicle/src/Vehicle/Backend/Agda/Compile.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ pattern TensorType ::
911911
Expr Name Builtin
912912
pattern TensorType p tElem tDims <-
913913
App
914-
(FreeVar p (Identifier StdLib "Tensor"))
914+
(FreeVar p TensorIdent)
915915
[ RelevantExplicitArg _ tElem,
916916
IrrelevantExplicitArg _ tDims
917917
]

0 commit comments

Comments
 (0)