w3c / N3

W3C's Notation 3 (N3) Community Group
46 stars 18 forks source link

Rationale for allowing directives throughout an N3 Document #175

Open jeswr opened 1 year ago

jeswr commented 1 year ago

I was wondering what the rationale for allowing @prefix and @base statements to occur in arbitrary positions throughout an N3 document was.

To me it seems quite messy and could create a lot of shadowing of prefixes and bases in different contexts. Why not just require that directives precede all statements?

jeswr commented 1 year ago

Cases in particular the the spec does not explicitly define the behavior of are if I had the following; whether the last triple in the document should be <http://example.org/1/s> <http://example.org/1/p> <http://example.org/1/o> or <http://example.org/2/s> <http://example.org/2/p> <http://example.org/2/o>.

@prefix ex: <http://example.org/1/>

<a> <b> {
  @prefix ex: <http://example.org/2/>
}

ex:s ex:p :exo .

Whilst <http://example.org/1/s> <http://example.org/1/p> <http://example.org/1/o> is more sensible from a user perspective; it is also more difficult & CPU memory intensive to implement.

william-vw commented 1 year ago

@jeswr Good point. We currently mention the following about this:

We recommend listing @prefix / PREFIX and @base / BASE declarations at the top of an N3 document. This is not mandatory, however, and they can technically be put anywhere before the prefixed name or relative IRI that relies on the declaration. Subsequent @prefix / PREFIX directives may "re-map" the same prefix label to another namespace IRI.

Behavior is inconsistent between reasoners when remapping the namespace inside a quoted graph: https://n3-editor.herokuapp.com/n3/editor/s/6TuP2UdU

IIRC, this decision was made in the original N3 proposal and was not revised afterwards.

Tying in @doerthe or @josd to help shed light on this.

josd commented 1 year ago

It is good to have declarations at the top of the file but it is another thing to throw exceptions when that is not the case.

jeswr commented 1 year ago

If this is to remain in the spec (personally I would prefer an error to be thrown) then what do you think the correct behaviour for https://github.com/w3c/N3/issues/175#issuecomment-1507942752 would be @josd ?

josd commented 1 year ago

The behaviour of Cwm and EYE ;-) For

@prefix ex: <http://example.org/1/> .
@prefix : <http://example.org/> .

<a> <b> {
  @prefix ex: <http://example.org/2/> .
} .

ex:s ex:p :exo .

Cwm gives

             @prefix : <./> .
    @prefix ex_: <http://example.org/2/> .

    :a     :b {} .

    ex_:s     ex_:p <#exo> .

and EYE gives

@prefix ex: <http://example.org/1/>.
@prefix : <http://example.org/>.
@prefix ns1: <http://example.org/2/>.

<file:///app/tmp/a> <file:///app/tmp/b> true.
ns1:s ns1:p :exo.
josd commented 1 year ago

Also remark that this behaviour is also needed for the namespace prefixes declared in graphs obtained via log:semantics.

gkellogg commented 1 year ago

It is good to have declarations at the top of the file but it is another thing to throw exceptions when that is not the case.

It’s interesting that N3 allows declarations anywhere you can have a statement; Turtle does this too, but does not have graph blocks. TriG only allows them outside of blocks, so anyplace at the top level.

Note that declarations are additive, so you can have an @base declaration use a relative IRI which makes it relative to either the document location or a previous @base. Scoping should be to the block in which it is contained, although I don’t believe there are any tests for this.

In actual use, there’s little reason to have declarations anywhere but at the top of the document; anything else could lead to at least human misinterpretation and doesn’t add any semantic value.

william-vw commented 1 year ago

Also remark that this behaviour is also needed for the namespace prefixes declared in graphs obtained via log:semantics.

In that case, I would expect the namespace declaration to only be applicable within the quoted graph, and not beyond it, as in @jeswr 's example.

TallTed commented 1 year ago

@william-vw — Please edit https://github.com/w3c/N3/issues/175#issuecomment-1508342049 and wrap each instance of @base and @prefix in backticks (like `@base`), so that those GitHub users are not drawn into this thread without their consent or interest.

TallTed commented 1 year ago

@gkellogg — Please edit https://github.com/w3c/N3/issues/175#issuecomment-1508939332 and wrap each instance of @base in backticks (like `@base`), so that those GitHub users are not drawn into this thread without their consent or interest.

TallTed commented 1 year ago

In actual use, there’s little reason to have declarations anywhere but at the top of the document

Concatenating files is a common cause of declarations being elsewhere, and seems (based on conversation and thread content; I have no empirical data) to be a fairly common actual use case.