Files
rose-ash/lib/ocaml/baseline/poly_stack.ml
giles ce75bd6848
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 39s
ocaml: phase 1+5.1 type aliases + poly_stack baseline (+3 tests, 469 / 19 baseline)
Parser: in parse-decl-type, dispatch on the post-= token:
  '|' or Ctor   -> sum type
  '{'           -> record type
  otherwise     -> type alias (skip to boundary)
AST (:type-alias NAME PARAMS) with body discarded. Runtime no-op since
SX has no nominal types.

poly_stack.ml baseline exercises:
  module type ELEMENT = sig type t val show : t -> string end
  module IntElem = struct type t = int let show x = ... end
  module Make (E : ELEMENT) = struct ... use E.show ... end
  module IntStack = Make(IntElem)

Demonstrates the substrate handles signature decls + abstract types +
functor parameter with sig constraint.
2026-05-08 20:49:26 +00:00

28 lines
651 B
OCaml

(* Baseline: polymorphic stack via functor over an Element module *)
module type ELEMENT = sig type t val show : t -> string end ;;
module IntElem = struct
type t = int
let show x = Int.to_string x
end ;;
module Make (E : ELEMENT) = struct
let create () = ref []
let push x s = s := x :: !s
let pop s =
match !s with
| [] -> None
| h :: t -> s := t ; Some h
let length s = List.length !s
let to_string s =
String.concat "," (List.map E.show !s)
end ;;
module IntStack = Make(IntElem) ;;
let s = IntStack.create () ;;
IntStack.push 1 s ;;
IntStack.push 2 s ;;
IntStack.push 3 s ;;
String.length (IntStack.to_string s)