File: ppl_prolog_common.cc

package info (click to toggle)
ppl 1:1.2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 44,328 kB
  • sloc: cpp: 212,085; sh: 12,176; makefile: 7,192; perl: 6,333; java: 2,220; ansic: 1,842; ml: 1,132; sed: 80
file content (3106 lines) | stat: -rw-r--r-- 100,053 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
/* Common part of the Prolog interfaces: variables and non-inline functions.
   Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
   Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)

This file is part of the Parma Polyhedra Library (PPL).

The PPL is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The PPL is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.

For the most up-to-date information see the Parma Polyhedra Library
site: http://bugseng.com/products/ppl/ . */

#include "ppl_prolog_common_defs.hh"
#include <exception>
#include <stdexcept>
#include <sstream>
#include <climits>
#include <typeinfo>

namespace Parma_Polyhedra_Library {

namespace Interfaces {

namespace Prolog {

#if PROLOG_TRACK_ALLOCATION || NOISY_PROLOG_TRACK_ALLOCATION

Allocation_Tracker::Allocation_Tracker() {
}

Allocation_Tracker::~Allocation_Tracker() {
  Set::size_type n = s.size();
  if (n > 0)
    std::cerr
      << "Interfaces::Prolog::Allocation_Tracker: "
      << n << " object(s) leaked!"
      << std::endl;
}

Allocation_Tracker allocation_tracker;

#endif // PROLOG_TRACK_ALLOCATION || NOISY_PROLOG_TRACK_ALLOCATION



// For the out-of-memory exception.
Prolog_atom out_of_memory_exception_atom;

// For variables.
Prolog_atom a_dollar_VAR;

// For linear expressions.
Prolog_atom a_plus;
Prolog_atom a_minus;
Prolog_atom a_asterisk;

// To represent rational numbers as fractions.
Prolog_atom a_slash;

// For constraints.
Prolog_atom a_less_than;
Prolog_atom a_equal_less_than;
Prolog_atom a_equal;
Prolog_atom a_greater_than_equal;
Prolog_atom a_greater_than;

// For congruences.
Prolog_atom a_is_congruent_to;
Prolog_atom a_modulo;

// For generators.
Prolog_atom a_line;
Prolog_atom a_ray;
Prolog_atom a_point;
Prolog_atom a_closure_point;

// For grid_generators.
Prolog_atom a_grid_line;
Prolog_atom a_parameter;
Prolog_atom a_grid_point;

// For artificial_parameters.
Prolog_atom a_divided_by;

// For the relation between a polyhedron and a constraint.
Prolog_atom a_is_disjoint;
Prolog_atom a_strictly_intersects;
Prolog_atom a_is_included;
Prolog_atom a_saturates;

// For the relation between a polyhedron and a generator.
Prolog_atom a_subsumes;

// Denotes a closed interval boundary.
Prolog_atom a_c;

// Denotes the empty set such as the empty interval or polyhedron.
Prolog_atom a_empty;

// Denotes the universe polyhedron.
Prolog_atom a_universe;

// Denotes the maximization mode for optimization problems.
Prolog_atom a_max;

// Denotes the minimization mode for optimization problems.
Prolog_atom a_min;

// Denote possible widths of bounded integer types.
Prolog_atom a_bits_8;
Prolog_atom a_bits_16;
Prolog_atom a_bits_32;
Prolog_atom a_bits_64;
Prolog_atom a_bits_128;

// Denote possible representations of bounded integer types.
Prolog_atom a_unsigned;
Prolog_atom a_signed_2_complement;

// Denote possible overflow behavior of bounded integer types.
Prolog_atom a_overflow_wraps;
Prolog_atom a_overflow_undefined;
Prolog_atom a_overflow_impossible;

// Denote possible outcomes of MIP and PIP problems solution attempts.
Prolog_atom a_unfeasible;
Prolog_atom a_unbounded;
Prolog_atom a_optimized;

// Denotes an open interval boundary.
Prolog_atom a_o;

// Denotes the constructor that turns two boundaries into a proper interval.
Prolog_atom a_i;

// Denote the -infinity and +infinity interval boundaries.
Prolog_atom a_minf;
Prolog_atom a_pinf;

// Denote complexity classes.
Prolog_atom a_polynomial;
Prolog_atom a_simplex;
Prolog_atom a_any;

// Denote control_parameters.
Prolog_atom a_pricing;
Prolog_atom a_pricing_steepest_edge_float;
Prolog_atom a_pricing_steepest_edge_exact;
Prolog_atom a_pricing_textbook;

Prolog_atom a_cutting_strategy;
Prolog_atom a_cutting_strategy_first;
Prolog_atom a_cutting_strategy_deepest;
Prolog_atom a_cutting_strategy_all;

Prolog_atom a_pivot_row_strategy;
Prolog_atom a_pivot_row_strategy_first;
Prolog_atom a_pivot_row_strategy_max_column;

// Default timeout exception atom.
Prolog_atom a_time_out;

// "Out of memory" exception atom.
Prolog_atom a_out_of_memory;

// Boolean constants.
Prolog_atom a_true;
Prolog_atom a_false;

// To build exception terms.
Prolog_atom a_ppl_overflow_error;
Prolog_atom a_ppl_domain_error;
Prolog_atom a_ppl_length_error;
Prolog_atom a_ppl_invalid_argument;
Prolog_atom a_ppl_logic_error;
Prolog_atom a_ppl_representation_error;
Prolog_atom a_expected;
Prolog_atom a_found;
Prolog_atom a_where;

const Prolog_Interface_Atom prolog_interface_atoms[] = {
  { &a_dollar_VAR,               "$VAR" },

  { &a_plus,                     "+" },
  { &a_minus,                    "-" },
  { &a_asterisk,                 "*" },

  { &a_slash,                    "/" },

  { &a_equal,                    "=" },
  { &a_greater_than_equal,       ">=" },
  { &a_equal_less_than,          "=<" },
  { &a_greater_than,             ">" },
  { &a_less_than,                "<" },

  { &a_is_congruent_to,          "=:=" },
  { &a_modulo,                   "/" },

  { &a_divided_by,               "/" },

  { &a_line,                     "line" },
  { &a_ray,                      "ray" },
  { &a_point,                    "point" },
  { &a_closure_point,            "closure_point" },

  { &a_grid_line,                "grid_line" },
  { &a_parameter,                "parameter" },
  { &a_grid_point,               "grid_point" },

  { &a_is_disjoint,              "is_disjoint" },
  { &a_strictly_intersects,      "strictly_intersects" },
  { &a_is_included,              "is_included" },
  { &a_saturates,                "saturates" },

  { &a_subsumes,                 "subsumes" },

  { &a_c,                        "c" },

  { &a_empty,                    "empty" },
  { &a_universe,                 "universe" },

  { &a_max,                      "max" },
  { &a_min,                      "min" },

  { &a_bits_8,                   "bits_8" },
  { &a_bits_16,                  "bits_16" },
  { &a_bits_32,                  "bits_32" },
  { &a_bits_64,                  "bits_64" },
  { &a_bits_128,                 "bits_128" },

  { &a_unsigned,                 "unsigned" },
  { &a_signed_2_complement,      "signed_2_complement" },

  { &a_overflow_wraps,           "overflow_wraps" },
  { &a_overflow_undefined,       "overflow_undefined" },
  { &a_overflow_impossible,      "overflow_impossible" },

  { &a_unfeasible,               "unfeasible" },
  { &a_unbounded,                "unbounded" },
  { &a_optimized,                "optimized" },

  { &a_o,                        "o" },
  { &a_i,                        "i" },

  { &a_minf,                     "minf" },
  { &a_pinf,                     "pinf" },

  { &a_polynomial,               "polynomial" },
  { &a_simplex,                  "simplex" },
  { &a_any,                      "any" },

  { &a_pricing,                  "pricing" },
  { &a_pricing_steepest_edge_float,
                                 "pricing_steepest_edge_float" },
  { &a_pricing_steepest_edge_exact,
                                 "pricing_steepest_edge_exact" },
  { &a_pricing_textbook,         "pricing_textbook" },

  { &a_cutting_strategy,         "cutting_strategy" },
  { &a_cutting_strategy_first,   "cutting_strategy_first" },
  { &a_cutting_strategy_deepest, "cutting_strategy_deepest" },
  { &a_cutting_strategy_all,     "cutting_strategy_all" },

  { &a_pivot_row_strategy,       "pivot_row_strategy" },
  { &a_pivot_row_strategy_first, "pivot_row_strategy_first" },
  { &a_pivot_row_strategy_max_column,
                                 "pivot_row_strategy_max_column" },

  { &a_time_out,                 "time_out" },
  { &a_out_of_memory,            "out_of_memory" },

  { &a_true,                     "true" },
  { &a_false,                    "false" },

  { &a_ppl_invalid_argument,     "ppl_invalid_argument" },
  { &a_ppl_overflow_error,       "ppl_overflow_error" },
  { &a_ppl_domain_error,         "ppl_domain_error" },
  { &a_ppl_length_error,         "ppl_length_error" },
  { &a_ppl_invalid_argument,     "ppl_invalid_argument" },
  { &a_ppl_logic_error,          "ppl_logic_error" },
  { &a_ppl_representation_error, "ppl_representation_error" },
  { &a_expected,                 "expected" },
  { &a_found,                    "found" },
  { &a_where,                    "where" },
  { 0,                           0 }
};

Prolog_term_ref
Prolog_atom_term_from_string(const char* s) {
  Prolog_term_ref t = Prolog_new_term_ref();
  Prolog_put_atom(t, Prolog_atom_from_string(s));
  return t;
}

void
handle_exception(const Prolog_unsigned_out_of_range& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref max = Prolog_new_term_ref();
  Prolog_put_ulong(max, e.max());
  Prolog_construct_compound(max,
                            Prolog_atom_from_string("unsigned_integer"
                                                    "_less_or_equal"),
                            max);
  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_construct_compound(expected, a_expected, max);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));

  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_unsigned_integer& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_construct_compound(expected, a_expected,
                            Prolog_atom_term_from_string("unsigned_integer"));

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));

  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const non_linear& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_construct_compound(expected, a_expected,
                            Prolog_atom_term_from_string
                            ("linear_expression_or_constraint"));

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));

  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_variable& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found,
                            e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_construct_compound(expected, a_expected,
                            Prolog_atom_term_from_string
                            ("$VAR(unsigned_integer)"));

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));

  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_an_integer& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_construct_compound(expected, a_expected,
                            Prolog_atom_term_from_string("integer"));

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));

  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const ppl_handle_mismatch& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_construct_compound(expected, a_expected,
                            Prolog_atom_term_from_string("handle"));

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));

  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_an_optimization_mode& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("max"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("min"), expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_complexity_class& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("polynomial"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("simplex"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("any"), expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
  handle_exception(const not_a_control_parameter_name& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("pricing"), expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
  handle_exception(const not_a_control_parameter_value& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("pricing_steepest_edge_float"),
                        expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("pricing_steepest_edge_exact"),
                        expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("pricing_textbook"),
                        expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
  handle_exception(const not_a_pip_problem_control_parameter_name& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("cutting_strategy"),
                        expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("pivot_row_strategy"),
                        expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
  handle_exception(const not_a_pip_problem_control_parameter_value& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("cutting_strategy_first"),
                        expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("cutting_strategy_deepest"),
                        expected);
   Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("cutting_strategy_all"),
                        expected);
   Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("pivot_row_strategy_first"),
                        expected);
    Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("pivot_row_strategy_max_column"),
                        expected);
 Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_universe_or_empty& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("universe"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("empty"), expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_boolean& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("true"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("false"), expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_bounded_integer_type_width& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("bits_8"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("bits_16"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("bits_32"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("bits_64"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("bits_128"), expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_bounded_integer_type_representation& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("unsigned"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("signed_2_complement"),
                        expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_bounded_integer_type_overflow& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("overflow_wraps"),
                        expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("overflow_undefined"),
                        expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("overflow_impossible"),
                        expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_relation& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("="), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string(">="), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("=<"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string(">"), expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string("<"), expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const not_a_nil_terminated_list& e) {
  Prolog_term_ref found = Prolog_new_term_ref();
  Prolog_construct_compound(found, a_found, e.term());

  Prolog_term_ref expected = Prolog_new_term_ref();
  Prolog_put_nil(expected);
  Prolog_construct_cons(expected,
                        Prolog_atom_term_from_string
                        ("Prolog_list"), expected);
  Prolog_construct_compound(expected, a_expected, expected);

  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string(e.where()));
  Prolog_term_ref exception_term = Prolog_new_term_ref();
  Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                            found, expected, where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const PPL_integer_out_of_range& e) {
  Prolog_term_ref where = Prolog_new_term_ref();
  Prolog_construct_compound(where, a_where,
                            Prolog_atom_term_from_string("Coefficient_to_integer_term"));

  Prolog_term_ref exception_term = Prolog_new_term_ref();
  std::ostringstream s;
  s << e.value();
  std::string str = s.str();
  Prolog_construct_compound(exception_term, a_ppl_representation_error,
                            Prolog_atom_term_from_string(str.c_str()),
                            where);
  Prolog_raise_exception(exception_term);
}

void
handle_exception(const unknown_interface_error& e) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_put_atom_chars(et, e.where());
  Prolog_raise_exception(et);
}

void
handle_exception(const std::overflow_error& e) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_construct_compound(et, a_ppl_overflow_error,
                            Prolog_atom_term_from_string(e.what()));
  Prolog_raise_exception(et);
}

void
handle_exception(const std::domain_error& e) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_construct_compound(et, a_ppl_domain_error,
                            Prolog_atom_term_from_string(e.what()));
  Prolog_raise_exception(et);
}

void
handle_exception(const std::length_error& e) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_construct_compound(et, a_ppl_length_error,
                            Prolog_atom_term_from_string(e.what()));
  Prolog_raise_exception(et);
}

void
handle_exception(const std::invalid_argument& e) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_construct_compound(et, a_ppl_invalid_argument,
                            Prolog_atom_term_from_string(e.what()));
  Prolog_raise_exception(et);
}

void
handle_exception(const std::logic_error& e) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_construct_compound(et, a_ppl_logic_error,
                            Prolog_atom_term_from_string(e.what()));
  Prolog_raise_exception(et);
}

void
handle_exception(const std::bad_alloc&) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_put_atom(et, out_of_memory_exception_atom);
  Prolog_raise_exception(et);
}

void
handle_exception(const std::exception& e) {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_put_atom_chars(et, e.what());
  Prolog_raise_exception(et);
}

void
handle_exception() {
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_put_atom_chars(et, "PPL bug: unknown exception raised");
  Prolog_raise_exception(et);
}

Parma_Polyhedra_Library::Watchdog* p_timeout_object = 0;

typedef
Parma_Polyhedra_Library::Threshold_Watcher
<Parma_Polyhedra_Library::Weightwatch_Traits> Weightwatch;

Weightwatch* p_deterministic_timeout_object = 0;

void
reset_timeout() {
  if (p_timeout_object) {
    delete p_timeout_object;
    p_timeout_object = 0;
    abandon_expensive_computations = 0;
  }
}

void
reset_deterministic_timeout() {
  if (p_deterministic_timeout_object) {
    delete p_deterministic_timeout_object;
    p_deterministic_timeout_object = 0;
    abandon_expensive_computations = 0;
  }
}

Prolog_atom timeout_exception_atom;

void
handle_exception(const timeout_exception&) {
  assert(p_timeout_object);
  reset_timeout();
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_put_atom(et, timeout_exception_atom);
  Prolog_raise_exception(et);
}

void
handle_exception(const deterministic_timeout_exception&) {
  assert(p_deterministic_timeout_object);
  reset_deterministic_timeout();
  Prolog_term_ref et = Prolog_new_term_ref();
  Prolog_put_atom(et, timeout_exception_atom);
  Prolog_raise_exception(et);
}

Prolog_term_ref
variable_term(dimension_type varid) {
  Prolog_term_ref v = Prolog_new_term_ref();
  Prolog_put_ulong(v, varid);
  Prolog_term_ref t = Prolog_new_term_ref();
  Prolog_construct_compound(t, a_dollar_VAR, v);
  return t;
}

Prolog_atom
term_to_boolean(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_true || name == a_false))
      return name;
  }
  throw not_a_boolean(t, where);
}

