caml-list - the Caml user's mailing list
 help / Atom feed
From: Jesse Haber-Kucharsky <jesse@haberkucharsky.com>
To: Sam Kuper <sampablokuper@posteo.net>,  "ML caml-list (ocaml discuss)" <caml-list@inria.fr>
Subject: Re: [Caml-list] Restrict type to specific chars
Date: Tue, 30 Jun 2020 14:07:30 -0400
Message-ID: <CA+p0s_fFrLUORGz0m5Bci0nh95NF20=MrVJMhjpLrQy97YOHzg@mail.gmail.com> (raw)
In-Reply-To: <20200630162539.itp2j2xpdgyvaeif@posteo.net>

[-- Attachment #1: Type: text/plain, Size: 2480 bytes --]

On Tue, Jun 30, 2020 at 12:26 PM Sam Kuper <sampablokuper@posteo.net> wrote:

> 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.
>

Hi Sam.

As a small variation of Yawar's excellent reply, consider the following
(the file is named `sam.ml`):

    module AB : sig
      type t = private char

      val a : t
      val b : t
      val char : t -> char
    end = struct
      type t = char

      let a = 'a'
      let b = 'b'
      let char t = t
    end

    let () =
      let x = AB.a in
      let y = AB.b in
      assert (AB.char x = 'a') ;
      assert (AB.char y = 'b')

I like this approach because it statically guarantees that a value of
`Ab.t` is always either 'a' or 'b', and attempting to do otherwise results
in a compilation error.

Best,
--
Jesse

[-- Attachment #2: Type: text/html, Size: 3625 bytes --]

      parent reply index

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-30 16:25 Sam Kuper
2020-06-30 17:34 ` Yawar Amin
2020-06-30 17:55   ` Yotam Barnoy
2020-06-30 18:07 ` Jesse Haber-Kucharsky [this message]

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+p0s_fFrLUORGz0m5Bci0nh95NF20=MrVJMhjpLrQy97YOHzg@mail.gmail.com' \
    --to=jesse@haberkucharsky.com \
    --cc=caml-list@inria.fr \
    --cc=sampablokuper@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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