Sitemap
Rob Simmons

Rob Simmons

Messy draft notes on C1

--

typedef void* ht_elem;
typedef void* ht_key;
typedef ht_key (*elem_key) (ht_elem e1, ht_elem e2)
/*@requires e1 != NULL && e2 != NULL;
@requires \sametag(e1, e2);
@ensures \result != NULL @*/ ;

Ambiguity: (void*) e <- disallow this as “untag,” can’t tag everything

c0_value x; 
x.inl = 4;
x.inr = (void*)y;
//@assert \tagged(x,inl) || \tagged(x,inr); // always true
void* z = x.inr;
int y = x.inl; // RUNTIME ERROR

Equality check for void* —
Both tagged, deref and check for equality
Both untagged, true
Either tagged — deref and check for NULL

Best seeming solution: \sametag(NULL, _) always returns true. This is weird because if int* i = NULL, then \sametag((void*)i, (void*)alloc(bool)) is true.

Handling tagged NULL is tricky, because of the desire to do a data structure

push(S, (void*)NULL);
push(S, NULL);
int* x = NULL;
push(S, (void*)x);
push(S, x);

Requirement: you should be able to run the exact same code in unsafe mode.

We do want NULL to be a value of type void*,

Maybe the sametag check is the problem?

Notes in February 2014:

Returning to this later, I don’t remember why the best seeming solution was for \sametag(NULL,_) to always return true. What we did seem to decide was that, in contract to Frank’s compiler notes, the C1 compiler should not tag NULL, because in normal C code it’s not always clear, given a NULL, how it should be tagged. This does mean that nonsense code like

bool* x = (bool*)(void*)(int*)NULL;

will run successfully; but it would certainly run successfully in C as well.

Maximally exception-raising \sametag

To some degree (in hindsight), what I think I want is for a void* to be a “dyn option” from the ML perspective, where dyn is the dynamically tagged type. This means that \sametag(e1,e2) turns into Dyn.sametag(valOf e1, valOf e2) and our contracts have to be more verbose: to implement the behavior where \sametag(NULL,_) returns true, we write the contract

e1 == NULL || e2 == NULL || \sametag(e1, e2)

Comparison with the less exception-raising \sametag

This is fundamentally a “principle of least surprise” issue, not a fundamental design decision, though. If we are given the \sametag that aborts when given any nulls and want to instead implement the version that accepts either argument as NULL, we write

e1 != NULL && e2 != NULL && \sametag(e1, e2)

So ultimately my argument is a belief that the \sametag(e1,e2) that doesn’t allow any NULLs will be easier to explain, but I don’t have any reason to particularly believe that.

Update August 6

Frank: if we decide \sametag(NULL, <.,.>) = true, then \sametag will not be transitive. This might defeat its purpose in the way we are trying to use it to specify the uniformity of generic data structures.

Get Rob Simmons’s stories in your inbox

Join Medium for free to get updates from this writer.

But if it is false or an error, then I wonder how complicated the code for enforcing uniformity would get. A stack of int pointers could have NULL’s in them, so it’s not clear how to proceed.

Rob: I think it would be reasonable for generic stacks to never include NULLs. (Generic hashtables don’t use NULL, all the applications of stacks we use in assignments it doesn’t make sense to have NULLs. Stacks of integers are kind of kooky in the generic formulation in C, anyway — you’re effectively going to need to box everything, as we discussed.)

struct list_node {
ht_elem data;
struct list_node* next;
}
typedef struct list_node list;
// Diverges if list is circular!
bool is_segment(list* start, list* end) {
list* p = start;
void* prev = NULL;
while (p != end) {
if (p == NULL) return false;
if (p->data == NULL) return false;
//@assert \sametag(prev, p->data);
prev = p->data;
p = p->next;
}
return true;
}

It’s not actually much more complicated (it’s less code!) to allow nulls if we use the non-transitive semantics.

  while (p != end) {
if (p == NULL) return false;
//@assert \sametag(prev, p->data);
if (p->data != NULL) prev = p->data;
p = p->next;
}

The transitive, non-erroring semantics seem weird, though, because for the natural code you want to write all you’re allowing is a stack that contains no NULLs or all NULLs.

Options for function pointers

I think we both agree that a typedef-only approach is the way we want to go?In terms of syntax, there’s a lot of mix-and-match available.

(Additional point: will we need the typechecker for void* injections to not allow us to cast function pointers to void*...)

Typedef styles:

typedef int compare(void* x, void* y); 
// Almost correct C! But the pointer thing isn't obvious?
typedef int (*compare)(void* x, void* y);
// Correct C, relatively clear that NULL is a thing
typedef compare: int function_pointer(void*, void*);
// Something totally made up, "function_pointer" is a keyword

Constants:

sort(A, 0, 20, &compare_int); // Correct C!
sort(A, 0, 20, compare_int); // Also correct C!

Usage:

int cmp = compare(x, y); // Correct C, not obviously NULLable
int cmp = (*compare)(x, y); // Correct C, obviously a pointer

--

--