<commit_before>{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
-- | Futhark prettyprinter.  This module defines 'Pretty' instances for the
-- AST defined in "Language.Futhark.Syntax", but also a number of
-- convenience functions if you don't want to use the interface from
-- 'Pretty'.
module Language.Futhark.Pretty
  ( ppType
  , ppValue
  , ppUnOp
  , ppBinOp
  , ppExp
  , ppLambda
  , prettyPrint
  )
  where

import Data.Array
import Data.Hashable
import qualified Data.HashSet as HS

import Text.PrettyPrint.Mainland

import Language.Futhark.Syntax
import Language.Futhark.Attributes

-- | The document @'apply' ds@ separates @ds@ with commas and encloses them with
-- parentheses.
apply :: [Doc] -> Doc
apply = parens . commasep . map align

commastack :: [Doc] -> Doc
commastack = align . stack . punctuate comma

aliasComment :: (Eq vn, Hashable vn, Pretty vn, TypeBox ty) => PatternBase ty vn -> Doc -> Doc
aliasComment pat d = case aliasComment' pat of
                       []   -> d
                       l:ls -> foldl (</>) l ls </> d
  where aliasComment' Wildcard{} = []
        aliasComment' (TuplePattern pats _) = concatMap aliasComment' pats
        aliasComment' (Id ident) =
          case maybe [] (clean . HS.toList . aliases)
                 $ unboxType $ identType ident of
            [] -> []
            als -> [oneline $
                    text "// " <> ppr ident <> text " aliases " <>
                    commasep (map ppr als)]
          where clean = filter (/= identName ident)
                oneline s = text $ displayS (renderCompact s) ""

instance Pretty Value where
  ppr (PrimValue bv) = ppr bv
  ppr (TupValue vs)
    | any (not . primType . valueType) vs =
      braces $ commastack $ map ppr vs
    | otherwise =
      braces $ commasep $ map ppr vs
  ppr v@(ArrayValue a t)
    | Just s <- arrayString v = text $ show s
    | [] <- elems a = text "empty" <> parens (ppr t)
    | Array{} <- t = brackets $ commastack $ map ppr $ elems a
    | otherwise     = brackets $ commasep $ map ppr $ elems a

instance Pretty PrimType where
  ppr (Unsigned Int8) = text "u8"
  ppr (Unsigned Int16) = text "u16"
  ppr (Unsigned Int32) = text "u32"
  ppr (Unsigned Int64) = text "u64"
  ppr (Signed t) = ppr t
  ppr (FloatType t) = ppr t
  ppr Char = text"char"
  ppr Bool = text "bool"

instance Pretty PrimValue where
  ppr (UnsignedValue (Int8Value v)) = text (show v) <> text "u8"
  ppr (UnsignedValue (Int16Value v)) = text (show v) <> text "u16"
  ppr (UnsignedValue (Int32Value v)) = text (show v) <> text "u32"
  ppr (UnsignedValue (Int64Value v)) = text (show v) <> text "u64"
  ppr (SignedValue v) = ppr v
  ppr (CharValue c) = text $ show c
  ppr (BoolValue b) = text $ show b
  ppr (FloatValue v) = ppr v

instance Pretty Uniqueness where
  ppr Unique    = star
  ppr Nonunique = empty

instance (Eq vn, Hashable vn, Pretty vn) =>
         Pretty (TupleArrayElemTypeBase ShapeDecl as vn) where
  ppr (PrimArrayElem bt _) = ppr bt
  ppr (ArrayArrayElem at)   = ppr at
  ppr (TupleArrayElem ts)   = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) =>
         Pretty (TupleArrayElemTypeBase Rank as vn) where
  ppr (PrimArrayElem bt _) = ppr bt
  ppr (ArrayArrayElem at)   = ppr at
  ppr (TupleArrayElem ts)   = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) =>
         Pretty (ArrayTypeBase ShapeDecl as vn) where
  ppr (PrimArray et (ShapeDecl ds) u _) =
    ppr u <> foldl f (ppr et) ds
    where f s AnyDim       = brackets s
          f s (NamedDim v) = brackets $ s <> comma <> ppr v
          f s (ConstDim n) = brackets $ s <> comma <> ppr n

  ppr (TupleArray et (ShapeDecl ds) u) =
    ppr u <> foldl f (braces $ commasep $ map ppr et) ds
    where f s AnyDim       = brackets s
          f s (NamedDim v) = brackets $ s <> comma <> ppr v
          f s (ConstDim n) = brackets $ s <> comma <> ppr n

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (ArrayTypeBase Rank as vn) where
  ppr (PrimArray et (Rank n) u _) =
    ppr u <> foldl (.) id (replicate n brackets) (ppr et)
  ppr (TupleArray ts (Rank n) u) =
    ppr u <> foldl (.) id (replicate n brackets)
    (braces $ commasep $ map ppr ts)

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (TypeBase ShapeDecl as vn) where
  ppr (Prim et) = ppr et
  ppr (Array at) = ppr at
  ppr (Tuple ts) = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (TypeBase Rank as vn) where
  ppr (Prim et) = ppr et
  ppr (Array at) = ppr at
  ppr (Tuple ts) = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (IdentBase ty vn) where
  ppr = ppr . identName

