;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
;;;; 
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;;; 

(define-module (rdf xsd)
  #:use-module (ice-9 match)
  #:use-module (rdf rdf)
  #:export ())

;; This module implements the xsd datatypes, as presented in https://www.w3.org/TR/rdf11-concepts/#xsd-datatypes

(define (make-xsd-datatype iri description lexical? value? lexical->value
                           value->lexical)
  (make-rdf-datatype
    (list (string-append "http://www.w3.org/2001/XMLSchema#" iri))
    description
    lexical?
    value?
    lexical->value
    value->lexical))

(define-public string
  (make-xsd-datatype
    "string"
    "Character strings (but not all Unicode character strings)"
    string?
    string?
    identity
    identity))

(define-public boolean
  (make-xsd-datatype
    "boolean"
    "true, false"
    (lambda (lexical)
      (member lexical '("true" "false" "0" "1")))
    boolean?
    (lambda (lexical)
      (match lexical
        ("true" #t)
        ("1" #t)
        ("false" #f)
        ("0" #f)))
    (lambda (value)
      (match value
        (#t "true")
        (#f "false")))))

(define-public decimal
  (make-xsd-datatype
    "decimal"
    "Arbitrary-precision decimal numbers"
    string->number
    number?
    string->number
    number->string))

(define-public integer
  (make-xsd-datatype
    "integer"
    "Arbitrary-size integer numbers"
    (lambda (lexical)
      (integer? (string->number lexical)))
    integer?
    string->number
    number->string))