Standard ML / CML wrong operator - operand error - concurrency

I am trying to implement a concurrent list using CML extensions of Standard ML but i am running into errors that are probably to do with my being a newbie in Standard ML. I have implemented the clist as having an input and output channel and I store the list state in a loop. However my code does not compile and gives errors below
structure Clist : CLIST =
struct
open CML
datatype 'a request = CONS of 'a | HEAD
datatype 'a clist = CLIST of { reqCh : 'a request chan, replyCh : 'a chan }
(* create a clist with initial contents l *)
val cnil =
let
val req = channel()
val reply = channel()
fun loop l = case recv req of
CONS x =>
(loop (x::l))
|
HEAD => (send(reply, l); loop l)
in
spawn(fn () => loop nil);
CLIST {reqCh=req,replyCh=reply}
end
fun cons x (CLIST {reqCh, replyCh})=
(send (reqCh, CONS x); CLIST {reqCh = reqCh, replyCh = replyCh})
fun hd (CLIST {reqCh, replyCh}) = (send (reqCh, HEAD); recv replyCh)
end
This is the signature file
signature CLIST =
sig
type 'a clist
val cnil : 'a clist
val cons : 'a -> 'a clist -> 'a clist
val hd : 'a clist -> 'a
end
Errors I am getting:
clist.sml:21.4-21.35 Error: operator and operand don't agree [circularity]
operator domain: {replyCh:'Z list chan, reqCh:'Z list request chan}
operand: {replyCh:'Z list chan, reqCh:'Z request chan}
in expression:
CLIST {reqCh=req,replyCh=reply}

So your problem is in your definition of clist
datatype 'a clist = CLIST of { reqCh : 'a request chan, replyCh : 'a chan }
This is saying that the request channel takes in a request of 'a and replies with a 'a. This is not consistent with your implementation. When you send a CONS x request on the channel, you are saying add x of type 'a to the list, but when you send a HEAD request, you are saying give me back the entire list. Thus, a CONS request should take a 'a and a HEAD request should return a 'a list. You can fix your problem by changing your clist definition to
datatype 'a clist = CLIST of { reqCh : 'a request chan, replyCh : 'a list chan }
I would also suggest changing your definition of cnil to a unit -> 'a clist function, this way you can create distinct concurrent lists.
For example:
val l1 = Clist.cnil()
val l2 = Clist.cnil() (*cons'ing onto l2 won't affect l1*)

Related

Is there an universal printer in OCaml that detect the type?

