<commit_before>
module Ast where

import Data.Char (isDigit)
import Data.List (intercalate)
import Types
import Guid

data Module = Module [String] Exports Imports [Definition] JSFFI

type Exports = [String]

type Imports = [(String, ImportMethod)]
data ImportMethod = As String | Hiding [String] | Importing [String]

type JSFFI = ( [(String, Expr, String, Type)]
             , [(String, String, Type)] )


data Pattern = PData String [Pattern] | PVar String | PAnything
               deriving (Eq)

data Expr = IntNum Int
          | FloatNum Float
          | Chr Char
          | Str String
          | Boolean Bool
          | Range Expr Expr
          | Access Expr String
          | Binop String Expr Expr
          | Lambda String Expr
          | App Expr Expr
          | If Expr Expr Expr
          | Lift Expr [Expr]
          | Fold Expr Expr Expr
          | Async Expr
          | Input String
          | Let [Definition] Expr
          | Var String
          | Case Expr [(Pattern,Expr)]
          | Data String [Expr]
            deriving (Show, Eq)

data Definition = Definition String [String] Expr
                  deriving (Show, Eq)

cons h t = Data "Cons" [h,t]
nil      = Data "Nil" []
list     = foldr cons nil
tuple es = Data ("Tuple" ++ show (length es)) es

delist (Data "Cons" [h,t]) = h : delist t
delist _ = []


pcons h t = PData "Cons" [h,t]
pnil      = PData "Nil" []
plist     = foldr pcons pnil
ptuple es = PData ("Tuple" ++ show (length es)) es

instance Show Pattern where
    show (PVar x)  = x
    show PAnything = "_"
    show (PData "Cons" [hd@(PData "Cons" _),tl]) =
        parens (show hd) ++ " : " ++ show tl
            where parens s = "(" ++ s ++ ")"
    show (PData "Cons" [hd,tl]) = show hd ++ " : " ++ show tl
    show (PData "Nil" []) = "[]"
    show (PData name ps) =
        if take 5 name == "Tuple" && all isDigit (drop 5 name) then
            parens . intercalate ", " $ map show ps
        else (if null ps then id else parens) $ unwords (name : map show ps)
            where parens s = "(" ++ s ++ ")"<commit_msg>Add custom Show instance for Expr datatype.<commit_after>
module Ast where

import Data.Char (isDigit)
import Data.List (intercalate)
import Types
import Guid

data Module = Module [String] Exports Imports [Definition] JSFFI

type Exports = [String]

type Imports = [(String, ImportMethod)]
data ImportMethod = As String | Hiding [String] | Importing [String]

type JSFFI = ( [(String, Expr, String, Type)]
             , [(String, String, Type)] )


data Pattern = PData String [Pattern] | PVar String | PAnything
               deriving (Eq)

data Expr = IntNum Int
          | FloatNum Float
          | Chr Char
          | Str String
          | Boolean Bool
          | Range Expr Expr
          | Access Expr String
          | Binop String Expr Expr
          | Lambda String Expr
          | App Expr Expr
          | If Expr Expr Expr
          | Lift Expr [Expr]
          | Fold Expr Expr Expr
          | Async Expr
          | Input String
          | Let [Definition] Expr
          | Var String
          | Case Expr [(Pattern,Expr)]
          | Data String [Expr]
            deriving (Eq)

data Definition = Definition String [String] Expr
                  deriving (Eq)

cons h t = Data "Cons" [h,t]
nil      = Data "Nil" []
list     = foldr cons nil
tuple es = Data ("Tuple" ++ show (length es)) es

delist (Data "Cons" [h,t]) = h : delist t
delist _ = []


pcons h t = PData "Cons" [h,t]
pnil      = PData "Nil" []
plist     = foldr pcons pnil
ptuple es = PData ("Tuple" ++ show (length es)) es

instance Show Pattern where
    show (PVar x)  = x
    show PAnything = "_"
    show (PData "Cons" [hd@(PData "Cons" _),tl]) =
        parens (show hd) ++ " : " ++ show tl
            where parens s = "(" ++ s ++ ")"
    show (PData "Cons" [hd,tl]) = show hd ++ " : " ++ show tl
    show (PData "Nil" []) = "[]"
    show (PData name ps) =
        if take 5 name == "Tuple" && all isDigit (drop 5 name) then
            parens . intercalate ", " $ map show ps
        else (if null ps then id else parens) $ unwords (name : map show ps)
            where parens s = "(" ++ s ++ ")"

instance Show Expr where
  show (IntNum n) = show n
  show (FloatNum n) = show n
  show (Chr c) = show c
  show (Str s) = show s
  show (Boolean b) = show b
  show (Range e1 e2) = "[" ++ show e1 ++ ".." ++ show e2 ++ "]"
  show (Access e x) = show' e ++ "." ++ x
  show (Binop op e1 e2) = show' e1 ++ " " ++ op ++ " " ++ show' e2
  show (Lambda x e) = let (xs,e') = getLambdas (Lambda x e) in
                      concat [ "\\", intercalate " " xs, " -> ", show e' ]
  show (App e1 e2) = show' e1 ++ " " ++ show' e2
  show (If e1 e2 e3) = concat [ "if ", show e1, " then ", show e2, " else ", show e3 ]
  show (Let defs e) = "let { " ++ intercalate " ; " (map show defs) ++ " } in " ++ show e
  show (Var x) = x
  show (Case e pats) = "case " ++ show e ++ " of { " ++ intercalate " ; " (map (\(p,e) -> show p ++ " -> " ++ show e) pats) ++ " }"
  show (Data name es)
      | name == "Cons" = ("["++) . (++"]") . intercalate "," . map show $ delist (Data "Cons" es)
      | name == "Nil" = "[]"
      | otherwise = name ++ " " ++ intercalate " " (map show' es)
  show (Lift f es) = concat [ "lift", show $ length es, " ", show' f, " ", intercalate " " (map show' es) ]
  show (Fold e1 e2 e3) = concat [ "foldp ", show' e1, " ", show' e2, " ", show' e3 ]
  show (Async e) = "async " ++ show' e
  show (Input i) = i

instance Show Definition where
  show (Definition v [] e) = v ++ " = " ++ show e
  show (Definition f args e) = f ++ " " ++ intercalate " " args ++ " = " ++ show e

getLambdas (Lambda x e) = (x:xs,e')
    where (xs,e') = getLambdas e
getLambdas e = ([],e)

show' e = if needsParens e then "(" ++ show e ++ ")" else show e

needsParens (Binop _ _ _) = True
needsParens (Lambda _ _) = True
needsParens (App _ _) = True
needsParens (If _ _ _) = True
needsParens (Let _ _) = True
needsParens (Case _ _) = True
needsParens (Data name (x:xs)) = name /= "Cons"
needsParens (Lift _ _) = True
needsParens (Fold _ _ _) = True
needsParens (Async _) = True
needsParens _ = False
{'fraction_non_alphanumeric': 0.14237012987012987, 'fraction_numerical': 0.004545454545454545, 'mean_word_length': 2.1806917914300463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '4461200', 'n_tokens_mistral': 2150, 'n_tokens_neox': 2098, 'n_words': 757}