# When does assert fail? Running out of space?

**URL:** <https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681>\
**Category:** Help!\
**Created:** [December 31, 2019, 4:48pm UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681 "2019-12-31T16:48:23Z")\
**Posts on this page:** 8\
**Page:** 4

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [January 4, 2020, 5:27am UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/61 "2020-01-04T05:27:32Z")

</div>

> [@pmoura](#):
>
> Drawing lines in the sand and acting like there’s a wall serves no useful purpose.

In my personal and useless opinion, this is a very broad topic that deserves to be discussed somehow. By “this”, in this particular case, I mean “drawing lines in the sand and acting like there’s a wall”.

But this is not about Prolog, or Logtalk, strictly, and it is not about assert failing, or running out of space, and I am not even sure I have an appropriate transportation metaphor ready at the moment – it would at least allow me to introduce the topic to a technical audience.

For the “us versus them”, one place to start reading, very superficially, would be [“in-group and out-group”](https://en.wikipedia.org/wiki/In-group_and_out-group). See also the “See also” section at the bottom of the article.

For extra credit (and this concerns everyone reading this irrelevant and useless comment), compare and contrast:

- _How Things Should Be_™, a topic close to the heart of most people; and
- _How Things Are at the Moment_™, a topic universally hated.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [January 4, 2020, 12:20pm UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/62 "2020-01-04T12:20:29Z")

</div>

> [@jan](#):
>
> > Your alternative code doesn’t provide the same semantics (if you abstract the details of the example) as an error or failure at one of the steps doesn’t give a chance to the next step to handle it and instead jumps directly to the handler.
> 
> Yip. The expected interface does allow for that and that is (sometimes) a nice idea. The traditional exception interface just aborts the computation up to some parent willing to deal with the error. That is often also a good plan. Neither addresses distinguishing _who_ is interested in the error.

I update my related blog post ([Handling missing data - Logtalk](https://logtalk.org/2019/11/21/handling-missing-data.html)) with a discussion of the different (exception handling) semantics. Notably, using expected terms versus using `catch/3` or `setup_call_cleanup/3` wrappers.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [January 5, 2020, 7:16pm UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/63 "2020-01-05T19:16:19Z")

</div>

> [@pmoura](#):
>
> read\_stream\_to\_codes

Can you describe how this predicate is implemented?

the map expects a (expected?) closure – i wonder how this closure looks like …

thanks,

Dan

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [January 5, 2020, 8:00pm UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/64 "2020-01-05T20:00:15Z")

</div>

The `read_stream_to_codes/2` predicate used in that example is a library predicate documented at:  
[https://www.swi-prolog.org/pldoc/doc\_for?object=read\_stream\_to\_codes/2](https://www.swi-prolog.org/pldoc/doc_for?object=read_stream_to_codes/2)

The `expected/1::map/2` meta-predicate is documented at:  
[https://logtalk.org/library/expected\_1.html#map-2](https://logtalk.org/library/expected_1.html#map-2)

The `map/2` predicate takes a closure and construct a goal by appending two arguments: the stream value taken from the `ExpectedStream` parameter and the codes read from the stream and used as value for the expected term (`ExpectedCodes`) returned in the second argument. In the case that the attempt to open of the file generated an exception (e.g. non-existing file), `ExpectedCodes` is simply unified with `ExpectedStream`, thus passing along the unexpected exception.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [January 5, 2020, 8:05pm UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/65 "2020-01-05T20:05:15Z")

</div>

> [@pmoura](#):
>
> and the codes read from the stream

Aren’t the codes supplied as input argument as well, rather than read from the stream.

I now can see how the Stream is added to the Closure, but don’t yet understand how the Codes end up added as well …

Dan

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [January 5, 2020, 8:14pm UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/66 "2020-01-05T20:14:39Z")

</div>

The meta-predicate template for the predicate is `map(2,*)`. The predicate constructs and calls the goal `read_stream_to_codes(Stream, Codes)`. Assuming the call is successful, it unifies the variable `Codes` with the codes read from the stream, The `map/2` predicate wraps `Codes` in a new expected term and unifies it with its second argument.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [January 5, 2020, 8:19pm UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/67 "2020-01-05T20:19:43Z")

</div>

> [@pmoura](#):
>
> the predicate constructs and calls the goal `read_stream_to_codes(Stream, Codes)` .

Thank you.

It is clear that map constructs and calls the goal – what is missing here – at least in my mind – from where the second argument “Codes” comes from during construction.

The Stream is bound to ExpectedStream – if open succeeded; the closure read\_stream\_to\_codes has the Stream “appended”, which is taken from ExpectedStream – but, from where does map obtain and binds Codes as the second argument for the (read\_stream\_to\_codes) closure – a closure that expects two arguments.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [January 6, 2020, 9:18am UTC](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681/68 "2020-01-06T09:18:31Z")

</div>

> [@grossdan](#):
>
> It is clear that map constructs and calls the goal – what is missing here – at least in my mind – from where the second argument “Codes” comes from during construction.

The `Codes` variable is introduced by the `map/2` predicate. Look into its implementation; it should make it clear.

[Previous page](https://swi-prolog.discourse.group/t/when-does-assert-fail-running-out-of-space/1681.md?page=3)
