Stack(3)
π Stack(3) β the OCaml Stack module, explained bestie-style π₯
okay so this is <pink>Stack</pink>, from the <purple>OCaml library</purple> β the manpage you're reading rn is literally called Stack(3). and honestly? she's iconic, she's simple, she's giving main character energy. let's get into it β¨
π NAME π
Stack β last-in first-out stacks. periodt. π
πΈ Module πΈ
Module Stack. yes bestie that's the whole thing. the module is the module π«Ά
π Documentation π
Module Stack
that's a sig end situation β like, : sig end β and iykyk, it's just the signature wrapper, don't overthink it π
π¦ so what IS it π¦
Last-in first-out stacks. fr fr.
This module implements stacks (LIFOs), with in-place modification. so like, it literally mutates the stack in place β no cap, that's the whole vibe π
β οΈ Alert <hotpink>unsynchronized_access</hotpink>. Unsynchronized accesses to stacks are a programming error. like, straight up a bug on your end, bestie π
π Unsynchronized accesses β read this part, seriously π
Unsynchronized accesses to a stack may lead to an invalid queue state. Thus, concurrent accesses to stacks must be synchronized (for instance with a Mutex.t).
π yeah. "invalid queue state" is not a vibe. lock your stacks or suffer. use a Mutex.t, she's right there.
β¨ the types and the crew β¨
type !'a tβ the type of stacks containing elements of type'a. that!is doing a little something (it's there, we keep it, we love her) πexception Emptyβ raised whenStack.poporStack.topis applied to an empty stack. she WILL yell at you. π
π₯ the functions π₯
π the basics π
val create : unit -> 'a tβ return a new stack, initially empty. fresh out the box, clean slate, new era πval push : 'a -> 'a t -> unitβpush x sadds the elementxat the top of stacks. she's on top, as she deserves πval pop : 'a t -> 'aβpop sremoves and returns the topmost element in stacks, or raisesStack.Emptyif the stack is empty. take her off the top and hand her to you β but if there's nothing there?Stack.Empty, sorry bestie πval pop_opt : 'a t -> 'a optionβpop_opt sremoves and returns the topmost element in stacks, or returnsNoneif the stack is empty. same energy aspopbut the chill non-dramatic version π«Ά Since 4.08val drop : 'a t -> unitβdrop sremoves the topmost element in stacks, or raisesStack.Emptyif the stack is empty. no return value, she just vanishes. ghosting, but legal π Since 5.1val top : 'a t -> 'aβtop sreturns the topmost element in stacks, or raisesStack.Emptyif the stack is empty. peek only, no removal β look but don't touch energy β¨val top_opt : 'a t -> 'a optionβtop_opt sreturns the topmost element in stacks, orNoneif the stack is empty. peek, but make itoptionπΈ Since 4.08val clear : 'a t -> unitβ discard all elements from a stack. block button. delete. unfollow. she's empty now π§Ήval copy : 'a t -> 'a tβ return a copy of the given stack. twin flame behavior, but like, a duplicate stack π¦val is_empty : 'a t -> boolβ returntrueif the given stack is empty,falseotherwise. vibe check on the emptiness πval length : 'a t -> intβ return the number of elements in a stack. Time complexityO(1)β yes bestie, constant time, we stay efficient π₯
πΌ the iteration girlies πΌ
val iter : ('a -> unit) -> 'a t -> unitβiter f sappliesfin turn to all elements ofs, from the element at the top of the stack to the element at the bottom of the stack. The stack itself is unchanged. top to bottom, one by one, no touching the originals πval fold : ('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'accβfold f accu sis(f (... (f (f accu x1) x2) ...) xn)wherex1is the top of the stack,x2the second element, andxnthe bottom element. The stack is unchanged. yes she's got that nesting thing going on, it's giving recursion core, we love her β¨ Since 4.03
π Stacks and Sequences π
val to_seq : 'a t -> 'a Seq.tβ iterate on the stack, top to bottom. It is safe to modify the stack during iteration. ooooh heads up bestie, you CAN modify mid-iteration here, that's the special sauce π Since 4.07val add_seq : 'a t -> 'a Seq.t -> unitβ add the elements from the sequence on the top of the stack. dumping the whole seq on top, topping her off π Since 4.07val of_seq : 'a Seq.t -> 'a tβ create a stack from the sequence. seq goes in, stack comes out, no cap π Since 4.07
π
tiny reminder before you go: this whole module does in-place modification, so if you're doing concurrent accesses, synchronize them (like with a Mutex.t) or you're gonna have a bad time. the alert said it, we said it, it's real.
πΈ that's the whole page bestie! no notes, she's complete, every function and Since tag accounted for π
OCamldoc β 2026-06-22 β Stack(3) π and yes, that's the version + date sign-off, cute right? π«Ά
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms