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
//! Prolog contexts.
//!
//! As you interact with SWI-Prolog, the underlying prolog engine
//! moves into different states, where different things are
//! allowed. We keep track of this underlying state through Context
//! objects.
//!
//! Currently, there's four kind of states that we keep track of:
//! - ActivatedEngine - this is the state an engine will be in when we
//!   just created it. If you're directly working with engines, this
//!   will be your initial state.
//! - Unmanaged - this is the state an engine will be in when prolog
//!   is calling into the rust library, for example, when a foreign
//!   predicate implemented in rust is being called.
//! - Framed - In any context, you can create a prolog frame. A prolog
//!   frame allows you to rewind the state of all prolog terms to their
//!   state at the time of frame creation.
//! - OpenCall - While calling into prolog, this is the context you'll
//!   be in as you're walking through the solutions. This is a special
//!   context where a lot of the normal features are disabled.
//!
//! Contexts are either active or inactive. A context starts out as
//! active, but as soon as you do something that creates a new context
//! (create a frame, open a query), the context will become
//! inactive. Once the created context is dropped, the original
//! context will become active again.
//!
//! With the exception of the OpenCall context, all contexts let you
//! create new term refs, which are handles to data on the prolog
//! stack. These term refs can only be created while the context is
//! active. However, they can be manipulated as long as the context
//! that created them exists. As soon as the context is dropped
//! though, the Term will become invalid and trying to do anything
//! with it will result in a compile error.
//!
//! The OpenCall context is special in that no new terms are allowed
//! to be created, nor are you allowed to open another query. It is
//! however possible to create a new frame in this context, which
//! would once again put you in a state where these things are
//! possible. Of course, you'll have to drop this frame before you're
//! able to manipulate the OpenCall context again (such as retrieving
//! the next solution from the query).
//!
//! Various operations may cause the underlying engine to go into an
//! exceptional state. This is signaled by these operations returning
//! an `Err(PrologError::Exception)`. This means that a special
//! exception term has been set. Most context operations are
//! impossible while in this state, and attempting to perform them
//! will result in a panic. Your options are either to return back
//! into prolog (if you're implementing a foreign predicate), which
//! will then raise this exception in prolog, or to clear the
//! exception.
#[cfg(feature = "serde")]
use crate::term::ser::SerializerConfiguration;

use super::atom::*;
use super::callable::*;
use super::engine::*;
use super::fli::*;
use super::module::*;
use super::result::*;
use super::stream::*;
use super::term::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::cell::Cell;
use std::convert::TryInto;
use std::mem::MaybeUninit;

use swipl_macros::pred;

use swipl_macros::{prolog, term};

pub(crate) unsafe fn with_cleared_exception<R>(f: impl FnOnce() -> R) -> R {
    let error_term_ref = pl_default_exception();
    if error_term_ref != 0 {
        let backup_term_ref = PL_new_term_ref();
        assert!(PL_unify(backup_term_ref, error_term_ref) != 0);
        PL_clear_exception();
        let result = f();
        PL_raise_exception(backup_term_ref);
        PL_reset_term_refs(backup_term_ref);

        result
    } else {
        f()
    }
}

/// A term wrapper for the special exception term.
///
/// The exception term lives in a special place on the prolog stack
/// where frame rewinds have no effect.
pub struct ExceptionTerm<'a>(Term<'a>);

impl<'a> ExceptionTerm<'a> {
    /// Clear the exception, so that the engine is no longer in an
    /// exceptional state.
    pub fn clear_exception(self) {
        self.assert_term_handling_possible();

        unsafe { PL_clear_exception() }
    }

    /// Call the given function with a copy of the exception term in a context where the exception has been cleared.
    ///
    /// This function is marked unsafe because it is not safe to use
    /// the original ExceptionTerm from within the given function, but
    /// we still have a handle to it through self. The caller will
    /// have to ensure that the function that is passed in will not
    /// use this exception term.
    unsafe fn with_cleared_exception<'b, C: ContextType, R>(
        &'b self,
        ctx: &'b Context<C>,
        f: impl FnOnce(&Term) -> R,
    ) -> R {
        ctx.assert_activated();
        let backup_term_ref = PL_new_term_ref();
        assert!(PL_unify(backup_term_ref, self.0.term_ptr()) != 0);
        let backup_term = Term::new(backup_term_ref, ctx.as_term_origin());
        PL_clear_exception();

        // should we handle panics?
        let result = f(&backup_term);

        PL_raise_exception(backup_term_ref);

        backup_term.reset();

        result
    }
}

impl<'a> std::ops::Deref for ExceptionTerm<'a> {
    type Target = Term<'a>;
    fn deref(&self) -> &Term<'a> {
        &self.0
    }
}

/// A context that the underlying prolog engine is in.
///
/// See the module documentation for an explanation of this type.
pub struct Context<'a, T: ContextType> {
    parent: Option<&'a dyn ContextParent>,
    pub context: T,
    engine: PL_engine_t,
    activated: Cell<bool>,
    exception_handling: Cell<bool>,
}

impl<'a, T: ContextType> Context<'a, T> {
    unsafe fn new_activated_without_parent(context: T, engine: PL_engine_t) -> Self {
        Context {
            parent: None,
            context,
            engine,
            activated: Cell::new(true),
            exception_handling: Cell::new(false),
        }
    }

