Skip to content

Commit c971f24

Browse files
Reuse instance search for checking parameter/dataset/network/property types (#868)
* Use instances for declaration restrictions * Fix formatting * Fix
1 parent e2f6553 commit c971f24

File tree

38 files changed

+1096
-958
lines changed

38 files changed

+1096
-958
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,12 @@ We recommend you install Python using [pyenv].
466466

467467
1. Install [pyenv] following the instructions on the website: <https://github.com/pyenv/pyenv#installation>
468468

469-
2. Install the latest release of each supported Python version. Currently, those are 3.7, 3.8, 3.9, 3.10, and 3.11.
469+
2. Install the latest release of each supported Python version.
470470

471471
Run the following command:
472472

473473
```sh
474-
pyenv install 3.7 3.8 3.9 3.10 3.11
474+
pyenv install 3.9 3.10 3.11 3.12 3.13
475475
```
476476

477477
3. Check if your installation was successful.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Vehicle.Syntax.AST.Decl where
22

33
import Control.DeepSeq (NFData)
4+
import Data.Hashable (Hashable)
45
import Data.Serialize (Serialize)
56
import GHC.Generics (Generic)
67
import Prettyprinter (Pretty (..))
@@ -97,12 +98,14 @@ isAbstractDecl = \case
9798
data ParameterSort
9899
= Inferable
99100
| NonInferable
100-
deriving (Eq, Show, Generic)
101+
deriving (Eq, Ord, Show, Generic)
101102

102103
instance NFData ParameterSort
103104

104105
instance Serialize ParameterSort
105106

107+
instance Hashable ParameterSort
108+
106109
instance Pretty ParameterSort where
107110
pretty = \case
108111
Inferable -> "(infer=True)"

vehicle-syntax/src/Vehicle/Syntax/Builtin/TypeClass.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Data.Hashable (Hashable (..))
55
import Data.Serialize (Serialize)
66
import GHC.Generics (Generic)
77
import Prettyprinter (Pretty (..), (<+>))
8+
import Vehicle.Syntax.AST.Decl (ParameterSort)
89
import Vehicle.Syntax.Builtin.BasicOperations
910

1011
--------------------------------------------------------------------------------
@@ -27,6 +28,13 @@ data TypeClass
2728
HasNatLits
2829
| HasRatLits
2930
| HasVecLits
31+
| -- Declaration type restrictions
32+
ValidPropertyType
33+
| ValidParameterType ParameterSort
34+
| ValidNetworkType
35+
| ValidNetworkTensorType
36+
| ValidDatasetType
37+
| ValidDatasetElementType
3038
deriving (Eq, Ord, Generic, Show)
3139

3240
instance NFData TypeClass
@@ -51,6 +59,12 @@ instance Pretty TypeClass where
5159
HasNatLits -> "HasNatLiterals"
5260
HasRatLits -> "HasRatLiterals"
5361
HasVecLits -> "HasVecLiterals"
62+
ValidPropertyType -> "ValidPropertyType"
63+
ValidParameterType {} -> "ValidParameterType"
64+
ValidNetworkType -> "ValidNetworkType"
65+
ValidNetworkTensorType -> "ValidNetworkTensorType"
66+
ValidDatasetType -> "ValidDatasetType"
67+
ValidDatasetElementType -> "ValidDatasetElementType"
5468

5569
-- Builtin operations for type-classes
5670
data TypeClassOp

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ compileBuiltin _p b args = case b of
514514
HasFold -> unsupportedError
515515
HasMap -> unsupportedError
516516
HasQuantifierIn {} -> unsupportedError
517+
ValidPropertyType -> unsupportedError
518+
ValidParameterType {} -> unsupportedError
519+
ValidNetworkType {} -> unsupportedError
520+
ValidNetworkTensorType {} -> unsupportedError
521+
ValidDatasetType {} -> unsupportedError
522+
ValidDatasetElementType {} -> unsupportedError
517523
TypeClassOp op -> case op of
518524
QuantifierTC q -> case reverse args of
519525
(ExplicitArg _ _ (Lam _ binder body)) : _ -> compileTypeLevelQuantifier q [binder] body

vehicle/src/Vehicle/Compile/Error.hs

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import Control.Exception (IOException)
66
import Control.Monad.Except (MonadError, throwError)
77
import Data.List.NonEmpty (NonEmpty)
88
import Data.Map qualified as Map
9+
import Data.Text (Text)
10+
import Data.Typeable (Proxy)
911
import Data.Void (Void)
1012
import Prettyprinter (list)
1113
import Vehicle.Backend.LossFunction.Core (BooleanDifferentiableLogicField, TensorDifferentiableLogicField)
1214
import Vehicle.Backend.Prelude
15+
import Vehicle.Compile.Normalise.Builtin (NormalisableBuiltin)
1316
import Vehicle.Compile.Prelude
1417
import Vehicle.Compile.Print (PrettyFriendly)
1518
import Vehicle.Compile.Print.Builtin
@@ -32,6 +35,56 @@ type MonadCompile m =
3235
MonadError CompileError m
3336
)
3437