Prolog_atom
term_to_universe_or_empty(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_universe || name == a_empty))
      return name;
  }
  throw not_universe_or_empty(t, where);
}

Coefficient
integer_term_to_Coefficient(Prolog_term_ref t) {
  PPL_DIRTY_TEMP_COEFFICIENT(n);
  assert(Prolog_is_integer(t));
  if (!Prolog_get_Coefficient(t, n))
    abort();
  return n;
}

Prolog_term_ref
Coefficient_to_integer_term(const Coefficient& n) {
  Prolog_term_ref t = Prolog_new_term_ref();
  if (!Prolog_put_Coefficient(t, n))
    abort();
  return t;
}

bool
unify_long(Prolog_term_ref t, long l) {
  Prolog_term_ref t_l = Prolog_new_term_ref();
  return Prolog_put_long(t_l, l) && Prolog_unify(t, t_l);
}

bool
unify_ulong(Prolog_term_ref t, unsigned long l) {
  Prolog_term_ref t_l = Prolog_new_term_ref();
  return Prolog_put_ulong(t_l, l) && Prolog_unify(t, t_l);
}

Linear_Expression
build_linear_expression(Prolog_term_ref t, const char* where) {
  if (Prolog_is_integer(t))
    return Linear_Expression(integer_term_to_Coefficient(t));
  else if (Prolog_is_compound(t)) {
    Prolog_atom functor;
    size_t arity;
    Prolog_get_compound_name_arity(t, &functor, &arity);
    switch (arity) {
    case 1:
      {
        Prolog_term_ref arg = Prolog_new_term_ref();
        Prolog_get_arg(1, t, arg);
        if (functor == a_minus)
          // Unary minus.
          return -build_linear_expression(arg, where);
        else if (functor == a_dollar_VAR)
          // Variable.
          return Variable(term_to_unsigned<dimension_type>(arg, where));
      }
      break;
    case 2:
      {
        Prolog_term_ref arg1 = Prolog_new_term_ref();
        Prolog_term_ref arg2 = Prolog_new_term_ref();
        Prolog_get_arg(1, t, arg1);
        Prolog_get_arg(2, t, arg2);
        if (functor == a_plus)
          // Plus.
          if (Prolog_is_integer(arg1))
            return integer_term_to_Coefficient(arg1)
              + build_linear_expression(arg2, where);
          else if (Prolog_is_integer(arg2))
            return build_linear_expression(arg1, where)
              + integer_term_to_Coefficient(arg2);
          else
            return build_linear_expression(arg1, where)
              + build_linear_expression(arg2, where);
        else if (functor == a_minus)
          // Minus.
          if (Prolog_is_integer(arg1))
            return integer_term_to_Coefficient(arg1)
              - build_linear_expression(arg2, where);
          else if (Prolog_is_integer(arg2))
            return build_linear_expression(arg1, where)
              - integer_term_to_Coefficient(arg2);
          else
            return build_linear_expression(arg1, where)
              - build_linear_expression(arg2, where);
        else if (functor == a_asterisk) {
          // Times.
          if (Prolog_is_integer(arg1))
            return integer_term_to_Coefficient(arg1)
              * build_linear_expression(arg2, where);
          else if (Prolog_is_integer(arg2))
            return build_linear_expression(arg1, where)
              * integer_term_to_Coefficient(arg2);
        }
      }
    }
  }
  // Invalid.
  throw non_linear(t, where);
}