    pub(crate) unsafe fn new_activated<'b, T2: ContextType>(
        parent: &'a Context<'b, T2>,
        context: T,
        engine: PL_engine_t,
    ) -> Self {
        Context {
            parent: Some(parent as &dyn ContextParent),
            context,
            engine,
            activated: Cell::new(true),
            exception_handling: Cell::new(false),
        }
    }

    pub(crate) unsafe fn deactivate(&self) {
        self.activated.set(false)
    }

    /// Panics if this context is not active.
    pub fn assert_activated(&self) {
        if !self.activated.get() {
            panic!("tried to use inactive context");
        }
    }

    /// Panics if the engine is in an exceptional state.
    pub fn assert_no_exception(&self) {
        if self.has_exception() {
            panic!("tried to use context which has raised an exception");
        }
    }

    /// Returns the underlying engine pointer.
    pub fn engine_ptr(&self) -> PL_engine_t {
        self.engine
    }

    /// Return the engine pointer as a `TermOrigin`, which is used in the construction of a `Term` in unsafe code.
    pub(crate) fn as_term_origin(&self) -> TermOrigin {
        unsafe { TermOrigin::new(self.engine_ptr()) }
    }

    /// Wrap the given term_t into a Term with a lifetime corresponding to this context.
    ///
    /// # Safety
    /// This is unsafe because there's no way of checking that the
    /// given term_t is indeed from this context. The caller will have
    /// to ensure that the term lives at least as long as this
    /// context.
    pub unsafe fn wrap_term_ref(&self, term: term_t) -> Term {
        self.assert_activated();
        Term::new(term, self.as_term_origin())
    }

    /// Returns true if the underlying engine is in an exceptional state.
    pub fn has_exception(&self) -> bool {
        self.assert_activated();

        unsafe { pl_default_exception() != 0 }
    }

    /// Clear the current exception if there is any.
    pub fn clear_exception(&self) {
        self.with_uncleared_exception(|e| match e {
            None => (),
            Some(e) => e.clear_exception(),
        })
    }

    /// Call the given function with the exception term, if it exists.
    ///
    /// The given function is able to clear the exception term, but
    /// not much else is allowed from safe code. Any attempt to do a
    /// get, put or unify with the given term will result in a panic.
    pub fn with_uncleared_exception<'b, R>(
        &'b self,
        f: impl FnOnce(Option<ExceptionTerm<'b>>) -> R,
    ) -> R {
        self.assert_activated();
        if self.exception_handling.replace(true) {
            panic!("re-entered exception handler");
        }

        let exception = unsafe { pl_default_exception() };
        let arg = match exception == 0 {
            true => None,
            false => {
                let term = unsafe { self.wrap_term_ref(exception) };
                Some(ExceptionTerm(term))
            }
        };

        // TODO should we take panics into account when clearing exception handling status?
        let result = f(arg);

        self.exception_handling.set(false);

        result
    }

    /// Call the given function with a copy of the exception term, from a context where the exception state has temporarily been cleared.
    ///
    /// This allows analysis on the exception term using all the
    /// normal safe functions for doing so. When the function returns,
    /// the engine will go back into an exceptional state with the
    /// original exception term.
    pub fn with_exception<R>(&self, f: impl FnOnce(Option<&Term>) -> R) -> R {
        self.with_uncleared_exception(|e| match e {
            None => f(None),
            Some(e) => unsafe { e.with_cleared_exception(self, |e| f(Some(e))) },
        })
    }

    /// Put the engine in an exceptional state.
    ///
    /// The given term will be copied and put into the exception
    /// term. This function always returns
    /// `Err(PrologError::Exception)`.
    pub fn raise_exception<R>(&self, term: &Term) -> PrologResult<R> {
        self.assert_activated();
        if term.is_var() {
            panic!("tried to raise a var as an exception");
        } else {
            unsafe {
                PL_raise_exception(term.term_ptr());
            }
        }

        Err(PrologError::Exception)
    }

    /// Retrieve the current output stream.
    pub fn current_output<'b>(&self) -> WritablePrologStream<'b> {
        assert_some_engine_is_active();
        unsafe {
            let current_output = *_PL_streams().offset(4);

            WritablePrologStream::new(current_output)
        }
    }

    /// Handle any outstanding synchronous signals, including user interrupts.
    ///
    /// This could return PrologError::Exception, which should
    /// normally be propagated all the way back to SWI-Prolog. In
    /// particular, the raised exception will be a term containing the
    /// atom '$aborted' for the case where a user interrupts.
    ///
    /// This should be regularly called for long-running foreign code.
    /// SWI-Prolog handles signals synchronously at safe points, and
    /// therefore will never do so while a foreign predicate is
    /// running. The most visible outcome of this is that long-running
    /// predicates can normally not be interrupted by the user when
    /// they press ctrl-c once.
    ///
    /// On success, the number of outstanding signals handled is
    /// returned.
    pub fn handle_signals(&self) -> PrologResult<u32> {
        let result: i32 = unsafe { PL_handle_signals() };

        if result == -1 {
            Err(PrologError::Exception)
        } else {
            Ok(result as u32)
        }
    }
}

trait ContextParent {
    fn reactivate(&self);
}

impl<'a, T: ContextType> ContextParent for Context<'a, T> {
    fn reactivate(&self) {
        if self.activated.replace(true) {
            panic!("context already active");
        }
    }
}

impl<'a, T: ContextType> Drop for Context<'a, T> {
    fn drop(&mut self) {
        if let Some(parent) = self.parent {
            parent.reactivate();
        }
    }
}

/// A type of context.
///
/// This is the object that is wrapped by [Context]. Implementors can
/// use this to hold context-specific information. Any functions are
/// to be implemented on `Context<YourContextType>`.
///
/// # Safety
/// An active context ensures that callers are allowed to interact
/// with SWI-Prolog environments. Therefore, a bunch of assertions are
/// not performed. If an implementor to ensure that SWI-Prolog
/// environment is active, then this might lead to undefined behavior.
pub unsafe trait ContextType {}

/// Context type for an active engine. This wraps an `EngineActivation`.
///
/// Example:
/// ```
/// # use swipl::prelude::*;
/// let engine = Engine::new();
/// let activation = engine.activate();

/// let context: Context<ActivatedEngine> = activation.into();
/// // Note: Context<_> would also work as a type annotation
/// ```
pub struct ActivatedEngine<'a> {
    _activation: EngineActivation<'a>,
}

impl<'a> From<EngineActivation<'a>> for Context<'a, ActivatedEngine<'a>> {
    fn from(activation: EngineActivation<'a>) -> Context<'a, ActivatedEngine<'a>> {
        let engine = activation.engine_ptr();
        let context = ActivatedEngine {
            _activation: activation,
        };

        unsafe { Context::new_activated_without_parent(context, engine) }
    }
}

unsafe impl<'a> ContextType for ActivatedEngine<'a> {}

/// Context type for an unmanaged engine.
///
/// See [unmanaged_engine_context] for usage.
pub struct Unmanaged {
    // only here to prevent automatic construction
    _x: (),
}
unsafe impl ContextType for Unmanaged {}

