oxirs-core 0.2.3

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
Documentation
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
//! Numeric and mathematical functions for SPARQL 1.2

use crate::model::{Literal, NamedNode, Term};
use crate::OxirsError;

/// ABS - Absolute value
pub(super) fn fn_abs(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ABS requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ABS requires numeric argument".to_string()))?;
            let result = value.abs();

            // Preserve datatype
            let dt = lit.datatype();
            if dt.as_str() == "http://www.w3.org/2001/XMLSchema#integer" {
                Ok(Term::Literal(Literal::new_typed(
                    (result as i64).to_string(),
                    NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                        .expect("XSD integer datatype IRI should be valid"),
                )))
            } else {
                Ok(Term::Literal(Literal::new_typed(
                    result.to_string(),
                    NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                        .expect("XSD double datatype IRI should be valid"),
                )))
            }
        }
        _ => Err(OxirsError::Query(
            "ABS requires numeric literal".to_string(),
        )),
    }
}

/// CEIL - Ceiling (round up)
pub(super) fn fn_ceil(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "CEIL requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("CEIL requires numeric argument".to_string()))?;
            let result = value.ceil() as i64;
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "CEIL requires numeric literal".to_string(),
        )),
    }
}

/// FLOOR - Floor (round down)
pub(super) fn fn_floor(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "FLOOR requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("FLOOR requires numeric argument".to_string()))?;
            let result = value.floor() as i64;
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "FLOOR requires numeric literal".to_string(),
        )),
    }
}

/// ROUND - Round to nearest integer
pub(super) fn fn_round(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ROUND requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ROUND requires numeric argument".to_string()))?;
            let result = value.round() as i64;
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ROUND requires numeric literal".to_string(),
        )),
    }
}

/// RAND - Generate random number [0, 1)
pub(super) fn fn_rand(_args: &[Term]) -> Result<Term, OxirsError> {
    use scirs2_core::random::{Random, RngExt};
    let mut random = Random::default();
    let value: f64 = random.random();
    Ok(Term::Literal(Literal::new_typed(
        value.to_string(),
        NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
            .expect("XSD double datatype IRI should be valid"),
    )))
}

/// SQRT - Square root
pub(super) fn fn_sqrt(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "SQRT requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("SQRT requires numeric argument".to_string()))?;
            if value < 0.0 {
                return Err(OxirsError::Query("SQRT of negative number".to_string()));
            }
            let result = value.sqrt();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "SQRT requires numeric literal".to_string(),
        )),
    }
}

/// SIN - Sine
pub(super) fn fn_sin(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "SIN requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("SIN requires numeric argument".to_string()))?;
            let result = value.sin();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "SIN requires numeric literal".to_string(),
        )),
    }
}

/// COS - Cosine
pub(super) fn fn_cos(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "COS requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("COS requires numeric argument".to_string()))?;
            let result = value.cos();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "COS requires numeric literal".to_string(),
        )),
    }
}

/// TAN - Tangent
pub(super) fn fn_tan(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "TAN requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("TAN requires numeric argument".to_string()))?;
            let result = value.tan();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "TAN requires numeric literal".to_string(),
        )),
    }
}

/// ASIN - Arcsine
pub(super) fn fn_asin(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ASIN requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ASIN requires numeric argument".to_string()))?;
            if !(-1.0..=1.0).contains(&value) {
                return Err(OxirsError::Query(
                    "ASIN argument must be between -1 and 1".to_string(),
                ));
            }
            let result = value.asin();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ASIN requires numeric literal".to_string(),
        )),
    }
}

/// ACOS - Arccosine
pub(super) fn fn_acos(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ACOS requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ACOS requires numeric argument".to_string()))?;
            if !(-1.0..=1.0).contains(&value) {
                return Err(OxirsError::Query(
                    "ACOS argument must be between -1 and 1".to_string(),
                ));
            }
            let result = value.acos();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ACOS requires numeric literal".to_string(),
        )),
    }
}

/// ATAN - Arctangent
pub(super) fn fn_atan(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ATAN requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ATAN requires numeric argument".to_string()))?;
            let result = value.atan();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ATAN requires numeric literal".to_string(),
        )),
    }
}

/// ATAN2 - Two-argument arctangent
pub(super) fn fn_atan2(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 2 {
        return Err(OxirsError::Query(
            "ATAN2 requires exactly 2 arguments".to_string(),
        ));
    }

    match (&args[0], &args[1]) {
        (Term::Literal(y_lit), Term::Literal(x_lit)) => {
            let y = y_lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ATAN2 requires numeric arguments".to_string()))?;
            let x = x_lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ATAN2 requires numeric arguments".to_string()))?;
            let result = y.atan2(x);
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ATAN2 requires numeric literals".to_string(),
        )),
    }
}

