11 | ------------------------------------------
12 | --- Facilities for manual order tuning ---
13 | ------------------------------------------
17 | ||| A magic interface for tuning the order of generation in derived generators
18 | |||
19 | ||| This interface defines a function `isConstructor` which can be implemented only by calling a macro `itIsConstructor`.
20 | ||| If the argument of the implementation is not a name of a constructor, you will get a compilation error at this point.
21 | |||
22 | ||| `deriveFirst` function gets some information about given arguments in the current derivation task,
23 | ||| and returns constructor's arguments that must be generated first in the derived generator, in order.
24 | ||| If given arguments require some other arguments to be derived before them, these arguments would be generated appropriately.
25 | ||| Any repetitions, mentions of already given arguments and mentions of arguments that will be generated by arguments earlier in the list
26 | ||| will be simply ignored.
27 | |||
28 | ||| Constructor arguments can be given by their number or their name (using name literals).
29 | |||
30 | ||| Typical usage is the following:
31 | |||
32 | ||| ```idris
33 | ||| data X : Nat -> Nat -> Type where
34 | ||| X1 : (n, m : Nat) -> n `LT` m => X n m
35 | ||| X2 : X n n
36 | |||
37 | ||| GenOrderTuning "X1".dataCon where
38 | ||| isConstructor = itIsConstructor
39 | ||| deriveFirst gt gc = [`{m}, 2] -- 2 is the `LT` argument
40 | ||| ```
41 | |||
42 | ||| ---
43 | |||
44 | ||| This interface can be used for fine tuning when derivator could determine order not optimally.
45 | ||| For example, consider the following type:
46 | |||
47 | ||| ```idris
48 | ||| data Y : Type where
49 | ||| MkY : (first : Z (f n) a b) -> (second : Z (f n) b c) -> Y
50 | ||| ```
51 | |||
52 | ||| If for some reason we know that argument `b` is better to be generated with the argument `second` rather than with the argument `first`,
53 | ||| we can implement the following tuning for `MkY`:
54 | |||
55 | ||| ```idris
56 | ||| GenOrderTuning "MkY".dataCon where
57 | ||| isConstructor = itIsConstructor
58 | ||| deriveFirst _ _ = [`{second}]
59 | ||| ```
60 | |||
61 | ||| Please notice that there is no need to mention argument `first`,
62 | ||| and argument `n` before argument `second` despite it is needed for its generation.
63 | ||| Derivator will automatically understand arguments really needed to be generated before the given in by the `deriveFirst` function,
64 | ||| and after that it will decide the order of the rest arguments using its usual algorithm.
72 | -------------------------------
73 | --- Tuning of probabilities ---
74 | -------------------------------
84 | --- Single-argument variant ---
91 | ||| A function for tuning probability of a particular constructor relative to the default value or giving an absolute value.
92 | |||
93 | ||| This function takes a constructor to tweak and a function taking a default weight of a constuctor returning a final one.
94 | ||| Typical usage, considering that we want to have constructor `B` fire twice as often as the others, is the following.
95 | |||
96 | ||| ```idris
97 | ||| data X = A | B | C
98 | |||
99 | ||| %runElab tuneProbability B (*2)
100 | ||| ```
101 | |||
102 | ||| One can use both constructor itself, or its name, e.g.
103 | ||| ```idris
104 | ||| %runElab tuneProbability B (*2)
105 | ||| -- or --
106 | ||| %runElab tuneProbability "B" (*2)
107 | ||| -- or --
108 | ||| %runElab tuneProbability `{B} (*2)
109 | ||| ```
110 | |||
111 | ||| Each variant is fine, but, surely, only one is allowed for each constructor.
112 | |||
113 | ||| Please notice that this tuning will be actually used in derivation only when the result of this function
114 | ||| (which is an implementation of a special interface called `ProbabilityTuning`)
115 | ||| is visible at the site of calling to derivation.
116 | |||
117 | ||| Please also notice that the body of the given function must be computable *at compile-time* at the site of call to derivation.
118 | export
119 | tuneProbability : {default Public visibility : Visibility} -> (con : a) -> (tuning : Nat1 -> Nat1) -> Elab ()
128 | Nothing => failAt (getFC conExpr) "Must be a constructor expression or constructor's name, got \{show conExpr}"
131 | | _::_ => failAt (getFC conExpr) "Probability tuning for constructor \{show conName} is already defined"
132 | declare
133 | [ interfaceHint visibility implName `(Deriving.DepTyCheck.Gen.Tuning.Probability.ProbabilityTuning ~conNameExpr)
134 | , def implName $ pure $ var implName .= `(MkProbabilityTuning (itIsConstructor {n = ~conNameExpr}) ~tuning)
135 | ]
137 | --- Multi-argument variant ---
139 | export
143 | export
147 | export
148 | TuneProbabilityArgs r => TuneProbabilityArgs ((con : a) -> (tuning : Nat1 -> Nat1) -> r) where
152 | ||| A function for tuning probabilities of several constructors relative.
153 | |||
154 | ||| This function takes constructors to tweak and functions taking a default weight of a constuctor returning a final one.
155 | ||| Typical usage, considering that we want to have constructor `B` fire twice as often, and `C` thrice as often as the other, is the following.
156 | |||
157 | ||| ```idris
158 | ||| data X = A | B | C
159 | |||
160 | ||| %runElab tuneProbabilities
161 | ||| B (*2)
162 | ||| C (*3)
163 | ||| ```
164 | |||
165 | ||| Please refer to notices in the documentation of a function `tuneProbability`, they apply here too.
166 | export