comparison tests/type_class.ur @ 675:43430b7190f4

Type class inclusions
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Mar 2009 15:13:36 -0400
parents fab5998b840e
children 81573f62d6c3
comparison
equal deleted inserted replaced
674:fab5998b840e 675:43430b7190f4
7 val string_default : default string 7 val string_default : default string
8 val int_default : default int 8 val int_default : default int
9 9
10 val option_default : t ::: Type -> default t -> default (option t) 10 val option_default : t ::: Type -> default t -> default (option t)
11 val pair_default : a ::: Type -> b ::: Type -> default a -> default b -> default (pair a b) 11 val pair_default : a ::: Type -> b ::: Type -> default a -> default b -> default (pair a b)
12
13 class awesome
14 val awesome_default : t ::: Type -> awesome t -> default t
15
16 val float_awesome : awesome float
12 end = struct 17 end = struct
13 class default t = t 18 class default t = t
14 fun get (t ::: Type) (x : t) = x 19 fun get (t ::: Type) (x : t) = x
15 20
16 val string_default = "Hi" 21 val string_default = "Hi"
17 val int_default = 0 22 val int_default = 0
18 23
19 fun option_default (t ::: Type) (x : t) = Some x 24 fun option_default (t ::: Type) (x : t) = Some x
20 fun pair_default (a ::: Type) (b ::: Type) (x : a) (y : b) = Pair (x, y) 25 fun pair_default (a ::: Type) (b ::: Type) (x : a) (y : b) = Pair (x, y)
26
27 class awesome t = t
28 fun awesome_default (t ::: Type) (x : t) = x
29
30 val float_awesome = 1.23
21 end 31 end
22 32
23 open M 33 open M
24 34
25 fun default (t ::: Type) (_ : default t) : t = get 35 fun default (t ::: Type) (_ : default t) : t = get
26 val hi : string = default 36 val hi : string = default
27 val zero : int = default 37 val zero : int = default
28 val some_zero : option int = default 38 val some_zero : option int = default
29 val hi_zero : pair string int = default 39 val hi_zero : pair string int = default
40 val ott : float = default
30 41
31 fun frob (t ::: Type) (_ : default t) : t = default 42 fun frob (t ::: Type) (_ : default t) : t = default
32 val hi_again : string = frob 43 val hi_again : string = frob
33 val zero_again : int = frob 44 val zero_again : int = frob
34 45
42 mkShow (fn x => 53 mkShow (fn x =>
43 case x of 54 case x of
44 Pair (y, z) => "(" ^ show y ^ "," ^ show z ^ ")") 55 Pair (y, z) => "(" ^ show y ^ "," ^ show z ^ ")")
45 56
46 fun main () : transaction page = return <xml><body> 57 fun main () : transaction page = return <xml><body>
47 {[hi_again]}, {[zero_again]}, {[some_zero]}, {[hi_zero]} 58 {[hi_again]}, {[zero_again]}, {[some_zero]}, {[hi_zero]}, {[ott]}
48 </body></xml> 59 </body></xml>