summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/test/Minilang/Lambda/EvalSpec.hs
blob: e5075f0e2734d2bdbced115a973e60707cdf978b (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
{-# LANGUAGE LambdaCase #-}
module Minilang.Lambda.EvalSpec (spec) where

import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (race_)
import Data.Aeson (eitherDecodeFileStrict)
import Data.Text (Text, pack)
import qualified Data.Text as Text
import Minilang.API (Challenge (..))
import Minilang.Lambda (loadProgram)
import qualified Minilang.Lambda.DeBruijn as DB
import Minilang.Lambda.DeBruijn (toDeBruijn)
import Minilang.Lambda.Eval (Env, Term (..), Value (..), eval, normalise, builtinEnvValue, newEnv)
import Minilang.Lambda.Infer (newContext)
import Minilang.Lambda.Parser (parseProgram)
import Minilang.REPL.Types (Evaluator (..), Language (..))
import Test.Hspec (Spec, describe, it, runIO, shouldBe)
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (NonEmptyList (..), arbitrary, forAll, (===))

spec :: Spec
spec = do
  let y =
        Lam
          "f"
          ( App
              (Lam "x" (App (Var "f") (App (Var "x") (Var "x"))))
              (Lam "x" (App (Var "f") (App (Var "x") (Var "x"))))
          )
      z =
        Lam
          "f"
          ( App
              (Lam "x" (App (Var "f") (Lam "y" (App (App (Var "x") (Var "x")) (Var "y")))))
              (Lam "x" (App (Var "f") (Lam "y" (App (App (Var "x") (Var "x")) (Var "y")))))
          )

      omega =
        App (Lam "x" (App (Var "x") (Var "x"))) (Lam "x" (App (Var "x") (Var "x")))

      fst' = Lam "x" (Lam "y" (Var "x"))
      snd' = Lam "x" (Lam "y" (Var "y"))

  describe "call-by-value eval" $ do
    it "evaluates 'x' as itself" $ do
      let term = Var "x"
      normalise term `shouldBe` Var "x"
    it "evaluates '((λ x. x) y)' as 'y'" $ do
      let term = App (Lam "x" (Var "x")) (Var "y")
      normalise term `shouldBe` Var "y"
    it "evaluates '((λ x y. y) z t y)' as '(t y)'" $ do
      let term = App (App (App snd' (Var "z")) (Var "t")) (Var "y")
      normalise term `shouldBe` App (Var "t") (Var "y")
    it "evaluates '((λ x y. x) z t)' as 'z'" $ do
      let term = App (App fst' (Var "z")) (Var "t")
      normalise term `shouldBe` Var "z"
    it "diverges on evaluating ω" $
      shouldDiverge $
        normalise omega
    it "diverges on evaluating ω applied to a function that use it" $
      shouldDiverge $
        normalise (App fst' omega)
    it "diverges on evaluating ω applied to a function that do not use it" $
      shouldDiverge $
        normalise (App snd' omega)
  describe "Church numerals" $ do
    let zero = Lam "s" (Lam "z" (Var "z"))
        one = Lam "s" (Lam "z" (App (Var "s") (Var "z")))
        two = Lam "s" (Lam "z" (App "s" (App (Var "s") (Var "z"))))
        succ' = Lam "n" (Lam "s" (Lam "z" (App (Var "s") (App (App (Var "n") (Var "s")) (Var "z")))))
        add' = Lam "j" (Lam "k" (Lam "s" (Lam "z" (App (App "j" "s") (App (App "k" "s") "z")))))

    it "normalises successor" $
      normalise (App succ' zero)
        `shouldBe` one

    it "adds two numerals" $
      normalise (App (App add' one) one)
        `shouldBe` two

  describe "integer literals" $ do
    -- An integer literal is already a normal form: normalise must return it unchanged.
    prop "any integer literal normalises to itself" $ \n ->
      normalise (Num n) === Num (n :: Int)

    -- eval on a bare Num must always produce the VNum value form, never a closure
    -- or neutral, regardless of the integer's sign or magnitude.
    prop "eval of any integer literal produces VNum" $ \n ->
      eval [] (Num n) === VNum (n :: Int)

    -- A number is not a function.  Applying it to any argument must leave a stuck
    -- neutral application: App (Num n) (normalise arg).  We test this with free
    -- variables as arguments, which are themselves already in normal form.
    prop "an integer in function position produces a stuck application" $
      forAll arbitrary $ \n ->
        forAll (getNonEmpty <$> arbitrary) $ \x ->
          normalise (App (Num n) (Var (pack x)))
            === App (Num (n :: Int)) (Var (pack x))

  describe "built-in arithmetic" $ do
    -- Basic two-argument reductions
    it "(+ 3 4) normalises to 7" $
      normalise (App (App (Var "+") (Num 3)) (Num 4)) `shouldBe` Num 7

    it "(- 10 3) normalises to 7" $
      normalise (App (App (Var "-") (Num 10)) (Num 3)) `shouldBe` Num 7

    it "(* 2 5) normalises to 10" $
      normalise (App (App (Var "*") (Num 2)) (Num 5)) `shouldBe` Num 10

    it "(div 10 3) normalises to 3" $
      normalise (App (App (Var "div") (Num 10)) (Num 3)) `shouldBe` Num 3

    it "(mod 10 3) normalises to 1" $
      normalise (App (App (Var "mod") (Num 10)) (Num 3)) `shouldBe` Num 1

    it "(= 3 3) normalises to Church True" $
      normalise (App (App (Var "=") (Num 3)) (Num 3))
        `shouldBe` Lam "f" (Lam "t" (Var "t"))

    it "(= 3 4) normalises to Church False" $
      normalise (App (App (Var "=") (Num 3)) (Num 4))
        `shouldBe` Lam "f" (Lam "t" (Var "f"))

    it "(< 2 5) normalises to Church True" $
      normalise (App (App (Var "<") (Num 2)) (Num 5))
        `shouldBe` Lam "f" (Lam "t" (Var "t"))

    it "(< 5 2) normalises to Church False" $
      normalise (App (App (Var "<") (Num 5)) (Num 2))
        `shouldBe` Lam "f" (Lam "t" (Var "f"))

    -- Compose with Church ite: (ite (= 3 3) (Num 1) (Num 2)) == 1
    -- ite c t e = c e t  (True picks second arg, False picks first)
    it "(= 3 3) composes with Church ite to select then-branch" $
      let ite = Lam "c" (Lam "t" (Lam "e" (App (App (Var "c") (Var "e")) (Var "t"))))
      in normalise (App (App (App ite (App (App (Var "=") (Num 3)) (Num 3))) (Num 1)) (Num 2))
           `shouldBe` Num 1

    it "(< 5 2) composes with Church ite to select else-branch" $
      let ite = Lam "c" (Lam "t" (Lam "e" (App (App (Var "c") (Var "e")) (Var "t"))))
      in normalise (App (App (App ite (App (App (Var "<") (Num 5)) (Num 2))) (Num 1)) (Num 2))
           `shouldBe` Num 2

    it "(neg 7) normalises to -7" $
      normalise (App (Var "neg") (Num 7)) `shouldBe` Num (-7)

    it "(neg -3) normalises to 3" $
      normalise (App (Var "neg") (Num (-3))) `shouldBe` Num 3

    -- Nested arithmetic
    it "(* 2 (+ 1 4)) normalises to 10" $
      normalise (App (App (Var "*") (Num 2)) (App (App (Var "+") (Num 1)) (Num 4)))
        `shouldBe` Num 10

    -- Properties
    prop "addition is commutative" $ \x y ->
      normalise (App (App (Var "+") (Num x)) (Num y))
        === normalise (App (App (Var "+") (Num y)) (Num (x :: Int)))

    prop "subtraction is the inverse of addition" $ \x y ->
      normalise (App (App (Var "-") (App (App (Var "+") (Num x)) (Num y))) (Num y))
        === Num (x :: Int)

    prop "multiplication by 0 is 0" $ \x ->
      normalise (App (App (Var "*") (Num x)) (Num 0))
        === Num (0 :: Int)

    prop "neg is its own inverse" $ \x ->
      normalise (App (Var "neg") (App (Var "neg") (Num x)))
        === Num (x :: Int)

    -- Partial application
    it "(+ 1) evaluates to a Builtin waiting for one more arg" $
      eval builtinEnvValue (App (Var "+") (Num 1))
        `shouldBe` Builtin "+" 1 [VNum 1]

    it "partial builtin applied to its last arg reduces" $
      eval builtinEnvValue (App (App (Var "+") (Num 1)) (Num 2))
        `shouldBe` VNum 3

    it "partial builtin passed through a lambda reduces when applied" $
      normalise (App (Lam "f" (App (App (Var "f") (Num 3)) (Num 4))) (Var "+"))
        `shouldBe` Num 7

    -- Stuck / neutral cases
    it "(+ x 1) with free x stays stuck" $
      normalise (App (App (Var "+") (Var "x")) (Num 1))
        `shouldBe` App (App (Var "+") (Var "x")) (Num 1)

    it "(div 1 0) stays stuck" $
      normalise (App (App (Var "div") (Num 1)) (Num 0))
        `shouldBe` App (App (Var "div") (Num 1)) (Num 0)

    it "(mod 5 0) stays stuck" $
      normalise (App (App (Var "mod") (Num 5)) (Num 0))
        `shouldBe` App (App (Var "mod") (Num 5)) (Num 0)

  describe "evaluator equivalence on challenges" $ do
    challenges <- runIO $
      eitherDecodeFileStrict "challenges.json" >>= \case
        Left err -> fail $ "Could not load challenges.json: " <> err
        Right cs -> pure (cs :: [Challenge])

    mapM_ (challengeEquivalenceTest newEnv) challenges

  -- describe "call-by-name eval" $ do
  --   it "evaluates 'x' as itself" $ do
  --     normalise' (Var "x") `shouldBe` Var "x"
  --   it "evaluates '(λ x y . y) ⊥ z' as 'z'" $ do
  --     normalise' (App (App snd' omega) "z") `shouldBe` Var "z"
  --   it "diverges on evaluating ω" $
  --     shouldDiverge $
  --       normalise' omega

-- | For a single challenge expression, parse it and run both evaluators,
-- asserting they produce alpha-equivalent results.
-- Alpha-equivalence is checked by converting both results to De Bruijn-indexed
-- form (which strips variable names) before comparing.
challengeEquivalenceTest :: Env -> Challenge -> Spec
challengeEquivalenceTest env Challenge{level, expr} =
  it ("level " <> show level <> ": " <> Text.unpack (Text.take 60 expr)) $
    case parseProgram expr of
      Left err ->
        fail $ "Parse error for level " <> show level <> ": " <> show err
      Right asts ->
        let runWith ev = loadProgram ev Untyped env newContext asts
            extractTerms (Right (terms, _, _)) = map fst terms
            extractTerms (Left err)            = error $ "Type error: " <> show err
            -- Convert to annotation-erased De Bruijn to compare up to
            -- alpha-equivalence.  Both evaluators may choose different fresh
            -- names for lambda binders (NbE normalises under binders and must
            -- avoid capture); erasing names leaves only structure and indices.
            toAlpha = map (eraseNames . toDeBruijn) . extractTerms
            debruijnTerms = toAlpha (runWith DeBruijn)
            nbeTerms      = toAlpha (runWith NbE)
        in nbeTerms `shouldBe` debruijnTerms

-- | Erase all name annotations from a 'DeBruijn' term, leaving only the
-- structural skeleton (indices, numeric literals, free-variable names, and
-- builtin names).  Two terms that are identical up to alpha-renaming of bound
-- variables will be equal after erasure.
eraseNames :: DB.DeBruijn Text -> DB.DeBruijn ()
eraseNames (DB.FV x)       = DB.FV x
eraseNames (DB.FNum n)     = DB.FNum n
eraseNames (DB.FBuiltin b) = DB.FBuiltin b
eraseNames (DB.Var _ i)    = DB.Var () i
eraseNames (DB.Lam _ body) = DB.Lam () (eraseNames body)
eraseNames (DB.App f a)    = DB.App (eraseNames f) (eraseNames a)

shouldDiverge :: Term -> IO ()
shouldDiverge t =
  race_
    (t `seq` fail "Did not diverge")
    (threadDelay 1000000)