/// Create an unmanaged context for situations where the thread has an engine that rust doesn't know about.
///
/// Example:
/// ```
/// # use swipl::prelude::*;
/// # initialize_swipl_noengine();
/// # unsafe { swipl::fli::PL_thread_attach_engine(std::ptr::null_mut()); }
/// let context = unsafe { unmanaged_engine_context() };
/// # unsafe { swipl::fli::PL_thread_destroy_engine(); }
/// ```
///
/// # Safety
/// This is unsafe to call if we are not in a swipl environment, or if
/// some other context is active. Furthermore, the lifetime will most
/// definitely be wrong. This should be used by code that doesn't
/// promiscuously spread this context. all further accesses should be
/// through borrows.
pub unsafe fn unmanaged_engine_context() -> Context<'static, Unmanaged> {
    let current = current_engine_ptr();

    if current.is_null() {
        panic!("tried to create an unmanaged engine context, but no engine is active");
    }

    Context::new_activated_without_parent(Unmanaged { _x: () }, current)
}

enum FrameState {
    Active,
    Closed,
}

/// Context type for a prolog frame.
///
/// # Examples
/// Discard a frame through dropping:
/// ```
/// use swipl::prelude::*;
/// fn main() -> PrologResult<()> {
///    // create a context
///    let engine = Engine::new();
///    let activation = engine.activate();
///    let context: Context<_> = activation.into();
///
///    let term = context.new_term_ref();
///
///    {
///        let frame = context.open_frame();
///        term.unify(42_u64)?;
///    }
///
///    assert!(term.is_var());
///
///    Ok(())
/// }
/// ```
///
/// Discard a frame explicitely:
/// ```
/// use swipl::prelude::*;
/// fn main() -> PrologResult<()> {
///    // create a context
///    let engine = Engine::new();
///    let activation = engine.activate();
///    let context: Context<_> = activation.into();
///
///    let term = context.new_term_ref();
///
///    let frame = context.open_frame();
///    term.unify(42_u64)?;
///
///    frame.discard();
///    assert!(term.is_var());
///
///    Ok(())
/// }
/// ```
///
/// Close a frame:
/// ```
/// use swipl::prelude::*;
/// fn main() -> PrologResult<()> {
///    // create a context
///    let engine = Engine::new();
///    let activation = engine.activate();
///    let context: Context<_> = activation.into();
///
///    let term = context.new_term_ref();
///
///    let frame = context.open_frame();
///    term.unify(42_u64)?;
///    let term2 = frame.new_term_ref();
///
///    frame.close();
///    assert_eq!(42_u64, term.get()?);
///    // the following would result in a compile error:
///    // term2.unify(42_u64)?;
///
///    Ok(())
/// }
/// ```
///
/// Rewind a frame:
/// ```
/// use swipl::prelude::*;
/// fn main() -> PrologResult<()> {
///    // create a context
///    let engine = Engine::new();
///    let activation = engine.activate();
///    let context: Context<_> = activation.into();
///
///    let term = context.new_term_ref();
///
///    let frame = context.open_frame();
///    term.unify(42_u64)?;
///
///    let frame = frame.rewind();
///    // term is a variable again so the following unification will succeed
///    term.unify(43_u64)?;
///
///    frame.close();
///    assert_eq!(43_u64, term.get()?);
///
///    Ok(())
/// }
/// ```
///
pub struct Frame {
    fid: PL_fid_t,
    state: FrameState,
}

unsafe impl ContextType for Frame {}

impl Drop for Frame {
    fn drop(&mut self) {
        if let FrameState::Active = self.state {
            // unsafe justification: all instantiations of Frame happen in
            // this module.  This module only instantiates the frame as
            // part of the context mechanism. No 'free' Frames are ever
            // returned.  This mechanism ensures that the frame is only
            // discarded if there's no inner frame still
            // remaining. It'll also ensure that the engine of the
            // frame is active while dropping.
            unsafe {
                PL_discard_foreign_frame(self.fid);
            }
        }
    }
}

impl<'a> Context<'a, Frame> {
    /// Close the frame.
    ///
    /// After closing, any terms created in the context of this frame
    /// will no longer be usable. Any data created and put in terms
    /// that are still in scope will be retained.
    pub fn close(mut self) {
        self.context.state = FrameState::Closed;
        // unsafe justification: reasons for safety are the same as in a normal drop. Also, since we just set framestate to discarded, the drop won't try to subsequently close this same frame.
        unsafe { PL_close_foreign_frame(self.context.fid) };
    }

    /// Discard the frame.
    ///
    /// This will destroy the frame. Any terms created in the context
    /// of this frame will no longer be usable. Furthermore, any term
    /// manipulation that happened since opening this frame will be
    /// undone. This is equivalent to a rewind followed by a close.
    pub fn discard(self) {
        // would happen automatically but might as well be explicit
        std::mem::drop(self)
    }

    /// Rewind the frame.
    ///
    /// This will rewind the frame. Any terms created in the context
    /// of this frame will no longer be usable. Furthermore, any term
    /// manipulation that happened since opening this frame will be
    /// undone.
    ///
    /// This returns a new context which is to be used for further
    /// manipulation of this frame.
    pub fn rewind(self) -> Context<'a, Frame> {
        self.assert_activated();
        // unsafe justification: We just checked that this frame right here is currently the active context. Therefore it can be rewinded.
        unsafe { PL_rewind_foreign_frame(self.context.fid) };

        self
    }
}

/// A trait marker for context types for which it is safe to open frames.
pub trait FrameableContextType: ContextType {}
impl FrameableContextType for Unmanaged {}
impl<'a> FrameableContextType for ActivatedEngine<'a> {}
impl FrameableContextType for Frame {}

impl<'a, C: FrameableContextType> Context<'a, C> {
    /// Open a new frame.
    ///
    /// This returns a new context for the frame. The current context
    /// will become inactive, until the new context is dropped. This
    /// may happen implicitely, when it goes out of scope, or
    /// explicitely, by calling `close()` or `discard()` on it.
    pub fn open_frame(&self) -> Context<Frame> {
        self.assert_activated();
        let fid = unsafe { PL_open_foreign_frame() };

        let frame = Frame {
            fid,
            state: FrameState::Active,
        };

        self.activated.set(false);
        unsafe { Context::new_activated(self, frame, self.engine) }
    }
}

