mirror of
				https://github.com/Z3Prover/z3
				synced 2025-11-04 13:29:11 +00:00 
			
		
		
		
	re-add model example
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
		
							parent
							
								
									2c27056283
								
							
						
					
					
						commit
						7ed32f4315
					
				
					 1 changed files with 194 additions and 173 deletions
				
			
		| 
						 | 
				
			
			@ -2844,52 +2844,67 @@ void fpa_example() {
 | 
			
		|||
   \brief Demonstrates some basic features of model construction
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#if 0
 | 
			
		||||
void mk_model_example() {
 | 
			
		||||
    Z3_context ctx;
 | 
			
		||||
    Z3_model m;
 | 
			
		||||
    Z3_sort intSort;
 | 
			
		||||
    Z3_symbol aSymbol, bSymbol, cSymbol;
 | 
			
		||||
    Z3_func_decl aFuncDecl, bFuncDecl, cFuncDecl;
 | 
			
		||||
    Z3_ast aApp, bApp, cApp;
 | 
			
		||||
    Z3_sort int2intArraySort;
 | 
			
		||||
    Z3_ast zeroNumeral, oneNumeral, twoNumeral, threeNumeral, fourNumeral;
 | 
			
		||||
    Z3_sort arrayDomain[1];
 | 
			
		||||
    Z3_func_decl cAsFuncDecl;
 | 
			
		||||
    Z3_func_interp cAsFuncInterp;
 | 
			
		||||
    Z3_ast_vector zeroArgs;
 | 
			
		||||
    Z3_ast_vector oneArgs;
 | 
			
		||||
    Z3_ast cFuncDeclAsArray;
 | 
			
		||||
    Z3_string modelAsString;
 | 
			
		||||
 | 
			
		||||
    printf("\nmk_model_example\n");
 | 
			
		||||
    Z3_context ctx = mk_context();
 | 
			
		||||
    ctx = mk_context();
 | 
			
		||||
    // Construct empty model
 | 
			
		||||
    Z3_model m = Z3_mk_model(ctx);
 | 
			
		||||
    m = Z3_mk_model(ctx);
 | 
			
		||||
    Z3_model_inc_ref(ctx, m);
 | 
			
		||||
 | 
			
		||||
    // Create constants "a" and "b"
 | 
			
		||||
    Z3_sort intSort = Z3_mk_int_sort(ctx);
 | 
			
		||||
    Z3_symbol aSymbol = Z3_mk_string_symbol(ctx, "a");
 | 
			
		||||
    Z3_func_decl aFuncDecl = Z3_mk_func_decl(ctx, aSymbol,
 | 
			
		||||
    intSort = Z3_mk_int_sort(ctx);
 | 
			
		||||
    aSymbol = Z3_mk_string_symbol(ctx, "a");
 | 
			
		||||
    aFuncDecl = Z3_mk_func_decl(ctx, aSymbol,
 | 
			
		||||
                                /*domain_size=*/0,
 | 
			
		||||
                                /*domain=*/NULL,
 | 
			
		||||
                                /*range=*/intSort);
 | 
			
		||||
    Z3_ast aApp = Z3_mk_app(ctx, aFuncDecl,
 | 
			
		||||
    aApp = Z3_mk_app(ctx, aFuncDecl,
 | 
			
		||||
                     /*num_args=*/0,
 | 
			
		||||
                     /*args=*/NULL);
 | 
			
		||||
    Z3_symbol bSymbol = Z3_mk_string_symbol(ctx, "b");
 | 
			
		||||
    Z3_func_decl bFuncDecl = Z3_mk_func_decl(ctx, bSymbol,
 | 
			
		||||
    bSymbol = Z3_mk_string_symbol(ctx, "b");
 | 
			
		||||
    bFuncDecl = Z3_mk_func_decl(ctx, bSymbol,
 | 
			
		||||
                                /*domain_size=*/0,
 | 
			
		||||
                                /*domain=*/NULL,
 | 
			
		||||
                                /*range=*/intSort);
 | 
			
		||||
    Z3_ast bApp = Z3_mk_app(ctx, bFuncDecl,
 | 
			
		||||
    bApp = Z3_mk_app(ctx, bFuncDecl,
 | 
			
		||||
                     /*num_args=*/0,
 | 
			
		||||
                     /*args=*/NULL);
 | 
			
		||||
 | 
			
		||||
    // Create array "c" that maps int to int.
 | 
			
		||||
    Z3_symbol cSymbol = Z3_mk_string_symbol(ctx, "c");
 | 
			
		||||
    Z3_sort int2intArraySort = Z3_mk_array_sort(ctx,
 | 
			
		||||
    cSymbol = Z3_mk_string_symbol(ctx, "c");
 | 
			
		||||
    int2intArraySort = Z3_mk_array_sort(ctx,
 | 
			
		||||
                                        /*domain=*/intSort,
 | 
			
		||||
                                        /*range=*/intSort);
 | 
			
		||||
    Z3_func_decl cFuncDecl = Z3_mk_func_decl(ctx, cSymbol,
 | 
			
		||||
    cFuncDecl = Z3_mk_func_decl(ctx, cSymbol,
 | 
			
		||||
                                /*domain_size=*/0,
 | 
			
		||||
                                /*domain=*/NULL,
 | 
			
		||||
                                /*range=*/int2intArraySort);
 | 
			
		||||
    Z3_ast cApp = Z3_mk_app(ctx, cFuncDecl,
 | 
			
		||||
    cApp = Z3_mk_app(ctx, cFuncDecl,
 | 
			
		||||
                     /*num_args=*/0,
 | 
			
		||||
                     /*args=*/NULL);
 | 
			
		||||
 | 
			
		||||
    // Create numerals to be used in model
 | 
			
		||||
    Z3_ast zeroNumeral = Z3_mk_int(ctx, 0, intSort);
 | 
			
		||||
    Z3_ast oneNumeral = Z3_mk_int(ctx, 1, intSort);
 | 
			
		||||
    Z3_ast twoNumeral = Z3_mk_int(ctx, 2, intSort);
 | 
			
		||||
    Z3_ast threeNumeral = Z3_mk_int(ctx, 3, intSort);
 | 
			
		||||
    Z3_ast fourNumeral = Z3_mk_int(ctx, 4, intSort);
 | 
			
		||||
    zeroNumeral = Z3_mk_int(ctx, 0, intSort);
 | 
			
		||||
    oneNumeral = Z3_mk_int(ctx, 1, intSort);
 | 
			
		||||
    twoNumeral = Z3_mk_int(ctx, 2, intSort);
 | 
			
		||||
    threeNumeral = Z3_mk_int(ctx, 3, intSort);
 | 
			
		||||
    fourNumeral = Z3_mk_int(ctx, 4, intSort);
 | 
			
		||||
 | 
			
		||||
    // Add assignments to model
 | 
			
		||||
    // a == 1
 | 
			
		||||
| 
						 | 
				
			
			@ -2899,25 +2914,25 @@ void mk_model_example() {
 | 
			
		|||
 | 
			
		||||
    // Create a fresh function that represents
 | 
			
		||||
    // reading from array.
 | 
			
		||||
    Z3_sort arrayDomain[] = {intSort};
 | 
			
		||||
    Z3_func_decl cAsFuncDecl = Z3_mk_fresh_func_decl(ctx,
 | 
			
		||||
    arrayDomain[0] = intSort;
 | 
			
		||||
    cAsFuncDecl = Z3_mk_fresh_func_decl(ctx,
 | 
			
		||||
                                        /*prefix=*/"",
 | 
			
		||||
                                        /*domain_size*/ 1,
 | 
			
		||||
                                        /*domain=*/arrayDomain,
 | 
			
		||||
                                        /*sort=*/intSort);
 | 
			
		||||
    // Create function interpretation with default
 | 
			
		||||
    // value of "0".
 | 
			
		||||
    Z3_func_interp cAsFuncInterp =
 | 
			
		||||
    cAsFuncInterp =
 | 
			
		||||
        Z3_add_func_interp(ctx, m, cAsFuncDecl,
 | 
			
		||||
                           /*default_value=*/zeroNumeral);
 | 
			
		||||
    Z3_func_interp_inc_ref(ctx, cAsFuncInterp);
 | 
			
		||||
    // Add [0] = 3
 | 
			
		||||
    Z3_ast_vector zeroArgs = Z3_mk_ast_vector(ctx);
 | 
			
		||||
    zeroArgs = Z3_mk_ast_vector(ctx);
 | 
			
		||||
    Z3_ast_vector_inc_ref(ctx, zeroArgs);
 | 
			
		||||
    Z3_ast_vector_push(ctx, zeroArgs, zeroNumeral);
 | 
			
		||||
    Z3_func_interp_add_entry(ctx, cAsFuncInterp, zeroArgs, threeNumeral);
 | 
			
		||||
    // Add [1] = 4
 | 
			
		||||
    Z3_ast_vector oneArgs = Z3_mk_ast_vector(ctx);
 | 
			
		||||
    oneArgs = Z3_mk_ast_vector(ctx);
 | 
			
		||||
    Z3_ast_vector_inc_ref(ctx, oneArgs);
 | 
			
		||||
    Z3_ast_vector_push(ctx, oneArgs, oneNumeral);
 | 
			
		||||
    Z3_func_interp_add_entry(ctx, cAsFuncInterp, oneArgs, fourNumeral);
 | 
			
		||||
| 
						 | 
				
			
			@ -2925,15 +2940,16 @@ void mk_model_example() {
 | 
			
		|||
    // Now use the `(_ as_array)` to associate
 | 
			
		||||
    // the `cAsFuncInterp` with the `cFuncDecl`
 | 
			
		||||
    // in the model
 | 
			
		||||
    Z3_ast cFuncDeclAsArray = Z3_mk_as_array(ctx, cAsFuncDecl);
 | 
			
		||||
    cFuncDeclAsArray = Z3_mk_as_array(ctx, cAsFuncDecl);
 | 
			
		||||
    Z3_add_const_interp(ctx, m, cFuncDecl, cFuncDeclAsArray);
 | 
			
		||||
 | 
			
		||||
    // Print the model
 | 
			
		||||
    Z3_string modelAsString = Z3_model_to_string(ctx, m);
 | 
			
		||||
    modelAsString = Z3_model_to_string(ctx, m);
 | 
			
		||||
    printf("Model:\n%s\n", modelAsString);
 | 
			
		||||
 | 
			
		||||
    // Check the interpretations we expect to be present
 | 
			
		||||
    // are.
 | 
			
		||||
    {
 | 
			
		||||
        Z3_func_decl expectedInterpretations[] = {aFuncDecl, bFuncDecl, cFuncDecl};
 | 
			
		||||
        for (int index = 0;
 | 
			
		||||
             index < sizeof(expectedInterpretations) / sizeof(Z3_func_decl);
 | 
			
		||||
| 
						 | 
				
			
			@ -2947,7 +2963,9 @@ void mk_model_example() {
 | 
			
		|||
                exit(1);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        // Evaluate a + b under model
 | 
			
		||||
        Z3_ast addArgs[] = {aApp, bApp};
 | 
			
		||||
        Z3_ast aPlusB = Z3_mk_add(ctx,
 | 
			
		||||
| 
						 | 
				
			
			@ -2961,6 +2979,7 @@ void mk_model_example() {
 | 
			
		|||
            printf("Failed to evaluate model\n");
 | 
			
		||||
            exit(1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        int aPlusBValue = 0;
 | 
			
		||||
        Z3_bool getAPlusBValueSuccess =
 | 
			
		||||
            Z3_get_numeral_int(ctx, aPlusBEval, &aPlusBValue);
 | 
			
		||||
| 
						 | 
				
			
			@ -2973,7 +2992,9 @@ void mk_model_example() {
 | 
			
		|||
            printf("a+b did not evaluate to expected value\n");
 | 
			
		||||
            exit(1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    {    
 | 
			
		||||
        // Evaluate c[0] + c[1] + c[2] under model
 | 
			
		||||
        Z3_ast c0 = Z3_mk_select(ctx, cApp, zeroNumeral);
 | 
			
		||||
        Z3_ast c1 = Z3_mk_select(ctx, cApp, oneNumeral);
 | 
			
		||||
| 
						 | 
				
			
			@ -3002,6 +3023,7 @@ void mk_model_example() {
 | 
			
		|||
            printf("c[0] + c[1] + c[2] did not evaluate to expected value\n");
 | 
			
		||||
            exit(1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Z3_ast_vector_dec_ref(ctx, oneArgs);
 | 
			
		||||
    Z3_ast_vector_dec_ref(ctx, zeroArgs);
 | 
			
		||||
| 
						 | 
				
			
			@ -3009,7 +3031,6 @@ void mk_model_example() {
 | 
			
		|||
    Z3_model_dec_ref(ctx, m);
 | 
			
		||||
    Z3_del_context(ctx);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*@}*/
 | 
			
		||||
/*@}*/
 | 
			
		||||
| 
						 | 
				
			
			@ -3059,6 +3080,6 @@ int main() {
 | 
			
		|||
    substitute_example();
 | 
			
		||||
    substitute_vars_example();
 | 
			
		||||
    fpa_example();
 | 
			
		||||
//    mk_model_example();
 | 
			
		||||
    mk_model_example();
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue