package cmdlang-stdlib-runner

  1. Overview
  2. Docs

Source file arg_runner.ml

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
(*********************************************************************************)
(*  cmdlang - Declarative command-line parsing for OCaml                         *)
(*  SPDX-FileCopyrightText: 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com>  *)
(*  SPDX-License-Identifier: MIT                                                 *)
(*********************************************************************************)

type 'a t =
  | Value : 'a -> 'a t
  | Map :
      { x : 'a t
      ; f : 'a -> 'b
      }
      -> 'b t
  | Both : 'a t * 'b t -> ('a * 'b) t
  | Apply :
      { f : ('a -> 'b) t
      ; x : 'a t
      }
      -> 'b t

let rec eval : type a. a t -> a =
  fun (type a) (t : a t) : a ->
  match t with
  | Value a -> a
  | Map { x; f } -> f (eval x)
  | Both (a, b) ->
    let a = eval a in
    let b = eval b in
    a, b
  | Apply { f; x } ->
    let f = eval f in
    let x = eval x in
    f x
;;