qleisli 0.2.2

Experimental quantum language with linear ownership, exact contracts and independent finite IR verification
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import Protocol
import Init.System.IO

/-
Copyright 2026 Masahiko G. Yamada.
Licensed under the Apache License, Version 2.0; see the repository LICENSE.

Unproved file/transport adapter for the experimental kernel profiles.
It does not issue production Qleisli VerifiedProgram or evidence handles.
-/

namespace QleisliKernel.Cli

inductive Stage where
  | artifact
  | requirement
  | verification
  | usage

private def stageName : Stage → String
  | .artifact => "artifact"
  | .requirement => "requirement"
  | .verification => "verification"
  | .usage => "usage"

inductive Code where
  | accepted
  | rejected
  | syntax
  | limit
  | io
  | usage

private def codeName : Code → String
  | .accepted => "accepted"
  | .rejected => "rejected"
  | .syntax => "syntax"
  | .limit => "limit"
  | .io => "io"
  | .usage => "usage"

private def emit (accepted : Bool) (code : Code) (stage : Stage) : IO Unit := do
  let truth := if accepted then "true" else "false"
  -- All strings are closed enum values, never unescaped external text.
  IO.println ("{\"format\":\"qleisli.kernel-result\",\"version\":1," ++
    "\"profile\":\"phase256-word-v1\",\"accepted\":" ++ truth ++
    ",\"code\":\"" ++ codeName code ++ "\",\"stage\":\"" ++ stageName stage ++ "\"}")

private def readBytes (handle : IO.FS.Handle) (fuel : Nat)
    (initial : ByteArray) : IO (Except Code String) :=
  -- An explicit structural recursor keeps the compiled project declaration
  -- total as well as its logical definition; no generated partial recursion
  -- replacement is needed for this IO loop.
  Nat.rec (motive := fun _ => ByteArray → IO (Except Code String))
    (fun _ => pure (.error .limit))
    (fun _ next bytes => do
      if bytes.size > Protocol.maxInputBytes then return .error .limit
      -- At most limit+1 bytes are ever retained, even for a growing file or
      -- short reads. Each nonempty read consumes one unit of explicit fuel.
      let remaining := Protocol.maxInputBytes + 1 - bytes.size
      let chunk ← handle.read (USize.ofNat (min 4096 remaining))
      if chunk.isEmpty then
        match String.fromUTF8? bytes with
        | some text => return .ok text
        | none => return .error .syntax
      else
        next (bytes ++ chunk))
    fuel initial

private def readInput (path : System.FilePath) : IO (Except Code String) := do
  try
    IO.FS.withFile path .read fun handle =>
      readBytes handle (Protocol.maxInputBytes + 2) ByteArray.empty
  catch _ =>
    return .error .io

private def parseCode : Protocol.Error → Code
  | .syntax => .syntax
  | .limit => .limit

def run (artifactPath requirementPath : String) : IO UInt32 := do
  let artifactText ← readInput artifactPath
  let artifact ← match artifactText with
    | .error code => emit false code .artifact *> pure none
    | .ok text =>
      match Protocol.parseArtifact text with
      | .error code => emit false (parseCode code) .artifact *> pure none
      | .ok artifact => pure (some artifact)
  let some artifact := artifact | return 1
  let requirementText ← readInput requirementPath
  let requirement ← match requirementText with
    | .error code => emit false code .requirement *> pure none
    | .ok text =>
      match Protocol.parseRequirement text with
      | .error code => emit false (parseCode code) .requirement *> pure none
      | .ok requirement => pure (some requirement)
  let some requirement := requirement | return 1
  let accepted := verify artifact.word artifact.claimed requirement
  emit accepted (if accepted then .accepted else .rejected) .verification
  return if accepted then 0 else 1

