Skip to content

Commit bbeb21a

Browse files
committed
[ experim ] Nail down every implementation of derivation interfaces
1 parent 8340e4a commit bbeb21a

7 files changed

Lines changed: 18 additions & 88 deletions

File tree

src/Deriving/DepTyCheck/Gen.idr

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ wrapFuel fuelArg = lam $ MkArg MW ExplicitArg (Just fuelArg) `(Data.Fuel.Fuel)
297297
------------------------------
298298

299299
export
300-
deriveGenExpr : DeriveBodyForType => (signature : TTImp) -> Elab TTImp
300+
deriveGenExpr : (signature : TTImp) -> Elab TTImp
301301
deriveGenExpr signature = do
302302
checkResult@(signature ** externals ** _) <- checkTypeIsGen DerivationTask signature
303303
let externalsSigToName = fromList $ externals.externals <&> \(sig, _) => (sig, nameForGen sig)
@@ -345,7 +345,7 @@ deriveGenExpr signature = do
345345
|||
346346
|||
347347
export %macro
348-
deriveGen : DeriveBodyForType => Elab a
348+
deriveGen : Elab a
349349
deriveGen = do
350350
Just signature <- goal
351351
| Nothing => fail "The goal signature is not found. Generators derivation must be used only for fully defined signatures"
@@ -365,7 +365,7 @@ deriveGen = do
365365
||| genX = deriveGenFor $ Fuel -> (Fuel -> Gen Y) => (a : A) -> (c : C) -> Gen (b ** X a b c)
366366
||| ```
367367
export %macro
368-
deriveGenFor : DeriveBodyForType => (0 a : Type) -> Elab a
368+
deriveGenFor : (0 a : Type) -> Elab a
369369
deriveGenFor a = do
370370
sig <- quote a
371371
tt <- deriveGenExpr sig
@@ -376,7 +376,7 @@ deriveGenFor a = do
376376
||| Caution! When `logDerivation` is set to `True`, this function would change the global logging state
377377
||| and wouldn't turn it back.
378378
export
379-
deriveGenPrinter : {default True printTTImp : _} -> {default True logDerivation : _} -> DeriveBodyForType => Type -> Elab Unit
379+
deriveGenPrinter : {default True printTTImp : _} -> {default True logDerivation : _} -> Type -> Elab Unit
380380
deriveGenPrinter ty = do
381381
ty <- quote ty
382382
when logDerivation $ declare `[%logging "deptycheck.derive.print" 5; %logging "deptycheck.derive.least-effort" 7]
@@ -391,12 +391,3 @@ deriveGenPrinter ty = do
391391
putStr $ if ~printTTImp then interpolate ~expr else show ~expr
392392
putStrLn ""
393393
]
394-
395-
-----------------------
396-
--- Global defaults ---
397-
-----------------------
398-
399-
%defaulthint %inline
400-
public export
401-
DefaultConstructorDerivator : DeriveBodyRhsForCon
402-
DefaultConstructorDerivator = LeastEffort

src/Deriving/DepTyCheck/Gen/ForAllNeededTypes/Impl.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ considerNewType ty = do
5050
_ : (NamesInfoInTypes, ConsRecs) <- get
5151
when .| not (isTypeKnown ty) .| updateNamesAndConsRecs ty >>= put
5252

53-
Deriving.DepTyCheck.Gen.ForAllNeededTypes.Interface.DerivationClosure m = (DeriveBodyForType, Monad m, Elaboration m, ClosuringContext m)
53+
Deriving.DepTyCheck.Gen.ForAllNeededTypes.Interface.DerivationClosure m = (Monad m, Elaboration m, ClosuringContext m)
5454

5555
Deriving.DepTyCheck.Gen.ForAllNeededTypes.Interface.needWeightFun ty = when (not !(gets $ contains ty.name)) $ do
5656
modify {stateType=SortedSet Name} $ insert ty.name
@@ -124,7 +124,7 @@ Deriving.DepTyCheck.Gen.ForAllNeededTypes.Interface.callGen sig fuel values = do
124124
--- Canonic-dischagring function ---
125125

126126
export
127-
runCanonic : DeriveBodyForType => NamesInfoInTypes => ConsRecs =>
127+
runCanonic : NamesInfoInTypes => ConsRecs =>
128128
SortedMap ExternalGenSignature Name -> (forall m. DerivationClosure m => m a) -> Elab (a, List Decl)
129129
runCanonic exts calc = do
130130
let exts = SortedMap.fromList $ exts.asList <&> \namedSig => (fst $ internalise $ fst namedSig, namedSig)

src/Deriving/DepTyCheck/Gen/ForOneType/Impl.idr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ canonicDefaultRHS = canonicDefaultRHS' id
3636
--- Derivation functions ---
3737
----------------------------
3838

39-
export
40-
DeriveBodyRhsForCon => DeriveBodyForType where
41-
canonicBody sig n = do
39+
Deriving.DepTyCheck.Gen.ForOneType.Interface.canonicBody sig n = do
4240

4341
-- check that there is at least one constructor
4442
Prelude.when .| null sig.targetType.cons .| fail "No constructors found for the type `\{show sig.targetType.name}`"
@@ -107,7 +105,3 @@ DeriveBodyRhsForCon => DeriveBodyForType where
107105

108106
callConsGen : (fuel : TTImp) -> Con -> TTImp
109107
callConsGen fuel con = canonicDefaultRHS' interimNamesWrapper sig .| consGenName con .| fuel
110-
111-
export
112-
MainCoreDerivator : DeriveBodyRhsForCon => DeriveBodyForType
113-
MainCoreDerivator = %search

src/Deriving/DepTyCheck/Gen/ForOneType/Interface.idr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ import public Deriving.DepTyCheck.Gen.Signature
99
--- Deriving body of a generator for a single type ---
1010
------------------------------------------------------
1111

12-
public export
13-
interface DeriveBodyForType where
14-
canonicBody : DerivationClosure m => Elaboration m => NamesInfoInTypes => ConsRecs => GenSignature -> Name -> m $ List Clause
12+
export
13+
canonicBody : DerivationClosure m => Elaboration m => NamesInfoInTypes => ConsRecs => GenSignature -> Name -> m $ List Clause

src/Deriving/DepTyCheck/Gen/ForOneTypeCon/Impl.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ isSimpleBindVar $ IBindVar {} = True
2020
isSimpleBindVar _ = False
2121

2222
export
23-
canonicConsBody : DeriveBodyRhsForCon => DerivationClosure m => Elaboration m => NamesInfoInTypes => GenSignature -> Name -> Con -> m $ List Clause
23+
canonicConsBody : DerivationClosure m => Elaboration m => NamesInfoInTypes => GenSignature -> Name -> Con -> m $ List Clause
2424
canonicConsBody sig name con = do
2525

2626
-- Get file position of the constructor definition (for better error reporting)

src/Deriving/DepTyCheck/Gen/ForOneTypeConRhs/Impl.idr

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,11 @@ callCon con = reAppAny (var con.name) . toList . mapI (appArg . index' con.args)
216216
-- "Non-obligatory" means that some present external generator of some type
217217
-- may be ignored even if its type is really used in a generated data constructor.
218218

219-
||| Least-effort non-obligatory tactic is one which *does not use externals* during taking a decision on the order.
220-
||| It uses externals if decided order happens to be given by an external generator, but is not obliged to use any.
221-
||| It is seemingly most simple to implement, maybe the fastest and
222-
||| fits well when external generators are provided for non-dependent types.
223-
export
224-
[LeastEffort] DeriveBodyRhsForCon where
225-
consGenExpr sig con givs fuel = do
219+
-- Least-effort non-obligatory tactic is one which *does not use externals* during taking a decision on the order.
220+
-- It uses externals if decided order happens to be given by an external generator, but is not obliged to use any.
221+
-- It is seemingly most simple to implement, maybe the fastest and
222+
-- fits well when external generators are provided for non-dependent types.
223+
Deriving.DepTyCheck.Gen.ForOneTypeConRhs.Interface.consGenExpr sig con givs fuel = do
226224

227225
-------------------------------------------------------------
228226
-- Prepare intermediate data and functions using this data --
@@ -355,54 +353,3 @@ export
355353
let df = believe_me $ deriveFirst @{impl} (rewrite tyLen in Prelude.toList sig.givenParams) (rewrite conLen in Prelude.toList givs)
356354
let userImposed = filter (not . contains' givs) $ nub $ conArgIdx <$> df
357355
logValue FineDetails "deptycheck.derive.least-effort" [sig, con] "- user-imposed: \{userImposed}" userImposed
358-
359-
--||| Best effort non-obligatory tactic tries to use as much external generators as possible
360-
--||| but discards some there is a conflict between them.
361-
--||| All possible non-conflicting layouts may be produced in the generated values list.
362-
--|||
363-
--||| E.g. when we have external generators ``(a : _) -> (b : T ** C a b)`` and ``(b : T ** D b)`` and
364-
--||| a constructor of form ``C a b -> D b -> ...``, we can use values from both pairs
365-
--||| ``(a : _) -> (b : T ** C a b)`` with ``(b : T) -> D b`` and
366-
--||| ``(a : _) -> (b : T) -> C a b`` with ``(b : T ** D b)``,
367-
--||| i.e. to use both of external generators to form the generated values list
368-
--||| but not obligatorily all the external generators at the same time.
369-
--export
370-
--[BestEffort] DeriveBodyRhsForCon where
371-
-- consGenExpr sig con givs fuel = do
372-
-- ?cons_body_besteff_nonoblig_rhs
373-
374-
--------------------------
375-
--- Obligatory tactics ---
376-
--------------------------
377-
378-
-- "Obligatory" means that is some external generator is present and a constructor has
379-
-- an argument of a type which is generated by this external generator, it must be used
380-
-- in the constructor's generator.
381-
--
382-
-- Dependent types are important here, i.e. generator ``(a : _) -> (b ** C a b)``
383-
-- is considered to be a generator for the type ``C``.
384-
-- The problem with obligatory generators is that some external generators may be incompatible.
385-
--
386-
-- E.g. once we have ``(a : _) -> (b ** C a b)`` and ``(a ** b ** C a b)`` at the same time,
387-
-- once ``C`` is used in the same constructor, we cannot guarantee that we will use both external generators.
388-
--
389-
-- The same problem is present once we have external generators for ``(a : _) -> (b : T ** C a b)`` and ``(b : T ** D b)`` at the same time,
390-
-- and both ``C`` and ``D`` are used in the same constructor with the same parameter of type ``T``,
391-
-- i.e. when constructor have something like ``C a b -> D b -> ...``.
392-
--
393-
-- Notice, that this problem does not arise in constructors of type ``C a b1 -> D b2 -> ...``
394-
--
395-
-- In this case, we cannot decide in general which value of type ``T`` to be used for generation is we have to use both generators.
396-
-- We can either fail to generate a value for such constructor (``FailFast`` tactic),
397-
-- or alternatively we can try to match all the generated values of type ``T`` from both generators
398-
-- using ``DecEq`` and leave only intersection (``DecEqConflicts`` tactic).
399-
400-
--export
401-
--[FailFast] DeriveBodyRhsForCon where
402-
-- consGenExpr sig con givs fuel = do
403-
-- ?cons_body_obl_ff_rhs
404-
405-
--export
406-
--[DecEqConflicts] DeriveBodyRhsForCon where
407-
-- consGenExpr sig con givs fuel = do
408-
-- ?cons_body_obl_deceq_rhs

src/Deriving/DepTyCheck/Gen/ForOneTypeConRhs/Interface.idr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import public Deriving.DepTyCheck.Gen.ForAllNeededTypes.Interface
1010

1111
--- Interface ---
1212

13-
public export
14-
interface DeriveBodyRhsForCon where
15-
consGenExpr : DerivationClosure m => Elaboration m => NamesInfoInTypes =>
16-
GenSignature -> (con : Con) -> (given : SortedSet $ Fin con.args.length) -> (fuel : TTImp) -> m TTImp
13+
export
14+
consGenExpr : DerivationClosure m => Elaboration m => NamesInfoInTypes =>
15+
GenSignature -> (con : Con) -> (given : SortedSet $ Fin con.args.length) -> (fuel : TTImp) -> m TTImp

0 commit comments

Comments
 (0)