[m-users.] Impurity needed?

Volker Wysk post at volker-wysk.de
Tue Feb 14 02:13:53 AEDT 2023


Am Dienstag, dem 14.02.2023 um 00:40 +1100 schrieb Zoltan Somogyi:
> 2023-02-14 00:24 GMT+11:00 "Volker Wysk" <post at volker-wysk.de>:
> 
> > The type of the odbc.transaction predicate is:
> > 
> > :- pred odbc.transaction(data_source::in, user_name::in, password::in,
> >      transaction(T)::transaction, odbc.result(T)::out, io::di, io::uo) 
> > is det.
> > 
> > 
> > Would it be possible to just change the transaction type from this:
> > 
> > :- type odbc.transaction(T) ==  pred(T, odbc.state, odbc.state).
> > :- mode odbc.transaction ==  (pred(out, di, uo) is det).
> > 
> > 
> > To this:
> > 
> > 
> > :- type odbc.transaction(T) ==  
> >      pred(T, odbc.state, odbc.state, io.state, io.state).
> > :- mode odbc.transaction ==  (pred(out, di, uo, di, uo) is det).
> > 
> > 
> > So the transaction predicate can do IO, when called. This looks rather easy,
> > but I'm not sure I've understood correctly.
> 
> It would be trivial to change the signature as you ask. It would also be
> a very bad idea to do so. The point of making an operation a transaction
> is to ensure that only two things can happen:
> 
> - the transaction succeeds, and ALL of its effects happen, or
> - the transaction fails, and NONE of its effects happen.

In my understanding, this applies only to the effects on the database...

> If you allowed I/O in the middle of a transaction, and then later
> it turned out that the transaction cannot succeed, then neither
> of those end states would be reachable.

So someone using a vanilla imperative language is forbidden to do IO when
being inside a transaction, too -?  You're effectively saying that it can't
be done with one transaction (if the effects of the IO alter the state of
the world).

> For more about why transaction predicate is designed the way it is,
> have a look at the paper
> 
> - Database transactions in a purely declarative logic programming language
> 
> which is available at https://mercurylang.org/documentation/papers.html#mu_96_45

Okay, thanks. I'll look into it.

> In your use case, you propose to do I/O only to read from a file; you don't
> intend to update the state of the world. In such cases, using impure code
> to read in the file, and wrapping a promise_pure scope around that code
> to signify that the impure code implements a pure interface, would be ok.

That makes sense.

Cheers,
Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20230213/a63edf47/attachment-0001.sig>


More information about the users mailing list