private def emitDag (accepted : Bool) (code : String) (stage : Stage)
    (failureNode : Option Nat := none) (stats : Option Hierarchy.Stats := none) : IO Unit := do
  let truth := if accepted then "true" else "false"
  let node := match failureNode with
    | none => "null"
    | some value => toString value
  let metrics := match stats with
    | none => "null"
    | some value =>
      "{\"nodes\":" ++ toString value.nodes ++
      ",\"references\":" ++ toString value.references ++
      ",\"work_units\":" ++ toString value.workUnits ++
      ",\"depth\":" ++ toString value.depth ++
      ",\"expanded_gates\":" ++ toString value.expandedGates ++
      ",\"dense_dimension\":0}"
  IO.println ("{\"format\":\"qleisli.kernel-result\",\"version\":1," ++
    "\"profile\":\"phase256-dag-v1\",\"accepted\":" ++ truth ++
    ",\"code\":\"" ++ code ++ "\",\"stage\":\"" ++ stageName stage ++
    "\",\"node\":" ++ node ++ ",\"stats\":" ++ metrics ++ "}")

def runDag (artifactPath requirementPath : String) : IO UInt32 := do
  let artifactText ← readInput artifactPath
  let artifact ← match artifactText with
    | .error code => emitDag false (codeName code) .artifact *> pure none
    | .ok text =>
      match Protocol.Dag.parseArtifact text with
      | .error code => emitDag false (codeName (parseCode code)) .artifact *> pure none
      | .ok artifact => pure (some artifact)
  let some artifact := artifact | return 1
  let requirementText ← readInput requirementPath
  let requirement ← match requirementText with
    | .error code => emitDag false (codeName code) .requirement *> pure none
    | .ok text =>
      match Protocol.Dag.parseRequirement text with
      | .error code => emitDag false (codeName (parseCode code)) .requirement *> pure none
      | .ok requirement => pure (some requirement)
  let some requirement := requirement | return 1
  match Hierarchy.check artifact.definitions artifact.entry requirement with
  | .ok stats =>
    emitDag true "accepted" .verification none (some stats)
    return 0
  | .error failure =>
    let code := match failure.kind with
      | .invalidIr => "invalid_ir"
      | .contract => "contract"
      | .limit => "limit"
    emitDag false code .verification (some failure.node)
    return 1

private def emitLayout (accepted : Bool) (code : String) (stage : Stage)
    (stats : Option Layout.Stats := none) : IO Unit := do
  let truth := if accepted then "true" else "false"
  let metrics := match stats with
    | none => "null"
    | some value =>
      "{\"owners\":" ++ toString value.owners ++
      ",\"axes\":" ++ toString value.axes ++
      ",\"type_atoms\":" ++ toString value.typeAtoms ++
      ",\"work_units\":" ++ toString value.workUnits ++ ",\"dense_dimension\":0}"
  IO.println ("{\"format\":\"qleisli.kernel-result\",\"version\":1," ++
    "\"profile\":\"typed-layout-v1\",\"accepted\":" ++ truth ++
    ",\"code\":\"" ++ code ++ "\",\"stage\":\"" ++ stageName stage ++
    "\",\"stats\":" ++ metrics ++ "}")

def runLayout (artifactPath requirementPath : String) : IO UInt32 := do
  let artifactText ← readInput artifactPath
  let artifact ← match artifactText with
    | .error code => emitLayout false (codeName code) .artifact *> pure none
    | .ok text =>
      match Protocol.Layout.parseArtifact text with
      | .error code => emitLayout false (codeName (parseCode code)) .artifact *> pure none
      | .ok artifact => pure (some artifact)
  let some artifact := artifact | return 1
  let requirementText ← readInput requirementPath
  let requirement ← match requirementText with
    | .error code => emitLayout false (codeName code) .requirement *> pure none
    | .ok text =>
      match Protocol.Layout.parseRequirement text with
      | .error code => emitLayout false (codeName (parseCode code)) .requirement *> pure none
      | .ok requirement => pure (some requirement)
  let some requirement := requirement | return 1
  match Layout.check artifact.layout artifact.witness requirement with
  | .ok stats =>
    emitLayout true "accepted" .verification (some stats)
    return 0
  | .error failure =>
    let code := match failure with
      | .invalidIr => "invalid_ir"
      | .contract => "contract"
      | .limit => "limit"
    emitLayout false code .verification
    return 1

