WffNode *instantiate_wff( WffNode *w ) { WffNode *res = NULL, *tmp, *i; int j, c0, c1, m, h; Bool ok; switch ( w->connective ) { case AND: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == FAL ) { free_WffNode( res ); return tmp; } if ( tmp->connective == TRU ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( TRU ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( AND ); tmp->sons = res; res = tmp; break; case OR: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == TRU ) { free_WffNode( res ); return tmp; } if ( tmp->connective == FAL ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( FAL ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( OR ); tmp->sons = res; res = tmp; break; case NOT: /* must be non-equality */ c0 = ( w->son->fact->args[0] < 0 ) ? linst_table[DECODE_VAR( w->son->fact->args[0] )] : w->son->fact->args[0]; c1 = ( w->son->fact->args[1] < 0 ) ? linst_table[DECODE_VAR( w->son->fact->args[1] )] : w->son->fact->args[1]; if ( c0 < 0 || c1 < 0 ) { /* ef param while checking ef conds in inst op */ res = new_WffNode( ATOM ); res->fact = new_Fact(); res->fact->predicate = -2; res->fact->args[0] = ( c0 < 0 ) ? w->son->fact->args[0] : c0; res->fact->args[1] = ( c1 < 0 ) ? w->son->fact->args[1] : c1; break; } if ( c0 != c1 ) { res = new_WffNode( TRU ); } else { res = new_WffNode( FAL ); } break; case ATOM: res = new_WffNode( ATOM ); res->fact = new_Fact(); res->fact->predicate = w->fact->predicate; ok = TRUE; for ( j = 0; j < garity[res->fact->predicate]; j++ ) { h = ( w->fact->args[j] < 0 ) ? linst_table[DECODE_VAR( w->fact->args[j] )] : w->fact->args[j]; if ( h < 0 ) { ok = FALSE; res->fact->args[j] = w->fact->args[j]; } else { res->fact->args[j] = h; } } if ( !ok ) {/* contains ef params */ break; } if ( !full_possibly_negative( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = TRU; break; } if ( !full_possibly_positive( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = FAL; break; } break; case TRU: case FAL: res = new_WffNode( w->connective ); break; default: printf("\n\nillegal connective %d in instantiate formula\n\n", w->connective); exit( 1 ); } return res; }
WffNode *instantiate_wff( WffNode *w ) { WffNode *res = NULL, *tmp, *i; int j, m, h; Bool ok, ct; switch ( w->connective ) { case AND: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == FAL ) { free_WffNode( res ); return tmp; } if ( tmp->connective == TRU ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( TRU ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( AND ); tmp->sons = res; res = tmp; break; case OR: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == TRU ) { free_WffNode( res ); return tmp; } if ( tmp->connective == FAL ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( FAL ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( OR ); tmp->sons = res; res = tmp; break; case ATOM: res = new_WffNode( ATOM ); res->fact = new_Fact(); res->fact->predicate = w->fact->predicate; ok = TRUE; for ( j = 0; j < garity[res->fact->predicate]; j++ ) { h = ( w->fact->args[j] < 0 ) ? linst_table[DECODE_VAR( w->fact->args[j] )] : w->fact->args[j]; if ( h < 0 ) { ok = FALSE; res->fact->args[j] = w->fact->args[j]; } else { res->fact->args[j] = h; } } if ( !ok ) {/* contains ef params */ break; } if ( !full_possibly_negative( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = TRU; break; } if ( !full_possibly_positive( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = FAL; break; } break; case COMP: res = new_WffNode( COMP ); res->comp = w->comp; res->lh = copy_Exp( w->lh ); res->rh = copy_Exp( w->rh ); instantiate_exp( &(res->lh) ); instantiate_exp( &(res->rh) ); if ( res->lh->connective != NUMBER || res->rh->connective != NUMBER ) { /* logical simplification only possible if both parts are numbers */ break; } ct = number_comparison_holds( res->comp, res->lh->value, res->rh->value ); if ( ct ) { res->connective = TRU; free_ExpNode( res->lh ); res->lh = NULL; free_ExpNode( res->rh ); res->rh = NULL; res->comp = -1; } else { res->connective = FAL; free_ExpNode( res->lh ); res->lh = NULL; free_ExpNode( res->rh ); res->rh = NULL; res->comp = -1; } break; case TRU: case FAL: res = new_WffNode( w->connective ); break; default: printf("\n\nillegal connective %d in instantiate formula\n\n", w->connective); exit( 1 ); } return res; }