Git Product home page Git Product logo

Comments (13)

triska avatar triska commented on July 28, 2024 2

Here is a quick and rather untested ad hoc definition of term//1:

:- use_module(library(dcgs)).
:- use_module(library(charsio)).
:- use_module(library(lists)).

term(T) -->
        call(term_(T)).

term_(T, Cs0, Cs) :-
        append(Prefix, Cs, Cs0),
        catch(read_from_chars(Prefix, T), error(syntax_error(_),_), false).

Example:

?- phrase(("<",term(T),">"), "<  hello. >").
   T = hello
;  false.

Tested with Scryer Prolog.

from trealla.

guregu avatar guregu commented on July 28, 2024 1

A DCG nonterminal would be perfect. I'm currently using a DCG to look for delimiters (<? and ?> in my case 😉) and then calling read_from_chars/3, but I could use term//1 directly to do that quite elegantly.

Thanks for the workaround as well @triska!

from trealla.

infradig avatar infradig commented on July 28, 2024 1

In Trealla that is...

term(T) -->
        call(term_(T)).

term_(T, Cs0, Cs) :-
        append(Prefix, Cs, Cs0),
        catch(read_term_from_chars(T, Prefix), error(syntax_error(_),_), false).
?- phrase(("<",term(T),">"), "<  hello. >").
   T = hello
;  false.

EDIT: note this is no longer correct. it is.. catch(read_term_from_chars(Prefix,T), error(syntax_error(),), false).
EDIT2: or use the code directly as provided by Markus.

from trealla.

triska avatar triska commented on July 28, 2024

I think a DCG nonterminal term//1 would be useful for this. With it, one could use phrase/3 to read a term, and have the remainder available for further reasoning.

For example, one could read two terms with:

?- phrase((term(T1),term(T2),...), Cs).

or, using list differences, with:

?- phrase(term(T1), Cs0, Cs1), phrase(term(T2), Cs1, Cs2).

where Cs2 is the remainder after T1 and T2 are read from the list of characters Cs0.

from trealla.

triska avatar triska commented on July 28, 2024

By the way, as a workaround, until such a nonterminal is available, you can manually try every possible string prefix to read a term:

?- append(Prefix, Rest, Cs), read_from_chars(Prefix, T).

with suitable use of catch/3 to catch syntax errors, failing if Prefix cannot (yet) be parsed. This incurs quadratic overhead, which is unacceptable for longer strings, but may be acceptable for the present use case, and until a faster (linear) construct is available.

from trealla.

infradig avatar infradig commented on July 28, 2024
$ tpl
?- help(read_term_from_chars/2).
read_term_from_chars/2: read_term_from_chars(+chars,?term)
   true.

?- help(read_term_from_chars/3).
read_term_from_chars/3: read_term_from_chars(+chars,?term,+list)
   true.

from trealla.

triska avatar triska commented on July 28, 2024

Personally, I think a good building block would be read_from_chars_(T, Cs0, Cs), reading from Cs0 the term T, leaving remainder Cs. This can be used (internally, since it relies on the implementation dependent order of arguments) in DCGs, and we can also (portably) implement term//1 and read_term_from_chars/3 on top of it.

For example, with this building block, term//1 could be implemented as:

term(T) --> call(read_from_chars_(T)).

from trealla.

infradig avatar infradig commented on July 28, 2024

Sounds good.

from trealla.

infradig avatar infradig commented on July 28, 2024

from trealla.

guregu avatar guregu commented on July 28, 2024

Is this supposed to happen?

?- '$read_term_from_chars'(T,[],"abc.",Rest).
   T = abc, Rest = "abc.".

Puts my naïve DCG into an infinite loop.

from trealla.

infradig avatar infradig commented on July 28, 2024

from trealla.

guregu avatar guregu commented on July 28, 2024

This is working quite nicely now. Very quick and no problem with decimal points. Thanks a bunch!

Here it is in action: https://php.energy/moon.html

from trealla.

infradig avatar infradig commented on July 28, 2024

from trealla.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.