Constraint
build_constraint(Prolog_term_ref t, const char* where) {
  if (Prolog_is_compound(t)) {
    Prolog_atom functor;
    size_t arity;
    Prolog_get_compound_name_arity(t, &functor, &arity);
    if (arity == 2) {
      Prolog_term_ref arg1 = Prolog_new_term_ref();
      Prolog_term_ref arg2 = Prolog_new_term_ref();
      Prolog_get_arg(1, t, arg1);
      Prolog_get_arg(2, t, arg2);
      if (functor == a_equal)
        // =
        if (Prolog_is_integer(arg1))
          return integer_term_to_Coefficient(arg1)
            == build_linear_expression(arg2, where);
        else if (Prolog_is_integer(arg2))
          return build_linear_expression(arg1, where)
            == integer_term_to_Coefficient(arg2);
        else
          return build_linear_expression(arg1, where)
            == build_linear_expression(arg2, where);
      else if (functor == a_equal_less_than)
        // =<
        if (Prolog_is_integer(arg1))
          return integer_term_to_Coefficient(arg1)
            <= build_linear_expression(arg2, where);
        else if (Prolog_is_integer(arg2))
          return build_linear_expression(arg1, where)
            <= integer_term_to_Coefficient(arg2);
        else
          return build_linear_expression(arg1, where)
            <= build_linear_expression(arg2, where);
      else if (functor == a_greater_than_equal)
        // >=
        if (Prolog_is_integer(arg1))
          return integer_term_to_Coefficient(arg1)
            >= build_linear_expression(arg2, where);
        else if (Prolog_is_integer(arg2))
          return build_linear_expression(arg1, where)
            >= integer_term_to_Coefficient(arg2);
        else
          return build_linear_expression(arg1, where)
            >= build_linear_expression(arg2, where);
      else if (functor == a_less_than)
        // <
        if (Prolog_is_integer(arg1))
          return integer_term_to_Coefficient(arg1)
            < build_linear_expression(arg2, where);
        else if (Prolog_is_integer(arg2))
          return build_linear_expression(arg1, where)
            < integer_term_to_Coefficient(arg2);
        else
          return build_linear_expression(arg1, where)
            < build_linear_expression(arg2, where);
      else if (functor == a_greater_than) {
        // >
        if (Prolog_is_integer(arg1))
          return integer_term_to_Coefficient(arg1)
            > build_linear_expression(arg2, where);
        else if (Prolog_is_integer(arg2))
          return build_linear_expression(arg1, where)
            > integer_term_to_Coefficient(arg2);
        else
          return build_linear_expression(arg1, where)
            > build_linear_expression(arg2, where);
      }
    }
  }
  // Invalid.
  throw non_linear(t, where);
}

Congruence
build_congruence(Prolog_term_ref t, const char* where) {
  if (Prolog_is_compound(t)) {
    Prolog_atom functor;
    size_t arity;
    Prolog_get_compound_name_arity(t, &functor, &arity);
    if (arity == 2) {
      Prolog_term_ref arg1 = Prolog_new_term_ref();
      Prolog_term_ref arg2 = Prolog_new_term_ref();
      Prolog_get_arg(1, t, arg1);
      Prolog_get_arg(2, t, arg2);
      if (functor == a_modulo) {
        // /
        if (Prolog_is_integer(arg2)) {
          Prolog_atom functor1;
          size_t arity1;
          Prolog_get_compound_name_arity(arg1, &functor1, &arity1);
          if (arity1 == 2) {
            if (functor1 == a_is_congruent_to) {
              // =:=
              Prolog_term_ref arg11 = Prolog_new_term_ref();
              Prolog_term_ref arg12 = Prolog_new_term_ref();
              Prolog_get_arg(1, arg1, arg11);
              Prolog_get_arg(2, arg1, arg12);
              if (Prolog_is_integer(arg12))
                return (build_linear_expression(arg11, where)
                        %= integer_term_to_Coefficient(arg12))
                  / integer_term_to_Coefficient(arg2);
              else
                return (build_linear_expression(arg11, where)
                        %= build_linear_expression(arg12, where))
                  / integer_term_to_Coefficient(arg2);
            }
          }
        }
      }
      else
        if (functor == a_is_congruent_to)
          // =:=
          if (Prolog_is_integer(arg2))
            return build_linear_expression(arg1, where)
              %= integer_term_to_Coefficient(arg2);
          else
            return build_linear_expression(arg1, where)
              %= build_linear_expression(arg2, where);
        else
          if (functor == a_equal) {
            // =
            if (Prolog_is_integer(arg1))
              return (build_linear_expression(arg2, where)
                      %= integer_term_to_Coefficient(arg1)) / 0;
            else if (Prolog_is_integer(arg2))
              return (build_linear_expression(arg1, where)
                      %= integer_term_to_Coefficient(arg2)) / 0;
            else
              return (build_linear_expression(arg1, where)
                      %= build_linear_expression(arg2, where)) / 0;
          }
    }
  }
  // Invalid.
  throw non_linear(t, where);
}

Generator
build_generator(Prolog_term_ref t, const char* where) {
  if (Prolog_is_compound(t)) {
    Prolog_atom functor;
    size_t arity;
    Prolog_get_compound_name_arity(t, &functor, &arity);
    if (arity == 1) {
      Prolog_term_ref arg = Prolog_new_term_ref();
      Prolog_get_arg(1, t, arg);
      if (functor == a_line)
        return Generator::line(build_linear_expression(arg, where));
      else if (functor == a_ray)
        return Generator::ray(build_linear_expression(arg, where));
      else if (functor == a_point)
        return Generator::point(build_linear_expression(arg, where));
      else if (functor == a_closure_point)
        return Generator::closure_point(build_linear_expression(arg, where));
    }
    else if (arity == 2) {
      Prolog_term_ref arg1 = Prolog_new_term_ref();
      Prolog_term_ref arg2 = Prolog_new_term_ref();
      Prolog_get_arg(1, t, arg1);
      Prolog_get_arg(2, t, arg2);
      if (Prolog_is_integer(arg2)) {
        if (functor == a_point)
          return Generator::point(build_linear_expression(arg1, where),
                                  integer_term_to_Coefficient(arg2));
        else if (functor == a_closure_point)
          return Generator::closure_point(build_linear_expression(arg1, where),
                                          integer_term_to_Coefficient(arg2));
      }
    }
  }
  // Invalid.
  throw non_linear(t, where);
}

Grid_Generator
build_grid_generator(Prolog_term_ref t, const char* where) {
  if (Prolog_is_compound(t)) {
    Prolog_atom functor;
    size_t arity;
    Prolog_get_compound_name_arity(t, &functor, &arity);
    if (arity == 1) {
      Prolog_term_ref arg = Prolog_new_term_ref();
      Prolog_get_arg(1, t, arg);
      if (functor == a_grid_line)
        return Grid_Generator::grid_line(build_linear_expression(arg, where));
      else if (functor == a_parameter)
        return Grid_Generator::parameter(build_linear_expression(arg, where));
      else if (functor == a_grid_point)
        return Grid_Generator::grid_point(build_linear_expression(arg, where));
    }
    else if (arity == 2) {
      Prolog_term_ref arg1 = Prolog_new_term_ref();
      Prolog_term_ref arg2 = Prolog_new_term_ref();
      Prolog_get_arg(1, t, arg1);
      Prolog_get_arg(2, t, arg2);
      if (Prolog_is_integer(arg2)) {
        if (functor == a_grid_point)
          return Grid_Generator::grid_point(build_linear_expression(arg1,
                                                                    where),
                                            integer_term_to_Coefficient(arg2));
        else if (functor == a_parameter)
          return Grid_Generator::parameter(build_linear_expression(arg1,
                                                                   where),
                                          integer_term_to_Coefficient(arg2));
      }
    }
  }
  // Invalid.
  throw non_linear(t, where);
}

template <typename R>
Prolog_term_ref
get_homogeneous_expression(const R& r) {
  Prolog_term_ref so_far = Prolog_new_term_ref();
  PPL_DIRTY_TEMP_COEFFICIENT(coefficient);
  dimension_type varid = 0;
  dimension_type space_dimension = r.space_dimension();
  while (varid < space_dimension
         && (coefficient = r.coefficient(Variable(varid))) == 0)
    ++varid;
  if (varid >= space_dimension) {
    Prolog_put_long(so_far, 0);
  }
  else {
    Prolog_construct_compound(so_far, a_asterisk,
                              Coefficient_to_integer_term(coefficient),
                              variable_term(varid));
    while (true) {
      ++varid;
      while (varid < space_dimension
             && (coefficient = r.coefficient(Variable(varid))) == 0)
        ++varid;
      if (varid >= space_dimension)
        break;
      else {
        Prolog_term_ref addendum = Prolog_new_term_ref();
        Prolog_construct_compound(addendum, a_asterisk,
                                  Coefficient_to_integer_term(coefficient),
                                  variable_term(varid));
        Prolog_term_ref new_so_far = Prolog_new_term_ref();
        Prolog_construct_compound(new_so_far, a_plus,
                                  so_far, addendum);
        so_far = new_so_far;
      }
    }
  }
  return so_far;
}

Prolog_term_ref
get_linear_expression(const Linear_Expression& le) {
  Prolog_term_ref t_homo = get_homogeneous_expression(le);
  if (le.inhomogeneous_term() == 0)
    return t_homo;
  else {
    Prolog_term_ref t_in
      = Coefficient_to_integer_term(le.inhomogeneous_term());
    if (unify_long(t_homo, 0))
      return t_in;
    else {
      Prolog_term_ref t_le = Prolog_new_term_ref();
      Prolog_construct_compound(t_le, a_plus, t_homo, t_in);
      return t_le;
    }
  }
}