I want to print a list with different element in it (for educational purpose)
I have read a tutorial that explain how to store different type in list.
type _ list =
[] : unit list
| ( :: ) : 'b * 'a list -> ('b ->'a) list;;
1 :: "e" :: 'r' :: [];; (* this is allowed *)
how I can do something like this pseudo-code:
match typeof(my_expr) with
int -> print_int
| string -> print_string
we will have "1,e,r" printed.
Some solutions i have searched
Change my type in text and printing it
Use a different type definition maybe ('a, 'b) list ?
I ask this because the OCaml toplevel know the type of every variable and show always the type in the right format: can I call this printer ?
Is there a solution only for toplevel that we can install with the #install_printer ?
I know that compiler discard type's info after the type checking pass.
The printer of the toplevel should work fine:
[1; "one"; 1.];;
- : (int -> string -> float -> unit) list =
(::) (1, (::) ("one", (::) (1., [])))
(The unoptimal printing is an unfortunate consequence of ensuring that values printed by the toplevel can be copy-pasted back to the top-level and yields the same value)
But this is only possible outside of the language itself: the toplevel printers can inspect the typing environment which is purposefully not possible in the language itself. Indeed functions like typeof would break parametricity. There is thus no universal printer function in OCaml (without looking at the internal memory representation) and no universal heterogeneous list printer.
If you want to print an heterogeneous list, there are three possible paths:
print a specific type of the heterogeneous list
let print_concrete ppf (x::y::z::rest) = Format.fprintf ppf "%f %f %f" x y z
(Contrary to appearance, this function is total: its type makes it impossible to use on lists with fewer than three elements)
Use heterogeneous lists that always pack a printing function along its main value
type 'a printer = Format.formatter -> 'a -> unit
type _ showable_list =
| [] : unit showable_list
| (::):
('a * 'a printer) * 'b showable_list
-> ('a -> 'b) showable_list
let rec print: type a. a showable_list printer =
fun ppf l -> match l with
| [] -> ()
| (a,printer) :: rest -> Format.fprintf ppf "%a# %a" printer a print rest
provide a matching heterogeneous list of printing functions
type 'a plist =
| []: unit plist
| (::): 'a printer * 'b plist -> ('a -> 'b) plist
let rec print: type l. l plist -> l list printer = fun printers ppf values ->
match printers, values with
| [], [] -> ()
| p :: prest, a :: rest -> Format.fprintf ppf "%a# %a" p a (print prest) rest
The fact that you often need to specialize the heterogeneous list type may make it worthwhile to introduce a functor for generating them:
module Hlist(Specialization: sig type 'a t end) = struct
open Specialization
type 'a list =
| []: unit list
| (::): 'a t * 'b list -> ('a -> 'b) list
end
then the previous specialized type can be constructed with
module Showable_list = Hlist(struct type 'a t = 'a * 'a printer end)
module Printer_list = Hlist (struct type 'a t = 'a printer end)

Reverse Mutable List Ocaml

I need to write a function that takes two mutable lists as input and as an output has the first list reversed appended to the second list. The type definition is:
type 'a mylist = 'a listcell ref
and 'a listcell = Nil | Cons of 'a * ('a mylist)
I am aware how to reverse a regular list but am having some confusion on writing a function that does this by destructively changing the first list.
Here is what I have but I keep getting a type error:
let rec rev_app l1 l2 =
let rec rev_app' l3 l4 =
match !l1 with
| Nil -> l2
| Cons (x,t) -> ref (rev_app t (Cons(x,l4)))
in rev_app' l1 l2
Your inner call to is to rev_app, not to rev_app'. So your code is somewhat similar to saying:
let rec f x = ref (f x)
As a side comment, it is good manners to quote the actual error you're getting. Just saying "a type error" isn't so helpful.

How do I hide a constructor?

I'm trying to build a type LazyList but hide the definition of LazyList.t. I've got the following files:
LazyList.ml
type 'a t =
| Cell of ('a * 'a t) option Lazy.t
;;
let rec from_list l = ...;;
let rec from_string s = ...;;
let rec from_in_channel c = ...;;
let rec to_list l = ...;;
LazyList.mli
type 'a t;;
val from_list : 'a list -> 'a t;
val from_string : string -> char t;
val from_in_channel : in_channel -> char t;
val to_list : 'a t -> 'a list;
When I run ocamlc LazyList.mli I get the following error:
File "LazyList.mli", line 1, characters 9-10:
Error: Syntax error
What gives? Do I need to expose the constructor if I'm going to be writing all of the methods I need in LazyList.ml?
This is only a syntax error. Semicolons cannot appear in mli file. Also you really do not need to use this ugly double semicolons in ml file, although it is not an error.

OCaml: the variant type unit has no constructor ::

I'm trying to implement sets through lists.. This is the code with the implementation (I omitted the interface):
module MySet : Set =
struct
type 'a set = 'a list
let empty : 'a set = []
let add (x: 'a) (s: 'a set) : 'a set =
if not(List.mem x s) then x::s
let remove (x: 'a) (s: 'a set) : 'a set =
let rec foo s res =
match s with
| [] -> List.rev res
| y::ys when y = x -> foo ys res
| y::ys -> foo ys (y::res)
in foo s []
let list_to_set (l: 'a list) : 'a set =
let rec foo l res =
match l with
| [] -> List.rev res
| x::xs when member x xs -> foo xs res
| x::xs -> foo xs (x::res)
in foo l []
let member (x: 'a) (s: 'set) : bool =
List.mem x s
let elements (s: 'a set) : 'a list =
let rec foo s res =
match s with
| [] -> List.rev res
| x::xs -> foo xs (x::res)
in foo s []
end;;
This is the error I get
Characters 162-164:
if not(List.mem x s) then x::s
^^
Error: The variant type unit has no constructor ::
I can't understand the error
It's a very confusing message that we got since 4.01 that stems from the fact that you have no else branch and that () is a valid constructor for unit.
Since you have no else branch the whole if must type to unit and thus the then branch aswell and it tries to unify the expression in the then branch with a value of type unit and detects that :: is not a constructor for values of type unit.
What you wanted to write is:
if not (List.mem x s) then x :: s else s
Without an else branch your add function needs to type to 'a -> 'a set -> unit
The strange error message is being bug tracked in OCaml's issue tracker, see PR 6173.

Lazy suspended tail in sml

I was going through some notes and I realized something is amiss.
When emulating lazy computation (without open Lazy;) one can do the following for a stream of ones.
datatype 'a susp = Susp of (unit -> 'a)
datatype 'a stream' = Cons of 'a * ('a stream') susp
type 'a stream = ('a stream') susp
fun delay (f ) = Susp(f);
fun force (Susp(f)) = f ();
val rec ones' = fn () => Cons(1, delay(ones'));
val ones = delay(ones')
fun ltail(Susp(s)) = ltail'(force s)
and ltail' (Cons(x,s)) = s
But for getting a suspended tail the types do not match up.
operator domain: 'Z susp
operand: unit -> 'Y
What will need to change for the proper types for ltail ?
I know what happens with a tail not suspended.
I just want to figure out what the notes were saying for the suspended version.
fun ltail(Susp(s)) = ltail'(force s)
The problem here is that force takes a value of type susp, but you call it with a value of type () -> 'a. I.e. you take the function out of the susp value and then call force on the function instead of the susp value. You should just do:
fun ltail s = ltail' (force s)