guile-rdf/rdf/xsd.scm

xsd.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library is distributed in the hope that it will be useful,
9
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (rdf xsd)
19
  #:use-module (ice-9 match)
20
  #:use-module (rdf rdf)
21
  #:export (datatypes order compatible?))
22
23
;; This module implements the xsd datatypes, as presented in https://www.w3.org/TR/rdf11-concepts/#xsd-datatypes
24
25
(define (make-xsd-datatype iri description lexical? value? lexical->value
26
                           value->lexical)
27
  (make-rdf-datatype
28
    (list (string-append "http://www.w3.org/2001/XMLSchema#" iri))
29
    description
30
    lexical?
31
    value?
32
    lexical->value
33
    value->lexical))
34
35
(define-public string
36
  (make-xsd-datatype
37
    "string"
38
    "Character strings (but not all Unicode character strings)"
39
    string?
40
    string?
41
    identity
42
    identity))
43
44
(define-public boolean
45
  (make-xsd-datatype
46
    "boolean"
47
    "true, false"
48
    (lambda (lexical)
49
      (member lexical '("true" "false" "0" "1")))
50
    boolean?
51
    (lambda (lexical)
52
      (match lexical
53
        ("true" #t)
54
        ("1" #t)
55
        ("false" #f)
56
        ("0" #f)))
57
    (lambda (value)
58
      (match value
59
        (#t "true")
60
        (#f "false")))))
61
62
(define-public decimal
63
  (make-xsd-datatype
64
    "decimal"
65
    "Arbitrary-precision decimal numbers"
66
    string->number
67
    number?
68
    string->number
69
    number->string))
70
71
(define-public integer
72
  (make-xsd-datatype
73
    "integer"
74
    "Arbitrary-size integer numbers"
75
    (lambda (lexical)
76
      (integer? (string->number lexical)))
77
    integer?
78
    string->number
79
    number->string))
80
81
(define-public int
82
  (make-xsd-datatype
83
    "int"
84
    "Limited-range integer numbre (32 bits)"
85
    (lambda (lexical)
86
      (and (integer? (string->number lexical))
87
           (>= (string->number lexical) -2147483648)
88
           (<= (string->number lexical) 2147483647)))
89
    (lambda (value)
90
      (and (integer? value)
91
           (>= value 2147483648)
92
           (<= value 2147483647)))
93
    string->number
94
    number->string))
95
96
(define-public double
97
  (make-xsd-datatype
98
    "double"
99
    "The double datatype is patterned after the IEEE double-precision 64-bit
100
floating point datatype.  Each floating point datatype has a value space that
101
is a subset of the rational numbers."
102
    string->number
103
    number?
104
    string->number
105
    number->string))
106
107
(define datatypes
108
  (list string boolean decimal integer int))
109
110
(define sub-classes
111
  (list
112
    (list rdf:langString)
113
    (list string)
114
    (list boolean)
115
    (list decimal integer int)
116
    (list integer int)
117
    (list int)))
118
119
(define (order d1 d2)
120
  "Return whether d1's value space is included in d2's"
121
  (member d1 (assoc-ref sub-classes d2)))
122
123
;; TODO: this is not entirely correct
124
(define (compatible? d1 d2)
125
  (or (order d1 d2) (order d2 d1)))
126