Prolog_term_ref
constraint_term(const Constraint& c) {
  Prolog_atom relation = 0;
  switch (c.type()) {
  case Constraint::EQUALITY:
    relation = a_equal;
    break;
  case Constraint::NONSTRICT_INEQUALITY:
    relation = a_greater_than_equal;
    break;
  case Constraint::STRICT_INEQUALITY:
    relation = a_greater_than;
    break;
  default:
    throw unknown_interface_error("generator_term()");
  }
  Prolog_term_ref t = Prolog_new_term_ref();
  Prolog_construct_compound
    (t,
     relation,
     get_homogeneous_expression(c),
     Coefficient_to_integer_term(-c.inhomogeneous_term()));
  return t;
}

Prolog_term_ref
congruence_term(const Congruence& cg) {
  Prolog_atom relation1 = a_is_congruent_to;
  Prolog_atom relation2 = a_modulo;
  Prolog_term_ref t_tmp = Prolog_new_term_ref();
  Prolog_term_ref t = Prolog_new_term_ref();
  Prolog_construct_compound
    (t_tmp,
     relation1,
     get_homogeneous_expression(cg),
     Coefficient_to_integer_term(-cg.inhomogeneous_term()));
  Prolog_construct_compound
    (t,
     relation2,
     t_tmp,
     Coefficient_to_integer_term(cg.modulus()));
  return t;
}

Prolog_term_ref
generator_term(const Generator& g) {
  Prolog_term_ref t = Prolog_new_term_ref();
  Prolog_atom constructor = 0;
  switch (g.type()) {
  case Generator::LINE:
    constructor = a_line;
    break;
  case Generator::RAY:
    constructor = a_ray;
    break;
  case Generator::POINT:
    {
      constructor = a_point;
      const Coefficient& divisor = g.divisor();
      if (divisor == 1)
        break;
      else {
        Prolog_construct_compound(t, constructor,
                                  get_homogeneous_expression(g),
                                  Coefficient_to_integer_term(divisor));
        return t;
      }
    }
  case Generator::CLOSURE_POINT:
    {
      constructor = a_closure_point;
      const Coefficient& divisor = g.divisor();
      if (divisor == 1)
        break;
      else {
        Prolog_construct_compound(t, constructor,
                                  get_homogeneous_expression(g),
                                  Coefficient_to_integer_term(divisor));
        return t;
      }
    }
  default:
    throw unknown_interface_error("generator_term()");
  }
  Prolog_construct_compound(t, constructor, get_homogeneous_expression(g));
  return t;
}

Prolog_term_ref
grid_generator_term(const Grid_Generator& g) {
  Prolog_term_ref t = Prolog_new_term_ref();
  Prolog_atom constructor = 0;
  switch (g.type()) {
  case Grid_Generator::LINE:
    constructor = a_grid_line;
    break;
  case Grid_Generator::PARAMETER:
    {
      constructor = a_parameter;
      const Coefficient& divisor = g.divisor();
      if (divisor == 1)
        break;
      else {
        Prolog_construct_compound(t, constructor,
                                  get_homogeneous_expression(g),
                                  Coefficient_to_integer_term(divisor));
        return t;
      }
    }
  case Grid_Generator::POINT:
    {
      constructor = a_grid_point;
      const Coefficient& divisor = g.divisor();
      if (divisor == 1)
        break;
      else {
        Prolog_construct_compound(t, constructor,
                                  get_homogeneous_expression(g),
                                  Coefficient_to_integer_term(divisor));
        return t;
      }
    }
  default:
    throw unknown_interface_error("grid_generator_term()");
  }
  Prolog_construct_compound(t, constructor, get_homogeneous_expression(g));
  return t;
}

Prolog_term_ref
artificial_parameter_term(const PIP_Tree_Node::Artificial_Parameter& art) {
  Prolog_term_ref t = Prolog_new_term_ref();
  Prolog_construct_compound(t, a_divided_by,
                            get_linear_expression(art),
                            Coefficient_to_integer_term(art.denominator()));
  return t;
}

Variable
term_to_Variable(Prolog_term_ref t, const char* where) {
  if (Prolog_is_compound(t)) {
    Prolog_atom functor;
    size_t arity;
    Prolog_get_compound_name_arity(t, &functor, &arity);
    if (functor == a_dollar_VAR && arity == 1) {
      Prolog_term_ref arg = Prolog_new_term_ref();
      Prolog_get_arg(1, t, arg);
      return
        Variable(term_to_unsigned<dimension_type>(arg, "term_to_Variable"));
    }
  }
  throw not_a_variable(t, where);
}

Coefficient
term_to_Coefficient(Prolog_term_ref t, const char* where) {
  if (Prolog_is_integer(t))
    return integer_term_to_Coefficient(t);
  else
    throw not_an_integer(t, where);
}

Prolog_atom
term_to_bounded_integer_type_width(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_bits_8 || name == a_bits_16
            || name == a_bits_32 || name == a_bits_64
            || name == a_bits_128))
      return name;
  }
  throw not_a_bounded_integer_type_width(t, where);
}

Prolog_atom
term_to_bounded_integer_type_representation(Prolog_term_ref t,
                                            const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_unsigned || name == a_signed_2_complement))
      return name;
  }
  throw not_a_bounded_integer_type_representation(t, where);
}

Prolog_atom
term_to_bounded_integer_type_overflow(Prolog_term_ref t,
                                      const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_overflow_wraps
            || name == a_overflow_undefined
            || name == a_overflow_impossible))
      return name;
  }
  throw not_a_bounded_integer_type_overflow(t, where);
}

Prolog_atom
term_to_optimization_mode(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_max || name == a_min))
      return name;
  }
  throw not_an_optimization_mode(t, where);
}

Prolog_atom
term_to_control_parameter_name(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_pricing || name == a_cutting_strategy))
      return name;
  }
  throw not_a_control_parameter_name(t, where);
}

Prolog_atom
term_to_pip_problem_control_parameter_name(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_cutting_strategy || name == a_pivot_row_strategy))
      return name;
  }
  throw not_a_pip_problem_control_parameter_name(t, where);
}

Prolog_atom
term_to_control_parameter_value(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_pricing_steepest_edge_float
            || name == a_pricing_steepest_edge_exact
            || name == a_pricing_textbook
            || name == a_cutting_strategy_first
            || name == a_cutting_strategy_deepest))
      return name;
  }
  throw not_a_control_parameter_value(t, where);
}

Prolog_atom
term_to_pip_problem_control_parameter_value(Prolog_term_ref t,
                                            const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_cutting_strategy_first
            || name == a_cutting_strategy_deepest
            || name == a_cutting_strategy_all
            || name == a_pivot_row_strategy_first
            || name == a_pivot_row_strategy_max_column))
      return name;
  }
  throw not_a_pip_problem_control_parameter_value(t, where);
}

bool Prolog_interface_initialized = false;

void
check_nil_terminating(Prolog_term_ref t, const char* where) {
  if (Prolog_get_nil(t))
    return;
  throw not_a_nil_terminated_list(t, where);
}

inline dimension_type
max_representable_dimension(dimension_type d) {
  return
    Prolog_has_unbounded_integers
    ? d
    : std::min(d, static_cast<dimension_type>(Prolog_max_integer));
}

bool
term_to_boundary(Prolog_term_ref t_b, Boundary_Kind kind,
                 bool& finite, bool& closed,
                 Coefficient& n, Coefficient& d) {
  if (!Prolog_is_compound(t_b))
    return false;

  Prolog_atom functor;
  size_t arity;

  Prolog_get_compound_name_arity(t_b, &functor, &arity);
  // A boundary term is either of the form c(Limit) or o(Limit).
  if (arity != 1 || (functor != a_c && functor != a_o))
    return false;

  Prolog_atom open_closed_atom = functor;

  Prolog_term_ref t_limit = Prolog_new_term_ref();
  Prolog_get_arg(1, t_b, t_limit);
  if (Prolog_is_integer(t_limit)) {
    // A finite, integral limit.
    finite = true;
    closed = (open_closed_atom == a_c);
    n = integer_term_to_Coefficient(t_limit);
    d = 1;
  }
  else if (Prolog_is_atom(t_limit)) {
    Prolog_atom a;
    Prolog_get_atom_name(t_limit, &a);
    Prolog_atom allowed_infinity = (kind == LOWER_BOUNDARY ? a_minf : a_pinf);
    // Only open bounds may be unbounded.
    if (a != allowed_infinity || open_closed_atom != a_o)
      return false;

    finite = false;
  }
  else if (Prolog_is_compound(t_limit)) {
    Prolog_get_compound_name_arity(t_limit, &functor, &arity);
    if (arity != 2 || functor != a_slash)
      return false;

    Prolog_term_ref t_n = Prolog_new_term_ref();
    Prolog_term_ref t_d = Prolog_new_term_ref();
    Prolog_get_arg(1, t_limit, t_n);
    Prolog_get_arg(2, t_limit, t_d);

    if (!Prolog_is_integer(t_n) || !Prolog_is_integer(t_d))
      return false;
    else {
      finite = true;
      closed = (open_closed_atom == a_c);
      n = integer_term_to_Coefficient(t_n);
      d = integer_term_to_Coefficient(t_d);
      // Catch negative denominators and divisions by zero here.
      if (d <= 0)
        return false;
    }
  }
  return true;
}

Prolog_atom
term_to_relation(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_equal
            || name == a_greater_than_equal
            || name == a_equal_less_than
            || name == a_greater_than
            || name == a_less_than))
      return name;
  }
  throw not_a_relation(t, where);
}

Relation_Symbol
term_to_relation_symbol(Prolog_term_ref t_r, const char* where) {
  Prolog_atom ra = term_to_relation(t_r, where);
  Relation_Symbol r;
  if (ra == a_less_than)
    r = LESS_THAN;
  else if (ra == a_equal_less_than)
    r = LESS_OR_EQUAL;
  else if (ra == a_equal)
    r = EQUAL;
  else if (ra == a_greater_than_equal)
    r = GREATER_OR_EQUAL;
  else {
    assert(ra == a_greater_than);
    r = GREATER_THAN;
  }
  return r;
}