/// A trait marker for context types for which it is safe to open queries and create new term refs.
pub trait QueryableContextType: FrameableContextType {}
impl QueryableContextType for Unmanaged {}
impl<'a> QueryableContextType for ActivatedEngine<'a> {}
impl QueryableContextType for Frame {}

prolog! {
    #[module("user")]
    fn read_term_from_atom(atom_term, result, options);
    #[module("user")]
    #[name("call")]
    fn open_call(term);
}

pub type GenericQueryableContext<'a> = Context<'a, GenericQueryableContextType>;

impl<'a, T: QueryableContextType> Context<'a, T> {
    /// Create a new Term reference in the current context.
    ///
    /// The term ref takes on the lifetime of the Context reference,
    /// ensuring that it cannot outlive the context that created it.
    pub fn new_term_ref(&self) -> Term {
        self.assert_activated();
        unsafe {
            let term = PL_new_term_ref();
            Term::new(term, self.as_term_origin())
        }
    }

    /// create an array of term references.
    ///
    /// The term refs all take on the lifetime of the Context
    /// reference, ensuring that it cannot outlive the context that
    /// created it.
    pub fn new_term_refs<const N: usize>(&self) -> [Term; N] {
        // TODO: this should be a compile time thing ideally
        // TODO: swipl 9.1.19 changed the paramater type to usize
        // if N > i32::MAX as usize {
        //     panic!("too many term refs requested: {}", N);
        // }

        #[allow(clippy::useless_conversion)]
        let mut term_ptr = unsafe { PL_new_term_refs(N.try_into().unwrap()) };
        let mut result: [MaybeUninit<Term>; N] = unsafe { MaybeUninit::uninit().assume_init() };
        for r in result.iter_mut() {
            let term = unsafe { Term::new(term_ptr, self.as_term_origin()) };
            r.write(term);
            term_ptr += 1;
        }

        // It would be nicer if we could do a transmute here, as
        // transmute ensures that the conversion converts between
        // types of the same size, but it seems like this doesn't work
        // yet with const generic arrays. We do a pointer cast
        // instead.
        let magic = result.as_ptr() as *const [Term; N];
        // allowing forget_non_drop cause I don't want us to accidentally drop something should the underlying things at one point implement drop.
        #[allow(clippy::forget_non_drop)]
        std::mem::forget(result);

        unsafe { magic.read() }
    }

    /// create a vec of term references.
    ///
    /// The term refs all take on the lifetime of the Context
    /// reference, ensuring that it cannot outlive the context that
    /// created it.
    pub fn new_term_refs_vec(&self, count: usize) -> Vec<Term> {
        #[allow(clippy::useless_conversion)]
        let mut term_ptr = unsafe { PL_new_term_refs(count.try_into().unwrap()) };
        let mut result = Vec::with_capacity(count);
        for _ in 0..count {
            let term = unsafe { Term::new(term_ptr, self.as_term_origin()) };
            result.push(term);
            term_ptr += 1;
        }

        result
    }

    /// Open a query.
    ///
    /// Example:
    /// ```
    /// # use swipl::prelude::*;
    /// # fn main() -> PrologResult<()> {
    /// #  let engine = Engine::new();
    /// #  let activation = engine.activate();
    /// #  let context: Context<_> = activation.into();
    ///
    ///    let query = context.open(pred!{format/2},
    ///                             [&term!{context: "hello, ~q~n"}?,
    ///                              &term!{context: ["world"]}?]);
    ///    query.next_solution()?;
    ///    query.cut();
    /// #
    /// #  Ok(())
    /// # }
    /// ```
    pub fn open<C: Callable<N>, const N: usize>(
        &self,
        callable: C,
        args: [&Term; N],
    ) -> Context<C::ContextType> {
        callable.open(self, None, args)
    }

    /// Open a query, get a single result and cut.
    ///
    /// Example:
    /// ```
    /// # use swipl::prelude::*;
    /// # fn main() -> PrologResult<()> {
    /// #  let engine = Engine::new();
    /// #  let activation = engine.activate();
    /// #  let context: Context<_> = activation.into();
    ///
    ///    context.call_once(pred!{format/2},
    ///                      [&term!{context: "hello, ~q~n"}?,
    ///                      &term!{context: ["world"]}?])?;
    /// #
    /// #  Ok(())
    /// # }
    /// ```
    pub fn call_once<C: Callable<N>, const N: usize>(
        &self,
        callable: C,
        args: [&Term; N],
    ) -> PrologResult<()> {
        let query = callable.open(self, None, args);
        query.next_solution()?;
        query.cut();

        Ok(())
    }

    /// Open a query, optionally passing in a context module.
    pub fn open_with_module<C: Callable<N>, const N: usize>(
        &self,
        callable: C,
        module: Option<Module>,
        args: [&Term; N],
    ) -> Context<C::ContextType> {
        callable.open(self, module, args)
    }

    /// Turn the given string into a prolog term.
    ///
    /// This uses the prolog predicate `read_term_from_atom/3` for the
    /// heavy lifting.
    ///
    /// Consider using the `term!` macro instead.
    pub fn term_from_string(&self, s: &str) -> PrologResult<Term> {
        let term = self.new_term_ref();
        let frame = self.open_frame();

        let arg1 = frame.new_term_ref();
        let arg3 = frame.new_term_ref();

        assert!(arg1.unify(s).is_ok());
        assert!(arg3.unify(Nil).is_ok());

        read_term_from_atom(&frame, &arg1, &term, &arg3).once()?;
        frame.close();

        Ok(term)
    }

    /// Turn the given string into a prolog term.
    ///
    /// This uses the prolog predicate `read_term_from_atom/3` for the
    /// heavy lifting.
    ///
    /// Consider using the `term!` macro instead.
    pub fn string_from_term(&self, t: &Term) -> PrologResult<String> {
        let frame = self.open_frame();
        let out = frame.new_term_ref();

        frame.call_once(pred!("term_string/2"), [t, &out])?;
        let s: String = out.get()?;
        frame.close();

        Ok(s)
    }