private def emitLayoutDag (accepted : Bool) (code : String) (stage : Stage)
    (node : Option Nat := none) (stats : Option LayoutDag.Stats := none) : IO Unit := do
  let truth := if accepted then "true" else "false"
  let node := match node with | none => "null" | some value => toString value
  let metrics := match stats with
    | none => "null"
    | some value =>
      "{\"nodes\":" ++ toString value.nodes ++
      ",\"references\":" ++ toString value.references ++
      ",\"work_units\":" ++ toString value.workUnits ++
      ",\"depth\":" ++ toString value.depth ++
      ",\"expanded_layouts\":" ++ toString value.expandedLayouts ++
      ",\"dense_dimension\":0}"
  IO.println ("{\"format\":\"qleisli.kernel-result\",\"version\":1," ++
    "\"profile\":\"typed-layout-dag-v1\",\"accepted\":" ++ truth ++
    ",\"code\":\"" ++ code ++ "\",\"stage\":\"" ++ stageName stage ++
    "\",\"node\":" ++ node ++ ",\"stats\":" ++ metrics ++ "}")

def runLayoutDag (artifactPath requirementPath : String) : IO UInt32 := do
  let artifactText ← readInput artifactPath
  let artifact ← match artifactText with
    | .error code => emitLayoutDag false (codeName code) .artifact *> pure none
    | .ok text =>
      match Protocol.LayoutDag.parseArtifact text with
      | .error code => emitLayoutDag false (codeName (parseCode code)) .artifact *> pure none
      | .ok artifact => pure (some artifact)
  let some artifact := artifact | return 1
  let requirementText ← readInput requirementPath
  let requirement ← match requirementText with
    | .error code => emitLayoutDag false (codeName code) .requirement *> pure none
    | .ok text =>
      match Protocol.Layout.parseRequirement text with
      | .error code => emitLayoutDag false (codeName (parseCode code)) .requirement *> pure none
      | .ok requirement => pure (some requirement)
  let some requirement := requirement | return 1
  match LayoutDag.check artifact.definitions artifact.entry requirement with
  | .ok stats =>
    emitLayoutDag true "accepted" .verification none (some stats)
    return 0
  | .error failure =>
    let code := match failure.kind with
      | .invalidIr => "invalid_ir"
      | .contract => "contract"
      | .limit => "limit"
    emitLayoutDag false code .verification (some failure.node)
    return 1

def usage : IO UInt32 := do
  emit false .usage .usage
  return 2

private def emitPhaseLayout (accepted : Bool) (code : String) (stage : Stage)
    (node : Option Nat := none) (stats : Option PhaseLayout.Stats := none) : IO Unit := do
  let truth := if accepted then "true" else "false"
  let node := match node with | none => "null" | some value => toString value
  let metrics := match stats with
    | none => "null"
    | some value =>
      "{\"nodes\":" ++ toString value.layout.nodes ++
      ",\"references\":" ++ toString value.layout.references ++
      ",\"work_units\":" ++ toString (value.layout.workUnits + value.phaseWork) ++
      ",\"phase_work_units\":" ++ toString value.phaseWork ++
      ",\"depth\":" ++ toString value.layout.depth ++
      ",\"expanded_layouts\":" ++ toString value.layout.expandedLayouts ++
      ",\"expanded_phase_terms\":" ++ toString value.expandedPhaseTerms ++
      ",\"terms\":" ++ toString value.terms ++ ",\"dense_dimension\":0}"
  IO.println ("{\"format\":\"qleisli.kernel-result\",\"version\":1," ++
    "\"profile\":\"typed-phase256-dag-v1\",\"accepted\":" ++ truth ++
    ",\"code\":\"" ++ code ++ "\",\"stage\":\"" ++ stageName stage ++
    "\",\"node\":" ++ node ++ ",\"stats\":" ++ metrics ++ "}")

