You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The idea, as far as I understand it, is to generate a list of mappings from a to b, and convert that list to a function.
There are a bunch of functions to change these: map works as usual, to change the b into c, but we can also have coMap (profunctor?) that works on the input instead of on the output: given a z -> a function, it changes a -> b to z -> b.
The simplest generated functions map all values to some default value, and more complicated functions have exceptions from the default (ie. "map 9 to 1, map everything else to 0"). Such functions with exceptions do shrink towards the "no exception" case (a constant function).
A classic poster child example of "hey we can generate functions" in the PBT context is testing that map f (filter p xs) == filter p (map f xs). (This is not true.)
The easier way to do this is to have mutability under the hood and simply run the fuzzer for the output type every time the function is called with an input you haven't seen before. Then you can print the resulting table.
The other option I've done in Elm is ask the user for a hashing function for the input type, then use that as the seed for the output type generator. This works well, but a) the ergonomics leave something to be desired b) there is no way to print a table, since it's fully the size of the input.
Neither option is that great and neither is obvious how to shrink them.
If I understand properly, what the proposal here roughly is you generate a positive integer n, then generate n inputs and n+1 outputs, then see if the function input matches one of the generated inputs, in which case return the corresponding output, otherwise return the final output? Shrinking is then making n smaller and shrinking the outputs1. Printing is also relatively straightforward since you can just print the table (or turn it into semi-valid elm by interspersing if then else).
I guess what that would require is to make fuzzers and tests have a separate representation for executing tests and a separate representation for printing them (which could be useful for other things).
Footnotes
Question: do you also shrink the inputs? Seems like a tricky piece of business with this approach will be shrinking efficiently. If a test both provides a function that acts on some type and a value of that type, then for the effect to keep persisting, one would somehow need to shrink both in lockstep... ↩
AST seems quite difficult to me and difficult to generalize. Like what would the nodes be if the user is trying to do some arbitrary datatype?
I should also point out that the main downside with the table based solution is that it requires equality on the inputs. This makes higher order functions in particular impossible.
I suppose one thing we could try is sort of a defunctionalized approach, where the user supplies operations and we automatically construct a graph out of them:
type Op=AddOpOp|MutliplyOpOp|NegateOp|ConstantInt|Arg1eval:Op->Int->Inteval op arg1 =case op ofAdd a b ->
eval a arg1 + eval b arg1
Mutliply a b ->
eval a arg1 * eval b arg1
Negate a ->-(eval a arg1)Constant num ->
num
Arg1->
arg1
fuzzer:FuzzOpfuzzer =Fuzz.graph
[Fuzz.Graph.op2 Add,Fuzz.Graph.op2 Multiply,Fuzz.Graph.op1 Negate,Fuzz.Graph.constF (Fuzz.map ConstantFuzz.int),Fuzz.Graph.const Arg1]test:Testtest =Test.fuzz3 fuzzer fuzzer (Fuzz.list Fuzz.int)"map f >> map g == map (f >> g)"<|\fO gO inp ->let
f = eval fO
g = eval gO
in
map f (map g inp)|>Expect.equal (map (f >> g) inp)
or some such. Seems like a lot of manual work to get any sort of "interesting" behavior. However, this sort of defunctionlized fuzzing could be quite useful for constructing app tests, especially if we could embelish the graph fuzzer with state and pre/post conditions.
I haven't thought about feasibility / usability much, but (from the user's perspective) I'm imagining Fuzz.function : Fuzzer a -> Fuzzer b -> Fuzzer (a -> b) and:
test:Testtest =Test.fuzz3 "example test"(myStructureFuzzer Fuzz.int)(Fuzz.function Fuzz.int Fuzz.int)(Fuzz.function Fuzz.int Fuzz.char)<|\input f g ->(input |>MyStructure.map f |>MyStructure.map g)|>Expect.equal (input |>MyStructure.map (f >> g))
Yeah that's more the table approach what you sketched.
All I was trying to point out with the defunctionalized AST is an alternative that might have other usecases, yet still be somewhat useful for function fuzzing.
BTW examples of fuzzers that don't shrink well could be a good fodder for some optimizations.
Hypothesis has a bunch of tricks that we don't yet use (like labeled ranges, it basically knows exactly how many (and which) RandomRun integers a given fuzzed value spans, allowing for precise deletions instead of trying to get lucky by deleting 8,4,3,2,1-sized chunks) and improving things will be easier when we have an example that doesn't shrink well yet.
Basically could be yet another benchmark suite! :)
Yeah this is kind of a terrible thing to ask shrinkers to do, because this is effectively a random walk with backtracking, so chances are there are good chunks of the random run that are effectively useless (since they dictated a path that was discarded by backtracking), but there is no way for my fuzzer to communicate that back. However, since you're fuzzing a program, even small changes that do prove to be significant can lead to drastically different behavior.
The other thing this would really benefit from is if we exposed an API to allow attaching a pretty printer to a fuzzer somehow. This approaches ADT is pretty low level (which in tern makes implementing tractable), but it would really make it a lot easier to debug if you could turn
Ap(Ap(Var"+")(Constant(IntV1)))(Constant(IntV2))
into
1+2
in the failure given.
Since we'd likely need something like that for the other approaches (after all printing <internals> isn't terribly useful for functions), maybe that could be the first step to design here.
There are some PRs already open for that line of reasoning I believe, yes it would be nice if we could give test writers a better API for more control. (a -> String) and other ways to customize the tests.
Activity
gampleman commentedon Sep 30, 2026
I've done this twice before.
The easier way to do this is to have mutability under the hood and simply run the fuzzer for the output type every time the function is called with an input you haven't seen before. Then you can print the resulting table.
The other option I've done in Elm is ask the user for a hashing function for the input type, then use that as the seed for the output type generator. This works well, but a) the ergonomics leave something to be desired b) there is no way to print a table, since it's fully the size of the input.
Neither option is that great and neither is obvious how to shrink them.
If I understand properly, what the proposal here roughly is you generate a positive integer
n, then generateninputs andn+1outputs, then see if the function input matches one of the generated inputs, in which case return the corresponding output, otherwise return the final output? Shrinking is then makingnsmaller and shrinking the outputs1. Printing is also relatively straightforward since you can just print the table (or turn it into semi-valid elm by interspersingif then else).I guess what that would require is to make fuzzers and tests have a separate representation for executing tests and a separate representation for printing them (which could be useful for other things).
Footnotes
Question: do you also shrink the inputs? Seems like a tricky piece of business with this approach will be shrinking efficiently. If a test both provides a function that acts on some type and a value of that type, then for the effect to keep persisting, one would somehow need to shrink both in lockstep... ↩
Janiczek commentedon Oct 1, 2026
Interesting, my first thought would be to generate some kind of AST that you'd then interpret to get the final generated function.
gampleman commentedon Oct 1, 2026
AST seems quite difficult to me and difficult to generalize. Like what would the nodes be if the user is trying to do some arbitrary datatype?
I should also point out that the main downside with the table based solution is that it requires equality on the inputs. This makes higher order functions in particular impossible.
gampleman commentedon Oct 1, 2026
I suppose one thing we could try is sort of a defunctionalized approach, where the user supplies operations and we automatically construct a graph out of them:
or some such. Seems like a lot of manual work to get any sort of "interesting" behavior. However, this sort of defunctionlized fuzzing could be quite useful for constructing app tests, especially if we could embelish the graph fuzzer with state and pre/post conditions.
Janiczek commentedon Oct 1, 2026
I haven't thought about feasibility / usability much, but (from the user's perspective) I'm imagining
Fuzz.function : Fuzzer a -> Fuzzer b -> Fuzzer (a -> b)and:ie. no ASTs they'd need to care about.
gampleman commentedon Oct 1, 2026
Yeah that's more the table approach what you sketched.
All I was trying to point out with the defunctionalized AST is an alternative that might have other usecases, yet still be somewhat useful for function fuzzing.
gampleman commentedon Oct 7, 2026
I managed to nerd-snipe myself into building the defunctionalized approach as a little experiment: gampleman/elm-experiments@4b296a9
I haven't yet figured out how to allow it to generate safe recursion.
But it did manage to disprove the monadic laws 🤣
(aka NaN strikes again)
Janiczek commentedon Oct 9, 2026
BTW examples of fuzzers that don't shrink well could be a good fodder for some optimizations.
Hypothesis has a bunch of tricks that we don't yet use (like labeled ranges, it basically knows exactly how many (and which) RandomRun integers a given fuzzed value spans, allowing for precise deletions instead of trying to get lucky by deleting 8,4,3,2,1-sized chunks) and improving things will be easier when we have an example that doesn't shrink well yet.
Basically could be yet another benchmark suite! :)
gampleman commentedon Oct 12, 2026
Yeah this is kind of a terrible thing to ask shrinkers to do, because this is effectively a random walk with backtracking, so chances are there are good chunks of the random run that are effectively useless (since they dictated a path that was discarded by backtracking), but there is no way for my fuzzer to communicate that back. However, since you're fuzzing a program, even small changes that do prove to be significant can lead to drastically different behavior.
The other thing this would really benefit from is if we exposed an API to allow attaching a pretty printer to a fuzzer somehow. This approaches ADT is pretty low level (which in tern makes implementing tractable), but it would really make it a lot easier to debug if you could turn
into
in the failure given.
Since we'd likely need something like that for the other approaches (after all printing
<internals>isn't terribly useful for functions), maybe that could be the first step to design here.Janiczek commentedon Oct 12, 2026
There are some PRs already open for that line of reasoning I believe, yes it would be nice if we could give test writers a better API for more control.
(a -> String)and other ways to customize the tests.gampleman commentedon Oct 13, 2026
I can't find any. Happy to give it a stab if they don't exist or are unsuitable for some reason.
Janiczek commentedon Oct 13, 2026
Apologies, it was issues, not PRs. #239, #240, your #277.