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 Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/terms.js.

    22     22   // Prototype object for all term-like structures (atoms, variables, compounds)
    23     23   var Term = {
    24     24       isAtom : function() { return false; },
    25     25       isVariable : function() { return false; },
    26     26       isCompound : function() { return false; },
    27     27       isNil : function() { return false; }
    28     28   }
           29  +
           30  +/* PVar(a)
           31  + * Normal "Variables" encode our specification-style variables, ' x'. But
           32  + * sometimes we need to encode proper Prolog variables, which is what this
           33  + * stands for. They act like normal variables in all other respects, except
           34  + * that incoming Prolog variables will be encoded to them, and they will 
           35  + * encode to Prolog variables.
           36  + */
           37  +terms.PVar = function(a) {
           38  +    this.name = a;
           39  +}
           40  +terms.PVar.prototype = Object.create(Term);
           41  +terms.PVar.prototype.constructor = terms.PVar;
           42  +terms.PVar.prototype.toString = function() { return this.name; }
           43  +terms.PVar.prototype.tt = function() { return "variables" };
           44  +terms.PVar.prototype.walk = function() { f(this); }
           45  +terms.PVar.prototype.isVariable = function() { return true; }
           46  +
    29     47   
    30     48   /* Atom, Variable, Compound
    31     49    * Constructors for atoms, variables, and compound terms. */
    32     50   
    33     51   /* Atoms used to be a thing in their own right. Now they are just 0-arity
    34     52      compounds (for internal consistency), but we retain the constructor for them
    35     53      for convenience. */
................................................................................
   271    289           // variables are converted to the string "_". Nuts.
   272    290           if(_.isObject(j)) {
   273    291               var head = j['functor'];
   274    292               var body = j['args'].map(terms.fromJSON);
   275    293   
   276    294               return new terms.Compound(head,body);
   277    295           }
   278         -        else if(typeof j === "string" && j === "_")
          296  +        else if(j === "_")
   279    297               // This might actually be any variable at all
   280         -            return terms.varWildcard;
          298  +            return new terms.PVar("_");
          299  +        else if(typeof j === "string" && j.match(/[_A-Z].*/))
          300  +            // Starts with upper-case: encode as PVar.
          301  +            return new terms.PVar(j);
   281    302           else if(typeof j === "string" && j.startsWith(" "))
   282    303               return new terms.Variable(j.trimLeft());
   283    304           else 
   284    305               // Everything else: atoms, numbers, etc. becomes an atom.
   285    306               return new terms.Atom(j + "");
   286    307       }
   287    308   }
................................................................................
   301    322                   ret += " , ";
   302    323   
   303    324               l = l.rhs();
   304    325           }
   305    326       }
   306    327   
   307    328       function stringify(t) {
          329  +        if(t instanceof terms.PVar)
          330  +            return t.name;
   308    331           if(t.isVariable())
   309    332               return " ' " + t.name + "' ";
   310    333           else if(t.isAtom()) {
   311    334               // Prolog distinguishes between '0' and 0 (atom vs. number),
   312    335               // so we have to make sure that numeric-ish atoms get sent 
   313    336               // unquoted.
   314    337               if(!isNaN(parseInt(t.head,10)))
   315    338                   return " " + parseInt(t.head,10).toString() + " ";
          339  +            else if(t.head === "[]")
          340  +                return "[]"
   316    341               else
   317    342                   return " '" + t + "' ";
   318    343           }
   319    344           else if(t.isList())
   320         -            return "[ " + stringify_list(t) + " ]";
          345  +            return "[ " + terms.toArray(t).map(stringify).join(",") + " ]";
   321    346           else if(t.isCompound())
   322    347               return " '" + t.head + 
   323    348                      "'(" + t.body.map(stringify).join(" , ") + ") ";
   324    349       }
   325    350   
   326    351       return stringify(t);
   327    352   }