<commit_before>{-# LANGUAGE FlexibleInstances #-}
module PTS.AST.Arbitrary where

import Test.QuickCheck

import PTS.AST
import PTS.Instances
import PTS.Instances.Arbitrary

instance Arbitrary Name where
  arbitrary = do
    growingElements (map read ["a", "b", "c", "d", "e", "f", "g", "h"])

instance Arbitrary Term where
  arbitrary = arbitraryTerm

class DistributeSize t where
  distributeSize :: Gen t

instance DistributeSize Int where
  distributeSize = sized (\size -> do
    return size)

instance DistributeSize (Int, Int) where
  distributeSize = sized (\size ->
    if size > 1 then do
      size1 <- choose (1, size - 1)
      return (size1, size - size1)
    else do
      fail "not enough size to distribute over two subterms.")

instance DistributeSize (Int, Int, Int) where
  distributeSize = sized (\size ->
    if size > 2 then do
      size1 <- choose (1, size - 2)
      size2 <- choose (1, size - size1 - 1)
      return (size1, size2, size - size1 - size2)
    else do
      fail "not enough size to distribute over three subterms.")

arbitraryTerm = sized (\size -> oneof $
    [  arbitraryNat
    ,  arbitraryVar
    ,  arbitraryPos
    ,  arbitraryConst
    ] ++
    if size >= 2 then
    [  arbitraryApp
    ,  arbitraryNatOp
    ] else [] ++
    if size >= 3 then
    [  arbitraryIfZero
    ,  arbitraryLam
    ,  arbitraryPi
    ] else [])

arbitraryNat = do
  value <- arbitrary
  return (mkNat value)

arbitraryNatOp = do
  (s1, s2) <- distributeSize

  t1      <-  resize s1 arbitrary
  t2      <-  resize s2 arbitrary
  (n, f)  <-  elements
                [  (read "add", Add)
                ,  (read "sub", Sub)
                ,  (read "mul", Mul)
                ,  (read "div", Div)
                ]
  return (mkNatOp n f t1 t2)

arbitraryIfZero = do
  (s1, s2, s3) <- distributeSize
<commit_msg>Fix test case generation (see #23 #31).<commit_after>{-# LANGUAGE FlexibleInstances #-}
module PTS.AST.Arbitrary where

import Test.QuickCheck

import PTS.AST
import PTS.Instances
import PTS.Instances.Arbitrary

instance Arbitrary Name where
  arbitrary = do
    growingElements (map read ["a", "b", "c", "d", "e", "f", "g", "h"])

instance Arbitrary Term where
  arbitrary = arbitraryTerm

class DistributeSize t where
  distributeSize :: Gen t

instance DistributeSize Int where
  distributeSize = sized (\size -> do
    return size)

instance DistributeSize (Int, Int) where
  distributeSize = sized (\size ->
    if size > 1 then do
      size1 <- choose (1, size - 1)
      return (size1, size - size1)
    else do
      fail "not enough size to distribute over two subterms.")

instance DistributeSize (Int, Int, Int) where
  distributeSize = sized (\size ->
    if size > 2 then do
      size1 <- choose (1, size - 2)
      size2 <- choose (1, size - size1 - 1)
      return (size1, size2, size - size1 - size2)
    else do
      fail "not enough size to distribute over three subterms.")

arbitraryTerm = sized (\size -> oneof $
    [  arbitraryNat
    ,  arbitraryVar
    ,  arbitraryPos
    ,  arbitraryConst
    ] ++
    (if size >= 2 then
    [  arbitraryApp
    ,  arbitraryNatOp
    ] else []) ++
    (if size >= 3 then
    [  arbitraryIfZero
    ,  arbitraryLam
    ,  arbitraryPi
    ] else []))

arbitraryNat = do
  value <- arbitrary
  return (mkNat value)

arbitraryNatOp = do
  (s1, s2) <- distributeSize

  t1      <-  resize s1 arbitrary
  t2      <-  resize s2 arbitrary
  (n, f)  <-  elements
                [  (read "add", Add)
                ,  (read "sub", Sub)
                ,  (read "mul", Mul)
                ,  (read "div", Div)
                ]
  return (mkNatOp n f t1 t2)

arbitraryIfZero = do
  (s1, s2, s3) <- distributeSize

{'fraction_non_alphanumeric': 0.10718142548596113, 'fraction_numerical': 0.017818574514038878, 'mean_word_length': 2.2787610619469025, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, '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': '3841312', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1209, 'n_words': 404}