38+
--------------------------------------------------------------------------------
39+
-- Typing errors
40+
41+
data MissingExplicitArgError builtin = MissingExplicitArgError
42+
{ _ctx :: NamedBoundCtx,
43+
explicitBinder :: Binder builtin,
44+
nonExplicitArg :: Arg builtin
45+
}
46+
deriving (Show)
47+
48+
data RelevantUseOfIrrelevantVariableError builtin = RelevantUseOfIrrelevantVariableError
49+
{ _proxy :: Proxy builtin,
50+
_provenance :: Provenance,
51+
irrelevantVariableName :: Name
52+
}
53+
deriving (Show)
54+
55+
data FunctionTypeMismatchError builtin = FunctionTypeMismatchError
56+
{ _ctx :: NamedBoundCtx,
57+
originalFunction :: Expr builtin,
58+
currentExpectedType :: Expr builtin,
59+
currentUncheckedArgs :: [Arg builtin]
60+
}
61+
deriving (Show)
62+
63+
newtype FailedUnificationConstraintsError builtin = FailedUnificationConstraintsError
64+
{ failedConstraints :: NonEmpty (WithContext (UnificationConstraint builtin))
65+
}
66+
deriving (Show)
67+
68+
data FailedInstanceConstraintError builtin = FailedInstanceConstraintError
69+
{ _freeEnv :: FreeEnv builtin,
70+
failedConstraint :: WithContext (InstanceConstraint builtin),
71+
exploredCandidates :: [(WithContext (InstanceCandidate builtin), UnAnnDoc)]
72+
}
73+
deriving (Show)
74+
75+
-- | Errors thrown during type-checking
76+
data TypingError builtin
77+
= MissingExplicitArg (MissingExplicitArgError builtin)
78+
| FunctionTypeMismatch (FunctionTypeMismatchError builtin)
79+
| RelevantUseOfIrrelevantVariable (RelevantUseOfIrrelevantVariableError builtin)
80+
| FailedUnificationConstraints (FailedUnificationConstraintsError builtin)
81+
| FailedInstanceConstraint (FailedInstanceConstraintError builtin)
82+
| FailedIndexConstraintTooBig (ConstraintContext builtin) Int Int
83+
| FailedIndexConstraintUnknown (ConstraintContext builtin) (Value builtin) (VType builtin)
84+
| UnsolvedConstraints (NonEmpty (WithContext (Constraint builtin)))
85+
| UnsolvedMetas (Proxy builtin) (NonEmpty (MetaID, Provenance))
86+
deriving (Show)
87+
3588
--------------------------------------------------------------------------------
3689
-- Compilation errors
3790

