Lists and arrays of mixed types

Is it possible to make subtypes of datatypes, and then put them in a list or array of the parent type, and create a function that can pattern patch on subtypes, or directly on subtype constructor pair So?

The idea being a list of commands processed by a function, but each has variations, hence subtypes with multiple constructors. This is hard to do in Haskell, but useful for IO processing with queues…

Here is my first try:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test32.datsOn Tuesday, December 29, 2015 at 9:07:40 PM UTC-5, Mike Jones wrote:

Taking liberties with syntax, in particular h:hs for list head like
Haskell, which I really miss, the syntax, not the language -:slight_smile:

datatype Command = Command ()

datatype Run : Command =
| RunFast of (address, data)
| RunSlow of (address, data, speed)

datatype Wait : Command = Wait of delay

datatype Composite : Command = Command of list

val c = Composite (List (RunFast (0x90, 0x7765), Wait (50)))
val l = [c,c,c]: List

fun execute (cmd: List) = let
fun loop (h:hs) = case h of
Run => case h of
RunFast (c, w) => io operation
RunSlow (…) => …
Composite …
Wait (d) => delay(d)

val () = execute(l)

Yes. You can put any value into a list of type list(A) as long as
the type of the value is a subtype of A.On Wednesday, January 6, 2016 at 1:32:47 AM UTC-5, Mike Jones wrote:

So with this definition, can a list itself be covariant and contains types
and subtypes in the same list?

Sent from my iPad

On Jan 4, 2016, at 7:46 AM, gmhwxi <gmh...@gmail.com <javascript:>> wrote:

In ATS, the notion of subtype is used in a very narrow sense.
It is a bit like subset but the technical definition is somewhat involved.

Here are some examples:

int(0) is a subtype of intGte(0)
intGte(0) is a subtype of Int (= [i:int] int(i)).

Why do we need variance annotation when declaring datatypes or abstract
types?

Say we have a function for printing out a list of integers that are
greater than or
equal to 0:

fun print_intlist : List(IntGte(0)) → void

Suppose we have a list xs of the type List(int(0)). That is, every x in xs
is of
type int(0) (so x actually equals 0).

We can do

val () = print_intlist(xs)

because List is co-variant.

To typecheck print_list(xs), we need to show that List(int(0)) is a
subtype of List(intGte(0)).
We know int(0) is a subtype of intGte(0). Because List is co-variant, we
have what we want.

On Monday, January 4, 2016 at 1:10:29 AM UTC-5, Mike Jones wrote:

I’m still confused what is a subtype, and when covariance matters. An old
post had an example of two indexed types with a constraint that was a
subset of the other, but the post may only apply to ATS1.

Can someone perhaps explain in the context of the option datatype, why it
uses covariance, using an example of type/subtype?


You received this message because you are subscribed to a topic in the
Google Groups “ats-lang-users” group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/ats-lang-users/k7qXXrCvmsA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
ats-lang...@googlegroups.com <javascript:>.
To post to this group, send email to ats-l...@googlegroups.com
<javascript:>.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ats-lang-users/3cf9b907-550b-46cf-91d9-bc266abc4708%40googlegroups.com
https://groups.google.com/d/msgid/ats-lang-users/3cf9b907-550b-46cf-91d9-bc266abc4708%40googlegroups.com?utm_medium=email&utm_source=footer
.

Taking liberties with syntax, in particular h:hs for list head like Haskell, which I really miss, the syntax, not the language -:slight_smile:

datatype Command = Command ()

datatype Run : Command =
| RunFast of (address, data)
| RunSlow of (address, data, speed)

datatype Wait : Command = Wait of delay

datatype Composite : Command = Command of list

val c = Composite (List (RunFast (0x90, 0x7765), Wait (50)))
val l = [c,c,c]: List

fun execute (cmd: List) = let
fun loop (h:hs) = case h of
Run => case h of
RunFast (c, w) => io operation
RunSlow (…) => …
Composite …
Wait (d) => delay(d)

val () = execute(l)

So with this definition, can a list itself be covariant and contains types and subtypes in the same list?> On Jan 4, 2016, at 7:46 AM, gmhwxi gmh...@gmail.com wrote:

In ATS, the notion of subtype is used in a very narrow sense.
It is a bit like subset but the technical definition is somewhat involved.

Here are some examples:

int(0) is a subtype of intGte(0)
intGte(0) is a subtype of Int (= [i:int] int(i)).

