<commit_before>{-# LANGUAGE OverloadedLists   #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}

module Stg.Prelude.Number (
    -- * Arithmetic
    add,
    sub,
    mul,
    div,
    mod,

    -- * Comparisons
    eq_Int,
    lt_Int,
    leq_Int,
    gt_Int,
    geq_Int,
    neq_Int,

    -- * Other
    min,
    max,
) where



import Prelude (Integer)

import Data.Monoid ((<>))
import Data.Text   (Text)

import Stg.Language
import Stg.Parser.QuasiQuoter

-- $setup
-- >>> :set -XOverloadedStrings
-- >>> :set -XQuasiQuotes
-- >>> :module +Stg.Language.Prettyprint



binaryOp :: Text -> PrimOp -> Alts -> Program
binaryOp name op primAlts =
    Program (Binds
        [(Var name, LambdaForm [] NoUpdate [Var "x", Var "y"]
            (Case (AppF (Var "x") []) (Alts (AlgebraicAlts
                [AlgebraicAlt (Constr "Int#") [Var "x'"]
                    (Case (AppF (Var "y") []) (Alts (AlgebraicAlts
                        [AlgebraicAlt (Constr "Int#") [Var "y'"]
                            (Case (AppP op (AtomVar (Var "x'")) (AtomVar (Var "y'")))
                                primAlts) ])
                        (DefaultBound (Var "err") (AppC (Constr ("Error_" <> name <> "_1")) [AtomVar (Var "err")])) ))])
                (DefaultBound (Var "err") (AppC (Constr ("Error_" <> name <> "_2")) [AtomVar (Var "err")])) )))])



primToBool :: Alts
primToBool = [alts| 1# -> True; default -> False |]

eq_Int, lt_Int, leq_Int, gt_Int, geq_Int, neq_Int :: Program

-- |
eq_Int  = binaryOp "eq_Int"  Eq  primToBool

-- |
lt_Int  = binaryOp "lt_Int"  Lt  primToBool

-- |
leq_Int = binaryOp "leq_Int" Leq primToBool

-- |
gt_Int  = binaryOp "gt_Int"  Gt  primToBool

-- |
geq_Int = binaryOp "geq_Int" Geq primToBool

-- |
neq_Int = binaryOp "neq_Int" Neq primToBool



primIdInt :: Alts
primIdInt = [alts| v -> Int# v |]

add, sub, mul, div, mod :: Program

-- |
add = binaryOp "add" Add primIdInt

-- |
sub = binaryOp "sub" Sub primIdInt

-- |
mul = binaryOp "mul" Mul primIdInt

-- |
div = binaryOp "div" Div primIdInt

-- |
mod = binaryOp "mod" Mod primIdInt

min :: Program
min = [program|
    min = \x y -> case x of
        Int# x' -> case y of
            Int# y' -> case <=# x' y' of
                1#      -> x;
                default -> y;
            badInt -> Error_min badInt;
        badInt -> Error_min badInt
    |]

max :: Program
max = [program|
    max = \x y -> case x of
        Int# x' -> case y of
            Int# y' -> case >=# x' y' of
                1# -> x;
                default -> y;
            badInt -> Error_min badInt;
        badInt -> Error_min badInt
    |]
<commit_msg>Fix redundant import<commit_after>{-# LANGUAGE OverloadedLists   #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}

module Stg.Prelude.Number (
    -- * Arithmetic
    add,
    sub,
    mul,
    div,
    mod,

    -- * Comparisons
    eq_Int,
    lt_Int,
    leq_Int,
    gt_Int,
    geq_Int,
    neq_Int,

    -- * Other
    min,
    max,
) where



import Prelude ()

import Data.Monoid ((<>))
import Data.Text   (Text)

import Stg.Language
import Stg.Parser.QuasiQuoter

-- $setup
-- >>> :set -XOverloadedStrings
-- >>> :set -XQuasiQuotes
-- >>> :module +Stg.Language.Prettyprint



binaryOp :: Text -> PrimOp -> Alts -> Program
binaryOp name op primAlts =
    Program (Binds
        [(Var name, LambdaForm [] NoUpdate [Var "x", Var "y"]
            (Case (AppF (Var "x") []) (Alts (AlgebraicAlts
                [AlgebraicAlt (Constr "Int#") [Var "x'"]
                    (Case (AppF (Var "y") []) (Alts (AlgebraicAlts
                        [AlgebraicAlt (Constr "Int#") [Var "y'"]
                            (Case (AppP op (AtomVar (Var "x'")) (AtomVar (Var "y'")))
                                primAlts) ])
                        (DefaultBound (Var "err") (AppC (Constr ("Error_" <> name <> "_1")) [AtomVar (Var "err")])) ))])
                (DefaultBound (Var "err") (AppC (Constr ("Error_" <> name <> "_2")) [AtomVar (Var "err")])) )))])



primToBool :: Alts
primToBool = [alts| 1# -> True; default -> False |]

eq_Int, lt_Int, leq_Int, gt_Int, geq_Int, neq_Int :: Program

-- |
eq_Int  = binaryOp "eq_Int"  Eq  primToBool

-- |
lt_Int  = binaryOp "lt_Int"  Lt  primToBool

-- |
leq_Int = binaryOp "leq_Int" Leq primToBool

-- |
gt_Int  = binaryOp "gt_Int"  Gt  primToBool

-- |
geq_Int = binaryOp "geq_Int" Geq primToBool

-- |
neq_Int = binaryOp "neq_Int" Neq primToBool



primIdInt :: Alts
primIdInt = [alts| v -> Int# v |]

add, sub, mul, div, mod :: Program

-- |
add = binaryOp "add" Add primIdInt

-- |
sub = binaryOp "sub" Sub primIdInt

-- |
mul = binaryOp "mul" Mul primIdInt

-- |
div = binaryOp "div" Div primIdInt

-- |
mod = binaryOp "mod" Mod primIdInt

min :: Program
min = [program|
    min = \x y -> case x of
        Int# x' -> case y of
            Int# y' -> case <=# x' y' of
                1#      -> x;
                default -> y;
            badInt -> Error_min badInt;
        badInt -> Error_min badInt
    |]

max :: Program
max = [program|
    max = \x y -> case x of
        Int# x' -> case y of
            Int# y' -> case >=# x' y' of
                1# -> x;
                default -> y;
            badInt -> Error_min badInt;
        badInt -> Error_min badInt
    |]

{'fraction_non_alphanumeric': 0.15433962264150944, 'fraction_numerical': 0.0018867924528301887, 'mean_word_length': 1.9174463401210786, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 73, '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': '7117926', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1920, 'n_words': 527}