Prolog_term_ref
rational_term(const Rational_Box::interval_type::boundary_type& q) {
  Prolog_term_ref t = Prolog_new_term_ref();
  PPL_DIRTY_TEMP_COEFFICIENT(numerator);
  PPL_DIRTY_TEMP_COEFFICIENT(denominator);
  numerator = q.get_num();
  denominator = q.get_den();
  if (denominator == 1)
    Prolog_put_Coefficient(t, numerator);
  else
    Prolog_construct_compound(t, a_slash,
                              Coefficient_to_integer_term(numerator),
                              Coefficient_to_integer_term(denominator));
  return t;
}

Prolog_term_ref
interval_term(const Rational_Box::interval_type& i) {
  Prolog_term_ref t = Prolog_new_term_ref();
  if (i.is_empty())
    Prolog_put_atom(t, a_empty);
  else {
    // Lower bound.
    const Prolog_atom& l_oc = i.lower_is_open() ? a_o : a_c;
    Prolog_term_ref l_b = Prolog_new_term_ref();
    if (i.lower_is_boundary_infinity())
      Prolog_put_atom(l_b, a_minf);
    else
      Prolog_put_term(l_b, rational_term(i.lower()));
    Prolog_term_ref l_t = Prolog_new_term_ref();
    Prolog_construct_compound(l_t, l_oc, l_b);

    // Upper bound.
    const Prolog_atom& u_oc = i.upper_is_open() ? a_o : a_c;
    Prolog_term_ref u_b = Prolog_new_term_ref();
    if (i.upper_is_boundary_infinity())
      Prolog_put_atom(u_b, a_pinf);
    else
      Prolog_put_term(u_b, rational_term(i.upper()));
    Prolog_term_ref u_t = Prolog_new_term_ref();
    Prolog_construct_compound(u_t, u_oc, u_b);

    Prolog_construct_compound(t, a_i, l_t, u_t);
  }
  return t;
}

Prolog_atom
term_to_complexity_class(Prolog_term_ref t, const char* where) {
  if (Prolog_is_atom(t)) {
    Prolog_atom name;
    if (Prolog_get_atom_name(t, &name)
        && (name == a_polynomial || name == a_simplex || name == a_any))
      return name;
  }
  throw not_a_complexity_class(t, where);
}

} // namespace Prolog

} // namespace Interfaces

} // namespace Parma_Polyhedra_Library

using namespace Parma_Polyhedra_Library::Interfaces::Prolog;