Why do we need variance annotation when declaring datatypes or abstract
types?

Say we have a function for printing out a list of integers that are greater than or
equal to 0:

fun print_intlist : List(IntGte(0)) → void

Suppose we have a list xs of the type List(int(0)). That is, every x in xs is of
type int(0) (so x actually equals 0).

We can do

val () = print_intlist(xs)

because List is co-variant.

To typecheck print_list(xs), we need to show that List(int(0)) is a subtype of List(intGte(0)).
We know int(0) is a subtype of intGte(0). Because List is co-variant, we have what we want.

On Monday, January 4, 2016 at 1:10:29 AM UTC-5, Mike Jones wrote:
I’m still confused what is a subtype, and when covariance matters. An old post had an example of two indexed types with a constraint that was a subset of the other, but the post may only apply to ATS1.
Can someone perhaps explain in the context of the option datatype, why it uses covariance, using an example of type/subtype?


You received this message because you are subscribed to a topic in the Google Groups “ats-lang-users” group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ats-lang-users/k7qXXrCvmsA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/3cf9b907-550b-46cf-91d9-bc266abc4708%40googlegroups.com.

Yes. You can put any value into a list of type list(A) as long as
the type of the value is a subtype of A.On Wednesday, January 6, 2016 at 1:32:47 AM UTC-5, Mike Jones wrote:

So with this definition, can a list itself be covariant and contains types
and subtypes in the same list?

Sent from my iPad

On Jan 4, 2016, at 7:46 AM, gmhwxi <gmh...@gmail.com <javascript:>> wrote:

In ATS, the notion of subtype is used in a very narrow sense.
It is a bit like subset but the technical definition is somewhat involved.

Here are some examples:

int(0) is a subtype of intGte(0)
intGte(0) is a subtype of Int (= [i:int] int(i)).

Why do we need variance annotation when declaring datatypes or abstract
types?

Say we have a function for printing out a list of integers that are
greater than or
equal to 0:

fun print_intlist : List(IntGte(0)) → void

Suppose we have a list xs of the type List(int(0)). That is, every x in xs
is of
type int(0) (so x actually equals 0).

We can do

val () = print_intlist(xs)

because List is co-variant.

To typecheck print_list(xs), we need to show that List(int(0)) is a
subtype of List(intGte(0)).
We know int(0) is a subtype of intGte(0). Because List is co-variant, we
have what we want.

On Monday, January 4, 2016 at 1:10:29 AM UTC-5, Mike Jones wrote:

I’m still confused what is a subtype, and when covariance matters. An old
post had an example of two indexed types with a constraint that was a
subset of the other, but the post may only apply to ATS1.

Can someone perhaps explain in the context of the option datatype, why it
uses covariance, using an example of type/subtype?


You received this message because you are subscribed to a topic in the
Google Groups “ats-lang-users” group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/ats-lang-users/k7qXXrCvmsA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
ats-lang...@googlegroups.com <javascript:>.
To post to this group, send email to ats-l...@googlegroups.com
<javascript:>.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ats-lang-users/3cf9b907-550b-46cf-91d9-bc266abc4708%40googlegroups.com
https://groups.google.com/d/msgid/ats-lang-users/3cf9b907-550b-46cf-91d9-bc266abc4708%40googlegroups.com?utm_medium=email&utm_source=footer
.

I’m still confused what is a subtype, and when covariance matters. An old post had an example of two indexed types with a constraint that was a subset of the other, but the post may only apply to ATS1.

Can someone perhaps explain in the context of the option datatype, why it uses covariance, using an example of type/subtype?

If there is a way in execute() to pattern match on the datakind it would behave more like subtypes in that the resulting operation could operate on the supertype.

For example, if all commands had an integer code, that would be in the supertype, and subtypes would add data. The first example sort of does that. And in the first example I think it is possible to pattern match on Run alone.

In neither example is the subtype idea defined separately in that one file defines the supertype and related functions, and another file contains subtypes that can be used with supertype functions.

In this sense libraries could be extended by users. I think type classes sort of do this, but in a different way. Rather than extending data, you adorn it with type classes and functions. Perhaps what I pointed to of extending data is closer to an OO world view than a functional one.

Given the mention of subtypes in the intro doc, what was the original intent?

In ATS, the notion of subtype is used in a very narrow sense.
It is a bit like subset but the technical definition is somewhat involved.

