···
+
module type Applicative = sig
+
val return : 'a -> 'a t
+
val fmap : ('a -> 'b) -> 'a t -> 'b t
+
val mbind : ('a -> 'b t) -> 'a t -> 'b t
+
val apply : ('a -> 'b) t -> 'a t -> 'b t
+
module type Selective = sig
+
val select : ('a, 'b) Either.t t -> ('a -> 'b) t -> 'b t
+
module T (A : Applicative) = struct
+
let do_thing (a : _ A.t) (v : _ A.t) =
+
let v1 = A.mbind (fun i -> if Random.int i < 5 then A.mbind (fun v -> A.return @@ v ^ "hello") v else A.return "world") a in
+
let v2 = A.fmap (fun i -> if Random.int i < 5 then "hello" else "world") a in
+
module Make (S : Selective) = struct
+
let ( <*? ) x f = S.select x f
+
let map ~f x = apply (return f) x
+
map x ~f:(Either.map ~left:Fun.id ~right:Either.left)
+
<*? map l ~f:(Fun.compose Either.right)
+
(map x ~f:(fun b -> if b then Either.Left () else Either.Right ()))
+
(map t ~f:Fun.const) (map f ~f:Fun.const)
+
let when' x act = if' x act (return ())
+
let ( <||> ) a b = if' a (return true) b
+
let ( <&&> ) a b = if' a b (return false)
+
module Shl (S : Selective) = struct
+
module Shelter = Shelter_main
+
| From : string -> step
+
| Copy : string * string -> step
+
| Parallel : string list -> step
+
type 'a with_session = { session : string; step : 'a }
+
type 'a llist = Singleton of 'a | Cons of 'a * 'a llist
+
let rec map f = function
+
| Singleton v -> Singleton (f v)
+
| Cons (x, xs) -> Cons (f x, map f xs)
+
type t = step with_session llist
+
Select.return (Singleton { session = "main"; step = From image })
+
Select.return (function (Singleton prev | Cons (prev, _)) as l ->
+
Cons ({ prev with step = Run cmd }, l))
+
let copy ~src ~dst = Select.return (Copy (src, dst))
+
Select.return (function (Singleton step | Cons (step, _)) as l ->
+
Cons ({ step with session }, l))
+
let with_session session = Select.return (map (fun v -> { v with session }))
+
let rec to_list = function
+
| Cons (x, xs) -> x :: to_list xs
+
(Select.return (fun steps ->
+
to_list steps |> List.rev
+
| { session; step = From from } ->
+
Printf.sprintf "(%s) FROM %s" session from
+
| { session; step = Run cmd } ->
+
Printf.sprintf "(%s) RUN %s" session cmd
+
| { session; step = Copy (src, dst) } ->
+
Printf.sprintf "(%s) COPY %s %s" session src dst
+
|> String.concat "\n"))
+
module Identity = Make (struct
+
let select e f = match e with Either.Left v -> f v | Either.Right b -> b
+
module D = Shl (Identity)
+
let base_image = from "alpine" in
+
let is_node_lst img = String.equal "v22.15.0" (stdout img) in
+
let node_version = run "node --version" base in
+
(Select.map ~f:is_node_lst node_version)
+
(run "node -e 'console.log('success!')")
+
(run "node -e 'console.log('failure!')")
+
with_session "node" (cmds base_image)
+
let () = print_endline (D.build dockerfile)