ellisk42 / ec

MIT License
487 stars 140 forks source link

Defining ocaml primitives of type tuple #106

Open thelogicalgrammar opened 2 years ago

thelogicalgrammar commented 2 years ago

If I understand correctly, the situation is the following. To define new domains, we sometimes have to define new primitives in ocaml, which means we have to define their types. However, we can't use ocaml's typing system directly, but rather we have to define types with dreamcoder's typing system. Dreamcoder's types (in ocaml) are objects of type tp. Tp is a sum type, with two kinds of elements, TID (for type variables) and TCon (for all other types). Each TCon object then is a tuple with three elements:

  1. The name of the primitive
  2. A (possibly empty) list of the types of the arguments to that primitive
  3. A boolean, specifying recursively whether any of the arguments is a type variable

For instance, the tlist type is defined as

let tlist t = TCon("list",[t], ts |> List.exists ~f:is_polymorphic);;

and the type of a function from int to int is defined as

let tint = TCon('int',[],false);;
let tintintf = TCon('intintf',[tint;tint],false);;

What I am wondering is whether it would be possible to implement a tuple type, e.g. as:

let ttuple t1 t2 = TCon('tuple',[t1*t2],  [t1;t2] |> List.exists ~f:is_polymorphic);;
let ttriple t1 t2 t3 = TCon('tuple',[t1*t2*t3], [t1;t2;t3] |> List.exists ~f:is_polymorphic);;

and how would we use it to construct primitives and how it would interface with python primitives.

DanieleMarchei commented 2 years ago

I stumbled upon this line of code while reading type.py

def tpair(a, b): return TypeConstructor("pair", [a, b])

I guess pairs are already defined, and I think they are easily extensible too.