00001 #ifndef PROLOG_STRUCTS_H
00002 #define PROLOG_STRUCTS_H
00003
00004 #include <vector>
00005 #include <string>
00006 #include <set>
00007
00008 #include "stringsholder.h"
00009 #include "unify.h"
00010 #include "constraint.h"
00011
00012 using namespace std;
00013
00014
00015
00019 enum { T_VARIABLE = 1, T_FUNCTOR = 2, T_LIST = 3, T_NUMBER = 4 };
00020
00024 struct c_base
00025 {
00029 c_base();
00030
00037 static void set_safety(const bool safe);
00038
00042 static void delete_us();
00043
00048 virtual void deep_delete() = 0;
00049
00053 virtual ~c_base();
00054
00058 static vector<c_base*> c_elements;
00059
00063 static bool safety;
00064 };
00065
00071 struct c_term: public c_base
00072 {
00076 string name;
00077
00081 int num;
00082
00086 vector<c_term*> arglist;
00087
00091 bool negated;
00092
00096 c_term *list_head;
00097
00101 c_term *list_tail;
00102
00106 bool list_empty;
00107
00112 bool match_rest;
00113
00117 int type;
00118
00122 c_term();
00123
00127 static int count;
00128
00132 ~c_term();
00133
00139 virtual void deep_delete();
00140
00146 void deep_copy(c_term *copy, bool reindex = false) const;
00147
00152 void get_variables(set<string> &vars) const;
00153
00161 void insert_list(const vector<c_term*> &list,
00162 const c_term *rest = 0,
00163 const size_t start_index = 0);
00164 };
00165
00169 struct n_base
00170 {
00174 n_base();
00175
00179 static int count;
00180
00184 ~n_base();
00185
00189 static void delete_us();
00190
00194 static vector<n_base*> nodes;
00195 };
00196
00200 struct n_arg: public n_base
00201 {
00205 n_arg();
00206
00210 c_term *term;
00211 };
00212
00216 struct n_arglist: public n_base
00217 {
00221 n_arglist(const n_arg*, const n_arglist*);
00222
00226 vector<c_term*> terms;
00227 };
00228
00232 struct n_functor: public n_arg
00233 {
00237 n_functor(const string, const n_arglist*);
00238
00242 void negate();
00243 };
00244
00248 struct n_variable: public n_arg
00249 {
00253 n_variable(const string);
00254 };
00255
00259 struct n_list: public n_arg
00260 {
00264 n_list(const n_arglist*);
00265
00269 n_list(const n_arglist*, const n_arg*);
00270
00274 n_list();
00275 };
00276
00280 struct n_number: public n_arg
00281 {
00285 n_number(const int num);
00286 };
00287
00292 struct c_rule: public c_base
00293 {
00297 c_rule();
00298
00302 static int count;
00303
00307 ~c_rule();
00308
00314 void deep_delete();
00315
00321 void deep_copy(c_rule *copy, bool reindex = false) const;
00322
00327 void project_constraint() const;
00328
00333 void get_variables(set<string> &vars) const;
00334
00338 c_term *head;
00339
00344 vector<c_term*> body;
00345
00349 mutable con_type constraint;
00350
00355 static int global_variable_index;
00356 };
00357
00361 struct n_body: public n_base
00362 {
00366 n_body(const n_functor*, const n_body*);
00367
00371 vector<c_term*> functors;
00372
00377 static con_type constraint;
00378 };
00379
00383 struct n_rule: public n_base
00384 {
00388 n_rule(const n_functor*, const n_body*);
00389
00393 n_rule(const n_functor*);
00394
00398 c_rule *rule;
00399 };
00400
00404 struct n_rules: public n_base
00405 {
00409 n_rules(const n_rule*, const n_rules*);
00410
00414 vector<c_rule*> rules;
00415 };
00416
00420 struct prolog_program
00421 {
00426 prolog_program(const vector<c_rule*> &rs);
00427
00431 vector<c_rule*> rules;
00432 };
00433
00437 struct c_sorter
00438 {
00453 static bool c_rule_lt(const c_rule *t1, const c_rule *t2);
00454
00473 static bool c_term_lt(const c_term *t1, const c_term *t2);
00474 };
00475
00481 ostream &operator<<(ostream &os, const prolog_program *p);
00482
00488 ostream &operator<<(ostream &os, const c_rule *r);
00489
00495 ostream &operator<<(ostream &os, const c_term *t);
00496
00497
00498 #endif // PROLOG_STRUCTS