    /// Open a query for the given term using the `call/1` prolog predicate.
    pub fn open_call(&'a self, t: &Term<'a>) -> Context<'a, impl OpenCall> {
        open_call(self, t)
    }

    pub fn call_term_once(&'a self, t: &Term<'a>) -> PrologResult<()> {
        let open_call = self.open_call(t);
        open_call.next_solution()?;
        open_call.cut();

        Ok(())
    }

    /// Turn a result into a `PrologResult`.
    ///
    /// For this to work, the `Err` component of the `Result` needs to
    /// implement the trait `IntoPrologException`. This is currently
    /// only the case for [std::io::Error].
    pub fn try_or_die<R, E: IntoPrologException>(&self, r: Result<R, E>) -> PrologResult<R> {
        match r {
            Ok(ok) => Ok(ok),
            Err(e) => {
                let reset_term = self.new_term_ref();
                let exception_term = e.into_prolog_exception(self)?;
                let result = self.raise_exception(&exception_term);

                unsafe {
                    reset_term.reset();
                }

                result
            }
        }
    }

    /// Turn a result into a `PrologResult`.
    ///
    /// For this to work, the `Err` component of the `Result` needs to
    /// implement the trait [Error](std::error::Error).
    pub fn try_or_die_generic<R, E: std::error::Error>(&self, r: Result<R, E>) -> PrologResult<R> {
        match r {
            Ok(ok) => Ok(ok),
            Err(e) => {
                let reset_term = self.new_term_ref();
                let msg = format!("{}", e);

                // TODO: term macro doesn't like self, which is
                // probably only a problem for things inside this
                // crate but still should probably be resolved.
                let self_ = self;
                let exception_term = term! {self_: error(rust_error(#msg), _)}?;
                let result = self.raise_exception(&exception_term);

                unsafe {
                    reset_term.reset();
                }

                result
            }
        }
    }

    /// Iterate over a term list.
    ///
    /// this returns a TermListIterator made out of the given
    /// term. The TermListIterator will assume this is a cons cell,
    /// and unify head and tail on each step of the iterator,
    /// returning the head term and storing the tail term. If this
    /// unification fails, the iterator stops.
    ///
    /// Note that the terms created by this iterator are not
    /// automatically thrown away. It is the caller's responsibility
    /// to clean up terms if this is required, for example by using a
    /// frame.
    pub fn term_list_iter<'b>(&'b self, list: &Term) -> TermListIterator<'b, 'a, T> {
        self.assert_activated();
        let cur = self.new_term_ref();
        cur.unify(list).expect("unifying terms should work");
        TermListIterator { context: self, cur }
    }

    /// Retrieve a term list as a fixed-size array.
    ///
    /// This is useful when a term contains a list whose supposed size
    /// is known at compile time. If the actual list is larger than
    /// this, only the first N elements are used. If the list is
    /// smaller, the remaining terms in the array remain variables.
    pub fn term_list_array<const N: usize>(&self, list: &Term) -> [Term; N] {
        self.assert_activated();
        // allocate these terms inside the scope of this context
        let terms = self.new_term_refs();

        let frame = self.open_frame();
        let terms_iter = terms.iter();
        let list_iter = frame.term_list_iter(list);

        for (term, elt) in terms_iter.zip(list_iter) {
            term.unify(elt).unwrap();
        }
        frame.close();

        terms
    }

    /// Retrieve a term list as a Vec.
    ///
    /// This will iterate over the given prolog list twice - once to
    /// figure out its size, and then another time to actually
    /// retrieve the elements. This is done so that we can allocate
    /// the terms in a way that leaves no unused terms behind on the
    /// stack (as would normally happen when iterating the list using
    /// [term_list_iter](Context::term_list_iter)).
    ///
    /// If you know in advance what the size is going to be (or you
    /// know a reasonable upper bound), consider using
    /// [term_list_array](Context::term_list_array). If you just wish
    /// to iterate over the elements, or don't care about garbage
    /// terms being created, consider using
    /// [term_list_iter](Context::term_list_iter).
    pub fn term_list_vec(&self, list: &Term) -> Vec<Term> {
        self.assert_activated();
        let frame = self.open_frame();
        let count = frame.term_list_iter(list).count();
        frame.discard();

        // allocate these terms inside the scope of this context
        let terms = self.new_term_refs_vec(count);

        let frame = self.open_frame();
        let terms_iter = terms.iter();
        let list_iter = frame.term_list_iter(list);

        for (term, elt) in terms_iter.zip(list_iter) {
            term.unify(elt).unwrap();
        }
        frame.close();

        terms
    }

    /// Retrieve compound terms as a fixed size array.
    ///
    /// This will ensure that the given term is indeed a compound with
    /// arity N. If this is true, N terms will be allocated in this
    /// context, unified with the argument terms of the compound, and
    /// returned as an array. If not, this method will fail.
    pub fn compound_terms<const N: usize>(&self, compound: &Term) -> PrologResult<[Term; N]> {
        self.assert_activated();
        if N > (i32::MAX - 1) as usize {
            panic!("requested compound term array too large: {}", N);
        }

        let mut size = 0;
        if unsafe {
            PL_get_compound_name_arity(compound.term_ptr(), std::ptr::null_mut(), &mut size) != 1
        } {
            return Err(PrologError::Failure);
        }
        if (size as usize) != N {
            return Err(PrologError::Failure);
        }

        let terms: [Term; N] = self.new_term_refs();
        for (i, term) in terms.iter().enumerate() {
            unsafe {
                assert!(PL_get_arg((i + 1) as i32, compound.term_ptr(), term.term_ptr()) == 1);
            }
        }

        Ok(terms)
    }

    /// Retrieve compound terms as a Vec.
    ///
    /// This will ensure that the given term is indeed a compound of
    /// any arity. If this is true, arity terms will be allocated in
    /// this context, unified with the argument terms of the compound,
    /// and returned as a Vec. If not, this method will fail.
    pub fn compound_terms_vec(&self, compound: &Term) -> PrologResult<Vec<Term>> {
        self.assert_activated();

        let mut size = 0;
        if unsafe {
            PL_get_compound_name_arity(compound.term_ptr(), std::ptr::null_mut(), &mut size) != 1
        } {
            return Err(PrologError::Failure);
        }

        let terms = self.new_term_refs_vec(size as usize);
        for (i, term) in terms.iter().enumerate() {
            unsafe {
                assert!(PL_get_arg((i + 1) as i32, compound.term_ptr(), term.term_ptr()) == 1);
            }
        }

        Ok(terms)
    }

    /// Retrieve compound terms as a fixed size Vec.
    ///
    /// This will ensure that the given term is indeed a compound with
    /// arity `count`. If this is true, `count` terms will be
    /// allocated in this context, unified with the argument terms of
    /// the compound, and returned as an array. If not, this method
    /// will fail.
    pub fn compound_terms_vec_sized(
        &self,
        compound: &Term,
        count: usize,
    ) -> PrologResult<Vec<Term>> {
        self.assert_activated();

        let mut size = 0;
        if unsafe {
            PL_get_compound_name_arity(compound.term_ptr(), std::ptr::null_mut(), &mut size) != 1
        } {
            return Err(PrologError::Failure);
        }
        if (size as usize) != count {
            return Err(PrologError::Failure);
        }

        let terms = self.new_term_refs_vec(count);
        for (i, term) in terms.iter().enumerate() {
            unsafe {
                assert!(PL_get_arg((i + 1) as i32, compound.term_ptr(), term.term_ptr()) == 1);
            }
        }

        Ok(terms)
    }

    #[cfg(feature = "serde")]
    /// Deserialize a term into a rust value using serde.
    pub fn deserialize_from_term<'de, DT: Deserialize<'de>>(
        &'de self,
        term: &'de Term<'de>,
    ) -> super::term::de::Result<DT> {
        super::term::de::from_term(self, term)
    }

    #[cfg(feature = "serde")]
    /// Serialize a value into a prolog term using serde.
    ///
    /// This uses the default serialization configuration, meaning:
    /// - prolog dictionary tags will remain variables.
    /// - struct type names are ignored and will not be set as the dictionary tag.
    pub fn serialize_to_term<ST: Serialize>(
        &self,
        term: &Term,
        obj: &ST,
    ) -> Result<(), super::term::de::Error> {
        super::term::ser::to_term(self, term, obj)
    }

    #[cfg(feature = "serde")]
    /// Serialize a value into a prolog term using serde, providing configuration options.
    pub fn serialize_to_term_with_config<ST: Serialize>(
        &self,
        term: &Term,
        obj: &ST,
        config: SerializerConfiguration,
    ) -> Result<(), super::term::de::Error> {
        super::term::ser::to_term_with_config(self, term, obj, config)
    }

    /// Unify the term with the list functor, returning a term for the head and the tail.
    pub fn unify_list_functor<'b>(
        &'b self,
        term: &Term,
    ) -> Result<(Term<'b>, Term<'b>), PrologError> {
        let [head, tail] = self.new_term_refs();
        match unsafe { PL_unify_list(term.term_ptr(), head.term_ptr(), tail.term_ptr()) } {
            0 => {
                unsafe {
                    head.reset();
                }
                if unsafe { pl_default_exception() != 0 } {
                    Err(PrologError::Exception)
                } else {
                    Err(PrologError::Failure)
                }
            }
            _ => Ok((head, tail)),
        }
    }

    pub fn into_generic(&self) -> GenericQueryableContext {
        self.assert_activated();
        self.activated.set(false);
        unsafe { Context::new_activated(self, GenericQueryableContextType, self.engine) }
    }
}

