<commit_before>module RDF (Graph(empty, mkGraph, triplesOf, select, query),
            Triple, triple,
            Node(UNode, BNode, LNode),
            LValue(PlainL, TypedL),
            Selector, isUNode, isBNode, isLNode,
              hasSubject, hasPredicate, hasObject,
            subjectOf, predicateOf, objectOf, distinct,
            Subject, Predicate, Object,
            printT, printTs, printN, printNs)
where

import Namespace()

import qualified Data.Set as Set
import Text.Printf

-- |An alias for Node, for readability purposes.
type Subject = Node

-- |An alias for Node, for readability purposes.
type Predicate = Node

-- |An alias for Node, for readability purposes.
type Object = Node


class Graph gr where
  -- |Answer an empty graph.
  empty  :: gr
  -- |Answer a graph containing all the given triples.
  mkGraph :: [Triple] -> gr
  -- |Answer a list of all triples in the graph.
  triplesOf :: gr -> [Triple]
  -- |Select the triples in graph that match the selector.
  -- This function is a convenience function for interactive
  -- querying of smallish (<= 10,000 triples) graphs, as it
  -- must check all triples in the graph.
  select        :: Selector -> gr -> [Triple]
  query         :: gr -> Maybe Node -> Maybe Node -> Maybe Node -> [Triple]

-- |An RDF node, which may be either a URIRef node (UNode), a blank 
-- node (BNode), or a literal node (LNode).
data Node =  UNode String               -- a uri ref node
           | BNode String               -- a blank node
           | LNode LValue               -- a literal node
  deriving (Eq, Ord)


-- |An RDF statement is a Triple consisting of Subject, Predicate,
--  and Object nodes.
data Triple = Triple Subject Predicate Object
  deriving (Eq)

-- TODO: check spec for equality and comparison of literals
-- |A literal value, which may be a plain literal, optionally with
-- a language specifier, or a typed literal consisting of the value
-- and the URI of the datatype, respectively.
data LValue = PlainL String (Maybe String) -- a plain literal w/ opt lang
            | TypedL String String         -- a typed literal w/ type uri
  deriving (Eq, Ord)

-- |Create a triple with the given subject, predicate, and object.
triple :: Subject -> Predicate -> Object -> Triple
-- subject must be U or B
triple (LNode _) _         _    = error "subject cannot be LNode"
-- predicate must be U
triple _         (LNode _) _    = error "predicate cannot be LNode"
triple _         (BNode _) _    = error "predicate cannot be BNode"
-- no other constraints
triple subj      pred      obj  = Triple subj pred obj


-- String representations

instance Show Triple where
  show (Triple subj pred obj) = 
    printf "%s %s %s ." (show subj) (show pred) (show obj)

instance Show Node where
  show (UNode uri)                      = printf "<%s>" uri
  show (BNode  id)                      = show id
  show (LNode (PlainL lit Nothing))     = printf "\"%s\"" lit
  show (LNode (PlainL lit (Just lang))) = printf "\"%s\"@\"%s\"" lit lang<commit_msg>RDF.hs: changed Triple to be an Ord and added alias for list of triples.<commit_after>module RDF (Graph(empty, mkGraph, triplesOf, select, query),
            Triple, triple, Triples,
            Node(UNode, BNode, LNode),
            LValue(PlainL, TypedL),
            Selector, isUNode, isBNode, isLNode,
              hasSubject, hasPredicate, hasObject,
            subjectOf, predicateOf, objectOf, distinct,
            Subject, Predicate, Object,
            printT, printTs, printN, printNs)
where

import Namespace()

import qualified Data.Set as Set
import Text.Printf

-- |An alias for Node, for readability purposes.
type Subject = Node

-- |An alias for Node, for readability purposes.
type Predicate = Node

-- |An alias for Node, for readability purposes.
type Object = Node


class Graph gr where
  -- |Answer an empty graph.
  empty  :: gr
  -- |Answer a graph containing all the given triples.
  mkGraph :: [Triple] -> gr
  -- |Answer a list of all triples in the graph.
  triplesOf :: gr -> [Triple]
  -- |Select the triples in graph that match the selector.
  -- This function is a convenience function for interactive
  -- querying of smallish (<= 10,000 triples) graphs, as it
  -- must check all triples in the graph.
  select        :: Selector -> gr -> [Triple]
  query         :: gr -> Maybe Node -> Maybe Node -> Maybe Node -> [Triple]

-- |An RDF node, which may be either a URIRef node (UNode), a blank 
-- node (BNode), or a literal node (LNode).
data Node =  UNode String               -- a uri ref node
           | BNode String               -- a blank node
           | LNode LValue               -- a literal node
  deriving (Eq, Ord)


-- |An RDF statement is a Triple consisting of Subject, Predicate,
--  and Object nodes.
data Triple = Triple Subject Predicate Object
  deriving (Eq, Ord)

-- |A convenience definition for a list of triple.
type Triples = [Triple]

-- TODO: check spec for equality and comparison of literals
-- |A literal value, which may be a plain literal, optionally with
-- a language specifier, or a typed literal consisting of the value
-- and the URI of the datatype, respectively.
data LValue = PlainL String (Maybe String) -- a plain literal w/ opt lang
            | TypedL String String         -- a typed literal w/ type uri
  deriving (Eq, Ord)

-- |Create a triple with the given subject, predicate, and object.
triple :: Subject -> Predicate -> Object -> Triple
-- subject must be U or B
triple (LNode _) _         _    = error "subject cannot be LNode"
-- predicate must be U
triple _         (LNode _) _    = error "predicate cannot be LNode"
triple _         (BNode _) _    = error "predicate cannot be BNode"
-- no other constraints
triple subj      pred      obj  = Triple subj pred obj


-- String representations

instance Show Triple where
  show (Triple subj pred obj) = 
    printf "%s %s %s ." (show subj) (show pred) (show obj)

instance Show Node where
  show (UNode uri)                      = printf "<%s>" uri
  show (BNode  id)                      = show id
  show (LNode (PlainL lit Nothing))     = printf "\"%s\"" lit
  show (LNode (PlainL lit (Just lang))) = printf "\"%s\"@\"%s\"" lit lang
{'fraction_non_alphanumeric': 0.09422736774401029, 'fraction_numerical': 0.0016079755587715067, 'mean_word_length': 2.385955362003266, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4830315', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1771, 'n_words': 801}