Here are some examples:

int(0) is a subtype of intGte(0)
intGte(0) is a subtype of Int (= [i:int] int(i)).

Why do we need variance annotation when declaring datatypes or abstract
types?

Say we have a function for printing out a list of integers that are greater
than or
equal to 0:

fun print_intlist : List(IntGte(0)) → void

Suppose we have a list xs of the type List(int(0)). That is, every x in xs
is of
type int(0) (so x actually equals 0).

We can do

val () = print_intlist(xs)

because List is co-variant.

To typecheck print_list(xs), we need to show that List(int(0)) is a subtype
of List(intGte(0)).
We know int(0) is a subtype of intGte(0). Because List is co-variant, we
have what we want.On Monday, January 4, 2016 at 1:10:29 AM UTC-5, Mike Jones wrote:

I’m still confused what is a subtype, and when covariance matters. An old
post had an example of two indexed types with a constraint that was a
subset of the other, but the post may only apply to ATS1.

Can someone perhaps explain in the context of the option datatype, why it
uses covariance, using an example of type/subtype?

Maybe this one is closer to what you had in mind:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test33.datsOn Wednesday, December 30, 2015 at 12:41:10 AM UTC-5, Mike Jones wrote:

Looks like you deftly avoided subtypes -:slight_smile:

Looks like you deftly avoided subtypes -:slight_smile:

If you give an example of what you want, I may be able to suggest a way to
do it.

Is it possible to make subtypes of datatypes, and then put them in a list
or array of the parent type, and create a function that can pattern patch
on subtypes, or directly on subtype constructor pair So?

The idea being a list of commands processed by a function, but each has
variations, hence subtypes with multiple constructors. This is hard to do
in Haskell, but useful for IO processing with queues…


You received this message because you are subscribed to the Google Groups
“ats-lang-users” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ats-lang-users/b47f0866-9753-4e66-9cc3-bed27ab27e21%40googlegroups.com
.

Nice. So what does not yet exist is a subtype based on records or datatypes, which is harder because storage layouts are different and the type checking would have to prove no abuse. Pattern matching on subtypes could work and would be checkable at compile time. Basically iterating a list or array and matching on each item. But as you said, where is the time.> On Jan 6, 2016, at 7:46 AM, gmhwxi gmh...@gmail.com wrote:

Yes. You can put any value into a list of type list(A) as long as
the type of the value is a subtype of A.

On Wednesday, January 6, 2016 at 1:32:47 AM UTC-5, Mike Jones wrote:
So with this definition, can a list itself be covariant and contains types and subtypes in the same list?

Sent from my iPad

On Jan 4, 2016, at 7:46 AM, gmhwxi gmh...@gmail.com wrote:

In ATS, the notion of subtype is used in a very narrow sense.
It is a bit like subset but the technical definition is somewhat involved.

Here are some examples:

int(0) is a subtype of intGte(0)
intGte(0) is a subtype of Int (= [i:int] int(i)).

Why do we need variance annotation when declaring datatypes or abstract
types?

Say we have a function for printing out a list of integers that are greater than or
equal to 0:

fun print_intlist : List(IntGte(0)) → void

Suppose we have a list xs of the type List(int(0)). That is, every x in xs is of
type int(0) (so x actually equals 0).

We can do

val () = print_intlist(xs)

because List is co-variant.

To typecheck print_list(xs), we need to show that List(int(0)) is a subtype of List(intGte(0)).
We know int(0) is a subtype of intGte(0). Because List is co-variant, we have what we want.

On Monday, January 4, 2016 at 1:10:29 AM UTC-5, Mike Jones wrote:
I’m still confused what is a subtype, and when covariance matters. An old post had an example of two indexed types with a constraint that was a subset of the other, but the post may only apply to ATS1.
Can someone perhaps explain in the context of the option datatype, why it uses covariance, using an example of type/subtype?


You received this message because you are subscribed to a topic in the Google Groups “ats-lang-users” group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ats-lang-users/k7qXXrCvmsA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ats-lang...@googlegroups.com.
To post to this group, send email to ats-l...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/3cf9b907-550b-46cf-91d9-bc266abc4708%40googlegroups.com.


You received this message because you are subscribed to a topic in the Google Groups “ats-lang-users” group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ats-lang-users/k7qXXrCvmsA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/09e82744-650b-4d02-b036-5c07c94e5812%40googlegroups.com.