<commit_before>{-# LANGUAGE FlexibleInstances #-}
module Grammar where
import PGF
import PGF.Internal
import Data.Maybe

-- first CId is the the result category and [CId] are the parameter categories
data FunType = Fun CId [CId]

-- CId is the function name
data Rule = Function CId (Maybe FunType)

data Grammar = Grammar {
  startcat :: CId,
  rules :: [Rule]
  }

instance Show Grammar where
  show (Grammar startcat rules) =
    "Startcat: " ++ show startcat ++ "\nRules:\n " ++
    (unwords $ map show rules)
    
instance Show FunType where
  show (Fun cat []) =
    show cat
  show (Fun cat cats) =
    foldl (\a -> \b -> a ++ " -> " ++ (show b)) (show $ head cats) (tail cats) ++ " -> " ++ show cat

instance Show Rule where
  show (Function name funtype) =
    show name ++ " : " ++ show funtype ++ "\n" ;
    
-- instance Show Grammar where
--   show g = unwords $ map show g
       
getFunType :: PGF -> CId -> Maybe FunType
getFunType grammar id =
  let
    typ = functionType grammar id
  in
    do
      case typ of {
        (Just t) -> let
                (hypos,typeid,exprs) = unType $ fromJust typ
                cats = (map (\(_,_,DTyp _ cat _) -> cat) hypos)
                in
                  Just (Fun typeid cats) ;
        _ -> Nothing
        } ;

pgfToGrammar :: PGF -> Grammar
pgfToGrammar pgf =
  let<commit_msg>instance of Eq for FunTypes and new show for Rules<commit_after>{-# LANGUAGE FlexibleInstances #-}
module Grammar where
import PGF
import PGF.Internal
import Data.Maybe

-- first CId is the the result category and [CId] are the parameter categories
data FunType = Fun CId [CId]

-- CId is the function name
data Rule = Function CId (Maybe FunType)

data Grammar = Grammar {
  startcat :: CId,
  rules :: [Rule]
  }

instance Show Grammar where
  show (Grammar startcat rules) =
    "Startcat: " ++ show startcat ++ "\nRules:\n " ++
    (unwords $ map show rules)
    
instance Show FunType where
  show (Fun cat []) =
    show cat
  show (Fun cat cats) =
    foldl (\a -> \b -> a ++ " -> " ++ (show b)) (show $ head cats) (tail cats) ++ " -> " ++ show cat
instance Eq FunType where
  (==) (Fun id1 pre1) (Fun id2 pre2) = (id1 == id2) && (pre1 == pre2) 
instance Show Rule where
  show (Function name funtype) =
    show name ++ " : " ++ show funtype ;
    
-- instance Show Grammar where
--   show g = unwords $ map show g
       
getFunType :: PGF -> CId -> Maybe FunType
getFunType grammar id =
  let
    typ = functionType grammar id
  in
    do
      case typ of {
        (Just t) -> let
                (hypos,typeid,exprs) = unType $ fromJust typ
                cats = (map (\(_,_,DTyp _ cat _) -> cat) hypos)
                in
                  Just (Fun typeid cats) ;
        _ -> Nothing
        } ;

pgfToGrammar :: PGF -> Grammar
pgfToGrammar pgf =
  let
{'fraction_non_alphanumeric': 0.11660149306789903, 'fraction_numerical': 0.0028439388553146107, 'mean_word_length': 2.2494226327944573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1612678', 'n_tokens_mistral': 926, 'n_tokens_neox': 915, 'n_words': 333}