Back to index

texmacs  1.0.7.15
solutions.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : solutions.cpp
00004 * DESCRIPTION: solutions to property queries
00005 * COPYRIGHT  : (C) 2007  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "tmfs.hpp"
00013 
00014 /******************************************************************************
00015 * Extracting information from solutions
00016 ******************************************************************************/
00017 
00018 collection
00019 as_collection (solutions sols, string key) {
00020   collection c;
00021   for (int i=0; i<N(sols); i++)
00022     if (sols[i]->contains (key))
00023       c (sols[i][key])= 1;
00024   return c;
00025 }
00026 
00027 collection
00028 as_collection (solutions sols, property p) {
00029   int i, n= N(p);
00030   for (i=0; i<n; i++)
00031     if (is_unknown (p[i])) return as_collection (sols, p[i]);
00032   return collection ();
00033 }
00034 
00035 /******************************************************************************
00036 * Unification
00037 ******************************************************************************/
00038 
00039 solutions
00040 combine (solutions sols1, solutions sols2) {
00041   solutions sols;
00042   int i1, i2, n1= N(sols1), n2= N(sols2);
00043   for (i1=0; i1<n1; i1++)
00044     for (i2=0; i2<n2; i2++) {
00045       bool ok= true;
00046       solution sol= copy (sols1[i1]);
00047       iterator<string> it= iterate (sols2[i2]);
00048       while (it->busy ()) {
00049        string s= it->next ();
00050        if (sol->contains (s)) {
00051          ok= (sol[s] == sols2[i2][s]);
00052          if (!ok) break;
00053        }
00054        else sol(s)= sols2[i2][s];
00055       }
00056       if (ok) sols << sol;
00057     }
00058   return sols;
00059 }
00060 
00061 /******************************************************************************
00062 * Simplify a list of queries according to previously found solutions
00063 ******************************************************************************/
00064 
00065 property
00066 simplify (property p, solutions sols) {
00067   p= copy (p);
00068   int i, n= N(p);
00069   for (i=0; i<n; i++)
00070     if (is_unknown (p[i])) {
00071       string r= p[i];
00072       for (int j=0; j<N(sols); j++)
00073        if (sols[j]->contains (p[i])) {
00074          if (is_unknown (r)) r= sols[j][p[i]];
00075          else {
00076            if (sols[j][p[i]] == r) continue;
00077            r= p[i];
00078            break;
00079          }
00080        }
00081       p[i]= r;
00082     }
00083   return p;
00084 }