Add type recognition procedure

Julien LepillerSat Apr 04 00:46:58+0200 2020

44483a3

Add type recognition procedure

rdf/rdf.scm

2929
            rdf-datatype-lexical->value
3030
            rdf-datatype-value->lexical
3131
32+
            rdf:langString
33+
3234
            rdf-dataset
3335
            make-rdf-dataset
3436
            rdf-dataset?

5355
            rdf-graph?
5456
5557
            merge-graphs
56-
            rdf-isomorphic?))
58+
            rdf-isomorphic?
59+
            recognize))
5760
5861
;; From the specification:
5962
;;   Datatypes are used with RDF literals to represent values such as strings,

9093
  (lexical->value rdf-datatype-lexical->value)
9194
  (value->lexical rdf-datatype-value->lexical))
9295
96+
(define rdf:langString
97+
  (make-rdf-datatype
98+
    '("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString")
99+
    "A literal is a language-tagged string if the third element is present.
100+
Lexical representations of language tags MAY be converted to lower case.  The
101+
value space of language tags is always in lower case."
102+
    string?
103+
    string?
104+
    string-downcase
105+
    identity))
106+
93107
;; From the specification:
94108
;;   An RDF dataset is a collection of RDF graphs, and comprises:
95109
;;

134148
;;     be well-formed according to section 2.2.9 of [BCP47].
135149
136150
(define-record-type rdf-literal
137-
  (make-rdf-literal lexical-form datatype language-tag)
151+
  (make-rdf-literal lexical-form type language-tag)
138152
  rdf-literal?
139153
  (lexical-form rdf-literal-lexical-form)
140-
  (datatype     rdf-literal-datatype)
154+
  (type         rdf-literal-type)
141155
  (language-tag rdf-literal-language-tag))
142156
143157
;; From the specification:

157171
(define (rdf-graph? graph)
158172
  (and (list? graph) (null? (filter (lambda (t) (not (rdf-triple? t))) graph))))
159173
174+
;; The following is for a merging procedure, where we rename blank nodes to ensure
175+
;; we are not merging blank nodes that have the same name
176+
160177
(define (last-blank g)
161178
  "Retun the biggest blank node identifier in g"
162179
  (let loop ((g g) (m 0))

373390
                  (validate-mapping (reverse-mapping mapping) g2 g1))
374391
           #t
375392
           (loop disjunctions)))))))
393+
394+
;; Recognizing datatypes is a transformation on the graph to add the proper
395+
;; datatype to literals, and replace IRIs that represent a datatype with the
396+
;; datatype it represents.  This is useful for some entailment regimes, such
397+
;; as the RDF or RDFS entailment regimes.
398+
399+
(define (recognize-data d datatypes)
400+
  (match d
401+
    ((? string? iri) 
402+
     (let loop ((datatypes datatypes))
403+
       (if (null? datatypes)
404+
           iri
405+
           (if (member iri (rdf-datatype-iris (car datatypes)))
406+
               (car datatypes)
407+
               (loop (cdr datatypes))))))
408+
    (($ rdf-literal literal-form type langtag)
409+
     (let loop ((datatypes datatypes))
410+
       (if (null? datatypes)
411+
           (make-rdf-literal literal-form type langtag)
412+
           (if (member type (rdf-datatype-iris (car datatypes)))
413+
               (make-rdf-literal literal-form (car datatypes) langtag)
414+
               (loop (cdr datatypes))))))
415+
    (_ d)))
416+
417+
(define (recognize-triple t datatypes)
418+
  (match t
419+
    (($ rdf-triple subject predicate object)
420+
     (make-rdf-triple
421+
       (recognize-data subject datatypes)
422+
       predicate
423+
       (recognize-data object datatypes)))))
424+
425+
(define (recognize graph datatypes)
426+
  (match graph
427+
    (() '())
428+
    ((t graph ...)
429+
     (cons
430+
       (recognize-triple t datatypes)
431+
       (recognize graph datatypes)))))
432+