-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxQC.hs
53 lines (43 loc) · 1.77 KB
/
SyntaxQC.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
import AST
import Syntax
import Test.QuickCheck
import Control.Monad
-- Your code here
-- Randomly generate a Term, parseStringTerm transforms it into string
-- and use printTerm to process string into a term,
-- compare whether these two terms are the same
-- if same, then success, if otherwise it will fail.
optable = OpTable [(FNone, ["<=", "<"]),(FLeft, ["+", "-"]),(FLeft, ["*"]),(FRight, ["**"])]
alphaFreqList = [ (26, choose ('a', 'z')) ]
digitFreqList = [ (10, choose ('0', '9')) ]
letter = frequency alphaFreqList
letterOrDigit = frequency $ alphaFreqList ++ digitFreqList
-- generate variable name whos first char is a letter
varNameGenerator = liftM2 (:) letter (sized (\n -> replicateM n letterOrDigit))
tfunNameGenerator = oneof ([liftM2 (:) letter (sized (\n -> replicateM n letterOrDigit))] ++ map pure (allOps optable))
-- randomly choose one of TVar, TNum, TFun
-- generate a variable name
instance Arbitrary Term where
arbitrary = oneof
[ liftM TVar varNameGenerator
, liftM TNum arbitrary
, liftM2 TFun tfunNameGenerator (replicateM 2 arbitrary) ]
-- TFun contains some self-defined operators.
-- check whether the input term and output term is the same
checkTermParser term =
case (parseStringTerm optable (printTerm optable term)) of
Left _ -> False
Right a -> term == a
-- main test function, to make tests runnable as "runhaskell SyntaxQC"
main :: IO ()
main = quickCheck checkTermParser
allOps :: OpTable -> [String]
allOps (OpTable optable) = foldl (\acc x -> acc ++ snd x) [] optable