<commit_before>{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
{-# LANGUAGE TypeSynonymInstances, UndecidableInstances, OverlappingInstances #-}
{-# LANGUAGE PatternGuards  #-}
{-# LANGUAGE TypeFamilies  #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TemplateHaskell #-}

module Narradar.Types.PrologIdentifiers where

import Control.Applicative
import Control.Parallel.Strategies
import Data.AlaCarte (Expr)
import Data.Foldable(Foldable(..), toList)
import Data.Traversable as T (Traversable(..), mapM)
import Data.DeriveTH
import Data.Derive.Foldable
import Data.Derive.Functor
import Data.Derive.Traversable
import Data.Maybe
import Data.Monoid
import Text.PrettyPrint

import Narradar.Types.DPIdentifiers
import Data.Term.Ppr

-- -------------------
-- Prolog Identifiers
-- -------------------
data PrologId a = InId a | OutId a | UId Int | FunctorId a deriving (Eq,Ord)

class Ord (WithoutPrologId id) => RemovePrologId id where
  type WithoutPrologId id :: *
  removePrologId :: id -> Maybe (WithoutPrologId id)

instance Ord a => RemovePrologId (PrologId a) where
  type WithoutPrologId (PrologId a) = a
  removePrologId (InId x)      = Just x
  removePrologId (OutId x)     = Just x
  removePrologId (FunctorId x) = Just x
  removePrologId (UId _      ) = Nothing

instance (RemovePrologId a) => RemovePrologId (Identifier a) where
  type WithoutPrologId (Identifier a) = Identifier (WithoutPrologId a)
  removePrologId = T.mapM removePrologId

instance RemovePrologId String where
  type WithoutPrologId String = String
  removePrologId = Just

instance Ord (Expr p) => RemovePrologId (Expr p) where
  type WithoutPrologId (Expr p) = Expr p
  removePrologId = Just

class IsPrologId id where
    isInId      :: id -> Bool
    isOutId     :: id -> Bool
    isFunctorId :: id -> Bool
    getUId      :: id -> Maybe Int
    isUId       :: id -> Bool
    isUId       = isJust . getUId

instance IsPrologId (PrologId a) where
    isOutId OutId{} = True; isOutId  _ = False
    isInId InId{}   = True; isInId   _ = False
    getUId (UId i)  = Just i; getUId _ = Nothing
    isFunctorId FunctorId{} = True; isFunctorId _ = False

instance (Foldable l, IsPrologId a) => IsPrologId (l a) where
    isInId      = getAny . foldMap (Any . isInId)
    isOutId     = getAny . foldMap (Any . isOutId)
    getUId x | [x'] <- toList x = getUId x'; getUId _ = Nothing<commit_msg>PrologId.mapUId<commit_after>{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
{-# LANGUAGE TypeSynonymInstances, UndecidableInstances, OverlappingInstances #-}
{-# LANGUAGE PatternGuards  #-}
{-# LANGUAGE TypeFamilies  #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TemplateHaskell #-}

module Narradar.Types.PrologIdentifiers where

import Control.Applicative
import Control.Parallel.Strategies
import Data.AlaCarte (Expr)
import Data.Foldable(Foldable(..), toList)
import Data.Traversable as T (Traversable(..), mapM)
import Data.DeriveTH
import Data.Derive.Foldable
import Data.Derive.Functor
import Data.Derive.Traversable
import Data.Maybe
import Data.Monoid
import Text.PrettyPrint

import Narradar.Types.DPIdentifiers
import Data.Term.Ppr

-- -------------------
-- Prolog Identifiers
-- -------------------
data PrologId a = InId a | OutId a | UId Int | FunctorId a deriving (Eq,Ord)

class Ord (WithoutPrologId id) => RemovePrologId id where
  type WithoutPrologId id :: *
  removePrologId :: id -> Maybe (WithoutPrologId id)

instance Ord a => RemovePrologId (PrologId a) where
  type WithoutPrologId (PrologId a) = a
  removePrologId (InId x)      = Just x
  removePrologId (OutId x)     = Just x
  removePrologId (FunctorId x) = Just x
  removePrologId (UId _      ) = Nothing

instance (RemovePrologId a) => RemovePrologId (Identifier a) where
  type WithoutPrologId (Identifier a) = Identifier (WithoutPrologId a)
  removePrologId = T.mapM removePrologId

instance RemovePrologId String where
  type WithoutPrologId String = String
  removePrologId = Just

instance Ord (Expr p) => RemovePrologId (Expr p) where
  type WithoutPrologId (Expr p) = Expr p
  removePrologId = Just

class IsPrologId id where
    isInId      :: id -> Bool
    isOutId     :: id -> Bool
    isFunctorId :: id -> Bool
    mapUId      :: (Int -> Int) -> id -> Maybe id
    getUId      :: id -> Maybe Int
    isUId       :: id -> Bool
    isUId       = isJust . getUId

setUId i = mapUId (const i)

instance IsPrologId (PrologId a) where
    isOutId OutId{} = True; isOutId  _ = False
    isInId InId{}   = True; isInId   _ = False
    getUId   (UId i) = Just i         ; getUId   _ = Nothing
    mapUId f (UId i) = Just(UId $ f i); mapUId _ _ = Nothing
    isFunctorId FunctorId{} = True; isFunctorId _ = False

instance (Traversable l, IsPrologId a) => IsPrologId (l a) where
    isInId      = getAny . foldMap (Any . isInId)
    isOutId     = getAny . foldMap (Any . isOutId)
    getUId   x | [x'] <- toList x = getUId   x'; getUId   _ = Nothing
    mapUId f x = mapUId f `T.mapM` x; mapUId _ _ = Nothing
{'fraction_non_alphanumeric': 0.11115429459774583, 'fraction_numerical': 0.0, 'mean_word_length': 3.236213991769547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '4878293', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1786, 'n_words': 506}