Re: [shex] IF...THEN...ELSE pattern (#21)

I am trying to compare possible syntaxes. Let's take an example from [this question](https://gitter.im/shapeExpressions/Lobby?at=5e73f27a0f0687340b42573b) : 

> Can I have a shape that says "_if state is assigned, assignee must have a value, if it is unassigned, it must not_" ?

A possible definition with the current syntax could be something like:

```turtle
:IssueShape { a [ :Issue ] } 
AND ( NOT { :state [:assigned ] } OR { :assignee @:Value }) 
AND ( { :state [:assigned ] } OR { :assignee @:Value {0,0} } )

:Value { a [ :Value ] }
```

Which is not very readable. 

Using the IF-THEN-ELSE, we could write it as:

```
:IssueShape { a [ :Issue ] } AND 
IF { :state [ :assigned ] } 
THEN { :assignee @:Value }  
ELSE { :assignee @:Value {0,0} }
```

If we tried to use a CASE, it could be something like:

```
:IssueShape { a [ :Issue ] } AND 
CASE { :state [ :assigned ] } => { :assignee @:Value }  
CASE . => { :assignee @:Value {0,0} }
```
One concern about several CASE's is that the order of them affect the shape definition, while with the IF-THEN-ELSE, although is more verbose, it forces the author to think what he really intends to express. 

For example, if one wants to express now: 

> Can I have a shape that says "_if state is assigned, assignee must have a value, if, state is onReview, reviewer must have a value, and if state is neither assigned nor on review, then it must have no value_" ?

A simple representation of the above with CASEs could be:

```
:IssueShape { a [ :Issue ] } AND 
CASE { :state [ :assigned ] } => { :assignee @:Value }  
CASE { :state [ :onReview ] } => { :reviewer @:Value }
CASE . => { :assignee @:Value {0,0} }
```

It looks OK but the order of the cases can affect the validation. 

For example, if one issue has both values for `:assigned` and `:onReview` the validator would need to decide if it checks the first CASE or the second CASE (or both). Usually, programming languages like C or Java choose the first one...but having to take into account the order of definitions in ShEx could make it more complex than necessary. 

If we had only IF-THEN-ELSE expressions, the author would be more forced to take into account the different cases. For example, the following definition makes clear, that the validator checks first that the state is assigned so on:

```
:IssueShape { a [ :Issue ] } AND 
IF { :state [ :assigned ] } THEN { :assignee @:Value }  
ELSE IF { :state [ :onReview ] } THEN { :reviewer @:Value }
ELSE { :assignee @:Value {0,0} }
```
 

-- 
GitHub Notification of comment by labra
Please view or discuss this issue at https://github.com/shexSpec/shex/issues/21#issuecomment-604271467 using your GitHub account

Received on Thursday, 26 March 2020 07:23:07 UTC