Fork me on GitHub
#test-check
<
2019-11-03
>
leonoel09:11:21

does the empty generator exist ?

andy.fingerhut16:11:23

meaning, fails to generate anything?

andy.fingerhut16:11:03

What would you expect it to do if you tried to cause it to generate a value? Throw an exception?

gfredericks16:11:10

Can make it with such-that, if so

andy.fingerhut16:11:07

Can you describe a situation where it would be useful to have a generator that doesn't generate anything? I'm lacking imagination there.

leonoel16:11:43

I'm working on regular languages, and I'm willing to construct a generator for words of a given language

leonoel16:11:26

so, does it makes sense to have a generator for words of the empty language ?

andy.fingerhut16:11:57

Would it just throw an exception when you tried to generate such a word?

leonoel16:11:19

no, because it's not error

andy.fingerhut16:11:36

If it returns a value, that would be interpreted as a generated valid value, wouldn't it?

gfredericks16:11:37

What would it generate then?

leonoel16:11:23

it would generate nothing

andy.fingerhut16:11:43

What would a call to samples return? (edit: I meant a call to gen/sample)

leonoel16:11:57

an empty list

gfredericks16:11:12

There's no such thing as generating nothing

gfredericks16:11:57

Similar to there's no such thing as a function that returns nothing

andy.fingerhut16:11:21

e.g. a generator that returns nil is thereby indicating: nil is a value that satisfies the desired predicate. Not that there is no valid value satisfying the predicate.

gfredericks17:11:42

I guess a downside of using a throwing generator for the empty language is that test.check wouldn't let you do things like create a disjunction using gen/one-of like you'd want to

gfredericks17:11:06

That would just give you a generator that threw half the time, which is obviously wrong

leonoel17:11:14

I don't fully understand why it doesn't make sense, and why e.g (g/one-of []) is forbidden.

leonoel17:11:47

this is really a noob question, I'm not really familiar with this topic

gfredericks17:11:59

This might be a feature that could be added, but it would have to pervade a lot of the combinators

gfredericks17:11:46

Like one-of would have to filter out empty generators from its input, for example

gfredericks17:11:03

fmap and bind would have to short circuit

gfredericks17:11:09

And on and on

andy.fingerhut17:11:54

Well, if you used such-that with an arbitrary function predicate that always returned false, test.check would have no general way to identify all possible generate-nothing generators. It could only recognize ones that you made obvious were generating-nothing generators.

gfredericks17:11:17

Which is why the pervasive support is important

gfredericks17:11:30

To track emptiness

andy.fingerhut17:11:08

So one-of would have to try invoking empty generators, and have a way of getting back "I didn't generate anything", wouldn't it? i.e. in order to enable arbitrary empty gens.

gfredericks17:11:40

I guess you could do it that way

gfredericks17:11:11

And this would still be a generator that throws when you try to generate, so if that can't be useful to you, then even this idea wouldn't help

gfredericks17:11:05

Not sure how that would affect the usability of such-that

andy.fingerhut17:11:37

Not recommending that approach, necessarily, but it seems like one approach, the other being to say something like "empty gens using such-that with arbitrary Clojure functions as predicates may throw exceptions, or some other behavior you might not like, if you try to use them"

gfredericks17:11:39

That's a pretty deep breaking change, though, so I doubt it would be added as a feature

leonoel17:11:47

is (g/such-that (constantly false) generator) a terrible idea ?

gfredericks17:11:07

No, but it throws, and you said you didn't want that

gfredericks17:11:29

Also it doesn't compose like I suspect you'd want for regular languages

gfredericks17:11:11

But if you use it without composing, it'd work fine

leonoel17:11:23

that matches my intuition

leonoel17:11:20

I ended up working with nilable generators, and it's not that clumsy so I'll keep it that way

gfredericks17:11:16

Meaning a generator that generates nil, or a context where you either have a generator or you have nil?

leonoel17:11:31

the latter

leonoel17:11:15

OK thank you for clarification

👍 4