caml-list - the Caml user's mailing list
 help / Atom feed
* [Caml-list] Restrict type to specific chars
@ 2020-06-30 16:25 Sam Kuper
  2020-06-30 17:34 ` Yawar Amin
  2020-06-30 18:07 ` Jesse Haber-Kucharsky
  0 siblings, 2 replies; 4+ messages in thread
From: Sam Kuper @ 2020-06-30 16:25 UTC (permalink / raw)
  To: ML caml-list (ocaml discuss)

Dear list,

Forgive me for asking a very basic question, but I have not so far been
able to find an answer in any of the OCaml books to which I have access,
nor in the OCaml documentation or mailing list archive.

How does one define a type whose values are restricted to one of some
specified chars?

E.g. suppose I want to define a type `ab` whose values can only be
either 'a' or 'b'.  I imagine that should work something like this:

    # type ab = Ab of char 'a' | Ab of char 'b' ;;
    type ab = Ab of char 'a' | Ab of char 'b'

and thereby give the following functionality:

    # Ab 'a';;
    - : ab = Ab 'a'
    # Ab 'b';;
    - : ab = Ab 'b'
    # Ab 'c';;
    Error: <some error>

The definition above is essentially pseudo-code to illustrate what I
would like to achieve with real, valid OCaml code.  (If I knew how to
write valid OCaml to achieve this, then I would not be posting this
question on the mailing list.)

Here are several of my failed attempts at writing OCaml code for what I
want to achieve:

    # type ab = 'a' | 'b';;
    Error: Syntax error

    # type ab = char 'a' | char 'b';;
    Error: Syntax error

    # type ab = Ab of char 'a' | Ab of char 'b';;
    Error: Syntax error

    # type 'a ab = 'a constraint 'a = 'a' | 'b';;
    Error: Syntax error

    # type 'a ab = 'a constraint 'a = 'a' | 'a = 'b';;
    Error: Syntax error

How can I actually achieve it?

Thank you in advance,

Sam

-- 
A: When it messes up the order in which people normally read text.
Q: When is top-posting a bad thing?

()  ASCII ribbon campaign. Please avoid HTML emails & proprietary
/\  file formats. (Why? See e.g. https://v.gd/jrmGbS ). Thank you.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-30 16:25 [Caml-list] Restrict type to specific chars Sam Kuper
2020-06-30 17:34 ` Yawar Amin
2020-06-30 17:55   ` Yotam Barnoy
2020-06-30 18:07 ` Jesse Haber-Kucharsky

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror https://inbox.ocaml.org/caml-list

AGPL code for this site: git clone https://public-inbox.org/ public-inbox