http://www.impredicative.com/wiki/index.php?title=Basic_Examples_of_the_Type_Class_Mechanism_in_Ur/Web&feed=atom&action=historyBasic Examples of the Type Class Mechanism in Ur/Web - Revision history2024-03-29T12:19:07ZRevision history for this page on the wikiMediaWiki 1.19.20+dfsg-2.3http://www.impredicative.com/wiki/index.php?title=Basic_Examples_of_the_Type_Class_Mechanism_in_Ur/Web&diff=104&oldid=prevKarn Kallio: Clarification between constraint and polymorphism2010-12-12T16:48:18Z<p>Clarification between constraint and polymorphism</p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 16:48, 12 December 2010</td>
</tr></table>Karn Kalliohttp://www.impredicative.com/wiki/index.php?title=Basic_Examples_of_the_Type_Class_Mechanism_in_Ur/Web&diff=103&oldid=prevKarn Kallio: Simple usage example of the type class facility2010-12-12T15:35:11Z<p>Simple usage example of the type class facility</p>
<p><b>New page</b></p><div>Ur/Web permits regulated ad hoc polymorphism via a type class mechanism.<br />
Here are some simple example usages.<br />
<br />
<pre><br />
(*<br />
* Here is some simple examples of the typeclass facility in Ur/Web.<br />
* Note that classes are not available in the module which defines them,<br />
* so to make the examples work they may not be at the top level.<br />
*)<br />
<br />
structure Measurable : sig<br />
class measurable<br />
<br />
val length : t ::: Type -> measurable t -> t -> int<br />
<br />
val measurable_string : measurable string<br />
val measurable_list : t ::: Type -> measurable t -> measurable (list t)<br />
end = struct<br />
class measurable :: Type -> Type = fn (t :: Type) => t -> int<br />
<br />
val length : t ::: Type -> measurable t -> t -> int = fn [t ::: Type] (m : measurable t) => m<br />
<br />
val measurable_string : measurable string = strlen<br />
<br />
(* Here we copy listlength for sake of a self-contained example, but it is in the<br />
* library as List.length; you can get it by $/list<br />
*)<br />
<br />
val listlength = fn [a] =><br />
let<br />
fun length' acc (ls : list a) =<br />
case ls of<br />
[] => acc<br />
| _ :: ls => length' (acc + 1) ls<br />
in<br />
length' 0<br />
end<br />
<br />
val measurable_list : t ::: Type -> measurable t -> measurable (list t) = fn [t ::: Type] (m : measurable t) => listlength<br />
end<br />
<br />
open Measurable<br />
<br />
(* <br />
* These will not compile because there is no instance for these types in the class<br />
*)<br />
<br />
(*<br />
val i : int = length 7<br />
val i : int = length (1 :: 3 :: 9 :: [])<br />
*)<br />
<br />
fun main () = return <xml><br />
<head><br />
<title>Type Classes in Ur/Web</title><br />
</head><br />
<body><br />
<h1>Type Classes</h1><br />
<p><br />
Ur/Web includes a type class mechanism for controlling ad hoc polymorphism.<br />
</p><br />
<h2><br />
An illustracion of the Measurable class on strings and lists.<br />
</h2><br />
<p><br />
Here we see length "Ur/Web" = {[length "Ur/Web"]}<br />
</p><br />
<p><br />
Here we see length ("a" :: "b" :: "C" :: []) = {[length ("a" :: "b" :: "C" :: [])]}<br />
</p><br />
</body><br />
</xml><br />
</pre></div>Karn Kallio