<commit_before>module Lambda.Language.PrettyPrint
  ( Rep(..)
  , ifSugar
  , selectRep
  , toggleRep
  , Doc
  , doc
  , sugar
  , raw
  , class PrettyPrint
  , prettyPrint
  , parensIf
  ) where

import Lambda.Prelude

-- | Either syntactic sugar or raw text.
data Rep
  = Sugar
  | Raw

-- | Project the appropriate `Doc` representation.
selectRep :: forall a. Doc a -> Rep -> a
selectRep (Doc s r) = ifSugar s r

-- | Flip `Raw` to `Sugar` and vice-versa.
toggleRep :: Rep -> Rep
toggleRep = ifSugar Raw Sugar

-- | Select the first argument given `Sugar`.
ifSugar :: forall a. a -> a -> Rep -> a
ifSugar s _ Sugar = s
ifSugar _ r Raw = r

-- | Document containing two representations of the same value.
data Doc a = Doc a a

-- | Project both representations out of `Doc`.
runDoc :: forall a. Doc a -> {sugar :: a, raw :: a}
runDoc (Doc s r) = {sugar: s, raw: r}

-- | Smart constructor for `Doc`.
doc :: forall a. {sugar :: a, raw :: a} -> Doc a
doc o = Doc o.sugar o.raw

-- | Project sugared representation out of `Doc`.
sugar :: forall a. Doc a -> a
sugar (Doc s _) = s
<commit_msg>Add withRep (flipped version of selectRep)<commit_after>module Lambda.Language.PrettyPrint
  ( Rep(..)
  , ifSugar
  , selectRep
  , withRep
  , toggleRep
  , Doc
  , doc
  , sugar
  , raw
  , class PrettyPrint
  , prettyPrint
  , parensIf
  ) where

import Lambda.Prelude

-- | Either syntactic sugar or raw text.
data Rep
  = Sugar
  | Raw

-- | Project the appropriate `Doc` representation.
selectRep :: forall a. Doc a -> Rep -> a
selectRep (Doc s r) = ifSugar s r

-- | Flipped version of 'selectRep'
withRep :: forall a. Rep -> Doc a -> a
withRep = flip selectRep

-- | Flip `Raw` to `Sugar` and vice-versa.
toggleRep :: Rep -> Rep
toggleRep = ifSugar Raw Sugar

-- | Select the first argument given `Sugar`.
ifSugar :: forall a. a -> a -> Rep -> a
ifSugar s _ Sugar = s
ifSugar _ r Raw = r

-- | Document containing two representations of the same value.
data Doc a = Doc a a

-- | Project both representations out of `Doc`.
runDoc :: forall a. Doc a -> {sugar :: a, raw :: a}
runDoc (Doc s r) = {sugar: s, raw: r}

-- | Smart constructor for `Doc`.
doc :: forall a. {sugar :: a, raw :: a} -> Doc a
doc o = Doc o.sugar o.raw

-- | Project sugared representation out of `Doc`.
sugar :: forall a. Doc a -> a
sugar (Doc s _) = s

{'fraction_non_alphanumeric': 0.12822719449225473, 'fraction_numerical': 0.0, 'mean_word_length': 2.7259615384615383, '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': '2998919', 'n_tokens_mistral': 815, 'n_tokens_neox': 813, 'n_words': 322}