def runPhaseLayout (artifactPath requirementPath : String) : IO UInt32 := do
  let artifactText ← readInput artifactPath
  let artifact ← match artifactText with
    | .error code => emitPhaseLayout false (codeName code) .artifact *> pure none
    | .ok text =>
      match Protocol.PhaseLayout.parseArtifact text with
      | .error code => emitPhaseLayout false (codeName (parseCode code)) .artifact *> pure none
      | .ok artifact => pure (some artifact)
  let some artifact := artifact | return 1
  let requirementText ← readInput requirementPath
  let requirement ← match requirementText with
    | .error code => emitPhaseLayout false (codeName code) .requirement *> pure none
    | .ok text =>
      match Protocol.PhaseLayout.parseRequirement text with
      | .error code => emitPhaseLayout false (codeName (parseCode code)) .requirement *> pure none
      | .ok requirement => pure (some requirement)
  let some requirement := requirement | return 1
  match PhaseLayout.check artifact.definitions artifact.entry requirement with
  | .ok stats =>
    emitPhaseLayout true "accepted" .verification none (some stats)
    return 0
  | .error failure =>
    let code := match failure.kind with
      | .invalidIr => "invalid_ir"
      | .contract => "contract"
      | .limit => "limit"
    emitPhaseLayout false code .verification (some failure.node)
    return 1

private def readHierarchyBytes (handle : IO.FS.Stream) (fuel : Nat)
    (initial : ByteArray) : IO (Except Code ByteArray) :=
  Nat.rec (motive := fun _ => ByteArray → IO (Except Code ByteArray))
    (fun _ => pure (.error .limit))
    (fun _ next bytes => do
      if bytes.size > Protocol.Hierarchical.maxBridgeBytes then return .error .limit
      let remaining := Protocol.Hierarchical.maxBridgeBytes + 1 - bytes.size
      let chunk ← handle.read (min 4096 remaining).toUSize
      if chunk.isEmpty then return .ok bytes
      next (bytes ++ chunk)) fuel initial

private def hierarchyFailure (code : String) : IO UInt32 := do
  IO.println ("qleisli.hierarchy-pending 1\nerror\n" ++ code)
  return 1

/-- A data-only pending response from a fresh actual whole-artifact check.
The host must reconstruct every returned finite index on this same artifact. -/
def runHierarchy : IO UInt32 := do
  let input ← try
      readHierarchyBytes (← IO.getStdin) 1000001 ByteArray.empty
    catch _ => pure (.error .io)
  let input ← match input with
    | .error .limit => return (← hierarchyFailure "limit")
    | .error _ => return (← hierarchyFailure "format")
    | .ok bytes => pure bytes
  let artifact ← match Protocol.Hierarchical.parse input with
    | .error .syntax => return (← hierarchyFailure "format")
    | .error .limit => return (← hierarchyFailure "limit")
    | .ok artifact => pure artifact
  match Hierarchical.Conditional.checkAll artifact.1 artifact.2 with
  | .error error => hierarchyFailure (match error.kind with
      | .limit => "limit" | .contract => "contract" | .invalidIr => "invalid_ir")
  | .ok pending =>
    IO.println "qleisli.hierarchy-pending 1\npending"
    IO.println pending.state.visits
    IO.println pending.state.requests.size
    for request in pending.state.requests do IO.println request.index
    return 0

private def requestFailure (code : String) : IO UInt32 := do
  IO.println ("qleisli.hierarchy-request-pending 1\nerror\n" ++ code)
  return 1

/-- Fresh binding to independent requested meaning. Finite equations remain
explicit host obligations; no request is synthesized from the artifact. -/
def runHierarchyRequest : IO UInt32 := do
  let input ← try
      readHierarchyBytes (← IO.getStdin) 1000001 ByteArray.empty
    catch _ => pure (.error .io)
  let input ← match input with
    | .error .limit => return (← requestFailure "limit")
    | .error _ => return (← requestFailure "format")
    | .ok bytes => pure bytes
  let packet ← match Protocol.Hierarchical.parseRequest input with
    | .error .syntax => return (← requestFailure "format")
    | .error .limit => return (← requestFailure "limit")
    | .ok packet => pure packet
  match Hierarchical.Root.checkAll packet.artifact packet.order packet.request packet.pairs packet.pairOrder with
  | .error error => requestFailure (match error.kind with
      | .limit => "limit" | .contract => "contract" | .invalidIr => "invalid_ir")
  | .ok checked =>
    IO.println "qleisli.hierarchy-request-pending 1\npending"
    IO.println (checked.artifact.state.visits + checked.binding.visits)
    IO.println checked.artifact.state.requests.size
    for request in checked.artifact.state.requests do IO.println request.index
    IO.println checked.binding.requests.length
    for index in checked.binding.requests do IO.println index
    return 0

