<commit_before>{-# LANGUAGE QuasiQuotes #-}

-- | Common Haskell functions, translated to STG. Use the 'Monoid' instance
-- for 'Program' to mix them.
--
-- Each definition is written so that it can be used directly; for example,
-- 'iterate' includes the definition of 'cons', and you don't have to add it
-- manually.
--
-- This module should be imported qualified, since it heavily conflicts with the
-- standard Haskell "Prelude".
module Stg.Language.Prelude (
    -- * Lists
    listC,
    foldl,
    foldl',
    foldr,
    iterate,

    -- * Numbers
    add,
    numbers,

    -- * Other
    seq,
) where



import           Prelude      ()

import           Data.Monoid

import           Stg.Language (Program)
import           Stg.Parser



listC, foldl, foldl', foldr, iterate :: Program
numbers, add :: Program
seq :: Program

-- | List standard constructors.
--
-- @
-- nil : [a]
-- cons : (a, [a]) -> [a]
-- @
listC = [stg|
    nil = () \n () -> Nil ();
    cons = () \n (x,xs) -> Cons (x,xs) |]

-- | Integer addition.
--
-- @
-- add : (Int, Int) -> Int
-- @
add = [stg|
    add = () \n (x,y) -> case x () of
        Int# (x') -> case y () of
            Int# (y') -> case +# x' y' of
                1# -> Int# (1#); -- FIXME type hint
                v  -> Int# (v)
            default -> Error_add ()
        default -> Error_add () |]

-- | Lazy left list fold.
--
-- @
-- foldl : (b -> a -> b, b, [a]) -> b
-- @
foldl = [stg|
    foldl = () \n (f, acc, xs) -> case xs () of
        Nil () -> acc ();
        Cons (y,ys) ->
            let acc' = (f,acc,y) \u () -> case f (acc, y) of
                    v -> v ()
            in foldl (f, acc', ys)
        default -> Error_foldl () |]

-- | Strict left list fold.
--
-- @
-- foldl : (b -> a -> b, b, [a]) -> b
-- @
foldl' = [stg|
    foldl' = () \n (f, acc, xs) -> case xs () of
        Nil () -> acc ();
        Cons (y,ys) -> case f (acc, y) of
            acc' -> foldl' (f, acc', ys)
        default -> Error_foldl' ()
    |]

-- | Right list fold.
--
-- @
-- foldl : (a -> b -> b, b, [a]) -> b
-- @
foldr = [stg|
    foldr = () \n (f, z, xs) -> case xs () of
        Nil () -> z ();
        Cons (y,ys) ->
            let rest = () \n () -> foldr (f,z,ys)
            in f (y, rest)
        default -> Error_foldr () |]

-- | Build a list by repeatedly applying a function to an initial value.
--<commit_msg>Fix closure args of foldr, make 'rest' updatable<commit_after>{-# LANGUAGE QuasiQuotes #-}

-- | Common Haskell functions, translated to STG. Use the 'Monoid' instance
-- for 'Program' to mix them.
--
-- Each definition is written so that it can be used directly; for example,
-- 'iterate' includes the definition of 'cons', and you don't have to add it
-- manually.
--
-- This module should be imported qualified, since it heavily conflicts with the
-- standard Haskell "Prelude".
module Stg.Language.Prelude (
    -- * Lists
    listC,
    foldl,
    foldl',
    foldr,
    iterate,

    -- * Numbers
    add,
    numbers,

    -- * Other
    seq,
) where



import           Prelude      ()

import           Data.Monoid

import           Stg.Language (Program)
import           Stg.Parser



listC, foldl, foldl', foldr, iterate :: Program
numbers, add :: Program
seq :: Program

-- | List standard constructors.
--
-- @
-- nil : [a]
-- cons : (a, [a]) -> [a]
-- @
listC = [stg|
    nil = () \n () -> Nil ();
    cons = () \n (x,xs) -> Cons (x,xs) |]

-- | Integer addition.
--
-- @
-- add : (Int, Int) -> Int
-- @
add = [stg|
    add = () \n (x,y) -> case x () of
        Int# (x') -> case y () of
            Int# (y') -> case +# x' y' of
                1# -> Int# (1#); -- FIXME type hint
                v  -> Int# (v)
            default -> Error_add ()
        default -> Error_add () |]

-- | Lazy left list fold.
--
-- @
-- foldl : (b -> a -> b, b, [a]) -> b
-- @
foldl = [stg|
    foldl = () \n (f, acc, xs) -> case xs () of
        Nil () -> acc ();
        Cons (y,ys) ->
            let acc' = (f,acc,y) \u () -> case f (acc, y) of
                    v -> v ()
            in foldl (f, acc', ys)
        default -> Error_foldl () |]

-- | Strict left list fold.
--
-- @
-- foldl : (b -> a -> b, b, [a]) -> b
-- @
foldl' = [stg|
    foldl' = () \n (f, acc, xs) -> case xs () of
        Nil () -> acc ();
        Cons (y,ys) -> case f (acc, y) of
            acc' -> foldl' (f, acc', ys)
        default -> Error_foldl' ()
    |]

-- | Right list fold.
--
-- @
-- foldl : (a -> b -> b, b, [a]) -> b
-- @
foldr = [stg|
    foldr = () \n (f, z, xs) -> case xs () of
        Nil () -> z ();
        Cons (y,ys) ->
            let rest = (f,z,ys) \u () -> foldr (f,z,ys)
            in f (y, rest)
        default -> Error_foldr () |]

-- | Build a list by repeatedly applying a function to an initial value.
--
{'fraction_non_alphanumeric': 0.19334027055150885, 'fraction_numerical': 0.0008324661810613944, 'mean_word_length': 1.5563829787234043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 75, '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': '331345', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1702, 'n_words': 564}