Check-in [8949a9e372]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added a new "PVar" subtype of Term, for Prolog variables that need to be preserved as such (i.e., distinguished from specification variables).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:8949a9e372a18d6d66139f1994a3a41d71644302
User & Date: andy 2015-04-15 20:17:26
Context
2015-04-15
21:36
Changed the encoding for inductive goals from \size to |size. The backslash was getting lost in the string encoding in some places. check-in: 23db977deb user: andy tags: trunk
20:17
Added a new "PVar" subtype of Term, for Prolog variables that need to be preserved as such (i.e., distinguished from specification variables). check-in: 8949a9e372 user: andy tags: trunk
19:51
Fixed a bug in the conversion to Prolog of numeric atoms (0 vs. '0'). check-in: 76226466c7 user: andy tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/terms.js.

22
23
24
25
26
27
28


















29
30
31
32
33
34
35
...
271
272
273
274
275
276
277
278
279
280



281
282
283
284
285
286
287
...
301
302
303
304
305
306
307


308
309
310
311
312
313
314
315


316
317
318
319
320
321
322
323
324
325
326
327
// Prototype object for all term-like structures (atoms, variables, compounds)
var Term = {
    isAtom : function() { return false; },
    isVariable : function() { return false; },
    isCompound : function() { return false; },
    isNil : function() { return false; }
}



















/* Atom, Variable, Compound
 * Constructors for atoms, variables, and compound terms. */

/* Atoms used to be a thing in their own right. Now they are just 0-arity
   compounds (for internal consistency), but we retain the constructor for them
   for convenience. */
................................................................................
        // variables are converted to the string "_". Nuts.
        if(_.isObject(j)) {
            var head = j['functor'];
            var body = j['args'].map(terms.fromJSON);

            return new terms.Compound(head,body);
        }
        else if(typeof j === "string" && j === "_")
            // This might actually be any variable at all
            return terms.varWildcard;



        else if(typeof j === "string" && j.startsWith(" "))
            return new terms.Variable(j.trimLeft());
        else 
            // Everything else: atoms, numbers, etc. becomes an atom.
            return new terms.Atom(j + "");
    }
}
................................................................................
                ret += " , ";

            l = l.rhs();
        }
    }

    function stringify(t) {


        if(t.isVariable())
            return " ' " + t.name + "' ";
        else if(t.isAtom()) {
            // Prolog distinguishes between '0' and 0 (atom vs. number),
            // so we have to make sure that numeric-ish atoms get sent 
            // unquoted.
            if(!isNaN(parseInt(t.head,10)))
                return " " + parseInt(t.head,10).toString() + " ";


            else
                return " '" + t + "' ";
        }
        else if(t.isList())
            return "[ " + stringify_list(t) + " ]";
        else if(t.isCompound())
            return " '" + t.head + 
                   "'(" + t.body.map(stringify).join(" , ") + ") ";
    }

    return stringify(t);
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|

|
>
>
>







 







>
>








>
>




|







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Prototype object for all term-like structures (atoms, variables, compounds)
var Term = {
    isAtom : function() { return false; },
    isVariable : function() { return false; },
    isCompound : function() { return false; },
    isNil : function() { return false; }
}

/* PVar(a)
 * Normal "Variables" encode our specification-style variables, ' x'. But
 * sometimes we need to encode proper Prolog variables, which is what this
 * stands for. They act like normal variables in all other respects, except
 * that incoming Prolog variables will be encoded to them, and they will 
 * encode to Prolog variables.
 */
terms.PVar = function(a) {
    this.name = a;
}
terms.PVar.prototype = Object.create(Term);
terms.PVar.prototype.constructor = terms.PVar;
terms.PVar.prototype.toString = function() { return this.name; }
terms.PVar.prototype.tt = function() { return "variables" };
terms.PVar.prototype.walk = function() { f(this); }
terms.PVar.prototype.isVariable = function() { return true; }


/* Atom, Variable, Compound
 * Constructors for atoms, variables, and compound terms. */

/* Atoms used to be a thing in their own right. Now they are just 0-arity
   compounds (for internal consistency), but we retain the constructor for them
   for convenience. */
................................................................................
        // variables are converted to the string "_". Nuts.
        if(_.isObject(j)) {
            var head = j['functor'];
            var body = j['args'].map(terms.fromJSON);

            return new terms.Compound(head,body);
        }
        else if(j === "_")
            // This might actually be any variable at all
            return new terms.PVar("_");
        else if(typeof j === "string" && j.match(/[_A-Z].*/))
            // Starts with upper-case: encode as PVar.
            return new terms.PVar(j);
        else if(typeof j === "string" && j.startsWith(" "))
            return new terms.Variable(j.trimLeft());
        else 
            // Everything else: atoms, numbers, etc. becomes an atom.
            return new terms.Atom(j + "");
    }
}
................................................................................
                ret += " , ";

            l = l.rhs();
        }
    }

    function stringify(t) {
        if(t instanceof terms.PVar)
            return t.name;
        if(t.isVariable())
            return " ' " + t.name + "' ";
        else if(t.isAtom()) {
            // Prolog distinguishes between '0' and 0 (atom vs. number),
            // so we have to make sure that numeric-ish atoms get sent 
            // unquoted.
            if(!isNaN(parseInt(t.head,10)))
                return " " + parseInt(t.head,10).toString() + " ";
            else if(t.head === "[]")
                return "[]"
            else
                return " '" + t + "' ";
        }
        else if(t.isList())
            return "[ " + terms.toArray(t).map(stringify).join(",") + " ]";
        else if(t.isCompound())
            return " '" + t.head + 
                   "'(" + t.body.map(stringify).join(" , ") + ") ";
    }

    return stringify(t);
}