@@ -45,12 +98,9 @@ data CompileError
4598
| DeclarationBoundShadowing Provenance Name
4699
| MissingPrunedName Name
47100
| -- Type checking errors
48-
UnresolvedHole Provenance Name
49-
| forall builtin.
50-
(PrintableBuiltin builtin, Show builtin) =>
101+
forall builtin.
102+
(Eq builtin, PrintableBuiltin builtin, NormalisableBuiltin builtin, Show builtin) =>
51103
TypingError (TypingError builtin)
52-
| UnsolvedMetas (NonEmpty (MetaID, Provenance))
53-
| RelevantUseOfIrrelevantVariable Provenance Name
54104
| -- Resource loading errors
55105
ResourceNotProvided DeclProvenance ExternalResource
56106
| ResourceIOError DeclProvenance ExternalResource IOException
@@ -59,32 +109,23 @@ data CompileError
59109
| -- Unsupported networks
60110
NetworkTypeHasVariableSizeTensor DeclProvenance (GluedType Builtin) (VType Builtin) InputOrOutput
61111
| NetworkTypeHasImplicitSizeTensor DeclProvenance (GluedType Builtin) Identifier InputOrOutput
62-
| NetworkTypeIsNotAFunction DeclProvenance (GluedType Builtin)
63-
| NetworkTypeIsNotOverTensors DeclProvenance (GluedType Builtin) (VType Builtin) InputOrOutput
64-
| NetworkTypeHasNonExplicitArguments DeclProvenance (GluedType Builtin) (VBinder Builtin)
65-
| NetworkTypeHasUnsupportedElementType DeclProvenance (GluedType Builtin) (VType Builtin) InputOrOutput
66112
| -- Unsupported datasets
67-
DatasetTypeUnsupportedContainer DeclProvenance (GluedType Builtin)
68-
| DatasetTypeUnsupportedElement DeclProvenance (GluedType Builtin) (VType Builtin)
69-
| DatasetVariableSizeTensor DeclProvenance (GluedType Builtin) (VType Builtin)
113+
DatasetVariableSizeTensor DeclProvenance (GluedType Builtin) (VType Builtin)
70114
| DatasetDimensionSizeMismatch DeclProvenance FilePath Int Int TensorShape TensorShape
71115
| DatasetDimensionsMismatch DeclProvenance FilePath (GluedExpr Builtin) TensorShape
72116
| DatasetTypeMismatch DeclProvenance FilePath (GluedType Builtin) (VType Builtin) (Doc Void)
73117
| DatasetInvalidIndex DeclProvenance FilePath Int Int
74118
| DatasetInvalidNat DeclProvenance FilePath Int
75119
| -- Unsupported parameters
76-
ParameterTypeUnsupported DeclProvenance (GluedType Builtin)
77-
| ParameterTypeVariableSizeIndex DeclProvenance (GluedType Builtin)
120+
ParameterTypeVariableSizeIndex DeclProvenance (GluedType Builtin)
78121
| ParameterTypeInferableParameterIndex DeclProvenance Identifier
79122
| ParameterValueUnparsable DeclProvenance String BuiltinType
80123
| ParameterValueInvalidIndex DeclProvenance Int Int
81124
| ParameterValueInvalidNat DeclProvenance Int
82-
| InferableParameterTypeUnsupported DeclProvenance (GluedType Builtin)
83125
| InferableParameterContradictory Identifier (DeclProvenance, ExternalResource, Int) (DeclProvenance, ExternalResource, Int)
84126
| InferableParameterUninferrable DeclProvenance
85127
| -- Unsupported properties
86-
PropertyTypeUnsupported DeclProvenance (GluedType Builtin)
87-
| NoPropertiesFound
128+
NoPropertiesFound
88129
| -- Verification backend errors
89130
forall builtin.
90131
(PrintableBuiltin builtin, Eq builtin, PrettyFriendly (Contextualised (VType builtin) NamedBoundCtx)) =>
@@ -183,3 +224,37 @@ lookupInFreeCtx ::
183224
lookupInFreeCtx pass ident ctx = case Map.lookup ident ctx of
184225
Nothing -> internalScopingError pass ident
185226
Just x -> return x
227+
228+
--------------------------------------------------------------------------------
229+
-- The final error type
230+
231+
-- | Errors from external code that we have no control over.
232+
-- These may be either user or developer errors but in general we
233+
-- can't distinguish between the two.
234+
newtype ExternalError = ExternalError Text
235+
236+
-- | Errors that are the user's responsibility to fix.
237+
data UserError = UserError
238+
{ provenance :: Provenance,
239+
problem :: UnAnnDoc,
240+
fix :: Maybe UnAnnDoc
241+
}
242+
243+
data VehicleError
244+
= UError UserError
245+
| EError ExternalError
246+
| DError (Doc ())
247+
248+
instance Pretty VehicleError where
249+
pretty (UError (UserError p prob probFix)) =
250+
unAnnotate $
251+
"Error in"
252+
<+> pretty p
253+
<> ":"
254+
<+> prob
255+
<> maybe "" (\fix -> line <> fixText fix) probFix
256+
pretty (EError (ExternalError text)) = pretty text
257+
pretty (DError text) = unAnnotate text
258+
259+
fixText :: Doc ann -> Doc ann
260+
fixText t = "Fix:" <+> t

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module Vehicle.Compile.Normalise.Builtin where
66

