<commit_before>import SwiftCheck
import XCTest
import Runes

class OptionalSpec: XCTestCase {
    func testFunctor() {
        // fmap id = id
        property("identity law") <- forAll { (optional: String?) in
            let lhs = id <^> optional
            let rhs = optional

            return lhs == rhs
        }

        // fmap (g . h) = (fmap g) . (fmap h)
        property("function composition law") <- forAll { (optional: String?) in
            let lhs = compose(append, prepend) <^> optional
            let rhs = compose(curry(<^>)(append), curry(<^>)(prepend))(optional)

            return lhs == rhs
        }
    }

    func testApplicative() {
        // pure id <*> v = v
        property("identity law") <- forAll { (optional: String?) in
            let lhs = pure(id) <*> optional
            let rhs = optional

            return lhs == rhs
        }

        // pure f <*> pure x = pure (f x)
        property("homomorphism law") <- forAll { (string: String) in
            let lhs: String? = pure(append) <*> pure(string)
            let rhs: String? = pure(append(string))

            return rhs == lhs
        }

        // u <*> pure y = pure ($ y) <*> u
        property("interchange law") <- forAll { (string: String) in
            let lhs: String? = pure(append) <*> pure(string)
            let rhs: String? = pure({ $0(string) }) <*> pure(append)

            return lhs == rhs
        }

        // u <*> (v <*> w) = pure (.) <*> u <*> v <*> w
        property("compospropertyion law") <- forAll { (optional: String?) in
            let lhs = pure(append) <*> (pure(prepend) <*> optional)
            let rhs = pure(curry(compose)) <*> pure(append)  <*> pure(prepend) <*> optional

            return lhs == rhs
        }
    }

    func testMonad() {
        // return x >>= f = f x
        property("left identity law") <- forAll { (string: String) in
            let lhs: String? = pure(string) >>- compose(append, pure)
            let rhs: String? = compose(append, pure)(string)

            return lhs == rhs
        }

        // m >>= return = m
        property("right identity law") <- forAll { (optional: String?) in
            let lhs = optional >>- pure
            let rhs = optional

            return lhs == rhs
        }

        // (m >>= f) >>= g = m >>= (\x -> f x >>= g)
        property("associativity law") <- forAll { (optional: String?) in
            let lhs = (optional >>- compose(append, pure)) >>- compose(prepend, pure)
            let rhs = optional >>- { x in compose(append, pure)(x) >>- compose(prepend, pure) }

            return lhs == rhs
        }
    }
}
<commit_msg>Generate functions for optional spec<commit_after>import SwiftCheck
import XCTest
import Runes

class OptionalSpec: XCTestCase {
    func testFunctor() {
        // fmap id = id
        property("identity law") <- forAll { (x: String?) in
            let lhs = id <^> x
            let rhs = x

            return lhs == rhs
        }

        // fmap (f . g) = (fmap f) . (fmap g)
        property("function composition law") <- forAll { (o: OptionalOf<String>, fa: ArrowOf<String, String>, fb: ArrowOf<String, String>) in
            let f = fa.getArrow
            let g = fb.getArrow
            let x = o.getOptional

            let lhs = compose(f, g) <^> x
            let rhs = compose(curry(<^>)(f), curry(<^>)(g))(x)

            return lhs == rhs
        }
    }

    func testApplicative() {
        // pure id <*> v = v
        property("identity law") <- forAll { (x: String?) in
            let lhs = pure(id) <*> x
            let rhs = x

            return lhs == rhs
        }

        // pure f <*> pure x = pure (f x)
        property("homomorphism law") <- forAll { (x: String, fa: ArrowOf<String, String>) in
            let f = fa.getArrow

            let lhs: String? = pure(f) <*> pure(x)
            let rhs: String? = pure(f(x))

            return rhs == lhs
        }

        // f <*> pure x = pure ($ x) <*> f
        property("interchange law") <- forAll { (x: String, fa: OptionalOf<ArrowOf<String, String>>) in
            let f = fa.getOptional?.getArrow

            let lhs = f <*> pure(x)
            let rhs = pure({ $0(x) }) <*> f

            return lhs == rhs
        }

        // f <*> (g <*> x) = pure (.) <*> f <*> g <*> x
        property("composition law") <- forAll { (o: OptionalOf<String>, fa: OptionalOf<ArrowOf<String, String>>, fb: OptionalOf<ArrowOf<String, String>>) in
            let x = o.getOptional
            let f = fa.getOptional?.getArrow
            let g = fb.getOptional?.getArrow

            let lhs = f <*> (g <*> x)
            let rhs = pure(curry(compose)) <*> f <*> g <*> x

            return lhs == rhs
        }
    }

    func testMonad() {
        // return x >>= f = f x
        property("left identity law") <- forAll { (x: String, fa: ArrowOf<String, String>) in
            let f: String -> String? = compose(fa.getArrow, pure)

            let lhs = pure(x) >>- f
            let rhs = f(x)

            return lhs == rhs
        }

        // m >>= return = m
        property("right identity law") <- forAll { (o: OptionalOf<String>) in
            let x = o.getOptional

            let lhs = x >>- pure
            let rhs = x

            return lhs == rhs
        }

        // (m >>= f) >>= g = m >>= (\x -> f x >>= g)
        property("associativity law") <- forAll { (o: OptionalOf<String>, fa: ArrowOf<String, String>, fb: ArrowOf<String, String>) in
            let m = o.getOptional
            let f: String -> String? = compose(fa.getArrow, pure)
            let g: String -> String? = compose(fb.getArrow, pure)

            let lhs = (m >>- f) >>- g
            let rhs = m >>- { x in f(x) >>- g }

            return lhs == rhs
        }
    }
}

{'fraction_non_alphanumeric': 0.1519641626464507, 'fraction_numerical': 0.00034458993797381116, 'mean_word_length': 1.611336032388664, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 116, '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': '6587700', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1756, 'n_words': 543}