# Phrase\_from\_file vs phrase

**URL:** <https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925>\
**Category:** Help!\
**Created:** [September 12, 2020, 12:44pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925 "2020-09-12T12:44:33Z")\
**Posts on this page:** 1\
**Showing post:** 31

<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:** [September 14, 2020, 11:52am UTC](https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925/31 "2020-09-14T11:52:57Z")

</div>

> [@Using phrase\_from\_file](https://swi-prolog.discourse.group/t/using-phrase-from-file/1880/1):
>
> It does, however, place certain restrictions on the DCG - particularly in handling eof.

Well, the only thing that works is unifying the lazy list with either `[_|_]` or `[]`. This is what DCGs do as long as you do not start dirty hacking, so there is not much of a problem. Things go wrong if you write e.g.

```
at_eof(End,End) :- End == [].

```

or the application expects the 3rd argument of phrase/2 to be a list and call e.g., length/2 on it.

> [@anniepoo](#):
>
> would it make sense to have a non-lazy phrase\_from\_file that supported the current line/char reporting?

Not that much. The way it works is a little tricky. When asked for the position it scans the list forward to the attributed variable. There it finds the position info for the start of the current block that enables it to compute the position for the current point in the list. This works fairly well as the amount to scan is just one block. Doing this without the lazy stuff simply means do the position calculation from the beginning of the list. If the list is long that gets pretty expensive …

> [@remy.s](#):
>
> Interestingly Neumerkel’s library “auto-adjusts” to codes vs chars:

That makes some sense, but unlike SICStus, most of SWI-Prolog syntax changes are module local. This holds for operators, but also the `double_quotes` flag. So, this can work but handling the module context correctly gets pretty hairy if you want to put DCGs in modules. The module-local syntax for `double_quotes` is intended to allow for using (typically ugly) code that relies all over the place on “” to be a list of character codes (chars).

One of the nasty issues with the code/chars is that your entire application has to agree if text is passed as lists between components. As most existing components are written with codes in mind, I think it is best to stick with codes and improve the environment to make working with them as pleasant as possible. With the development of SWI-7 I’ve considered adding `code` as a primary type. That would have solved this issue, but it became to complicated and expensive and I dropped the idea.

---

_[View the full topic](https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925)._
