(Rhetorical questions which are not asking for clarification to the question:) How do you define "rest"? Do you mean a physical or perceptional phenomenon such as lack of sound or an abrupt end of sound, or an element in musical notation i.e. written instructions to musicians? If you play whole-notes with a xylophone sound, at tempo 20 bpm, do you have "rests" or not? Most of the time there's no sound at all, but no rests are written. Looking at the audio, you probably can't even specify an exact time when each note ends. If you cannot distinguish symbols in playing instructions from perceptional events, you will stay confused.
The perception of rhythm is defined by transients. A transient is a change in sound that's short enough to be perceived as having a clear timing. Transients occur in all aspects of music - pitch, harmony, timbre, dynamics, lyrics. You've probably heard of something called "harmonic rhythm". When an organist plays constantly changing chords without the sound ever completely stopping, you still hear a rhythm in the chord changes. No rests, no pauses, just transients in harmony. The same goes for, say, timbre. Control a filter cutoff frequency with a square-wave LFO, and you have a very clear rhythm, even though the sound never cuts out, never rests.
The abrupt ending of a sound can be a significant rhythm-defining transient, particularly for bass and other comping instruments. For a snappy sound that gradually fades out, there is only a clear beginning transient.
Melody, what is that? Musical elements can be categorized and perceived as working in either a leading or a supporting role. Any single central attention-grabbing, relatable, leading musical element can be a melody. Maybe not an official lawful wedded and blessed-by-a-priest melody, but a common-law melody. Cultural conventions and copyright-legal definitions may disagree, but even a rhythm in itself can be the melody that practically everybody remembers and can reproduce, like the stomp-stomp-clap rhythm in "We Will Rock You" by Queen. Though once the singing starts, the same rhythm gets in more of a supporting role.
How "melodic" music is, is defined by how well its elements can remembered, related to and reproduced by people. In pop music of today, there are lots of loudly played timed pitch changes that are so completely unmelodic, forgettable and indifferent that calling them melodies is disgraceful. Legally they may be melodies, who knows. It's subjective too. What things are melodic to you? A person coming from a different culture may consider your most memorable "melody" as being just noise. You have to separate theory and practice.
In addition to melodic rhythm, i.e. a rhythm that grabs your attention and makes you perceive it as belonging to the main "story" of the music, transients have other, supporting rhythmic roles, particularly pulse and swaying and harmonic rhythm. These provide context for other elements and they can be reflected as different meters in notation, but again, notation and actual audible music are not equivalent.
To sum it up, rests can be used for rhythmic purposes and a melody usually has a significant rhythmic element to it. But a melody doesn't necessarily need any written or perceived rests to be a melody.
Then the programming aspect. When listening to beginner players improvising, or songs they've written and produced, it often makes me feel that they can't make heads or tails of what they're doing. They can't tell if something is a leading or a supporting element. They may think they're accompanying a melody when in fact they're playing attention-seeking distracting things, competing melodies, producing a chaotic mess. Or they're supposed to play a solo, but all that comes out is indifferent, random note salad with no melodic effect, no story. The same goes for many generative music efforts. Rests, yes, it's probably good to have some kind of a rest feature in a music program, but what you can do with them - many different things, or nothing at all, if you want.
To write a computer program that's able to produce an actual melody and supporting accompaniment to it, how is the program going to know if its accompaniment will be perceived as melodic and competing with the lead melody? How can it tell that the assigned "melody" is even melodic and memorable at all? If you want to construct a "formula" for this, I think you should do it like many song-writers do, from very large building blocks such as chord changes and chord tones, meter, lyrical form and song form.
How to use rests: for your lead melody, add rests in places where the story-telling lead singer needs to breathe. For accompaniment, use rests to support the pulse and rhythmic swaying. For example, cut bass notes at snare hits.