# Process the mode/type/determinism declarations into executable code

**URL:** <https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930>\
**Category:** Help!\
**Created:** [July 7, 2019, 5:23pm UTC](https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930 "2019-07-07T17:23:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 7, 2019, 5:23pm UTC](https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930/1 "2019-07-07T17:23:05Z")

</div>

> [@Bridge to Mercury](https://swi-prolog.discourse.group/t/bridge-to-mercury/923/16):
>
> If you want to say that mode and type declarations are an important part of documentation/specification, I fully agree.

“How hard would it be” to process the mode/type/determinism declarations into executable code, so that I’d get a run-time error if I violated the declarations? I’m guessing that the new “wrapper” facility is sufficient, but there would need to be a change to the parser, to get the `%!` comments (there must be a hook for this, but I can’t find it in the docs).

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [July 7, 2019, 5:44pm UTC](https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930/2 "2019-07-07T17:44:48Z")

</div>

This is implemented by `library(pldoc/doc_modes))`, mode/2. You do need to call

```
:- doc_collect(true).

```

before loading your code as otherwise PlDoc structured comments are processed as normal comments and thus ignored.

P.s. Please switch topics if you want to continue this

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 7, 2019, 5:49pm UTC](https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930/3 "2019-07-07T17:49:11Z")

</div>

I split the thread into a new topic; hope both of you don’t mind.

---

<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:** [July 7, 2019, 7:08pm UTC](https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930/4 "2019-07-07T19:08:59Z")

</div>

Hi Peter,

btw, in Mercury they also have a kind of case construct and the compiler can check if all cases have been covered as per type and non-determinism provided and flag when one or some cases are missing.

Dan

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 7, 2019, 7:39pm UTC](https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930/5 "2019-07-07T19:39:08Z")

</div>

> [@grossdan](#):
>
> Mercury they also have a kind of case construct and the compiler can check if all cases have been covered as per type and non-determinism provided and flag when one or some cases are missing.

That requires quite a bit of type declaration machinery (which could be a good idea with Prolog, but which doesn’t exist right now); I’ll settle for the Erlang solution of crashing if no clause matches. Unit tests ought to flush those out. 😉

The determinism case is both an efficiency issue (not good to leave unnecessary choice points lying around) but also a correctness one – if the code can backtrack into something I think is deterministic, there’s probably a nasty and difficult to detect latent bug.

---

<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:** [July 7, 2019, 7:56pm UTC](https://swi-prolog.discourse.group/t/process-the-mode-type-determinism-declarations-into-executable-code/930/6 "2019-07-07T19:56:07Z")

</div>

> The determinism case is both an efficiency issue (not good to leave unnecessary choice points lying around) but also a correctness one – if the code can backtrack into something I think is deterministic, there’s probably a nasty and difficult to detect latent bug.

Good point.

I must have learned something since last year; I would nor have understood your comment a year ago 🙂

Dan