77
import Control.Monad (zipWithM)
88
import Data.Foldable (foldrM)
9-
import Vehicle.Compile.Error (MonadCompile)
109
import Vehicle.Compile.Prelude
1110
import Vehicle.Compile.Print.Builtin
1211
import Vehicle.Data.Builtin.Core
@@ -44,15 +43,15 @@ import Vehicle.Data.Tensor (Tensor, foldTensor, mapTensor, zipWithTensor)
4443
-- Differential logics all normalise to the same type but have different
4544
-- methods of normalisation.
4645
type EvalBuiltinApp m builtin =
47-
(MonadCompile m) =>
46+
(MonadLogger m) =>
4847
EvalApp (Value builtin) m ->
4948
Eval builtin m ->
5049
builtin ->
5150
[Arg builtin] ->
5251
m (Value builtin)
5352

5453
type Eval builtin m =
55-
(MonadCompile m) =>
54+
(MonadLogger m) =>
5655
Expr builtin ->
5756
m (Value builtin)
5857

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Vehicle.Data.Builtin.Linearity (LinearityBuiltin (..))
55
import Vehicle.Data.Builtin.Loss (LossTensorBuiltin (..))
66
import Vehicle.Data.Builtin.Polarity (PolarityBuiltin (..))
77
import Vehicle.Data.Builtin.Tensor (TensorBuiltin (..))
8-
import Vehicle.Data.Code.Expr (Expr (..), mapBuiltins, normAppList, pattern BuiltinExpr)
8+
import Vehicle.Data.Code.Expr (Expr (..), mapBuiltins, normAppList, pattern App, pattern BuiltinExpr)
99
import Vehicle.Data.Code.Interface (tensorToExpr)
1010
import Vehicle.Libraries.StandardLibrary.Definitions (StdLibFunction (..))
1111
import Vehicle.Prelude
@@ -153,3 +153,9 @@ instance PrintableBuiltin TensorBuiltin where
153153

154154
instance PrintableBuiltin LossTensorBuiltin where
155155
isCoercion = const False
156+
157+
isCoercionExpr :: (PrintableBuiltin builtin) => Expr builtin -> Bool
158+
isCoercionExpr = \case
159+
Builtin _ b -> isCoercion b
160+
App (Builtin _ b) _ -> isCoercion b
161+
_ -> False

0 commit comments

Comments
 (0)