File size: 15,931 Bytes
a10b189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{-# LANGUAGE OverloadedStrings #-}

{-|
Module      : Assertica.Verify.Pipeline
Description : Main verification pipeline orchestrator
Copyright   : (c) 2026 Ahmad Ali Parr
License     : BSL-1.0

The verification pipeline orchestrates the complete compilation and verification workflow:

  SOURCE → LEXER → PARSER → ELABORATOR → TYPE CHECKER → PROOF CHECKER → CODE GEN → ACCEPT/REJECT

Each stage:
- Operates on the output of the previous stage
- Tracks execution time
- Captures errors with source locations
- Halts immediately on failure (fail-closed)

DESIGN PRINCIPLES:
1. Deterministic: Same input always produces same report
2. Fail-closed: First error halts pipeline
3. Observable: Each stage tracked independently
4. Composable: Results can be tested in isolation
-}

module Assertica.Verify.Pipeline
  ( -- * Main pipeline entry points
    verifyFile
  , verifyDirectory
  , verifyModule

    -- * Pipeline stages
  , PipelineState (..)
  , runPipelineStage
  , runLexer
  , runParser
  , runElaborator
  , runTypeChecker
  , runProofChecker
  , runCodeGen

    -- * Error handling
  , toPipelineError
  , PipelineError (..)
  ) where

import Control.Monad (filterM)
import Data.Time (getCurrentTime, diffUTCTime)
import Data.Time.Clock (UTCTime)
import System.Directory (doesDirectoryExist, doesFileExist, listDirectory)
import System.FilePath (takeExtension, (</>))
import Data.List (sort)
import Assertica.Verify.Report
  ( VerificationReport (..)
  , StageResult
  , Verdict (..)
  , ErrorDetail (..)
  , ErrorCategory (..)
  , SourceLocation (..)
  , mkStageResult
  , mkFailedStageResult
  , mkReport
  )

-- ============================================================================
-- PIPELINE STATE
-- ============================================================================

{-| State of the verification pipeline for a single file.
-}
data PipelineState = PipelineState
  { pstFile         :: FilePath
  , pstModule       :: Maybe String
  , pstSourceCode   :: String              -- ^ Original source
  , pstLexedTokens  :: Maybe String        -- ^ Lexer output
  , pstParsedAST    :: Maybe String        -- ^ Parser output
  , pstElaborated   :: Maybe String        -- ^ Elaborator output
  , pstTypeChecked  :: Maybe String        -- ^ Type checker output
  , pstProofChecked :: Maybe String        -- ^ Proof checker output
  , pstCodeGen      :: Maybe String        -- ^ Code generation output
  , pstErrors       :: [ErrorDetail]       -- ^ Accumulated errors
  }
  deriving (Show)

-- ============================================================================
-- PIPELINE ERRORS
-- ============================================================================

{-| Pipeline-specific error type.
-}
data PipelineError
  = PipelineError String (Maybe SourceLocation)
  | FileNotFound FilePath
  | DirectoryNotFound FilePath
  | InvalidExtension FilePath
  deriving (Show)

{-| Convert pipeline error to ErrorDetail.
-}
toPipelineError :: PipelineError -> ErrorDetail
toPipelineError (PipelineError msg loc) =
  ErrorDetail UnknownError msg loc Nothing Nothing
toPipelineError (FileNotFound path) =
  ErrorDetail UnknownError ("File not found: " ++ path) Nothing Nothing (Just "Check file path")
toPipelineError (DirectoryNotFound path) =
  ErrorDetail UnknownError ("Directory not found: " ++ path) Nothing Nothing (Just "Check directory path")
toPipelineError (InvalidExtension path) =
  ErrorDetail SyntaxError ("Invalid file extension: " ++ path) Nothing Nothing (Just "Use .as file extension")

-- ============================================================================
-- MAIN PIPELINE ENTRY POINTS
-- ============================================================================

{-| Verify a single .as file and return comprehensive report.
-}
verifyFile :: FilePath -> IO VerificationReport
verifyFile filePath = do
  start <- getCurrentTime

  -- Check file exists
  exists <- doesFileExist filePath
  if not exists
    then do
      end <- getCurrentTime
      let duration = realToFrac (diffUTCTime end start) * 1000
          err = toPipelineError (FileNotFound filePath)
          failStage = mkFailedStageResult "Read" 0 err
      return $ mkReport filePath Nothing Nothing (show start) failStage failStage failStage failStage failStage
    else do
      -- Check extension
      if takeExtension filePath /= ".as"
        then do
          end <- getCurrentTime
          let duration = realToFrac (diffUTCTime end start) * 1000
              err = toPipelineError (InvalidExtension filePath)
              failStage = mkFailedStageResult "Read" 0 err
          return $ mkReport filePath Nothing Nothing (show start) failStage failStage failStage failStage failStage
        else do
          -- Read source
          sourceCode <- readFile filePath

          -- Initialize pipeline state
          let initialState = PipelineState
                { pstFile = filePath
                , pstModule = Nothing
                , pstSourceCode = sourceCode
                , pstLexedTokens = Nothing
                , pstParsedAST = Nothing
                , pstElaborated = Nothing
                , pstTypeChecked = Nothing
                , pstProofChecked = Nothing
                , pstCodeGen = Nothing
                , pstErrors = []
                }

          -- Run pipeline stages
          result <- runPipeline filePath (show start) initialState

          end <- getCurrentTime
          let totalDuration = realToFrac (diffUTCTime end start) * 1000

          return $ result { vrTotalDuration = totalDuration }

{-| Verify all .as files in a directory.
-}
verifyDirectory :: FilePath -> IO [VerificationReport]
verifyDirectory dirPath = do
  exists <- doesDirectoryExist dirPath
  if not exists
    then do
      let err = toPipelineError (DirectoryNotFound dirPath)
          failStage = mkFailedStageResult "Read" 0 err
          report = mkReport dirPath Nothing Nothing "unknown" failStage failStage failStage failStage failStage
      return [report]
    else do
      files <- listDirectory dirPath
      let asserticaFiles = filter (\f -> takeExtension f == ".as") files
      let fullPaths = map (dirPath </>) (sort asserticaFiles)
      mapM verifyFile fullPaths

{-| Verify a single module (used internally after elaboration).
-}
verifyModule :: String -> String -> IO VerificationReport
verifyModule moduleName sourceCode = do
  -- Placeholder: verify a module given its name and source
  -- This would be used for internal verification of elaborated modules
  start <- getCurrentTime
  let failStage = mkStageResult "Module" 0
  return $ mkReport moduleName (Just moduleName) Nothing (show start) failStage failStage failStage failStage failStage

-- ============================================================================
-- PIPELINE ORCHESTRATION
-- ============================================================================

{-| Run the complete verification pipeline.
Halts on first error (fail-closed).
-}
runPipeline :: FilePath -> String -> PipelineState -> IO VerificationReport
runPipeline filePath timestamp state = do
  start <- getCurrentTime

  -- Stage 1: Lexer
  lexStart <- getCurrentTime
  (lexState, lexResult) <- runLexer state
  lexEnd <- getCurrentTime
  let lexDuration = realToFrac (diffUTCTime lexEnd lexStart) * 1000

  case lexResult of
    Left err -> do
      let failStage = mkFailedStageResult "Lexer" lexDuration err
      return $ mkReport filePath Nothing Nothing timestamp failStage failStage failStage failStage failStage
    Right _ -> do
      -- Stage 2: Parser
      parStart <- getCurrentTime
      (parState, parResult) <- runParser lexState
      parEnd <- getCurrentTime
      let parDuration = realToFrac (diffUTCTime parEnd parStart) * 1000

      case parResult of
        Left err -> do
          let lexStage = mkStageResult "Lexer" lexDuration
              failStage = mkFailedStageResult "Parser" parDuration err
          return $ mkReport filePath Nothing Nothing timestamp lexStage failStage failStage failStage failStage
        Right _ -> do
          -- Stage 3: Elaborator
          eStart <- getCurrentTime
          (eState, eResult) <- runElaborator parState
          eEnd <- getCurrentTime
          let eDuration = realToFrac (diffUTCTime eEnd eStart) * 1000

          case eResult of
            Left err -> do
              let lexStage = mkStageResult "Lexer" lexDuration
                  parStage = mkStageResult "Parser" parDuration
                  failStage = mkFailedStageResult "Elaborator" eDuration err
              return $ mkReport filePath Nothing Nothing timestamp lexStage parStage failStage failStage failStage
            Right _ -> do
              -- Stage 4: Type Checker
              tcStart <- getCurrentTime
              (tcState, tcResult) <- runTypeChecker eState
              tcEnd <- getCurrentTime
              let tcDuration = realToFrac (diffUTCTime tcEnd tcStart) * 1000

              case tcResult of
                Left err -> do
                  let lexStage = mkStageResult "Lexer" lexDuration
                      parStage = mkStageResult "Parser" parDuration
                      eStage = mkStageResult "Elaborator" eDuration
                      failStage = mkFailedStageResult "TypeChecker" tcDuration err
                  return $ mkReport filePath (pstModule tcState) Nothing timestamp lexStage parStage eStage failStage failStage
                Right _ -> do
                  -- Stage 5: Proof Checker
                  pcStart <- getCurrentTime
                  (pcState, pcResult) <- runProofChecker tcState
                  pcEnd <- getCurrentTime
                  let pcDuration = realToFrac (diffUTCTime pcEnd pcStart) * 1000

                  case pcResult of
                    Left err -> do
                      let lexStage = mkStageResult "Lexer" lexDuration
                          parStage = mkStageResult "Parser" parDuration
                          eStage = mkStageResult "Elaborator" eDuration
                          tcStage = mkStageResult "TypeChecker" tcDuration
                          failStage = mkFailedStageResult "ProofChecker" pcDuration err
                      return $ mkReport filePath (pstModule pcState) Nothing timestamp lexStage parStage eStage tcStage failStage
                    Right _ -> do
                      -- Stage 6: Code Generation
                      cgStart <- getCurrentTime
                      (cgState, cgResult) <- runCodeGen pcState
                      cgEnd <- getCurrentTime
                      let cgDuration = realToFrac (diffUTCTime cgEnd cgStart) * 1000

                      case cgResult of
                        Left err -> do
                          let lexStage = mkStageResult "Lexer" lexDuration
                              parStage = mkStageResult "Parser" parDuration
                              eStage = mkStageResult "Elaborator" eDuration
                              tcStage = mkStageResult "TypeChecker" tcDuration
                              pcStage = mkStageResult "ProofChecker" pcDuration
                              failStage = mkFailedStageResult "CodeGen" cgDuration err
                          return $ mkReport filePath (pstModule cgState) Nothing timestamp lexStage parStage eStage tcStage failStage
                        Right _ -> do
                          let lexStage = mkStageResult "Lexer" lexDuration
                              parStage = mkStageResult "Parser" parDuration
                              eStage = mkStageResult "Elaborator" eDuration
                              tcStage = mkStageResult "TypeChecker" tcDuration
                              pcStage = mkStageResult "ProofChecker" pcDuration
                              cgStage = mkStageResult "CodeGen" cgDuration
                          return $ mkReport filePath (pstModule cgState) Nothing timestamp lexStage parStage eStage tcStage cgStage

-- ============================================================================
-- INDIVIDUAL PIPELINE STAGES
-- ============================================================================

{-| Run lexer stage (tokenization).
-}
runLexer :: PipelineState -> IO (PipelineState, Either ErrorDetail ())
runLexer state = do
  -- Placeholder: real implementation would call Assertica.Surface.Lexer
  -- For now, we simulate successful lexing
  if null (pstSourceCode state)
    then do
      let err = ErrorDetail SyntaxError "Empty source code" Nothing Nothing Nothing
      return (state, Left err)
    else do
      let state' = state { pstLexedTokens = Just "tokens" }
      return (state', Right ())

{-| Run parser stage (build AST).
-}
runParser :: PipelineState -> IO (PipelineState, Either ErrorDetail ())
runParser state = do
  -- Placeholder: real implementation would call Assertica.Surface.Parser
  case pstLexedTokens state of
    Nothing ->
      let err = ErrorDetail SyntaxError "Lexer output missing" Nothing Nothing Nothing
      in return (state, Left err)
    Just _ ->
      let state' = state { pstParsedAST = Just "ast" }
      in return (state', Right ())

{-| Run elaborator stage (surface to core AST).
-}
runElaborator :: PipelineState -> IO (PipelineState, Either ErrorDetail ())
runElaborator state = do
  -- Placeholder: real implementation would call Assertica.Surface.Elaborator
  case pstParsedAST state of
    Nothing ->
      let err = ErrorDetail SyntaxError "Parser output missing" Nothing Nothing Nothing
      in return (state, Left err)
    Just _ ->
      let state' = state { pstElaborated = Just "core_ast" }
      in return (state', Right ())

{-| Run type checker stage.
-}
runTypeChecker :: PipelineState -> IO (PipelineState, Either ErrorDetail ())
runTypeChecker state = do
  -- Placeholder: real implementation would call Assertica.Core.TypeChecker
  case pstElaborated state of
    Nothing ->
      let err = ErrorDetail TypeError "Elaborated AST missing" Nothing Nothing Nothing
      in return (state, Left err)
    Just _ ->
      let state' = state { pstTypeChecked = Just "typed_ast" }
      in return (state', Right ())

{-| Run proof checker stage.
-}
runProofChecker :: PipelineState -> IO (PipelineState, Either ErrorDetail ())
runProofChecker state = do
  -- Placeholder: real implementation would call Agent 2B's proof checker
  case pstTypeChecked state of
    Nothing ->
      let err = ErrorDetail ProofObligation "Type checked AST missing" Nothing Nothing Nothing
      in return (state, Left err)
    Just _ ->
      let state' = state { pstProofChecked = Just "verified_proofs" }
      in return (state', Right ())

{-| Run code generation stage.
-}
runCodeGen :: PipelineState -> IO (PipelineState, Either ErrorDetail ())
runCodeGen state = do
  -- Placeholder: real implementation would call Agent 6A's code generator
  case pstProofChecked state of
    Nothing ->
      let err = ErrorDetail CodeGenError "Proof checked AST missing" Nothing Nothing Nothing
      in return (state, Left err)
    Just _ ->
      let state' = state { pstCodeGen = Just "haskell_code" }
      in return (state', Right ())

-- ============================================================================
-- PIPELINE UTILITY
-- ============================================================================

{-| Run a single pipeline stage with timing and error handling.
-}
runPipelineStage
  :: String
  -> (PipelineState -> IO (PipelineState, Either ErrorDetail ()))
  -> PipelineState
  -> IO (PipelineState, StageResult, Double)
runPipelineStage stageName stageFunc state = do
  start <- getCurrentTime
  (state', result) <- stageFunc state
  end <- getCurrentTime
  let duration = realToFrac (diffUTCTime end start) * 1000
  case result of
    Right () -> return (state', mkStageResult stageName duration, duration)
    Left err -> return (state', mkFailedStageResult stageName duration err, duration)