private def fourierFailure (code : String) : IO UInt32 := do
  IO.println ("qleisli.hierarchy-fourier-pending 1\nerror\n" ++ code)
  return 1

/-- Fresh full-artifact and named Fourier inspection. The host reconstructs
every finite proof and every additional exact-H index on the same input. -/
def runHierarchyFourier : IO UInt32 := do
  let input ← try
      readHierarchyBytes (← IO.getStdin) 1000001 ByteArray.empty
    catch _ => pure (.error .io)
  let input ← match input with
    | .error .limit => return (← fourierFailure "limit")
    | .error _ => return (← fourierFailure "format")
    | .ok bytes => pure bytes
  let packet ← match Protocol.Hierarchical.parseFourier input with
    | .error .syntax => return (← fourierFailure "format")
    | .error .limit => return (← fourierFailure "limit")
    | .ok packet => pure packet
  match Hierarchical.FourierRoot.checkAll packet.artifact packet.order packet.request packet.wiringOrder with
  | .error error => fourierFailure (match error.kind with
      | .limit => "limit" | .contract => "contract" | .invalidIr => "invalid_ir")
  | .ok checked =>
    IO.println "qleisli.hierarchy-fourier-pending 1\npending"
    IO.println (checked.artifact.state.visits + checked.binding.visits)
    IO.println checked.artifact.state.requests.size
    for request in checked.artifact.state.requests do IO.println request.index
    IO.println checked.binding.body.requests.length
    for request in checked.binding.body.requests do IO.println request.leafIndex
    return 0

private def readoutFailure (code : String) : IO UInt32 := do
  IO.println ("qleisli.readout-result 1\nerror\n" ++ code)
  return 1

/-- Check only the supplied readout slice. This result cannot stand in for
verification of a preceding circuit, source, initialization or QPE request. -/
def runReadout : IO UInt32 := do
  let input ← try
      readHierarchyBytes (← IO.getStdin) 1000001 ByteArray.empty
    catch _ => pure (.error .io)
  let input ← match input with
    | .error .limit => return (← readoutFailure "limit")
    | .error _ => return (← readoutFailure "format")
    | .ok bytes => pure bytes
  let packet ← match Protocol.Hierarchical.parseReadout input with
    | .error .syntax => return (← readoutFailure "format")
    | .error .limit => return (← readoutFailure "limit")
    | .ok packet => pure packet
  match Hierarchical.Readout.check packet.request packet.packet packet.budget with
  | .error error => readoutFailure (match error with
      | .limit => "limit" | .contract => "contract" | .invalidIr => "invalid_ir")
  | .ok checked =>
    IO.println "qleisli.readout-result 1\nchecked"
    IO.println checked.visits
    return 0

private def preparationFailure (code : String) : IO UInt32 := do
  IO.println ("qleisli.preparation-result 1\nerror\n" ++ code)
  return 1

def runPreparation : IO UInt32 := do
  let input ← try
      readHierarchyBytes (← IO.getStdin) 1000001 ByteArray.empty
    catch _ => pure (.error .io)
  let input ← match input with
    | .error .limit => return (← preparationFailure "limit")
    | .error _ => return (← preparationFailure "format")
    | .ok bytes => pure bytes
  let packet ← match Protocol.Hierarchical.parsePreparation input with
    | .error .syntax => return (← preparationFailure "format")
    | .error .limit => return (← preparationFailure "limit")
    | .ok packet => pure packet
  match Hierarchical.Preparation.check packet.request packet.packet packet.budget with
  | .error error => preparationFailure (match error with
      | .limit => "limit" | .contract => "contract" | .invalidIr => "invalid_ir")
  | .ok checked =>
    IO.println "qleisli.preparation-result 1\nchecked"
    IO.println checked.visits
    return 0

private def instrumentFailure (code : String) : IO UInt32 := do
  IO.println ("qleisli.instrument-pending 1\nerror\n" ++ code)
  return 1

