Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

~. sigdot Positive Assertion Rune #40

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions UIPS/UIP-0120.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
uip: 0120
sigilante marked this conversation as resolved.
Show resolved Hide resolved
title: Assertion Rune
description: "Add a ~: sigcol rune to handle positive assertions cleanly"
author: ~lagrev-nocfep (@sigilante)
status: Draft
type: Standards Track
category: Hoon
created: 2024-03-25
---

## Abstract

Hoon supports several related runes for error handling and debugging:

- `~&` is the classic `printf` side-effect rune.
- `~|` adds a stack annotation in case of crash.
- `~?` adds a debugging printf
- `?>` is a positive assertion (crash if fail).
- `?<` is a negative assertion (crash if pass).

A common design pattern in Hoon is to have an assertion to succeed, then to wrap
an intended error mesage with `~|` around `!!`. By coupling the `?:`, `~|`, and
`!!` together, we can achieve a more lapidary yet still clear code expression.
sigilante marked this conversation as resolved.
Show resolved Hide resolved
We propose to call this the `~:` sigcol rune.
sigilante marked this conversation as resolved.
Show resolved Hide resolved

```
~: test-to-pass
'message-on-fail'
continuation if pass
```

## Specification

The `~:` rune should desugar as follows:

```
~: test-to-pass
'message-on-fail'
continuation if pass
```

```
?: test-to-pass
sigilante marked this conversation as resolved.
Show resolved Hide resolved
~| 'message-on-fail' !!
continuation if pass
```

`~:` differs from `~?` in that it crashes with the error message (rather than the
conditional printf).

Alternatively, it could desugar via `?>`, for instance:

```
~| 'message-on-fail'
?> test-to-pass
continuation-if-pass
```

This latter branch would preserve the message in the stack trace if another element
sigilante marked this conversation as resolved.
Show resolved Hide resolved
of the tail fails. There may be a cleaner way to compose an expression using `?>`,
but the `?:` variant seems to satisfy our need.

This involves two modest changes, one to the parser and another to the AST in
`/sys/hoon`. It does not collide with any known Hoon or atom literal syntax.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ~:(jam 'foo' 12) ? That could either mean "run (jam 'foo' 12), then with that as the subject, return constant ~" -- or it could mean "if jam is no, then print 'foo' and crash; else return 12".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is resolvable, but it is a collision we'd need to make a decision about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now ~. instead, which resolves to the empty @ta.


## Rationale

- Producing code that cleanly wraps an error message around an implicit crash is
clear to read and write for many programmers.

## Backwards Compatibility

- This should be bundled into a Hoon decrement. It will not break any known
existing code.

## Copyright

Copyright and related rights waived via [CC0](../LICENSE.md).