std::path

std/src/path.mo

typePath#
pub type Path {
    path String
}

A validated filesystem path -- a plain String wrapper, following this codebase's existing newtype idiom (Identifier/Operator/ ModulePath, lang/types.mo). Construct via Path.of (validating); the bare Path.path constructor is for a string the caller has already established is non-empty (a compile-time literal, or a value it checked -- see default_output_dir in cli/src/main.mo).

instance_#
pub instance Copy Path {
    def copy (p : Path) : Pair Path Path := Pair.pair p p
}

A Path is a String wrapper, and String is Copy (an immutable pointer), so sharing one is a pointer copy, not an ownership move.

typePathError#
pub type PathError {
    empty,
}

The validating smart constructor -- the ONLY sanctioned way to build a Path from an arbitrary/external string (CLI argv, a hardcoded literal, ...). Rejects the empty string: an empty path is never a meaningful file target, and it's the concrete shape a defaulted or missing CLI argument can otherwise silently slip through as. This is deliberately narrow validation, not general path-syntax checking (no NUL-byte/..-traversal/Windows-path handling) -- fully addresses the bug this type was introduced to prevent (a naive ++-joined output path silently doubling up when one half was already absolute), without taking on open-ended "what is a valid path" scope nothing in this corpus actually needs yet. Why Path.of rejected a string. Structured rather than a message so a caller can branch on it or render it; to_string keeps the old text.

defPathError.to_string#
pub def PathError.to_string (e : PathError) : String
defPath.of#
pub def Path.of (s : String) : Result PathError Path
defPath.unsafe#
pub def Path.unsafe (s : String) : Path

The boundary constructor for a string the caller has ALREADY established is a path: a manifest-provided path, a compile-time literal, or a value that passed Path.of. It skips only the non-empty check; it is not a second validating constructor.

defPath.to_string#
pub def Path.to_string (p : Path) : String
defPath.is_absolute#
pub def Path.is_absolute (p : Path) : Bool
defraw_path_join#
pub def raw_path_join (a : String) (b : String) : String

Low-level, unvalidated join -- the same empty-component/trailing- slash-handling logic lang/module.mo's own path_join used to duplicate; that function now delegates here instead. Stays total, not routed through Path.of: the module resolver relies on an empty a/b behaving as a no-op, and forcing that through the validating constructor would change that existing, working behavior -- out of scope for this type.

An absolute b replaces a entirely (os.path.join-style semantics). This is THE fix for the bug the Path type was introduced to prevent -- the case that produced a mangled /tmp//tmp/monad_v2.ll when cli/src/main.mo's own link_ir joined a hardcoded /tmp output directory with an already-absolute user-supplied output name via plain ++. It used to live in Path.join alone, which left every DIRECT caller of this function with the bug: Mote.dep_dir_entries_go joins a manifest [dependencies.X] path onto the mote's own directory, and a bare monad check sets that directory to ".", so an absolute path dep came out as ".//home/.../init" and the dependency silently failed to resolve. Fixing it here rather than at that one call site is deliberate -- a rule kept in one place is inherited by callers that do not exist yet, which is what Path.join's old comment was asking for.

defPath.join#
pub def Path.join (a : Path) (b : Path) : Path

