Skip to content

Commit e2f6553

Browse files
Reorganised arg insertion problem (#867)
1 parent 45ce210 commit e2f6553

File tree

23 files changed

+519
-289
lines changed

23 files changed

+519
-289
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Data.Aeson (ToJSON)
55
import Data.Hashable (Hashable)
66
import Data.Serialize (Serialize)
77
import GHC.Generics (Generic)
8+
import Prettyprinter (Pretty (..))
89

910
--------------------------------------------------------------------------------
1011
-- Data
@@ -29,6 +30,11 @@ instance Semigroup Relevance where
2930
instance Monoid Relevance where
3031
mempty = Relevant
3132

33+
instance Pretty Relevance where
34+
pretty = \case
35+
Relevant -> "relevant"
36+
Irrelevant -> "irrelevant"
37+
3238
--------------------------------------------------------------------------------
3339
-- Type class
3440

vehicle/src/Vehicle/Compile/Normalise/NBE.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Vehicle.Compile.Normalise.NBE
99
normaliseClosure,
1010
eval,
1111
evalApp,
12+
evalClosure,
1213
traverseClosure,
1314
traverseClosureGeneric,
1415
)

vehicle/src/Vehicle/Compile/Print.hs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Data.IntMap (IntMap)
2424
import Data.IntMap qualified as IntMap (assocs)
2525
import Data.Text (Text)
2626
import GHC.TypeLits (ErrorMessage (..), TypeError)
27-
import Prettyprinter (list)
27+
import Prettyprinter (fill, list)
2828
import Vehicle.Compile.Descope
2929
import Vehicle.Compile.Normalise.Quote (unnormalise)
3030
import Vehicle.Compile.Prelude
@@ -160,8 +160,10 @@ type family StrategyFor (tags :: Tags) a :: Strategy where
160160
-----------------
161161
-- Constraints --
162162
-----------------
163+
StrategyFor tags (ArgInsertionProblem builtin `In` NamedBoundCtx) = StrategyFor tags (Expr builtin `In` NamedBoundCtx)
163164
StrategyFor tags (InstanceConstraint builtin `In` ConstraintContext builtin) = StrategyFor tags (Value builtin `In` NamedBoundCtx)
164165
StrategyFor tags (UnificationConstraint builtin `In` ConstraintContext builtin) = StrategyFor tags (Value builtin `In` NamedBoundCtx)
166+
StrategyFor tags (ApplicationConstraint builtin `In` ConstraintContext builtin) = StrategyFor tags (Value builtin `In` NamedBoundCtx)
165167
StrategyFor tags (Constraint builtin `In` ConstraintContext builtin) = StrategyFor tags (Value builtin `In` NamedBoundCtx)
166168
------------
167169
-- Pretty --
@@ -448,12 +450,37 @@ instance
448450
let e' = unnormalise @(Value builtin) @(Expr Builtin) (Lv $ length ctx) e
449451
prettyUsing @rest (e', ctx)
450452

453+
instance
454+
(PrettyUsing rest (Arg builtin `In` NamedBoundCtx), ConvertableBuiltin builtin Builtin) =>
455+
PrettyUsing ('QuoteValue rest) (Arg builtin `In` NamedBoundCtx)
456+
where
457+
prettyUsing (e, ctx) = prettyUsing @rest (e, ctx)
458+
459+
instance
460+
(PrettyUsing rest (Expr builtin `In` NamedBoundCtx), ConvertableBuiltin builtin Builtin) =>
461+
PrettyUsing ('QuoteValue rest) (Expr builtin `In` NamedBoundCtx)
462+
where
463+
prettyUsing (e, ctx) = prettyUsing @rest (e, ctx)
464+
451465
instance PrettyUsing rest (GenericBinder ()) where
452466
prettyUsing b = maybe "_" pretty (nameOf b)
453467

454468
--------------------------------------------------------------------------------
455469
-- Instances for constraints
456470

471+
instance
472+
( PrettyUsing rest (Expr builtin `In` NamedBoundCtx),
473+
PrettyUsing rest (Arg builtin `In` NamedBoundCtx)
474+
) =>
475+
PrettyUsing rest (ArgInsertionProblem builtin `In` NamedBoundCtx)
476+
where
477+
prettyUsing (problem, ctx) = do
478+
let checkedExpr = solutionSoFar problem
479+
let checkedExprDoc = prettyUsing @rest (checkedExpr, ctx)
480+
let expectedTypeDoc = prettyUsing @rest (currentExpectedType problem, ctx)
481+
let uncheckedArgsDoc = prettyUsing @rest (uncheckedArgs problem, ctx)
482+
parens (checkedExprDoc <+> ":" <+> expectedTypeDoc) <+> "@" <+> uncheckedArgsDoc
483+
457484
prettyConstraintContext :: ConstraintContext builtin -> Doc a
458485
prettyConstraintContext ctx =
459486
"#" <> pretty (constraintID ctx) <> ". " -- <+> pretty ctx
@@ -475,15 +502,25 @@ instance
475502
let expr' = prettyUsing @rest (expr, namedBoundCtxOf ctx)
476503
prettyConstraintContext ctx <+> pretty m <+> "<=" <+> expr'
477504

505+
instance
506+
(PrettyUsing rest (ArgInsertionProblem builtin `In` NamedBoundCtx)) =>
507+
PrettyUsing rest (ApplicationConstraint builtin `In` ConstraintContext builtin)
508+
where
509+
prettyUsing (InferArgs {..}, ctx) = do
510+
let problemDoc = prettyUsing @rest (argInsertionProblem, namedBoundCtxOf ctx)
511+
prettyConstraintContext ctx <+> parens (pretty exprSolutionMeta <+> "=" <+> problemDoc) <+> ":" <+> pretty typeSolutionMeta
512+
478513
instance
479514
( PrettyUsing rest (UnificationConstraint builtin `In` ctx),
480-
PrettyUsing rest (InstanceConstraint builtin `In` ctx)
515+
PrettyUsing rest (InstanceConstraint builtin `In` ctx),
516+
PrettyUsing rest (ApplicationConstraint builtin `In` ctx)
481517
) =>
482518
PrettyUsing rest (Constraint builtin `In` ctx)
483519
where
484520
prettyUsing (c, ctx) = case c of
485521
UnificationConstraint uc -> prettyUsing @rest (uc, ctx)
486522
InstanceConstraint tc -> prettyUsing @rest (tc, ctx)
523+
ApplicationConstraint tc -> prettyUsing @rest (tc, ctx)
487524

488525
--------------------------------------------------------------------------------
489526
-- Assertions
@@ -545,7 +582,7 @@ instance
545582
instance (PrettyUsing rest (a `In` ctx)) => PrettyUsing rest (MetaMap a `In` ctx) where
546583
prettyUsing (MetaMap m, ctx) = prettyMapEntries entries
547584
where
548-
entries = fmap (bimap MetaID (prettyUsing @rest . (,ctx))) (IntMap.assocs m)
585+
entries = fmap (bimap (fill 3 . pretty . MetaID) (prettyUsing @rest . (,ctx))) (IntMap.assocs m)
549586

550587
instance (PrettyUsing rest (a `In` ctx)) => PrettyUsing rest (MaybeTrivial a `In` ctx) where
551588
prettyUsing (e, ctx) = case e of

vehicle/src/Vehicle/Compile/Print/Error.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ instance MeaningfulError CompileError where
405405
InstanceConstraint (Resolve InstanceConstraintOrigin {..} _ _ _) ->
406406
"insufficient information to find a valid type for the overloaded expression"
407407
<+> squotes (prettyTypeClassConstraintOriginExpr ctx checkedInstanceOp checkedInstanceOpArgs)
408+
ApplicationConstraint {} ->
409+
"unsolved application constraint: " <+> prettyFriendly (WithContext constraint ctx)
408410
UnsolvedMetas ms ->
409411
UError $
410412
UserError

vehicle/src/Vehicle/Compile/Type.hs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Vehicle.Compile.Type
22
( typeCheckProg,
3-
typeCheckExpr,
3+
typeCheckSolitaryExpr,
44
)
55
where
66

@@ -15,6 +15,7 @@ import Vehicle.Compile.Normalise.Builtin (NormalisableBuiltin)
1515
import Vehicle.Compile.Prelude
1616
import Vehicle.Compile.Print
1717
import Vehicle.Compile.Type.Bidirectional
18+
import Vehicle.Compile.Type.Constraint.ApplicationSolver (runApplicationSolver)
1819
import Vehicle.Compile.Type.Constraint.Core (runConstraintSolver)
1920
import Vehicle.Compile.Type.Constraint.UnificationSolver
2021
import Vehicle.Compile.Type.Core
@@ -43,17 +44,17 @@ typeCheckProg instanceCandidates freeCtx (Main uncheckedProg) =
4344
xs <- typeCheckDecls uncheckedProg
4445
return $ Main xs
4546

46-
typeCheckExpr ::
47+
typeCheckSolitaryExpr ::
4748
forall builtin m.
4849
(HasTypeSystem builtin, NormalisableBuiltin builtin, MonadCompile m) =>
4950
InstanceDatabase builtin ->
5051
FreeCtx builtin ->
5152
Expr Builtin ->
5253
m (Expr builtin)
53-
typeCheckExpr instanceCandidates freeCtx expr1 = do
54+
typeCheckSolitaryExpr instanceCandidates freeCtx expr1 = do
5455
runTypeCheckerTInitially freeCtx instanceCandidates $ do
5556
expr2 <- convertExprFromStandardTypes expr1
56-
(expr3, _exprType) <- runMonadBidirectional (Proxy @builtin) $ inferExpr expr2
57+
(expr3, _exprType) <- inferExprType mempty Relevant expr2
5758
solveConstraints @builtin Nothing
5859
expr4 <- substMetas expr3
5960
checkAllUnknownsSolved (Proxy @builtin)
@@ -134,8 +135,9 @@ typeCheckFunction p ident anns typ body = do
134135

135136
-- Type check the body.
136137
let pass = bidirectionalPassDoc <+> "body of" <+> quotePretty ident
137-
checkedBody <- logCompilerPass MidDetail pass $ do
138-
runMonadBidirectional (Proxy @builtin) $ checkExpr checkedType body
138+
checkedBody <-
139+
logCompilerPass MidDetail pass $
140+
checkExprType mempty Relevant checkedType body
139141

140142
-- Reconstruct the function.
141143
let checkedDecl = DefFunction p ident anns checkedType checkedBody
@@ -167,7 +169,7 @@ checkDeclType :: forall builtin m. (TCM builtin m) => Identifier -> Expr builtin
167169
checkDeclType ident declType = do
168170
let pass = bidirectionalPassDoc <+> "type of" <+> quotePretty ident
169171
logCompilerPass MidDetail pass $ do
170-
runMonadBidirectional (Proxy @builtin) $ checkExpr (TypeUniverse mempty 0) declType
172+
checkExprType mempty Relevant (TypeUniverse mempty 0) declType
171173

172174
restrictAbstractDefType ::
173175
(TCM builtin m) =>
@@ -216,16 +218,21 @@ solveConstraints d = logCompilerPass MidDetail "constraint solving" $ do
216218
-- If we have made useful progress then start a new pass
217219
let passDoc = "constraint solving pass" <+> pretty loopNumber
218220
newMetasSolved <- logCompilerPass MaxDetail passDoc $ do
221+
metasSolvedDuringApplications <-
222+
trackSolvedMetas (Proxy @builtin) $
223+
runApplicationSolver (Proxy @builtin) recentlySolvedMetas
224+
219225
metasSolvedDuringUnification <-
220226
trackSolvedMetas (Proxy @builtin) $
221-
runUnificationSolver (Proxy @builtin) recentlySolvedMetas
227+
runUnificationSolver (Proxy @builtin) (metasSolvedDuringApplications <> recentlySolvedMetas)
222228

223229
logUnsolvedUnknowns updatedDecl (Just recentlySolvedMetas)
224230

225231
metasSolvedDuringInstanceResolution <-
226232
trackSolvedMetas (Proxy @builtin) $
227-
runInstanceSolver (Proxy @builtin) metasSolvedDuringUnification
228-
return metasSolvedDuringInstanceResolution
233+
runInstanceSolver (Proxy @builtin) (metasSolvedDuringUnification <> metasSolvedDuringApplications)
234+
235+
return (metasSolvedDuringInstanceResolution <> metasSolvedDuringUnification)
229236

230237
loopOverConstraints newMetasSolved (loopNumber + 1) updatedDecl
231238

@@ -318,7 +325,7 @@ logUnsolvedUnknowns maybeDecl maybeSolvedMetas = do
318325
return $
319326
"current-solution:"
320327
<> line
321-
<> prettyVerbose (fmap unnormalised updatedSubst)
328+
<> indent 2 (prettyVerbose (fmap unnormalised updatedSubst))
322329
<> line
323330
<> "unsolved-metas:"
324331
<> line

0 commit comments

Comments
 (0)