0 | module Deriving.DepTyCheck.Gen.Tuning
  1 |
  2 | import public Data.Nat1
  3 |
  4 | import public Language.Reflection.Compat.Constr
  5 | import public Language.Reflection.Expr
  6 |
  7 | import public Syntax.IHateParens.List
  8 |
  9 | %default total
 10 |
 11 | ------------------------------------------
 12 | --- Facilities for manual order tuning ---
 13 | ------------------------------------------
 14 |
 15 | namespace GenOrder
 16 |
 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.
 65 |   public export
 66 |   interface GenOrderTuning (0 n : Name) where
 67 |     isConstructor : (con : IsConstructor n ** GenuineProof con)
 68 |     deriveFirst : (givenTyArgs : List $ Fin isConstructor.fst.typeInfo.args.length) ->
 69 |                   (givenConArgs : List $ Fin isConstructor.fst.conInfo.args.length) ->
 70 |                   List $ ConArg isConstructor.fst.conInfo
 71 |
 72 | -------------------------------
 73 | --- Tuning of probabilities ---
 74 | -------------------------------
 75 |
 76 | namespace Probability
 77 |
 78 |   public export
 79 |   interface ProbabilityTuning (0 n : Name) where
 80 |     constructor MkProbabilityTuning
 81 |     0 isConstructor : (con : IsConstructor n ** GenuineProof con)
 82 |     tuneWeight : Nat1 -> Nat1
 83 |
 84 |   --- Single-argument variant ---
 85 |
 86 |   workConName : Name -> Elab (Name, TTImp)
 87 |   workConName conName = do
 88 |     conName <- dataCon conName
 89 |     (conName,) . cleanupNamedHoles <$> quote conName
 90 |
 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 ()
120 |   tuneProbability con tuning = do
121 |     tuning <- cleanupNamedHoles <$> quote tuning
122 |     conExpr <- cleanupNamedHoles <$> quote con
123 |     (conName, conNameExpr) : (Name, TTImp) <- case conExpr of
124 |       IVar _ conName => workConName conName
125 |       IPrimVal _ (Str conNameStr) => workConName $ fromString conNameStr
126 |       _ => case !(catch $ check {expected = Name} conExpr) of
127 |              Just conName => workConName conName
128 |              Nothing => failAt (getFC conExpr) "Must be a constructor expression or constructor's name, got \{show conExpr}"
129 |     let implName = UN $ Basic "\{show conName}^probability^tuning"
130 |     [] <- getType implName
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 |       ]
136 |
137 |   --- Multi-argument variant ---
138 |
139 |   export
140 |   interface TuneProbabilityArgs r where
141 |     doTuneProbabilities : Visibility -> Elab () -> r
142 |
143 |   export
144 |   TuneProbabilityArgs (Elab ()) where
145 |     doTuneProbabilities _ = id
146 |
147 |   export
148 |   TuneProbabilityArgs r => TuneProbabilityArgs ((con : a) -> (tuning : Nat1 -> Nat1) -> r) where
149 |     doTuneProbabilities visibility acc con tuning = doTuneProbabilities visibility $
150 |       acc >> tuneProbability {visibility} con tuning
151 |
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
167 |   tuneProbabilities : {default Public visibility : Visibility} -> TuneProbabilityArgs r => r
168 |   tuneProbabilities = doTuneProbabilities visibility $ pure ()
169 |