caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Dependent types ?
@ 2011-09-26 11:42 Jocelyn Sérot
  2011-09-26 12:07 ` Thomas Braibant
                   ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Jocelyn Sérot @ 2011-09-26 11:42 UTC (permalink / raw)
  To: OCaML Mailing List

Hello,

I've recently come across a problem while writing a domain specific  
language for hardware synthesis (http://wwwlasmea.univ-bpclermont.fr/Personnel/Jocelyn.Serot/caph.html 
).
The idea is to extend the type system to accept "size" annotations for  
int types (it could equally apply to floats).
The target language (VHDL in this case) accept "generic" functions,  
operating on ints with variable bit width and I'd like to reflect this  
in the source language.

For instance, I'd like to be able to declare :

val foo : int * int -> int

(where the type int is not annotated, i.e. "generic")

so that, when applied to, let say :

val x : int<16>
val y : int<16>

(where <16> is a size annotation),

like in

let z = foo (x,y)

then the compiler will infer type int<16> for z

In fact, the exact type signature for foo would be :

val foo : int<s> * int <s> -> int<s>

where "s" would be a "size variable" (playing a role similar to a type  
variable in, for ex : val map : 'a list -> ('a ->'b) -> 'b list).

In a sense, it has to do with the theory of sized types (Hughes and  
Paretto, .. ) and dependent types (DML for ex), but my goal is far  
less ambitious.
In particular, i dont want to do _computations_ (1) on the size (and,  
a fortiori, don't want to prove anything on the programs).
So sized types / dependent types seems a big machinery for a  
relatively small goal.
My intuition is that this is just a variant of polymorphism in which  
the variables ranged over are not types but integers.
Before testing this intuition by trying to implement it, I'd like to  
know s/o has already tackled this problem.
Any pointer - including "well, this is trivial" ! ;-) - will be  
appreciated.

Best wishes

Jocelyn

(1) i.e. i dont bother supporting declarations like : val mul : int<n>  
* int<n> -> int <2*n> ...

  

^ permalink raw reply	[flat|nested] 18+ messages in thread
* [Caml-list] Dependent types ?
@ 2011-09-27 22:12 Damien Guichard
  2011-09-28  7:27 ` Denis Berthod
  0 siblings, 1 reply; 18+ messages in thread
From: Damien Guichard @ 2011-09-27 22:12 UTC (permalink / raw)
  To: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 2869 bytes --]

That's pretty cool, everyone and his mother has a solution to the proposed problem.
I think, for the sake of exhaustivity, i can share my own weird hack.
It can express all power of 2 sizes (for example add, mul and div).
It uses a nested data type.


  type 'a size =
   | Word of 'a
   | DWord of ('a * 'a) size

   type n16 = int size
   type n32 = (n16 * n16) size
   type n64 = (n32 * n32) size

   add : 'a size -> 'a size -> 'a size 
   mul : 'a size -> 'a size -> ('a * 'a) size 
   div : ('a * 'a) size -> 'a size -> ('a size * 'a size)


- damien


Le 26/09/2011 à 13:42, "Jocelyn Sérot"  à écrit :
>Hello,
>
>I've recently come across a problem while writing a domain specific 
>language for hardware synthesis (http://wwwlasmea.univ-bpclermont.fr/Personnel/Jocelyn.Serot/caph.html
>).
>The idea is to extend the type system to accept "size" annotations for 
>int types (it could equally apply to floats).
>The target language (VHDL in this case) accept "generic" functions, 
>operating on ints with variable bit width and I'd like to reflect this 
>in the source language.
>
>For instance, I'd like to be able to declare :
>
>val foo : int * int -> int
>
>(where the type int is not annotated, i.e. "generic")
>
>so that, when applied to, let say :
>
>val x : int<16>
>val y : int<16>
>
>(where <16> is a size annotation),
>
>like in
>
>let z = foo (x,y)
>
>then the compiler will infer type int<16> for z
>
>In fact, the exact type signature for foo would be :
>
>val foo : int * int -> int
>
>where "s" would be a "size variable" (playing a role similar to a type 
>variable in, for ex : val map : 'a list -> ('a ->'b) -> 'b list).
>
>In a sense, it has to do with the theory of sized types (Hughes and 
>Paretto, .. ) and dependent types (DML for ex), but my goal is far 
>less ambitious.
>In particular, i dont want to do _computations_ (1) on the size (and, 
>a fortiori, don't want to prove anything on the programs).
>So sized types / dependent types seems a big machinery for a 
>relatively small goal.
>My intuition is that this is just a variant of polymorphism in which 
>the variables ranged over are not types but integers.
>Before testing this intuition by trying to implement it, I'd like to 
>know s/o has already tackled this problem.
>Any pointer - including "well, this is trivial" ! ;-) - will be 
>appreciated.
>
>Best wishes
>
>Jocelyn
>
>(1) i.e. i dont bother supporting declarations like : val mul : int   
>* int   -> int <2*n> ...
>
> 
>
>-- 
>Caml-list mailing list. Subscription management and archives:
>https://sympa-roc.inria.fr/wws/info/caml-list
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>Bug reports: http://caml.inria.fr/bin/caml-bugs
>


--
Mail created using EssentialPIM Free - www.essentialpim.com

[-- Attachment #1.2: Type: text/html, Size: 4090 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2011-09-28  7:28 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-26 11:42 [Caml-list] Dependent types ? Jocelyn Sérot
2011-09-26 12:07 ` Thomas Braibant
2011-09-26 12:13 ` Denis Berthod
2011-09-26 12:45   ` Yaron Minsky
2011-09-26 12:56     ` Denis Berthod
2011-09-26 15:55     ` Jocelyn Sérot
2011-09-26 16:44       ` Gabriel Scherer
2011-09-26 21:09         ` Christophe Raffalli
2011-09-27  8:34           ` Jocelyn Sérot
2011-09-27  8:23         ` Jocelyn Sérot
2011-09-27  9:16           ` Gabriel Scherer
2011-09-27  9:41             ` Arnaud Spiwack
2011-09-27 12:25               ` Jocelyn Sérot
2011-09-27 14:13           ` oliver
2011-09-27  8:27     ` Jocelyn Sérot
2011-09-26 22:51 ` oliver
2011-09-27 22:12 Damien Guichard
2011-09-28  7:27 ` Denis Berthod

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).