@@ -48,39 +48,40 @@ record TypeApp (0 con : Con) where
4848 argHeadType : TypeInfo
4949 {auto 0 argHeadTypeGood : AllTyArgsNamed argHeadType}
5050 argApps : Vect argHeadType.args.length .| Either (Fin con.args.length) TTImp
51- determ : Determination con
5251
53- getTypeApps : Elaboration m => NamesInfoInTypes => (con : Con) -> m $ Vect con.args.length $ TypeApp con
52+ getTypeApps : Elaboration m => NamesInfoInTypes => (con : Con) -> m $ Vect con.args.length ( Either (FC, String) $ TypeApp con, Determination con)
5453getTypeApps con = do
5554 let conArgIdxs = SortedMap . fromList $ mapI con. args $ \ idx, arg => (argName' arg, idx)
5655
5756 -- Analyse that we can do subgeneration for each constructor argument
5857 -- Fails using `Elaboration` if the given expression is not an application to a type constructor
59- let analyseTypeApp : TTImp -> m $ TypeApp con
58+ let analyseTypeApp : TTImp -> m ( Either (FC, String) $ TypeApp con, Determination con)
6059 analyseTypeApp expr = do
6160 let (lhs, args) = unAppAny expr
61+ let as = args. asVect <&> \ arg => case getExpr arg of
62+ expr@(IVar _ n) => mirror . maybeToEither expr $ lookup n conArgIdxs
63+ expr => Right expr
6264 ty <- case lhs of
6365 IVar _ lhsName => do let Nothing = lookupType lhsName -- TODO to support `lhsName` to be a type parameter of type `Type`
64- | Just found => pure found
66+ | Just found => pure $ pure found
6567 -- we haven't found, failing, there are at least two reasons
66- failAt (getFC lhs) $ if isNamespaced lhsName
67- then " Data type `\{lhsName}` is unavailable at the site of derivation (forgotten import?)"
68- else " Unsupported applications to a non-concrete type `\{lhsName}` in \{show con.name}"
69- IPrimVal _ (PrT t) => pure $ typeInfoForPrimType t
70- IType _ => pure typeInfoForTypeOfTypes
71- lhs@(IPi {}) => failAt (getFC lhs) " Fields with function types are not supported in constructors, like in \{show con.name}"
72- lhs => failAt (getFC lhs) " Unsupported type of a constructor's \{show con.name} field: \{show lhs}"
73- let Yes lengthCorrect = decEq ty. args. length args. length
74- | No _ => failAt (getFC lhs) " INTERNAL ERROR: wrong count of unapp when analysing type application"
75- _ <- ensureTyArgsNamed ty
76- let as = rewrite lengthCorrect in args. asVect <&> \ arg => case getExpr arg of
77- expr@(IVar _ n) => mirror . maybeToEither expr $ lookup n conArgIdxs
78- expr => Right expr
68+ if isNamespaced lhsName
69+ then failAt (getFC lhs) " Data type `\{lhsName}` is unavailable at the site of derivation (forgotten import?)"
70+ else pure $ Left (getFC lhs, " Unsupported applications to a non-concrete type `\{lhsName}` in \{show con.name}" )
71+ IPrimVal _ (PrT t) => pure $ pure $ typeInfoForPrimType t
72+ IType _ => pure $ pure typeInfoForTypeOfTypes
73+ lhs@(IPi {}) => pure $ Left (getFC lhs, " Fields with function types are not supported in constructors, like in \{show con.name}" )
74+ lhs => pure $ Left (getFC lhs, " Unsupported type of a constructor's \{show con.name} field: \{show lhs}" )
75+ ta <- Prelude . for ty $ \ ty : TypeInfo => do
76+ let Yes lengthCorrect = decEq ty. args. length args. length
77+ | No _ => failAt (getFC lhs) " INTERNAL ERROR: wrong count of unapp when analysing type application"
78+ _ <- ensureTyArgsNamed ty
79+ pure $ MkTypeApp ty $ rewrite lengthCorrect in as
7980 let strongDetermination = rights as. asList <&> mapMaybe (lookup ' conArgIdxs) . allVarNames
8081 let strongDeterminationWeight = concatMap @{Additive } (max 1 . length ) strongDetermination -- we add 1 for constant givens
8182 let stronglyDeterminedBy = fromList $ join strongDetermination
8283 let argsDependsOn = fromList (lefts as. asList) `difference` stronglyDeterminedBy
83- pure $ MkTypeApp ty as $ MkDetermination stronglyDeterminedBy argsDependsOn $ argsDependsOn. size + strongDeterminationWeight
84+ pure (ta, MkDetermination stronglyDeterminedBy argsDependsOn $ argsDependsOn. size + strongDeterminationWeight)
8485
8586 for con. args. asVect $ analyseTypeApp . type
8687
@@ -229,7 +230,7 @@ export
229230 -- -----------------------------------------------------------
230231
231232 -- Compute left-to-right need of generation when there are non-trivial types at the left
232- argsTypeApps <- getTypeApps con
233+ ( argsTypeApps, argsDeterms) <- unzip <$> getTypeApps con
233234
234235 -- Decide how constructor arguments would be named during generation
235236 let bindNames = argName' <$> fromList con. args
@@ -252,7 +253,8 @@ export
252253 genForOrder order = map (foldr apply callCons) $ evalStateT givs $ for order $ \ genedArg => do
253254
254255 -- Get info for the `genedArg`
255- let MkTypeApp typeOfGened argsOfTypeOfGened _ = index genedArg $ the (Vect _ $ TypeApp con) argsTypeApps
256+ let Right $ MkTypeApp typeOfGened argsOfTypeOfGened = index genedArg $ the (Vect _ $ Either _ $ TypeApp con) argsTypeApps
257+ | Left (fc, str) => failAt fc str
256258
257259 -- Acquire the set of arguments that are already present
258260 presentArguments <- get
@@ -308,7 +310,7 @@ export
308310 --------------------------------------------
309311
310312 -- Compute determination map without weak determination information
311- let determ = insertFrom' empty $ mapI ( \ i, ta => (i, ta . determ)) argsTypeApps
313+ let determ = insertFrom' empty $ withIndex argsDeterms
312314
313315 logPoint Debug " deptycheck.derive.least-effort" [sig, con] " - determ: \{determ}"
314316 logPoint Debug " deptycheck.derive.least-effort" [sig, con] " - givs: \{givs}"
0 commit comments