extern "C" Prolog_foreign_return_type
ppl_version_major(Prolog_term_ref t_v) {
  try {
    if (unify_ulong(t_v, version_major()))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_version_minor(Prolog_term_ref t_v) {
  try {
    if (unify_ulong(t_v, version_minor()))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_version_revision(Prolog_term_ref t_v) {
  try {
    if (unify_ulong(t_v, version_revision()))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_version_beta(Prolog_term_ref t_v) {
  try {
    if (unify_ulong(t_v, version_beta()))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_version(Prolog_term_ref t_v) {
  try {
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_atom_chars(tmp, version());
    if (Prolog_unify(t_v, tmp))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_banner(Prolog_term_ref t_b) {
  try {
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_atom_chars(tmp, banner());
    if (Prolog_unify(t_b, tmp))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_max_space_dimension(Prolog_term_ref t_msd) {
  try {
    if (unify_ulong(t_msd, max_representable_dimension(max_space_dimension())))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_initialize() {
  try {
    if (Prolog_interface_initialized)
      return PROLOG_SUCCESS;
    // Initialize the core library.
    initialize();
    for (size_t i = 0; prolog_interface_atoms[i].p_atom != 0; ++i) {
      Prolog_atom a = Prolog_atom_from_string(prolog_interface_atoms[i].name);
      *prolog_interface_atoms[i].p_atom = a;
    }
    timeout_exception_atom = a_time_out;
    out_of_memory_exception_atom = a_out_of_memory;
    ppl_Prolog_sysdep_init();
    Prolog_interface_initialized = true;
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_finalize() {
  try {
    if (!Prolog_interface_initialized)
      return PROLOG_SUCCESS;

    Prolog_interface_initialized = false;
    // Finalize the core library.
    finalize();
    // Release the pending timeout object, if any.
    reset_timeout();
    ppl_Prolog_sysdep_deinit();
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_set_rounding_for_PPL() {
  try {
    set_rounding_for_PPL();
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_restore_pre_PPL_rounding() {
  try {
    restore_pre_PPL_rounding();
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_irrational_precision(Prolog_term_ref t_p) {
  try {
    if (unify_ulong(t_p, irrational_precision()))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_set_irrational_precision(Prolog_term_ref t_p) {
  try {
    unsigned p
      = term_to_unsigned<unsigned>(t_p, "ppl_set_irrational_precision/1");
    set_irrational_precision(p);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_set_timeout_exception_atom(Prolog_term_ref t_tea) {
  try {
    if (Prolog_is_atom(t_tea)) {
      Prolog_atom tea;
      if (Prolog_get_atom_name(t_tea, &tea)) {
        timeout_exception_atom = tea;
        return PROLOG_SUCCESS;
      }
    }
    Prolog_term_ref found = Prolog_new_term_ref();
    Prolog_construct_compound(found, a_found, t_tea);

    Prolog_term_ref expected = Prolog_new_term_ref();
    Prolog_construct_compound(expected, a_expected,
                              Prolog_atom_term_from_string("atom"));

    Prolog_term_ref where = Prolog_new_term_ref();
    Prolog_construct_compound(where, a_where,
                              Prolog_atom_term_from_string
                              ("ppl_set_timeout_exception_atom"));

    Prolog_term_ref exception_term = Prolog_new_term_ref();
    Prolog_construct_compound(exception_term, a_ppl_invalid_argument,
                              found, expected, where);
    Prolog_raise_exception(exception_term);
    return PROLOG_FAILURE;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_timeout_exception_atom(Prolog_term_ref t) {
  try {
    Prolog_term_ref t_tea = Prolog_new_term_ref();
    Prolog_put_atom(t_tea, timeout_exception_atom);
    return Prolog_unify(t_tea, t) ? PROLOG_SUCCESS : PROLOG_FAILURE;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_set_timeout(Prolog_term_ref t_csecs) {
  try {
    // In case a timeout was already set.
    reset_timeout();
    static timeout_exception e;
    unsigned csecs = term_to_unsigned<unsigned>(t_csecs, "ppl_set_timeout/1");
    p_timeout_object =
      new Parma_Polyhedra_Library::Watchdog(csecs,
                                            abandon_expensive_computations,
                                            e);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_reset_timeout() {
  try {
    reset_timeout();
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_set_deterministic_timeout(Prolog_term_ref t_unscaled_weight,
                              Prolog_term_ref t_scale) {
  try {
    // In case a deterministic timeout was already set.
    reset_deterministic_timeout();
    static deterministic_timeout_exception e;
    unsigned long unscaled_weight
      = term_to_unsigned<unsigned long>(t_unscaled_weight,
                                        "ppl_set_deterministic_timeout/2");
    unsigned scale
      = term_to_unsigned<unsigned>(t_scale, "ppl_set_deterministic_timeout/2");
    typedef Parma_Polyhedra_Library::Weightwatch_Traits Traits;
    p_deterministic_timeout_object
      = new Weightwatch(Traits::compute_delta(unscaled_weight, scale),
                        abandon_expensive_computations, e);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_reset_deterministic_timeout() {
  try {
    reset_deterministic_timeout();
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_Coefficient_bits(Prolog_term_ref t_bits) {
  try {
    if (unify_ulong(t_bits, PPL_COEFFICIENT_BITS))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_Coefficient_is_bounded() {
  try {
    if (std::numeric_limits<Coefficient>::is_bounded)
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_Coefficient_min(Prolog_term_ref t_min) {
  try {
    if (std::numeric_limits<Coefficient>::is_bounded) {
      PPL_DIRTY_TEMP_COEFFICIENT(min);
      min = std::numeric_limits<Coefficient>::min();
      if (Prolog_has_unbounded_integers
          || (min >= Prolog_min_integer && min <= Prolog_min_integer))
        return Prolog_unify_Coefficient(t_min, min)
          ? PROLOG_SUCCESS : PROLOG_FAILURE;
    }
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_Coefficient_max(Prolog_term_ref t_max) {
  try {
    if (std::numeric_limits<Coefficient>::is_bounded) {
      PPL_DIRTY_TEMP_COEFFICIENT(max);
      max = std::numeric_limits<Coefficient>::max();
      if (Prolog_has_unbounded_integers
          || (max >= Prolog_min_integer && max <= Prolog_min_integer))
        return Prolog_unify_Coefficient(t_max, max)
          ? PROLOG_SUCCESS : PROLOG_FAILURE;
    }
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_new_MIP_Problem_from_space_dimension
(Prolog_term_ref t_nd, Prolog_term_ref t_mip) {
  static const char* where = "ppl_MIP_Problem_from_space_dimension/2";
  try {
    dimension_type d = term_to_unsigned<dimension_type>(t_nd, where);
    MIP_Problem* mip = new MIP_Problem(d);
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_address(tmp, mip);
    if (Prolog_unify(t_mip, tmp)) {
      PPL_REGISTER(mip);
      return PROLOG_SUCCESS;
    }
    else
      delete mip;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_new_MIP_Problem(Prolog_term_ref t_nd,
                    Prolog_term_ref t_clist,
                    Prolog_term_ref t_le_expr,
                    Prolog_term_ref t_opt,
                    Prolog_term_ref t_mip) {
  static const char* where = "ppl_new_MIP_Problem/5";
  try {
    Constraint_System cs;
    Prolog_term_ref c = Prolog_new_term_ref();
    while (Prolog_is_cons(t_clist)) {
      Prolog_get_cons(t_clist, c, t_clist);
      cs.insert(build_constraint(c, where));
    }
    // Check the list is properly terminated.
    check_nil_terminating(t_clist, where);

    const Linear_Expression le = build_linear_expression(t_le_expr, where);
    Prolog_atom opt = term_to_optimization_mode(t_opt, where);
    Optimization_Mode mode = (opt == a_max) ? MAXIMIZATION : MINIMIZATION;

    MIP_Problem* mip
      = new MIP_Problem(term_to_unsigned<dimension_type>(t_nd, where),
                        cs, le, mode);
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_address(tmp, mip);
    if (Prolog_unify(t_mip, tmp)) {
      PPL_REGISTER(mip);
      return PROLOG_SUCCESS;
    }
    else
      delete mip;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_new_MIP_Problem_from_MIP_Problem(Prolog_term_ref t_mip_source,
                                     Prolog_term_ref t_mip) {
  static const char* where = "ppl_new_MIP_Problem_from_MIP_Problem/2";
  try {
    const MIP_Problem* mip_source
      = static_cast<const MIP_Problem*>
      (term_to_handle<MIP_Problem>(t_mip_source, where));
    PPL_CHECK(mip_source);
    MIP_Problem* mip = new MIP_Problem(*mip_source);
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_address(tmp, mip);
    if (Prolog_unify(t_mip, tmp)) {
      PPL_REGISTER(mip);
      return PROLOG_SUCCESS;
    }
    else
      delete mip;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_swap(Prolog_term_ref t_lhs, Prolog_term_ref t_rhs) {
  static const char* where = "ppl_MIP_Problem_swap/2";
  try {
    MIP_Problem* lhs = term_to_handle<MIP_Problem>(t_lhs, where);
    MIP_Problem* rhs = term_to_handle<MIP_Problem>(t_rhs, where);
    PPL_CHECK(lhs);
    PPL_CHECK(rhs);
    swap(*lhs, *rhs);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_delete_MIP_Problem(Prolog_term_ref t_mip) {
  static const char* where = "ppl_delete_MIP_Problem/1";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_UNREGISTER(mip);
    delete mip;
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_space_dimension(Prolog_term_ref t_mip, Prolog_term_ref t_sd) {
  static const char* where = "ppl_MIP_Problem_space_dimension/2";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    if (unify_ulong(t_sd, mip->space_dimension()))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_integer_space_dimensions(Prolog_term_ref t_mip,
                                         Prolog_term_ref t_vlist) {
  static const char* where = "ppl_MIP_Problem_integer_space_dimensions/2";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);

    Prolog_term_ref tail = Prolog_new_term_ref();
    Prolog_put_nil(tail);
    const Variables_Set& i_vars = mip->integer_space_dimensions();

    for (Variables_Set::const_iterator i = i_vars.begin(),
           i_end = i_vars.end(); i != i_end; ++i)
      Prolog_construct_cons(tail, variable_term(*i), tail);

    if (Prolog_unify(t_vlist, tail))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_constraints(Prolog_term_ref t_mip,
                            Prolog_term_ref t_clist) {
  static const char* where = "ppl_MIP_Problem_constraints/2";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);

    Prolog_term_ref tail = Prolog_new_term_ref();
    Prolog_put_nil(tail);
    for (MIP_Problem::const_iterator i = mip->constraints_begin(),
           i_end = mip->constraints_end(); i != i_end; ++i)
      Prolog_construct_cons(tail, constraint_term(*i), tail);

    if (Prolog_unify(t_clist, tail))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_objective_function(Prolog_term_ref t_mip,
                                   Prolog_term_ref t_le_expr) {
  static const char* where = "ppl_MIP_Problem_objective_function/2";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);

    const Linear_Expression& le = mip->objective_function();
    Prolog_term_ref t = get_linear_expression(le);

    if (Prolog_unify(t_le_expr, t))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_optimization_mode(Prolog_term_ref t_mip,
                                  Prolog_term_ref t_opt) {
  static const char* where = "ppl_MIP_Problem_optimization_mode/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);

    Optimization_Mode mode = mip->optimization_mode();
    Prolog_term_ref t = Prolog_new_term_ref();
    Prolog_atom a = (mode == MAXIMIZATION) ? a_max : a_min;
    Prolog_put_atom(t, a);
    if (Prolog_unify(t_opt, t))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_clear(Prolog_term_ref t_mip) {
  static const char* where = "ppl_MIP_Problem_clear/1";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    mip->clear();
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_add_space_dimensions_and_embed
(Prolog_term_ref t_mip, Prolog_term_ref t_nnd) {
  static const char* where
    = "ppl_MIP_Problem_add_space_dimensions_and_embed/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    dimension_type d = term_to_unsigned<dimension_type>(t_nnd, where);
    mip->add_space_dimensions_and_embed(d);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_add_to_integer_space_dimensions(Prolog_term_ref t_mip,
                                                Prolog_term_ref t_vlist) {
  static const char* where
    = "ppl_MIP_Problem_add_to_integer_space_dimensions/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    Variables_Set i_vars;
    Prolog_term_ref v = Prolog_new_term_ref();

    while (Prolog_is_cons(t_vlist)) {
      Prolog_get_cons(t_vlist, v, t_vlist);
      i_vars.insert(term_to_Variable(v, where).id());
    }

    // Check the list is properly terminated.
    check_nil_terminating(t_vlist, where);

    mip->add_to_integer_space_dimensions(i_vars);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_add_constraint(Prolog_term_ref t_mip, Prolog_term_ref t_c) {
  static const char* where = "ppl_MIP_Problem_add_constraint/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    mip->add_constraint(build_constraint(t_c, where));
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_add_constraints(Prolog_term_ref t_mip,
                                Prolog_term_ref t_clist) {
  static const char* where = "ppl_MIP_Problem_add_constraints/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    Constraint_System cs;
    Prolog_term_ref c = Prolog_new_term_ref();

    while (Prolog_is_cons(t_clist)) {
      Prolog_get_cons(t_clist, c, t_clist);
      cs.insert(build_constraint(c, where));
    }

    // Check the list is properly terminated.
    check_nil_terminating(t_clist, where);

    mip->add_constraints(cs);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_set_objective_function(Prolog_term_ref t_mip,
                                       Prolog_term_ref t_le_expr) {
  static const char* where = "ppl_MIP_Problem_set_objective_function/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    mip->set_objective_function(build_linear_expression(t_le_expr, where));
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_set_optimization_mode(Prolog_term_ref t_mip,
                                      Prolog_term_ref t_opt) {
  static const char* where = "ppl_MIP_Problem_set_optimization_mode/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);

    Prolog_atom opt = term_to_optimization_mode(t_opt, where);
    Optimization_Mode mode = (opt == a_max) ? MAXIMIZATION : MINIMIZATION;
    mip->set_optimization_mode(mode);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_set_control_parameter(Prolog_term_ref t_mip,
                                      Prolog_term_ref t_cp_value) {
  static const char* where = "ppl_MIP_Problem_set_control_parameter/2";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);

    Prolog_atom cp_value = term_to_control_parameter_value(t_cp_value, where);
    if (cp_value == a_pricing_steepest_edge_float)
      mip->set_control_parameter(MIP_Problem::PRICING_STEEPEST_EDGE_FLOAT);
    else if (cp_value == a_pricing_steepest_edge_exact)
      mip->set_control_parameter(MIP_Problem::PRICING_STEEPEST_EDGE_EXACT);
    else if (cp_value == a_pricing_textbook)
      mip->set_control_parameter(MIP_Problem::PRICING_TEXTBOOK);
    else
      throw unknown_interface_error("ppl_MIP_Problem_get_control_parameter()");
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_get_control_parameter(Prolog_term_ref t_mip,
                                      Prolog_term_ref t_cp_name,
                                      Prolog_term_ref t_cp_value) {
  static const char* where = "ppl_MIP_Problem_get_control_parameter/3";
  try {
    MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    Prolog_atom cp_name = term_to_control_parameter_name(t_cp_name, where);
    MIP_Problem::Control_Parameter_Value ppl_cp_value;
    if (cp_name == a_pricing)
      ppl_cp_value = mip->get_control_parameter(MIP_Problem::PRICING);
    else
      throw unknown_interface_error("ppl_MIP_Problem_get_control_parameter()");

    Prolog_term_ref t = Prolog_new_term_ref();
    Prolog_atom a;
    switch (ppl_cp_value) {
    case MIP_Problem::PRICING_STEEPEST_EDGE_FLOAT:
      a = a_pricing_steepest_edge_float;
      break;
    case MIP_Problem::PRICING_STEEPEST_EDGE_EXACT:
      a = a_pricing_steepest_edge_exact;
      break;
    case MIP_Problem::PRICING_TEXTBOOK:
      a = a_pricing_textbook;
      break;
    default:
      throw unknown_interface_error("ppl_MIP_Problem_get_control_parameter()");
    }
    Prolog_put_atom(t, a);
    if (Prolog_unify(t_cp_value, t))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_is_satisfiable(Prolog_term_ref t_mip) {
  static const char* where = "ppl_MIP_Problem_is_satisfiable/1";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    if (mip->is_satisfiable())
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_solve(Prolog_term_ref t_mip, Prolog_term_ref t_status) {
  static const char* where = "ppl_MIP_Problem_solve/2";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);

    Prolog_atom a;
    switch (mip->solve()) {
    case UNFEASIBLE_MIP_PROBLEM:
      a = a_unfeasible;
      break;
    case UNBOUNDED_MIP_PROBLEM:
      a = a_unbounded;
      break;
    case OPTIMIZED_MIP_PROBLEM:
      a = a_optimized;
      break;
    default:
      throw unknown_interface_error("ppl_MIP_Problem_solve()");
    }
    Prolog_term_ref t = Prolog_new_term_ref();
    Prolog_put_atom(t, a);
    if (Prolog_unify(t_status, t))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_feasible_point(Prolog_term_ref t_mip,
                               Prolog_term_ref t_g) {
  static const char* where = "ppl_MIP_Problem_feasible_point/2";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    const Generator& g = mip->feasible_point();
    if (Prolog_unify(t_g, generator_term(g)))
      return PROLOG_SUCCESS;
 }
 CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_optimizing_point(Prolog_term_ref t_mip,
                                 Prolog_term_ref t_g) {
  static const char* where = "ppl_MIP_Problem_optimizing_point/2";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    const Generator& g = mip->optimizing_point();
    if (Prolog_unify(t_g, generator_term(g)))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_optimal_value(Prolog_term_ref t_mip,
                              Prolog_term_ref t_n,
                              Prolog_term_ref t_d) {
  static const char* where = "ppl_MIP_Problem_optimal_value/3";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    PPL_DIRTY_TEMP_COEFFICIENT(n);
    PPL_DIRTY_TEMP_COEFFICIENT(d);
    mip->optimal_value(n, d);
    if (Prolog_unify_Coefficient(t_n, n)
        && Prolog_unify_Coefficient(t_d, d))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_evaluate_objective_function(Prolog_term_ref t_mip,
                                            Prolog_term_ref t_g,
                                            Prolog_term_ref t_n,
                                            Prolog_term_ref t_d) {
  static const char* where = "ppl_MIP_Problem_evaluate_objective_function/4";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    PPL_DIRTY_TEMP_COEFFICIENT(n);
    PPL_DIRTY_TEMP_COEFFICIENT(d);
    mip->evaluate_objective_function(build_generator(t_g, where), n, d);
    if (Prolog_unify_Coefficient(t_n, n)
        && Prolog_unify_Coefficient(t_d, d))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_OK(Prolog_term_ref t_mip) {
  static const char* where = "ppl_MIP_Problem_OK/1";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    if (mip->OK())
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_MIP_Problem_ascii_dump(Prolog_term_ref t_mip) {
  static const char* where = "ppl_MIP_Problem_ascii_dump/1";
  try {
    const MIP_Problem* mip = term_to_handle<MIP_Problem>(t_mip, where);
    PPL_CHECK(mip);
    mip->ascii_dump(std::cout);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_new_PIP_Problem_from_space_dimension
(Prolog_term_ref t_nd, Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Problem_from_space_dimension/2";
  try {
    dimension_type d = term_to_unsigned<dimension_type>(t_nd, where);
    PIP_Problem* pip = new PIP_Problem(d);
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_address(tmp, pip);
    if (Prolog_unify(t_pip, tmp)) {
      PPL_REGISTER(pip);
      return PROLOG_SUCCESS;
    }
    else
      delete pip;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_new_PIP_Problem(Prolog_term_ref t_dim,
                    Prolog_term_ref t_cs,
                    Prolog_term_ref t_params,
                    Prolog_term_ref t_pip) {
  static const char* where = "ppl_new_PIP_Problem/4";
  try {
    dimension_type dim = term_to_unsigned<dimension_type>(t_dim, where);
    Constraint_System cs;
    Prolog_term_ref t_c = Prolog_new_term_ref();
    while (Prolog_is_cons(t_cs)) {
      Prolog_get_cons(t_cs, t_c, t_cs);
      cs.insert(build_constraint(t_c, where));
    }
    // Check the list is properly terminated.
    check_nil_terminating(t_cs, where);

    Variables_Set params;
    Prolog_term_ref t_par = Prolog_new_term_ref();
    while (Prolog_is_cons(t_params)) {
      Prolog_get_cons(t_params, t_par, t_params);
      params.insert(term_to_Variable(t_par, where).id());
    }
    // Check the list is properly terminated.
    check_nil_terminating(t_params, where);

    PIP_Problem* pip = new PIP_Problem(dim, cs.begin(), cs.end(), params);
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_address(tmp, pip);
    if (Prolog_unify(t_pip, tmp)) {
      PPL_REGISTER(pip);
      return PROLOG_SUCCESS;
    }
    else
      delete pip;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_new_PIP_Problem_from_PIP_Problem(Prolog_term_ref t_pip_source,
                                     Prolog_term_ref t_pip) {
  static const char* where = "ppl_new_PIP_Problem_from_PIP_Problem/2";
  try {
    const PIP_Problem* pip_source
      = static_cast<const PIP_Problem*>
      (term_to_handle<PIP_Problem>(t_pip_source, where));
    PPL_CHECK(pip_source);
    PIP_Problem* pip = new PIP_Problem(*pip_source);
    Prolog_term_ref tmp = Prolog_new_term_ref();
    Prolog_put_address(tmp, pip);
    if (Prolog_unify(t_pip, tmp)) {
      PPL_REGISTER(pip);
      return PROLOG_SUCCESS;
    }
    else
      delete pip;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_swap(Prolog_term_ref t_lhs, Prolog_term_ref t_rhs) {
  static const char* where = "ppl_PIP_Problem_swap/2";
  try {
    PIP_Problem* lhs = term_to_handle<PIP_Problem>(t_lhs, where);
    PIP_Problem* rhs = term_to_handle<PIP_Problem>(t_rhs, where);
    PPL_CHECK(lhs);
    PPL_CHECK(rhs);
    swap(*lhs, *rhs);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_delete_PIP_Problem(Prolog_term_ref t_pip) {
  static const char* where = "ppl_delete_PIP_Problem/1";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_UNREGISTER(pip);
    delete pip;
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_space_dimension(Prolog_term_ref t_pip, Prolog_term_ref t_sd) {
  static const char* where = "ppl_PIP_Problem_space_dimension/2";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    if (unify_ulong(t_sd, pip->space_dimension()))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_parameter_space_dimensions(Prolog_term_ref t_pip,
                                           Prolog_term_ref t_vlist) {
  static const char* where = "ppl_PIP_Problem_parameter_space_dimensions/2";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);

    Prolog_term_ref tail = Prolog_new_term_ref();
    Prolog_put_nil(tail);
    const Variables_Set& params = pip->parameter_space_dimensions();

    for (Variables_Set::const_iterator i = params.begin(),
           i_end = params.end(); i != i_end; ++i)
      Prolog_construct_cons(tail, variable_term(*i), tail);

    if (Prolog_unify(t_vlist, tail))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_constraints(Prolog_term_ref t_pip,
                            Prolog_term_ref t_clist) {
  static const char* where = "ppl_PIP_Problem_constraints/2";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);

    Prolog_term_ref tail = Prolog_new_term_ref();
    Prolog_put_nil(tail);
    for (PIP_Problem::const_iterator i = pip->constraints_begin(),
           i_end = pip->constraints_end(); i != i_end; ++i)
      Prolog_construct_cons(tail, constraint_term(*i), tail);

    if (Prolog_unify(t_clist, tail))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_clear(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Problem_clear/1";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    pip->clear();
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_add_space_dimensions_and_embed
(Prolog_term_ref t_pip,
 Prolog_term_ref t_num_vars,
 Prolog_term_ref t_num_params) {
  static const char* where
    = "ppl_PIP_Problem_add_space_dimensions_and_embed/3";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    dimension_type nv = term_to_unsigned<dimension_type>(t_num_vars, where);
    dimension_type np = term_to_unsigned<dimension_type>(t_num_params, where);
    pip->add_space_dimensions_and_embed(nv, np);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_add_to_parameter_space_dimensions(Prolog_term_ref t_pip,
                                                  Prolog_term_ref t_vlist) {
  static const char* where
    = "ppl_PIP_Problem_add_to_parameter_space_dimensions/2";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    Variables_Set params;
    Prolog_term_ref v = Prolog_new_term_ref();

    while (Prolog_is_cons(t_vlist)) {
      Prolog_get_cons(t_vlist, v, t_vlist);
      params.insert(term_to_Variable(v, where).id());
    }

    // Check the list is properly terminated.
    check_nil_terminating(t_vlist, where);

    pip->add_to_parameter_space_dimensions(params);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_add_constraint(Prolog_term_ref t_pip, Prolog_term_ref t_c) {
  static const char* where = "ppl_PIP_Problem_add_constraint/2";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    pip->add_constraint(build_constraint(t_c, where));
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_add_constraints(Prolog_term_ref t_pip,
                                Prolog_term_ref t_clist) {
  static const char* where = "ppl_PIP_Problem_add_constraints/2";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    Constraint_System cs;
    Prolog_term_ref c = Prolog_new_term_ref();

    while (Prolog_is_cons(t_clist)) {
      Prolog_get_cons(t_clist, c, t_clist);
      cs.insert(build_constraint(c, where));
    }

    // Check the list is properly terminated.
    check_nil_terminating(t_clist, where);

    pip->add_constraints(cs);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_get_control_parameter(Prolog_term_ref t_pip,
                                      Prolog_term_ref t_cp_name,
                                      Prolog_term_ref t_cp_value) {
  static const char* where = "ppl_PIP_Problem_get_control_parameter/3";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    Prolog_atom cp_name = term_to_pip_problem_control_parameter_name(t_cp_name, where);
    PIP_Problem::Control_Parameter_Value ppl_cp_value;
    Prolog_atom a;
    if (cp_name == a_cutting_strategy) {
      ppl_cp_value
        = pip->get_control_parameter(PIP_Problem::CUTTING_STRATEGY);
      switch (ppl_cp_value) {
      case PIP_Problem::CUTTING_STRATEGY_FIRST:
        a = a_cutting_strategy_first;
        break;
      case PIP_Problem::CUTTING_STRATEGY_DEEPEST:
        a = a_cutting_strategy_deepest;
        break;
      case PIP_Problem::CUTTING_STRATEGY_ALL:
        a = a_cutting_strategy_all;
        break;
      default:
        throw unknown_interface_error(
          "ppl_PIP_Problem_get_control_parameter()");
      }
    }
    else if (cp_name == a_pivot_row_strategy) {
      ppl_cp_value
        = pip->get_control_parameter(PIP_Problem::PIVOT_ROW_STRATEGY);
      switch (ppl_cp_value) {
      case PIP_Problem::PIVOT_ROW_STRATEGY_FIRST:
        a = a_pivot_row_strategy_first;
        break;
      case PIP_Problem::PIVOT_ROW_STRATEGY_MAX_COLUMN:
        a = a_pivot_row_strategy_max_column;
        break;
      default:
        throw unknown_interface_error(
          "ppl_PIP_Problem_get_control_parameter()");
      }
    }
    else
      throw unknown_interface_error("ppl_PIP_Problem_get_control_parameter()");

    Prolog_term_ref t = Prolog_new_term_ref();
    Prolog_put_atom(t, a);
    if (Prolog_unify(t_cp_value, t))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_set_control_parameter(Prolog_term_ref t_pip,
                                      Prolog_term_ref t_cp_value) {
  static const char* where = "ppl_PIP_Problem_set_control_parameter/2";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);

    Prolog_atom cp_value = term_to_pip_problem_control_parameter_value(t_cp_value, where);
    if (cp_value == a_cutting_strategy_first)
      pip->set_control_parameter(PIP_Problem::CUTTING_STRATEGY_FIRST);
    else if (cp_value == a_cutting_strategy_deepest)
      pip->set_control_parameter(PIP_Problem::CUTTING_STRATEGY_DEEPEST);
    else if (cp_value == a_cutting_strategy_all)
      pip->set_control_parameter(PIP_Problem::CUTTING_STRATEGY_ALL);
    else if (cp_value == a_pivot_row_strategy_first)
      pip->set_control_parameter(PIP_Problem::PIVOT_ROW_STRATEGY_FIRST);
    else if (cp_value == a_pivot_row_strategy_max_column)
      pip->set_control_parameter(PIP_Problem::PIVOT_ROW_STRATEGY_MAX_COLUMN);
    else
      throw unknown_interface_error("ppl_PIP_Problem_set_control_parameter()");
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_has_big_parameter_dimension(Prolog_term_ref t_pip,
                                            Prolog_term_ref t_d) {
  static const char* where = "ppl_PIP_Problem_get_big_parameter_dimension/2";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    dimension_type dim = pip->get_big_parameter_dimension();
    if (dim == not_a_dimension())
      return PROLOG_FAILURE;
    if (unify_ulong(t_d, dim))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_set_big_parameter_dimension(Prolog_term_ref t_pip,
                                            Prolog_term_ref t_d) {
  static const char* where = "ppl_MIP_Problem_set_big_parameter_dimension/2";
  try {
    PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    dimension_type d = term_to_unsigned<dimension_type>(t_d, where);
    pip->set_big_parameter_dimension(d);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_is_satisfiable(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Problem_is_satisfiable/1";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    if (pip->is_satisfiable())
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_solve(Prolog_term_ref t_pip, Prolog_term_ref t_status) {
  static const char* where = "ppl_PIP_Problem_solve/2";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);

    Prolog_atom a;
    switch (pip->solve()) {
    case UNFEASIBLE_PIP_PROBLEM:
      a = a_unfeasible;
      break;
    case OPTIMIZED_PIP_PROBLEM:
      a = a_optimized;
      break;
    default:
      throw unknown_interface_error("ppl_PIP_Problem_solve()");
    }
    Prolog_term_ref t = Prolog_new_term_ref();
    Prolog_put_atom(t, a);
    if (Prolog_unify(t_status, t))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_solution(Prolog_term_ref t_pip,
                         Prolog_term_ref t_pip_tree) {
  static const char* where = "ppl_PIP_Problem_solution/2";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    PIP_Tree_Node* sol = const_cast<PIP_Tree_Node*>(pip->solution());
    Prolog_term_ref t_sol = Prolog_new_term_ref();
    Prolog_put_address(t_sol, sol);
    if (Prolog_unify(t_pip_tree, t_sol)) {
      PPL_WEAK_REGISTER(sol);
      return PROLOG_SUCCESS;
    }
 }
 CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_optimizing_solution(Prolog_term_ref t_pip,
                                    Prolog_term_ref t_pip_tree) {
  static const char* where = "ppl_PIP_Problem_optimizing_solution/2";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    PIP_Tree_Node* sol = const_cast<PIP_Tree_Node*>(pip->optimizing_solution());
    Prolog_term_ref t_sol = Prolog_new_term_ref();
    Prolog_put_address(t_sol, sol);
    if (Prolog_unify(t_pip_tree, t_sol)) {
      PPL_WEAK_REGISTER(sol);
      return PROLOG_SUCCESS;
    }
 }
 CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_OK(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Problem_OK/1";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    if (pip->OK())
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Problem_ascii_dump(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Problem_ascii_dump/1";
  try {
    const PIP_Problem* pip = term_to_handle<PIP_Problem>(t_pip, where);
    PPL_CHECK(pip);
    pip->ascii_dump(std::cout);
    return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_constraints(Prolog_term_ref t_pip,
                              Prolog_term_ref t_cs) {
  static const char* where = "ppl_PIP_Tree_Node_constraints/2";
  try {
    const PIP_Tree_Node* pip = term_to_handle<PIP_Tree_Node>(t_pip, where);
    PPL_CHECK(pip);

    Prolog_term_ref tail = Prolog_new_term_ref();
    Prolog_put_nil(tail);
    const Constraint_System& ppl_cs = pip->constraints();
    for (Constraint_System::const_iterator i = ppl_cs.begin(),
           ppl_cs_end = ppl_cs.end(); i != ppl_cs_end; ++i)
      Prolog_construct_cons(tail, constraint_term(*i), tail);

    if (Prolog_unify(t_cs, tail)) {
      return PROLOG_SUCCESS;
    }
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_is_solution(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Tree_Node_as_solution/2";
  try {
    const PIP_Tree_Node* pip = term_to_handle<PIP_Tree_Node>(t_pip, where);
    PPL_CHECK(pip);

    if (pip != 0 && pip->as_solution() != 0)
      return PROLOG_SUCCESS;
    return PROLOG_FAILURE;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_is_decision(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Tree_Node_as_decision/2";
  try {
    const PIP_Tree_Node* pip = term_to_handle<PIP_Tree_Node>(t_pip, where);
    PPL_CHECK(pip);

    if (pip != 0 && pip->as_decision() != 0)
      return PROLOG_SUCCESS;
    return PROLOG_FAILURE;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_is_bottom(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Tree_Node_as_decision/2";
  try {
    const PIP_Tree_Node* pip = term_to_handle<PIP_Tree_Node>(t_pip, where);
    PPL_CHECK(pip);

    if (pip == 0)
      return PROLOG_SUCCESS;
    return PROLOG_FAILURE;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_artificials(Prolog_term_ref t_tree_node,
                              Prolog_term_ref t_artlist) {
  static const char* where = "ppl_PIP_Tree_Node_artificials/2";
  try {
    const PIP_Tree_Node* node
      = term_to_handle<PIP_Tree_Node>(t_tree_node, where);
    PPL_CHECK(node);

    Prolog_term_ref tail = Prolog_new_term_ref();
    Prolog_put_nil(tail);
    for (PIP_Tree_Node::Artificial_Parameter_Sequence::const_iterator
           i = node->art_parameter_begin(),
           arts_end = node->art_parameter_end(); i != arts_end; ++i)
      Prolog_construct_cons(tail, artificial_parameter_term(*i), tail);

    if (Prolog_unify(t_artlist, tail))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_parametric_values(Prolog_term_ref t_pip,
                                            Prolog_term_ref t_var,
                                            Prolog_term_ref t_le) {
  static const char* where = "ppl_PIP_Solution_Node_get_parametric_values/3";
  try {
    const PIP_Solution_Node* pip
      = term_to_handle<PIP_Solution_Node>(t_pip, where);
    PPL_CHECK(pip);
    Variable var = term_to_Variable(t_var, where);
    if (Prolog_unify(t_le, get_linear_expression(pip->parametric_values(var))))
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_true_child(Prolog_term_ref t_pip,
                                     Prolog_term_ref t_ptree) {
  static const char* where = "ppl_PIP_Decision_Node_get_true_child/2";
  try {
    const PIP_Decision_Node* pip
      = term_to_handle<PIP_Decision_Node>(t_pip, where);
    PPL_CHECK(pip);
    bool b = true;
    PIP_Tree_Node* ppl_ptree = const_cast<PIP_Tree_Node*>(pip->child_node(b));
    Prolog_term_ref t_ppl_ptree = Prolog_new_term_ref();
    Prolog_put_address(t_ppl_ptree, ppl_ptree);
    if (Prolog_unify(t_ptree, t_ppl_ptree)) {
      PPL_WEAK_REGISTER(ppl_ptree);
      return PROLOG_SUCCESS;
    }
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_false_child(Prolog_term_ref t_pip,
                                      Prolog_term_ref t_ptree) {
  static const char* where = "ppl_PIP_Decision_Node_get_false_child/2";
  try {
    const PIP_Decision_Node* pip
      = term_to_handle<PIP_Decision_Node>(t_pip, where);
    PPL_CHECK(pip);
    bool b = false;
    PIP_Tree_Node* ppl_ptree = const_cast<PIP_Tree_Node*>(pip->child_node(b));
    Prolog_term_ref t_ppl_ptree = Prolog_new_term_ref();
    Prolog_put_address(t_ppl_ptree, ppl_ptree);
    if (Prolog_unify(t_ptree, t_ppl_ptree)) {
      PPL_WEAK_REGISTER(ppl_ptree);
      return PROLOG_SUCCESS;
    }
  }
  CATCH_ALL;
}

extern "C" Prolog_foreign_return_type
ppl_PIP_Tree_Node_OK(Prolog_term_ref t_pip) {
  static const char* where = "ppl_PIP_Tree_Node_OK/1";
  try {
    const PIP_Tree_Node* pip = term_to_handle<PIP_Tree_Node>(t_pip, where);
    PPL_CHECK(pip);
    if (pip->OK())
      return PROLOG_SUCCESS;
  }
  CATCH_ALL;
}