tau-prolog / tau-prolog

An open source Prolog interpreter in JavaScript
http://tau-prolog.org
BSD 3-Clause "New" or "Revised" License
572 stars 55 forks source link

Feature request read_term_from_atom/3 #302

Closed Jean-Luc-Picard-2021 closed 5 months ago

Jean-Luc-Picard-2021 commented 2 years ago

In GNU Prolog I find:

8.15.1 read_term_from_atom/3 etc.. Like read_term/3 etc.. except that characters are not read from a text-stream but from Atom http://www.gprolog.org/manual/html_node/gprolog039.html

Is there something similar in Tau Prolog?

triska commented 2 years ago

For reference: SICStus Prolog and Scryer Prolog provide read_from_chars/2 in their respective library(charsio) to provide this functionality.

See also https://github.com/mthom/scryer-prolog/issues/1423#issuecomment-1100652091 for more information.

Jean-Luc-Picard-2021 commented 2 years ago

A library charsio does not anymore exist in SICStus Prolog 4.7.1:

| :- use_module(library(charsio)).
! Existence error in argument 1 of use_module/1
! file library(charsio) does not exist
! goal:  use_module(user:library(charsio))

Oh the irony, I did find a library codesio:

| ?- use_module(library(codesio)).
% loading d:/software/logic/sics/library/codesio.po...
% module codesio imported into user
%  loading d:/software/logic/sics/library/types.po...
%  module types imported into codesio
%  loaded d:/software/logic/sics/library/types.po in module types, 0 msec 6432 bytes
%  loading foreign resource d:/software/logic/sics/library/x86_64-win32-nt-4/codesio.dll in module codesio
% loaded d:/software/logic/sics/library/codesio.po in module codesio, 0 msec 64768 bytes
yes
| ?- read_from_codes("X+Y.", T).
T = _A+_B ? 
Jean-Luc-Picard-2021 commented 2 years ago

Meanwhile these two issues got closed:

https://github.com/mthom/scryer-prolog/issues/1423

https://github.com/trealla-prolog/trealla/issues/554

Any priorization for this feature request?

triska commented 2 years ago

read_from_chars/2 would also help solve a recent question about Tau Prolog on Stackoverflow:

https://stackoverflow.com/questions/72292279/how-convert-an-atom-to-a-term-in-tau-prolog

Jean-Luc-Picard-2021 commented 2 years ago

This does something:

// term_atom/2
"term_atom/2": function(thread, point, atom) {
    let term = atom.args[0];
    let atom1 = atom.args[1];
    if (pl.type.is_atom(atom1)) {
        let tokenizer = new Tokenizer( thread );
        tokenizer.new_text( atom1.id );
        let tokens = tokenizer.get_tokens();
        if( tokens === null ) {
            thread.throw_error(pl.error.type("term", atom1, atom.indicator));
            return;
        }
        let expr = parseExpr(thread, tokens, 0, thread.__get_max_priority(), false);
        if( expr.len !== tokens.length ) {
            thread.throw_error(pl.error.type("term", atom1, atom.indicator));
            return;
        }
        let eq = new Term( "=", [term, expr.value] );
        thread.prepend( [new State( point.goal.replace( eq ), point.substitution, point )] );
    } else {
        thread.throw_error( pl.error.type( "atom", atom1, atom.indicator ) );
    }
},

But I am not happy with the syntax error, its only a type error.