From: Andre Nathan <andre@digirati.com.br>
To: caml-list@inria.fr
Subject: [Caml-list] Encoding "links" with the type system
Date: Wed, 21 Sep 2016 16:12:33 -0300 [thread overview]
Message-ID: <86a4a20a-c5de-a3b3-85c0-621f09a56b81@digirati.com.br> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 2209 bytes --]
Hi
I'm trying to encode links (an example would be directed graph edges)
using the type system.
The idea is to have types such as
module Source = struct
type 'a t = { name : string }
let create name = { name }
end
module Sink = struct
type 'a t = { name : string }
let create name = { name }
end
module Link = struct
type ('a, 'b) t = 'a Source.t * 'b Sink.t
end
and then define a "link set" with the following characteristics:
* You initialize a link set with a sink;
* You can add links to set such that
* The first link's sink must be of the type of the sink;
* Additional links can have as sink types the original sink type
from the set creation, or the source types of previously added
links.
In other words, if a set is created from a "t1 Sink.t", the first link
on the set must be of type "t2 Source.t * t1 Sink.t", the second link
can be either "t3 Source.t * t1 Sink.t" or "t3 Source.t * t2 Sink.t" and
so on.
Is it possible at all to encode such invariants using the type system? I
couldn't get past something like
module Set = struct
type _ t =
| S : 'a Sink.t -> 'a t
| L : 'a t * ('b, 'a) Link.t -> ('a * 'b) t
let create sink = S sink
let add_link link set = L (set, link)
end
This allows me to insert the first link:
type t1
type t2
type t3
let _ =
let snk1 : t1 Sink.t = Sink.create "sink1" in
let set = Set.create snk1 in
let src1 : t2 Source.t = Source.create "source1" in
let lnk1 = (src1, snk1) in
let set1 = Set.add_link lnk1 set in
...
but now "set1" has type "(t1 * t2) Set.t" and I can't call "add_link" on
it anymore:
...
let src2 : t3 Source.t = Source.create "source2" in
let lnk2 = (src2, snk1) in
let set2 = Set.add_link lnk2 set1 in
...
which gives me "This expression has type (t1 * t2) Set.t but an
expression was expected of type t1 Set.t" when passing "set1" to "add_link".
I also tried to come up with a solution using difference lists for the
set but hit basically the same problem as above.
Any help would be appreciated.
Thanks,
Andre
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]
next reply other threads:[~2016-09-21 19:12 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-21 19:12 Andre Nathan [this message]
2016-09-21 22:22 ` Jeremy Yallop
2016-09-22 0:50 ` Andre Nathan
2016-09-30 13:54 ` Gabriel Scherer
2016-10-05 19:46 ` Andre Nathan
2016-10-05 20:15 ` Daniel Bünzli
2016-10-07 17:02 ` Shayne Fletcher
2016-10-08 21:51 ` Shayne Fletcher
2016-10-11 0:16 ` Jacques Garrigue
Reply instructions:
You may reply publicly 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=86a4a20a-c5de-a3b3-85c0-621f09a56b81@digirati.com.br \
--to=andre@digirati.com.br \
--cc=caml-list@inria.fr \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox