<commit_before>require 'spec_helper'

describe MicroKanren::Core do
  include MicroKanren::Core
  include MicroKanren::MiniKanrenWrappers
  include MicroKanren::Lisp

  describe "#call_fresh" do
    it "second-set t1" do
      res = call_fresh(-> (q) { eq(q, 5) }).call(empty_state)

      # The result should be  following the reference
      # implementation:
      # https://github.com/jasonhemann/microKanren/blob/master/microKanren-test.scm#L6

      #     ( ( (   #(0)                      . 5 ) ) . 1  ) )
      exp = [ [ [ [ MicroKanren::Var.new([0]) , 5 ] ] , 1  ] ]
      expected = ary_to_sexp(exp)

      lists_equal?(res, expected).must_equal true
    end

    it "second-set t2" do
      res = call_fresh(-> (q) { eq(q, 5) }).call(empty_state)

      # Following reference implementation:
      # https://github.com/jasonhemann/microKanren/blob/master/microKanren-test.scm#L11
      cdr(res).must_be_nil
    end

    def a_and_b
      a = -> (a) { eq(a, 7) }
      b = -> (b) { disj(eq(b, 5), eq(b, 6)) }

      conj(call_fresh(a), call_fresh(b))
    end

    it "second-set t3" do
      res = car(a_and_b.call(empty_state))

      # The result should be ((([1] . 5) ([0] . 7)) . 2)) following the reference
      # implementation:
      # https://github.com/jasonhemann/microKanren/blob/master/microKanren-test.scm#L13
      expected =
        ary_to_sexp([[[MicroKanren::Var.new([1]), 5], [[MicroKanren::Var.new([0]), 7]]], 2])

      lists_equal?(res, expected).must_equal true
    end

    def fives
      -> (x) {
        disj(eq(x, 5), -> (a_c) { -> { fives(x).call(a_c) } })
      }
    end

    it "who cares" do
      res = take(1, call_fresh(-> (q) { fives.call(q) }).call(empty_state))

      # Result should be: ( ( ( ( #(0) . 5) ) . 1) )
      expected_ast = ary_to_sexp([[[[MicroKanren::Var.new([0]), 5]], 1]])
      lists_equal?(res, expected_ast).must_equal true
    end

  end
end<commit_msg>Spec cleanup<commit_after>require 'spec_helper'

describe MicroKanren::Core do
  include MicroKanren::Core
  include MicroKanren::MiniKanrenWrappers
  include MicroKanren::Lisp

  describe "#call_fresh" do
    it "second-set t1" do
      res = call_fresh(-> (q) { eq(q, 5) }).call(empty_state)

      # The result should be  following the reference
      # implementation:
      # https://github.com/jasonhemann/microKanren/blob/master/microKanren-test.scm#L6

      #       ( ( ( #(0)                      . 5 ) ) . 1  )
      exp = [ [ [ [ MicroKanren::Var.new([0]) , 5 ] ] , 1  ] ]
      expected = ary_to_sexp(exp)

      lists_equal?(res, expected).must_equal true
    end

    it "second-set t2" do
      res = call_fresh(-> (q) { eq(q, 5) }).call(empty_state)

      # Following reference implementation:
      # https://github.com/jasonhemann/microKanren/blob/master/microKanren-test.scm#L11
      cdr(res).must_be_nil
    end

    def a_and_b
      a = -> (a) { eq(a, 7) }
      b = -> (b) { disj(eq(b, 5), eq(b, 6)) }

      conj(call_fresh(a), call_fresh(b))
    end

    # https://github.com/jasonhemann/microKanren/blob/master/microKanren-test.scm#L13
    it "second-set t3" do
      res = car(a_and_b.call(empty_state))

      #     ( ( ( #(1)                      . 5)    ( #(0)                     . 7 ) )   . 2)
      exp = [ [ [ MicroKanren::Var.new([1]) , 5], [ [MicroKanren::Var.new([0]) , 7 ] ] ] , 2]
      sexp = ary_to_sexp(exp)

      lists_equal?(res, sexp).must_equal true
    end

    def fives
      -> (x) {
        disj(eq(x, 5), -> (a_c) { -> { fives(x).call(a_c) } })
      }
    end

    it "who cares" do
      res = take(1, call_fresh(-> (q) { fives.call(q) }).call(empty_state))

      #     ( ( ( ( #(0)                     . 5) ) . 1 ) )
      exp = [ [ [ [ MicroKanren::Var.new([0]), 5] ] , 1 ] ]

      sexp = ary_to_sexp(exp)
      lists_equal?(res, sexp).must_equal true
    end

  end
end
{'fraction_non_alphanumeric': 0.16679636835278858, 'fraction_numerical': 0.0191958495460441, 'mean_word_length': 2.2267782426778244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2205804', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1444, 'n_words': 275}