# Statically typed (hash)maps/dicts?

**URL:** <https://discourse.ats-lang.org/t/statically-typed-hash-maps-dicts/173>\
**Category:** Google Group\
**Created:** [September 1, 2015, 9:14pm UTC](https://discourse.ats-lang.org/t/statically-typed-hash-maps-dicts/173 "2015-09-01T21:14:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raoul\_Duke](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Raoul\_Duke](https://discourse.ats-lang.org/u/Raoul_Duke)\
**Post date:** [September 1, 2015, 9:14pm UTC](https://discourse.ats-lang.org/t/statically-typed-hash-maps-dicts/173/1 "2015-09-01T21:14:43Z")

</div>

hi,

Might anybody have thoughts on how to have maps in ATS that can be  
nailed down in terms of what keys they can have, and what the type of  
the key’s values are? So I can still do map, filter, reduce, etc.,  
over them but also use the compiler to make sure e.g. args passed are  
statically checked to be of the correct form?

thank you.

---

<div class="post-metadata">

**Author:** ![Raoul\_Duke](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Raoul\_Duke](https://discourse.ats-lang.org/u/Raoul_Duke)\
**Post date:** [September 3, 2015, 1:05am UTC](https://discourse.ats-lang.org/t/statically-typed-hash-maps-dicts/173/2 "2015-09-03T01:05:35Z")

</div>

thank you for your time & thoughts on this! i’m trying to percolate it  
into my thick brain.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [September 2, 2015, 10:19pm UTC](https://discourse.ats-lang.org/t/statically-typed-hash-maps-dicts/173/3 "2015-09-02T22:19:31Z")

</div>

Here is one plausible way to support this;

(\* \*\*\*\*\*\* \*\*\*\*\*\* _)  
//  
datasort map =  
| map\_nil of ()  
| map\_cons of (string, t@ype, map)  
//  
abstype map(map)  
abstype key(string)  
//  
stacst ismember : (map, string, t@ype) → bool  
//  
extern  
fun  
{x:t@ype}  
map\_get{kxs:map}{k:string | ismember(kxs, k, x)} (map: map(kxs), key: key(k  
)): x  
extern  
fun  
{x:t@ype}  
map\_set{kxs:map}{k:string | ismember(kxs, k, x)} (map: map(kxs), key: key(k  
), x: x): void  
//  
(_ \*\*\*\*\*\* \*\*\*\*\*\* \*)

The built-in constraint-solver cannot handle a constraint involving  
‘ismember’,  
which needs to be exported so that it can be solved externally.On Tuesday, September 1, 2015 at 11:30:51 PM UTC-4, Raoul Duke wrote:

> (A) horrible hacked pseudocode not meant to be seen as any existing  
> language:  
> typedef Player = Map{ 'hitPoints : int, 'maxHitPoints : int, ‘x :  
> float, y’ : float, … };  
> function move( p:Player, dx:float, dy:float ):Player {  
> return p.{x=p.x+dx, y=p.y+dy};  
> }
> 
> (B) If something like row types were involved:  
> function move( p:{x:float, y:float | r}, dx:float, dy:float ):Player {  
> return p.{x=p.x+dx, y=p.y+dy | r};  
> }
> 
> (C) It would be somehow magically statically illegal to try to do:  
> var p:Player = Player{x:10, z:20};  
> or  
> move( {x:10, z:20}, 10, 20 );

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [September 2, 2015, 2:36am UTC](https://discourse.ats-lang.org/t/statically-typed-hash-maps-dicts/173/4 "2015-09-02T02:36:21Z")

</div>

I am not sure if I understand your question.

Could you put out an example to show what kind of static checking is  
expected?On Tuesday, September 1, 2015 at 5:14:43 PM UTC-4, Raoul Duke wrote:

> hi,
> 
> Might anybody have thoughts on how to have maps in ATS that can be  
> nailed down in terms of what keys they can have, and what the type of  
> the key’s values are? So I can still do map, filter, reduce, etc.,  
> over them but also use the compiler to make sure e.g. args passed are  
> statically checked to be of the correct form?
> 
> thank you.

---

<div class="post-metadata">

**Author:** ![Raoul\_Duke](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Raoul\_Duke](https://discourse.ats-lang.org/u/Raoul_Duke)\
**Post date:** [September 2, 2015, 3:30am UTC](https://discourse.ats-lang.org/t/statically-typed-hash-maps-dicts/173/5 "2015-09-02T03:30:51Z")

</div>

(A) horrible hacked pseudocode not meant to be seen as any existing language:  
typedef Player = Map{ 'hitPoints : int, 'maxHitPoints : int, ‘x :  
float, y’ : float, … };  
function move( p:Player, dx:float, dy:float ):Player {  
return p.{x=p.x+dx, y=p.y+dy};  
}

(B) If something like row types were involved:  
function move( p:{x:float, y:float | r}, dx:float, dy:float ):Player {  
return p.{x=p.x+dx, y=p.y+dy | r};  
}

© It would be somehow magically statically illegal to try to do:  
var p:Player = Player{x:10, z:20};  
or  
move( {x:10, z:20}, 10, 20 );