/// An iterator over a term list.
///
/// See [`Context::term_list_iter`] for more information.
pub struct TermListIterator<'a, 'b, CT: QueryableContextType> {
    context: &'a Context<'b, CT>,
    cur: Term<'a>,
}

impl<'a, 'b, CT: QueryableContextType> Iterator for TermListIterator<'a, 'b, CT> {
    type Item = Term<'a>;

    fn next(&mut self) -> Option<Term<'a>> {
        let head = self.context.new_term_ref();
        let tail = self.context.new_term_ref();
        let success =
            unsafe { PL_get_list(self.cur.term_ptr(), head.term_ptr(), tail.term_ptr()) != 0 };

        if success {
            self.cur = tail;
            Some(head)
        } else {
            None
        }
    }
}

/// Trait for turning errors into prolog exceptions
pub trait IntoPrologException {
    /// Turns this error into a prolog exception using the given context.
    ///
    /// The result is a `Term` containing the prolog exception.
    fn into_prolog_exception<'a, T: QueryableContextType>(
        self,
        context: &'a Context<'_, T>,
    ) -> PrologResult<Term<'a>>;
}

impl IntoPrologException for std::io::Error {
    fn into_prolog_exception<'a, T: QueryableContextType>(
        self,
        context: &'a Context<'_, T>,
    ) -> PrologResult<Term<'a>> {
        let kind_str = format!("{:?}", self.kind());
        let kind_atom = Atom::new(&kind_str);
        let msg = format!("{}", self);
        term! {context: error(rust_io_error(#kind_atom, #msg), _)}
    }
}

/// Call the given function, converting panics into prolog exceptions.
///
/// If the inner function panics, an exception of the form
/// `error(rust_error(panic("..the panic message..")))` will be
/// raised, and this function will return
/// `Err(PrologError::Exception)`. Otherwise, This function will
/// return `Ok(())`.
///
/// This is used by various macros to ensure that panics from user
/// code do not propagate into prolog.
///
/// # Safety
/// This is only safe to use from an environment from which we can
/// raise a prolog exception.
pub unsafe fn prolog_catch_unwind<F: FnOnce() -> R + std::panic::UnwindSafe, R>(
    f: F,
) -> PrologResult<R> {
    let result = std::panic::catch_unwind(f);
    match result {
        Ok(result) => Ok(result),
        Err(panic) => {
            let context = unmanaged_engine_context();
            let panic_term = context.new_term_ref();
            let error_term = term! {context: error(rust_error(panic(#&panic_term)), _)}?;

            match panic.downcast_ref::<&str>() {
                Some(panic_msg) => {
                    panic_term.unify(panic_msg).unwrap();
                }
                None => match panic.downcast_ref::<String>() {
                    Some(panic_msg) => {
                        panic_term.unify(panic_msg.as_str()).unwrap();
                    }
                    None => {
                        panic_term.unify("unknown panic type").unwrap();
                    }
                },
            }

            context.raise_exception::<()>(&error_term).unwrap_err();
            Err(PrologError::Exception)
        }
    }
}

#[derive(Clone)]
pub struct GenericQueryableContextType;
unsafe impl ContextType for GenericQueryableContextType {}
impl FrameableContextType for GenericQueryableContextType {}
impl QueryableContextType for GenericQueryableContextType {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::functor::*;
    use crate::predicate::*;
    use crate::predicates;

    #[test]
    fn get_term_ref_on_fresh_engine() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let _term = context.new_term_ref();
    }

    #[test]
    fn get_term_ref_on_frame() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context1: Context<_> = activation.into();
        let _term1 = context1.new_term_ref();

        let context2 = context1.open_frame();
        let _term2 = context2.new_term_ref();
        std::mem::drop(context2);
        let _term3 = context1.new_term_ref();
    }

    #[test]
    #[should_panic]
    fn get_term_ref_from_inactive_context_panics() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context1: Context<_> = activation.into();
        let _context2 = context1.open_frame();

        let _term = context1.new_term_ref();
    }

    #[test]
    fn query_det() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let functor_is = Functor::new("is", 2);
        let functor_plus = Functor::new("+", 2);
        let module = Module::new("user");
        let predicate = Predicate::new(functor_is, module);
        let callable = CallablePredicate::new(predicate).unwrap();

        let term1 = context.new_term_ref();
        let term2 = context.new_term_ref();

        term2.unify(functor_plus)?;
        term2.unify_arg(1, 40_u64)?;
        term2.unify_arg(2, 2_u64)?;

        let query = context.open(callable, [&term1, &term2]);
        let next = query.next_solution()?;

        assert!(!next);
        assert_eq!(42_u64, term1.get()?);

        let next = query.next_solution();
        assert!(next.is_err());

        Ok(())
    }

    #[test]
    fn query_auto_discard() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let functor_is = Functor::new("is", 2);
        let functor_plus = Functor::new("+", 2);
        let module = Module::new("user");
        let predicate = Predicate::new(functor_is, module);
        let callable = CallablePredicate::new(predicate).unwrap();

        let term1 = context.new_term_ref();
        let term2 = context.new_term_ref();

        assert!(term2.unify(functor_plus).is_ok());
        assert!(term2.unify_arg(1, 40_u64).is_ok());
        assert!(term2.unify_arg(2, 2_u64).is_ok());

        {
            let query = context.open(callable, [&term1, &term2]);
            let next = query.next_solution()?;

            assert!(!next);
            assert_eq!(42_u64, term1.get().unwrap());
        }

        // after leaving the block, we have discarded
        assert!(term1.get::<u64>().unwrap_err().is_failure());

        Ok(())
    }

    #[test]
    fn query_manual_discard() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let functor_is = Functor::new("is", 2);
        let functor_plus = Functor::new("+", 2);
        let module = Module::new("user");
        let predicate = Predicate::new(functor_is, module);
        let callable = CallablePredicate::new(predicate).unwrap();

        let term1 = context.new_term_ref();
        let term2 = context.new_term_ref();

        term2.unify(functor_plus)?;
        term2.unify_arg(1, 40_u64)?;
        term2.unify_arg(2, 2_u64)?;

        {
            let query = context.open(callable, [&term1, &term2]);
            let next = query.next_solution()?;

            assert!(!next);
            assert_eq!(42_u64, term1.get()?);
            query.discard();
        }

        // after leaving the block, we have discarded
        assert!(term1.get::<u64>().unwrap_err().is_failure());

        Ok(())
    }

    #[test]
    fn query_cut() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let functor_is = Functor::new("is", 2);
        let functor_plus = Functor::new("+", 2);
        let module = Module::new("user");
        let predicate = Predicate::new(functor_is, module);
        let callable = CallablePredicate::new(predicate).unwrap();

        let term1 = context.new_term_ref();
        let term2 = context.new_term_ref();

        term2.unify(functor_plus)?;
        term2.unify_arg(1, 40_u64)?;
        term2.unify_arg(2, 2_u64)?;

        {
            let query = context.open(callable, [&term1, &term2]);
            let next = query.next_solution()?;

            assert!(!next);
            assert_eq!(42_u64, term1.get()?);
            query.cut();
        }

        // a cut query leaves data intact
        assert_eq!(42_u64, term1.get()?);

        Ok(())
    }

    #[test]
    fn term_from_string_works() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term = context.term_from_string("foo(bar(baz,quux))").unwrap();
        let functor_foo = Functor::new("foo", 1);
        let functor_bar = Functor::new("bar", 2);

        assert_eq!(functor_foo, term.get().unwrap());
        assert_eq!(functor_bar, term.get_arg(1).unwrap());
    }

    #[test]
    fn open_call_nondet() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term = context.term_from_string("member(X, [a,b,c])").unwrap();
        let term_x = context.new_term_ref();
        assert!(term.unify_arg(1, &term_x).is_ok());

        let query = context.open_call(&term);
        assert!(query.next_solution()?);
        term_x.get_atom_name(|a| assert_eq!("a", a.unwrap()))?;

        assert!(query.next_solution()?);
        term_x.get_atom_name(|a| assert_eq!("b", a.unwrap()))?;

        assert!(!query.next_solution()?);
        term_x.get_atom_name(|a| assert_eq!("c", a.unwrap()))?;

        assert!(query.next_solution().unwrap_err().is_failure());

        Ok(())
    }

    #[test]
    fn open_query_with_0_arg_predicate() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let functor = Functor::new("true", 0);
        let module = Module::new("user");
        let predicate = Predicate::new(functor, module);
        let callable = CallablePredicate::new(predicate).unwrap();

        let query = context.open(callable, []);
        assert!(!query.next_solution()?);

        Ok(())
    }

    #[test]
    fn freeze_exception_is_delayed_until_next_query() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term = context.term_from_string("freeze(X, throw(foo))")?;
        let term_x = context.new_term_ref();
        term.unify_arg(1, &term_x)?;
        let query = context.open_call(&term);
        assert!(!query.next_solution()?);
        query.cut();

        assert!(term_x.unify(42_u64).is_ok());

        let term = context.new_term_ref();
        term.unify(true)?;
        let query = context.open_call(&term);
        let next = query.next_solution();
        assert!(next.unwrap_err().is_exception());
        query.with_exception(|e| {
            let exception_term = e.unwrap();
            let atomable: Atomable = exception_term.get().unwrap();
            assert_eq!("foo", atomable.name());

            assert!(term.get::<u64>().unwrap_err().is_failure());
        });

        Ok(())
    }

    prolog! {
        #[name("is")]
        fn prolog_arithmetic(term, e);
    }

    #[test]
    #[should_panic(expected = "tried to use context which has raised an exception")]
    fn call_prolog_with_raised_exception_panics() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term1 = context.new_term_ref();
        let term2 = context.new_term_ref();

        let query = prolog_arithmetic(&context, &term1, &term2);
        assert!(query.next_solution().unwrap_err().is_exception());
        assert!(query.has_exception());
        query.discard();
        let _query2 = prolog_arithmetic(&context, &term1, &term2);
    }

    predicates! {
        semidet fn unify_with_42(_context, term) {
            term.unify(42_u64)
        }
    }

    #[test]
    fn register_foreign_predicate() -> PrologResult<()> {
        let engine = Engine::new();
        let activation = engine.activate();

        assert!(register_unify_with_42());

        let context: Context<_> = activation.into();
        let term = context.new_term_ref();

        let functor = Functor::new("unify_with_42", 1);
        let module = Module::new("user");
        let predicate = Predicate::new(functor, module);
        let callable = CallablePredicate::new(predicate).unwrap();

        let query = context.open(callable, [&term]);
        assert!(!query.next_solution()?);
        assert_eq!(42, term.get::<u64>().unwrap());

        Ok(())
    }

    #[test]
    fn call_prolog_from_generated_rust_query_opener() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term = context.new_term_ref();
        let expr = context.term_from_string("2+2").unwrap();

        let q = prolog_arithmetic(&context, &term, &expr);
        assert!(q.next_solution().is_ok());
        assert_eq!(4, term.get::<u64>().unwrap());
    }

    #[test]
    fn iterate_over_term_list() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let list = context.term_from_string("[5, foo, \"bar\"]").unwrap();

        let mut iter = context.term_list_iter(&list);
        let first = iter.next().unwrap();
        let second = iter.next().unwrap();
        let third = iter.next().unwrap();
        assert!(iter.next().is_none());

        assert_eq!(5, first.get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), second.get::<Atom>().unwrap());
        assert_eq!("bar", third.get::<String>().unwrap());
    }

    #[test]
    fn iterate_over_term_that_is_not_a_list() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let list = context.term_from_string("foo(bar, baz)").unwrap();

        let mut iter = context.term_list_iter(&list);
        assert!(iter.next().is_none());
    }

    #[test]
    fn loop_over_term_list() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let list = context.term_from_string("[42, 42, 42, 42, 42]").unwrap();

        let mut count = 0;
        for term in context.term_list_iter(&list) {
            count += 1;
            assert_eq!(42, term.get::<u64>().unwrap());
        }

        assert_eq!(5, count);
    }

    #[test]
    fn term_list_to_array() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let list = context.term_from_string("[5, foo, \"bar\"]").unwrap();

        let terms: [Term; 3] = context.term_list_array(&list);
        assert_eq!(5, terms[0].get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), terms[1].get::<Atom>().unwrap());
        assert_eq!("bar", terms[2].get::<String>().unwrap());
    }

    #[test]
    fn term_list_to_array_large() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let list = context.term_from_string("[5, foo, \"bar\"]").unwrap();

        let terms: [Term; 4] = context.term_list_array(&list);
        assert_eq!(5, terms[0].get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), terms[1].get::<Atom>().unwrap());
        assert_eq!("bar", terms[2].get::<String>().unwrap());
        assert!(terms[3].is_var());
    }

    #[test]
    fn term_list_to_array_small() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let list = context.term_from_string("[5, foo, \"bar\"]").unwrap();

        let terms: [Term; 2] = context.term_list_array(&list);
        assert_eq!(5, terms[0].get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), terms[1].get::<Atom>().unwrap());
    }

    #[test]
    fn term_list_to_vec() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let list = context.term_from_string("[5, foo, \"bar\"]").unwrap();
        let terms = context.term_list_vec(&list);
        assert_eq!(3, terms.len());
        assert_eq!(5, terms[0].get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), terms[1].get::<Atom>().unwrap());
        assert_eq!("bar", terms[2].get::<String>().unwrap());
    }

    #[test]
    fn term_compound_to_array() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let compound = context.term_from_string("moo(5, foo, \"bar\")").unwrap();
        let terms: [Term; 3] = context.compound_terms(&compound).unwrap();
        assert_eq!(5, terms[0].get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), terms[1].get::<Atom>().unwrap());
        assert_eq!("bar", terms[2].get::<String>().unwrap());
    }

    #[test]
    fn term_compound_to_wrong_size_array() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let compound = context.term_from_string("moo(5, foo, \"bar\")").unwrap();
        let terms: Option<[Term; 4]> = attempt_opt(context.compound_terms(&compound)).unwrap();
        assert!(terms.is_none());
    }

    #[test]
    fn term_compound_to_vec() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let compound = context.term_from_string("moo(5, foo, \"bar\")").unwrap();
        let terms = context.compound_terms_vec(&compound).unwrap();
        assert_eq!(3, terms.len());
        assert_eq!(5, terms[0].get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), terms[1].get::<Atom>().unwrap());
        assert_eq!("bar", terms[2].get::<String>().unwrap());
    }

    #[test]
    fn term_compound_to_sized_vec() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let compound = context.term_from_string("moo(5, foo, \"bar\")").unwrap();
        let terms = context.compound_terms_vec_sized(&compound, 3).unwrap();
        assert_eq!(3, terms.len());
        assert_eq!(5, terms[0].get::<u64>().unwrap());
        assert_eq!(Atom::new("foo"), terms[1].get::<Atom>().unwrap());
        assert_eq!("bar", terms[2].get::<String>().unwrap());
    }

    #[test]
    fn term_compound_to_wrong_size_vec() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let compound = context.term_from_string("moo(5, foo, \"bar\")").unwrap();
        let terms = attempt_opt(context.compound_terms_vec_sized(&compound, 4)).unwrap();
        assert!(terms.is_none());
    }

    #[test]
    fn term_compound_not_a_compound() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let compound = context.term_from_string("\"moo\"").unwrap();
        let terms: Option<[Term; 4]> = attempt_opt(context.compound_terms(&compound)).unwrap();
        assert!(terms.is_none());
    }
}