instance Pretty UnOp where
  ppr Not = text "!"
  ppr Negate = text "-"
  ppr Complement = text "~"
  ppr Abs = text "abs "
  ppr Signum = text "signum "
  ppr (ToFloat t) = ppr t
  ppr (ToSigned t) = ppr (Unsigned t)
  ppr (ToUnsigned t) = ppr (Signed t)

instance Pretty BinOp where
  ppr Plus = text "+"
  ppr Minus = text "-"
  ppr Pow = text "**"<commit_msg>Fix ToSigned/ToUnsigned prettyprinting.<commit_after>{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
-- | Futhark prettyprinter.  This module defines 'Pretty' instances for the
-- AST defined in "Language.Futhark.Syntax", but also a number of
-- convenience functions if you don't want to use the interface from
-- 'Pretty'.
module Language.Futhark.Pretty
  ( ppType
  , ppValue
  , ppUnOp
  , ppBinOp
  , ppExp
  , ppLambda
  , prettyPrint
  )
  where

import Data.Array
import Data.Hashable
import qualified Data.HashSet as HS

import Text.PrettyPrint.Mainland

import Language.Futhark.Syntax
import Language.Futhark.Attributes

-- | The document @'apply' ds@ separates @ds@ with commas and encloses them with
-- parentheses.
apply :: [Doc] -> Doc
apply = parens . commasep . map align

commastack :: [Doc] -> Doc
commastack = align . stack . punctuate comma

aliasComment :: (Eq vn, Hashable vn, Pretty vn, TypeBox ty) => PatternBase ty vn -> Doc -> Doc
aliasComment pat d = case aliasComment' pat of
                       []   -> d
                       l:ls -> foldl (</>) l ls </> d
  where aliasComment' Wildcard{} = []
        aliasComment' (TuplePattern pats _) = concatMap aliasComment' pats
        aliasComment' (Id ident) =
          case maybe [] (clean . HS.toList . aliases)
                 $ unboxType $ identType ident of
            [] -> []
            als -> [oneline $
                    text "// " <> ppr ident <> text " aliases " <>
                    commasep (map ppr als)]
          where clean = filter (/= identName ident)
                oneline s = text $ displayS (renderCompact s) ""

instance Pretty Value where
  ppr (PrimValue bv) = ppr bv
  ppr (TupValue vs)
    | any (not . primType . valueType) vs =
      braces $ commastack $ map ppr vs
    | otherwise =
      braces $ commasep $ map ppr vs
  ppr v@(ArrayValue a t)
    | Just s <- arrayString v = text $ show s
    | [] <- elems a = text "empty" <> parens (ppr t)
    | Array{} <- t = brackets $ commastack $ map ppr $ elems a
    | otherwise     = brackets $ commasep $ map ppr $ elems a

instance Pretty PrimType where
  ppr (Unsigned Int8) = text "u8"
  ppr (Unsigned Int16) = text "u16"
  ppr (Unsigned Int32) = text "u32"
  ppr (Unsigned Int64) = text "u64"
  ppr (Signed t) = ppr t
  ppr (FloatType t) = ppr t
  ppr Char = text"char"
  ppr Bool = text "bool"

instance Pretty PrimValue where
  ppr (UnsignedValue (Int8Value v)) = text (show v) <> text "u8"
  ppr (UnsignedValue (Int16Value v)) = text (show v) <> text "u16"
  ppr (UnsignedValue (Int32Value v)) = text (show v) <> text "u32"
  ppr (UnsignedValue (Int64Value v)) = text (show v) <> text "u64"
  ppr (SignedValue v) = ppr v
  ppr (CharValue c) = text $ show c
  ppr (BoolValue b) = text $ show b
  ppr (FloatValue v) = ppr v

instance Pretty Uniqueness where
  ppr Unique    = star
  ppr Nonunique = empty

instance (Eq vn, Hashable vn, Pretty vn) =>
         Pretty (TupleArrayElemTypeBase ShapeDecl as vn) where
  ppr (PrimArrayElem bt _) = ppr bt
  ppr (ArrayArrayElem at)   = ppr at
  ppr (TupleArrayElem ts)   = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) =>
         Pretty (TupleArrayElemTypeBase Rank as vn) where
  ppr (PrimArrayElem bt _) = ppr bt
  ppr (ArrayArrayElem at)   = ppr at
  ppr (TupleArrayElem ts)   = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) =>
         Pretty (ArrayTypeBase ShapeDecl as vn) where
  ppr (PrimArray et (ShapeDecl ds) u _) =
    ppr u <> foldl f (ppr et) ds
    where f s AnyDim       = brackets s
          f s (NamedDim v) = brackets $ s <> comma <> ppr v
          f s (ConstDim n) = brackets $ s <> comma <> ppr n

  ppr (TupleArray et (ShapeDecl ds) u) =
    ppr u <> foldl f (braces $ commasep $ map ppr et) ds
    where f s AnyDim       = brackets s
          f s (NamedDim v) = brackets $ s <> comma <> ppr v
          f s (ConstDim n) = brackets $ s <> comma <> ppr n

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (ArrayTypeBase Rank as vn) where
  ppr (PrimArray et (Rank n) u _) =
    ppr u <> foldl (.) id (replicate n brackets) (ppr et)
  ppr (TupleArray ts (Rank n) u) =
    ppr u <> foldl (.) id (replicate n brackets)
    (braces $ commasep $ map ppr ts)

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (TypeBase ShapeDecl as vn) where
  ppr (Prim et) = ppr et
  ppr (Array at) = ppr at
  ppr (Tuple ts) = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (TypeBase Rank as vn) where
  ppr (Prim et) = ppr et
  ppr (Array at) = ppr at
  ppr (Tuple ts) = braces $ commasep $ map ppr ts

instance (Eq vn, Hashable vn, Pretty vn) => Pretty (IdentBase ty vn) where
  ppr = ppr . identName

instance Pretty UnOp where
  ppr Not = text "!"
  ppr Negate = text "-"
  ppr Complement = text "~"
  ppr Abs = text "abs "
  ppr Signum = text "signum "
  ppr (ToFloat t) = ppr t
  ppr (ToSigned t) = ppr (Signed t)
  ppr (ToUnsigned t) = ppr (Unsigned t)

instance Pretty BinOp where
  ppr Plus = text "+"
  ppr Minus = text "-"
  ppr Pow = text "**"
{'fraction_non_alphanumeric': 0.10851648351648352, 'fraction_numerical': 0.005494505494505495, 'mean_word_length': 2.6158212131961687, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 79, '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': '6019981', 'n_tokens_mistral': 3721, 'n_tokens_neox': 3540, 'n_words': 1322}