/// EXP - Exponential function (e^x)
pub(super) fn fn_exp(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "EXP requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("EXP requires numeric argument".to_string()))?;
            let result = value.exp();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "EXP requires numeric literal".to_string(),
        )),
    }
}

/// LOG - Natural logarithm
pub(super) fn fn_log(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "LOG requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("LOG requires numeric argument".to_string()))?;
            if value <= 0.0 {
                return Err(OxirsError::Query("LOG of non-positive number".to_string()));
            }
            let result = value.ln();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "LOG requires numeric literal".to_string(),
        )),
    }
}

/// LOG10 - Base-10 logarithm
pub(super) fn fn_log10(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "LOG10 requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("LOG10 requires numeric argument".to_string()))?;
            if value <= 0.0 {
                return Err(OxirsError::Query(
                    "LOG10 of non-positive number".to_string(),
                ));
            }
            let result = value.log10();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "LOG10 requires numeric literal".to_string(),
        )),
    }
}

/// POW - Power function
pub(super) fn fn_pow(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 2 {
        return Err(OxirsError::Query(
            "POW requires exactly 2 arguments".to_string(),
        ));
    }

    match (&args[0], &args[1]) {
        (Term::Literal(base_lit), Term::Literal(exp_lit)) => {
            let base = base_lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("POW requires numeric arguments".to_string()))?;
            let exp = exp_lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("POW requires numeric arguments".to_string()))?;
            let result = base.powf(exp);
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "POW requires numeric literals".to_string(),
        )),
    }
}

/// SINH - Hyperbolic sine
pub(super) fn fn_sinh(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "SINH requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let num = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("SINH requires numeric argument".to_string()))?;
            let result = num.sinh();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "SINH requires numeric literal".to_string(),
        )),
    }
}

/// COSH - Hyperbolic cosine
pub(super) fn fn_cosh(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "COSH requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let num = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("COSH requires numeric argument".to_string()))?;
            let result = num.cosh();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "COSH requires numeric literal".to_string(),
        )),
    }
}

/// TANH - Hyperbolic tangent
pub(super) fn fn_tanh(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "TANH requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let num = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("TANH requires numeric argument".to_string()))?;
            let result = num.tanh();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "TANH requires numeric literal".to_string(),
        )),
    }
}

/// ASINH - Inverse hyperbolic sine
pub(super) fn fn_asinh(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ASINH requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let num = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ASINH requires numeric argument".to_string()))?;
            let result = num.asinh();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ASINH requires numeric literal".to_string(),
        )),
    }
}

/// ACOSH - Inverse hyperbolic cosine
pub(super) fn fn_acosh(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ACOSH requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let num = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ACOSH requires numeric argument".to_string()))?;
            // acosh is only defined for x >= 1
            if num < 1.0 {
                return Err(OxirsError::Query(
                    "ACOSH requires argument >= 1".to_string(),
                ));
            }
            let result = num.acosh();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ACOSH requires numeric literal".to_string(),
        )),
    }
}

/// ATANH - Inverse hyperbolic tangent
pub(super) fn fn_atanh(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "ATANH requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let num = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("ATANH requires numeric argument".to_string()))?;
            // atanh is only defined for |x| < 1
            if num <= -1.0 || num >= 1.0 {
                return Err(OxirsError::Query(
                    "ATANH requires argument in range (-1, 1)".to_string(),
                ));
            }
            let result = num.atanh();
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
                    .expect("XSD double datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "ATANH requires numeric literal".to_string(),
        )),
    }
}

/// PI - Return the mathematical constant π (pi)
pub(super) fn fn_pi(_args: &[Term]) -> Result<Term, OxirsError> {
    Ok(Term::Literal(Literal::new_typed(
        std::f64::consts::PI.to_string(),
        NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
            .expect("XSD double datatype IRI should be valid"),
    )))
}

/// E - Return the mathematical constant e (Euler's number)
pub(super) fn fn_e(_args: &[Term]) -> Result<Term, OxirsError> {
    Ok(Term::Literal(Literal::new_typed(
        std::f64::consts::E.to_string(),
        NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
            .expect("XSD double datatype IRI should be valid"),
    )))
}

