Clamping Values

26 views
Skip to first unread message

Mike Jones

unread,
Nov 3, 2015, 8:30:12 PM11/3/15
to ats-lang-users
Is there some kind of template way to convert a value, say int, to meet a universal like {n:int | n > 3; n < 6} ?

Mainly to avoid writing functions for these cases. min/max functions clamp the value, but don't deal with the quantifier. 

Perhaps what I am looking for is a template that generates the quantifier, or perhaps that is a macro and one already exists?

Hongwei Xi

unread,
Nov 3, 2015, 8:39:26 PM11/3/15
to ats-lan...@googlegroups.com

fun{}
clamp
  {l,u:int | l <= u}
(
  l: int(l), x: int, u: int(u)
) : intBtwe(l,u) =
  let val x = g1ofg0(x) in min(max(l, x), u) end


--
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 http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/00523f2c-59d4-4582-b07e-a48d42ba6601%40googlegroups.com.

Brandon Barker

unread,
Nov 3, 2015, 8:40:11 PM11/3/15
to ats-lang-users
I think, if I understand correctly, there are a lot like this. Here's
the one for your case:
https://github.com/githwxi/ATS-Postiats/search?utf8=✓&q=intBtw

On Tue, Nov 3, 2015 at 8:30 PM, Mike Jones <proc...@gmail.com> wrote:
> --
> 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 http://groups.google.com/group/ats-lang-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ats-lang-users/00523f2c-59d4-4582-b07e-a48d42ba6601%40googlegroups.com.



--
Brandon Barker
brandon...@gmail.com

Mike Jones

unread,
Nov 3, 2015, 10:01:50 PM11/3/15
to ats-lang-users
Related to min/max clamping, what does this do:

val x: uint16 = cast{uint16}(5)
val y
: uint8 = cast{uint8}(x)

Does it actually copy memory from a uint16_t to a uint8_t? Does it just take the least significant byte? Does it reuse memory? Does it just play a dangerous pointer game?

gmhwxi

unread,
Nov 3, 2015, 10:24:04 PM11/3/15
to ats-lang-users

A type in C is just like shoe size.

In the following code, both x and y are values:


val x: uint16 = cast{uint16}(5)
val y: uint8 = cast{uint8}(x)

Most likely, the C compiler is to replace x and y with the constant 5
in this particular case.


>>Does it actually copy memory from a uint16_t to a uint8_t?

It could but it is very unlikely. Compilers like gcc and clang are very good
at performing all sorts of optimizations. I could not think of anything dangerous
in this case.

gmhwxi

unread,
Nov 3, 2015, 10:31:03 PM11/3/15
to ats-lang-users
Say you write the following code in C:

uint16_t x = 5;
  uint8_t y = x;

Casting is implicitly done. It is really equivalent to writing:

uint16_t x = (uint16_t)5;
  uint8_t y =  (uint8_t)x;

In ATS casting needs to be done explicitly.

On Tuesday, November 3, 2015 at 10:01:50 PM UTC-5, Mike Jones wrote:
Reply all
Reply to author
Forward
0 new messages