Delegates to raw_path_join, which owns the absolute-b rule for both spellings. Operates on already-validated Path values, so the non-empty invariant holds without re-validating (an absolute b is returned as-is and is non-empty by validation; concatenating two non-empty strings can't be empty either).

defPath.with_suffix#
pub def Path.with_suffix (p : Path) (suffix : String) : Path

Append a literal suffix (extension, filename tail) -- no separator inserted. Targets output_dir ++ "/" ++ name ++ ".ll"-style code.

defraw_parent_dir#
pub def raw_parent_dir (s : String) : String

The directory part of a path, or "" when there is none ("hello.mo" has no directory, and "" means "the current one" to every caller -- notably mkdir -p, which must not be run on it).

Total and unvalidated for the same reason raw_path_join is: callers rely on the empty result, which the validating constructor rejects. lang/src/module.mo's extract_directory delegates here rather than keeping its own copy, exactly as path_join delegates to raw_path_join.

defPath.parent#
pub def Path.parent (p : Path) : Option Path

The directory part of p, or none when it has none ("hello.mo", "/monad"): an empty Path is never constructed.

defPath.file_name#
pub def Path.file_name (p : Path) : String

The last component of p -- the whole of it when it has no /.

defPath.normalize#
pub def Path.normalize (p : Path) : Path

Collapse ./.. segments and repeated separators: a/../b -> b, ./x -> x, a//b -> a/b. Moved here from lang/module.mo so the loader, the CLI and build share one implementation; pure string work, so it never changes which file resolves, only how the winner is spelled. An empty result becomes "." (the current directory).

defPath.extension#
pub def Path.extension (p : Path) : Option String

The extension after the last . in the file name, without the dot. none when there is no dot or it is the file name's first byte (a dotfile such as .gitignore has no extension).

defPath.file_stem#
pub def Path.file_stem (p : Path) : String

The file name without its extension (/a/b.mo -> b); the whole file name when it has no extension.

defPath.child#
pub def Path.child (p : Path) (segment : String) : Path

Join a relative segment onto a path, with Path.join's absolute- segment rule but without requiring the segment to be a Path (segments are literals, never validated targets).

defPath.directory#
pub def Path.directory (p : Path) : Path

The directory that contains p, or . when it has none (main.mo, /monad): an empty Path is never constructed, and . is how every consumer spells "here".

defPath.unsafe_dir#
pub def Path.unsafe_dir (s : String) : Path

A directory spelling from text, where the empty string means the working directory and is spelled .: an empty Path is never constructed, and . is how every consumer spells "here".

defPath.child_under#
pub def Path.child_under (root : Path) (segment : String) : Path

Join a segment under a DIRECTORY root, where . is the working directory's spelling: Path.child_under (Path.unsafe_dir "") "src" is src, not ./src. That is the empty-first-component rule raw_path_join already applies, kept here so a root carried as a Path renders the same spellings it did as a string. Any other root joins normally, and an absolute segment still replaces it.

An empty SEGMENT under a . root yields the empty spelling, exactly as raw_path_join "" "" does -- a caller for which that is "not a path" filters it (lang::mote's Dependency.located_dir).

defpath_segments_go#
pub def path_segments_go (s : String) (i : I64) (start : I64) (acc : List String) : List String

s split on /, with empty segments dropped so a leading / and any // contribute nothing. Path.normalize restores the leading slash from the original string, which is the only thing it can carry.

defpath_push_segment#
pub def path_push_segment (seg : String) (acc : List String) : List String

Accumulate one segment, skipping an empty one.

defnormalize_segments_go#
pub def normalize_segments_go (segs : List String) (acc : List String) : List String

path_segments_go's output with . dropped and each .. cancelling the segment before it. A .. with nothing to cancel is KEPT, and two leading ..s keep both -- otherwise ../init/src would normalize to init/src and stop naming the file it found.

defjoin_path_segments#
pub def join_path_segments (segs : List String) (acc : String) : String

Re-join segments with a single / between them and none at either end.

defPath.beq#
pub def Path.beq (a b : Path) : Bool
instance_#
pub instance BEq Path {
    def beq (a b : Path) : Bool := Path.beq a b
}
instance_#
pub instance Hashable Path {
    def hash (p : Path) : U64 := String.hash (Path.to_string p)
}

Canonical-string hash/order, so a Path can key a map (the loader's file table) without every caller rendering it first.

instance_#
pub instance BOrd Path {
    def lt (a b : Path) : Bool := BOrd.lt (Path.to_string a) (Path.to_string b)
    def gt (a b : Path) : Bool := BOrd.gt (Path.to_string a) (Path.to_string b)
    def lte (a b : Path) : Bool := Bool.not (BOrd.gt (Path.to_string a) (Path.to_string b))
    def gte (a b : Path) : Bool := Bool.not (BOrd.lt (Path.to_string a) (Path.to_string b))
}