/-- Fresh composition of initialization, the independently requested pure root
and readout. Every finite equation remains a mandatory host obligation. -/
def runInstrument : IO UInt32 := do
  let input ← try
      readHierarchyBytes (← IO.getStdin) 1000001 ByteArray.empty
    catch _ => pure (.error .io)
  let input ← match input with
    | .error .limit => return (← instrumentFailure "limit")
    | .error _ => return (← instrumentFailure "format")
    | .ok bytes => pure bytes
  let packet ← match Protocol.Hierarchical.parseInstrument input with
    | .error .syntax => return (← instrumentFailure "format")
    | .error .limit => return (← instrumentFailure "limit")
    | .ok packet => pure packet
  match Hierarchical.Instrument.checkAll packet.request packet.packet with
  | .error error => instrumentFailure (match error.kind with
      | .limit => "limit" | .contract => "contract" | .invalidIr => "invalid_ir")
  | .ok pending =>
    IO.println "qleisli.instrument-pending 1\npending"
    IO.println pending.visits
    IO.println pending.circuit.artifact.state.requests.size
    for request in pending.circuit.artifact.state.requests do IO.println request.index
    IO.println pending.circuit.binding.requests.length
    for index in pending.circuit.binding.requests do IO.println index
    return 0

private def qpeInstrumentFailure (code : String) : IO UInt32 := do
  IO.println ("qleisli.qpe-instrument-pending 1\nerror\n" ++ code)
  return 1

/-- Every pure finite equation, independent provider pair and exact H role is
returned for fresh host reconstruction. This mode enables no external schema. -/
def runQpeInstrument : IO UInt32 := do
  let input ← try
      readHierarchyBytes (← IO.getStdin) 1000001 ByteArray.empty
    catch _ => pure (.error .io)
  let input ← match input with
    | .error .limit => return (← qpeInstrumentFailure "limit")
    | .error _ => return (← qpeInstrumentFailure "format")
    | .ok bytes => pure bytes
  let packet ← match Protocol.Hierarchical.parseQpeInstrument input with
    | .error .syntax => return (← qpeInstrumentFailure "format")
    | .error .limit => return (← qpeInstrumentFailure "limit")
    | .ok packet => pure packet
  match Hierarchical.QpeInstrument.checkAll packet.request packet.packet with
  | .error error => qpeInstrumentFailure (match error.kind with
      | .limit => "limit" | .contract => "contract" | .invalidIr => "invalid_ir")
  | .ok pending =>
    IO.println "qleisli.qpe-instrument-pending 1\npending"
    IO.println pending.visits
    IO.println pending.circuit.artifact.state.requests.size
    for request in pending.circuit.artifact.state.requests do IO.println request.index
    IO.println pending.circuit.provider.requests.length
    for index in pending.circuit.provider.requests do IO.println index
    let hadamards := ((packet.packet.circuit.candidate.hadamards.toList.map (·.index)) ++
      pending.circuit.schedule.inverse.fourier.body.requests.map (·.leafIndex)).eraseDups
    IO.println hadamards.length
    for index in hadamards do IO.println index
    return 0

end QleisliKernel.Cli

def main (args : List String) : IO UInt32 :=
  match args with
  | ["--qpe-instrument-pending"] => QleisliKernel.Cli.runQpeInstrument
  | ["--instrument-pending"] => QleisliKernel.Cli.runInstrument
  | ["--readout-check"] => QleisliKernel.Cli.runReadout
  | ["--preparation-check"] => QleisliKernel.Cli.runPreparation
  | ["--hierarchy-fourier-pending"] => QleisliKernel.Cli.runHierarchyFourier
  | ["--hierarchy-request-pending"] => QleisliKernel.Cli.runHierarchyRequest
  | ["--hierarchy-pending"] => QleisliKernel.Cli.runHierarchy
  | ["--phase-layout", artifact, requirement] => QleisliKernel.Cli.runPhaseLayout artifact requirement
  | ["--layout-dag", artifact, requirement] => QleisliKernel.Cli.runLayoutDag artifact requirement
  | ["--layout", artifact, requirement] => QleisliKernel.Cli.runLayout artifact requirement
  | ["--phase-dag", artifact, requirement] => QleisliKernel.Cli.runDag artifact requirement
  | [artifact, requirement] => QleisliKernel.Cli.run artifact requirement
  | _ => QleisliKernel.Cli.usage