/// TAU - Return the mathematical constant Ï„ (tau = 2Ï€)
pub(super) fn fn_tau(_args: &[Term]) -> Result<Term, OxirsError> {
    Ok(Term::Literal(Literal::new_typed(
        std::f64::consts::TAU.to_string(),
        NamedNode::new("http://www.w3.org/2001/XMLSchema#double")
            .expect("XSD double datatype IRI should be valid"),
    )))
}

/// SIGN - Return the sign of a number (-1, 0, or 1)
pub(super) fn fn_sign(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "SIGN requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("SIGN requires numeric argument".to_string()))?;

            let sign = if value > 0.0 {
                1
            } else if value < 0.0 {
                -1
            } else {
                0
            };

            Ok(Term::Literal(Literal::new_typed(
                sign.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "SIGN requires numeric literal".to_string(),
        )),
    }
}

/// MOD - Modulo operation
pub(super) fn fn_mod(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 2 {
        return Err(OxirsError::Query(
            "MOD requires exactly 2 arguments".to_string(),
        ));
    }

    match (&args[0], &args[1]) {
        (Term::Literal(a_lit), Term::Literal(b_lit)) => {
            let a = a_lit
                .value()
                .parse::<i64>()
                .map_err(|_| OxirsError::Query("MOD requires integer arguments".to_string()))?;
            let b = b_lit
                .value()
                .parse::<i64>()
                .map_err(|_| OxirsError::Query("MOD requires integer arguments".to_string()))?;

            if b == 0 {
                return Err(OxirsError::Query("MOD by zero".to_string()));
            }

            let result = a % b;
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "MOD requires numeric literals".to_string(),
        )),
    }
}

/// TRUNC - Truncate to integer (towards zero)
pub(super) fn fn_trunc(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 1 {
        return Err(OxirsError::Query(
            "TRUNC requires exactly 1 argument".to_string(),
        ));
    }

    match &args[0] {
        Term::Literal(lit) => {
            let value = lit
                .value()
                .parse::<f64>()
                .map_err(|_| OxirsError::Query("TRUNC requires numeric argument".to_string()))?;
            let result = value.trunc() as i64;
            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "TRUNC requires numeric literal".to_string(),
        )),
    }
}

/// GCD - Greatest Common Divisor using Euclidean algorithm
pub(super) fn fn_gcd(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 2 {
        return Err(OxirsError::Query(
            "GCD requires exactly 2 arguments".to_string(),
        ));
    }

    match (&args[0], &args[1]) {
        (Term::Literal(a_lit), Term::Literal(b_lit)) => {
            let mut a = a_lit
                .value()
                .parse::<i64>()
                .map_err(|_| OxirsError::Query("GCD requires integer arguments".to_string()))?
                .abs();
            let mut b = b_lit
                .value()
                .parse::<i64>()
                .map_err(|_| OxirsError::Query("GCD requires integer arguments".to_string()))?
                .abs();

            // Euclidean algorithm
            while b != 0 {
                let temp = b;
                b = a % b;
                a = temp;
            }

            Ok(Term::Literal(Literal::new_typed(
                a.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "GCD requires numeric literals".to_string(),
        )),
    }
}

/// LCM - Least Common Multiple
pub(super) fn fn_lcm(args: &[Term]) -> Result<Term, OxirsError> {
    if args.len() != 2 {
        return Err(OxirsError::Query(
            "LCM requires exactly 2 arguments".to_string(),
        ));
    }

    match (&args[0], &args[1]) {
        (Term::Literal(a_lit), Term::Literal(b_lit)) => {
            let a = a_lit
                .value()
                .parse::<i64>()
                .map_err(|_| OxirsError::Query("LCM requires integer arguments".to_string()))?
                .abs();
            let b = b_lit
                .value()
                .parse::<i64>()
                .map_err(|_| OxirsError::Query("LCM requires integer arguments".to_string()))?
                .abs();

            if a == 0 || b == 0 {
                return Ok(Term::Literal(Literal::new_typed(
                    "0",
                    NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                        .expect("XSD integer datatype IRI should be valid"),
                )));
            }

            // Calculate GCD first
            let mut gcd_a = a;
            let mut gcd_b = b;
            while gcd_b != 0 {
                let temp = gcd_b;
                gcd_b = gcd_a % gcd_b;
                gcd_a = temp;
            }

            // LCM = (a * b) / GCD(a, b)
            let result = (a / gcd_a) * b;

            Ok(Term::Literal(Literal::new_typed(
                result.to_string(),
                NamedNode::new("http://www.w3.org/2001/XMLSchema#integer")
                    .expect("XSD integer datatype IRI should be valid"),
            )))
        }
        _ => Err(OxirsError::Query(
            "LCM requires numeric literals".to_string(),
        )),
    }
}