Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / mlton / atoms / type-ops.fun
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2007 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-2000 NEC Research Institute.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9functor TypeOps (S: TYPE_OPS_STRUCTS): TYPE_OPS =
10struct
11
12open S
13
14local
15 open Tycon
16in
17 structure RealSize = RealSize
18 structure WordSize = WordSize
19end
20type realSize = RealSize.t
21type tycon = Tycon.t
22type wordSize = WordSize.t
23
24local
25 fun nullary tycon = con (tycon, Vector.new0 ())
26in
27 val bool = nullary Tycon.bool
28 val cpointer = nullary Tycon.cpointer
29 val exn = nullary Tycon.exn
30 val intInf = nullary Tycon.intInf
31 val real = RealSize.memoize (fn s => nullary (Tycon.real s))
32 val thread = nullary Tycon.thread
33 val word = WordSize.memoize (fn s => nullary (Tycon.word s))
34end
35
36local
37 fun unary tycon t = con (tycon, Vector.new1 t)
38in
39 val array = unary Tycon.array
40 val list = unary Tycon.list
41 val reff = unary Tycon.reff
42 val vector = unary Tycon.vector
43 val weak = unary Tycon.weak
44end
45
46val word8 = word WordSize.word8
47val word8Vector = vector word8
48val word32 = word WordSize.word32
49
50local
51 fun binary tycon (t1, t2) = con (tycon, Vector.new2 (t1, t2))
52in
53 val arrow = binary Tycon.arrow
54end
55
56val arrow =
57 Trace.trace ("TypeOps.arrow", Layout.tuple2 (layout, layout), layout) arrow
58
59fun deUnaryOpt tycon t =
60 case deConOpt t of
61 SOME (c, ts) => if Tycon.equals (c, tycon)
62 then SOME (Vector.first ts)
63 else NONE
64 | _ => NONE
65
66fun deUnary tycon t =
67 case deUnaryOpt tycon t of
68 SOME t => t
69 | NONE => Error.bug "TypeOps.deUnary"
70
71val deArray = deUnary Tycon.array
72val deRef = deUnary Tycon.reff
73val deVector = deUnary Tycon.vector
74val deWeak = deUnary Tycon.weak
75
76fun tuple ts =
77 if 1 = Vector.length ts
78 then Vector.first ts
79 else con (Tycon.tuple, ts)
80
81val unit = tuple (Vector.new0 ())
82
83fun deTupleOpt t =
84 case deConOpt t of
85 SOME (c, ts) => if Tycon.equals (c, Tycon.tuple) then SOME ts else NONE
86 | NONE => NONE
87
88val isTuple = Option.isSome o deTupleOpt
89
90fun deTuple t =
91 case deTupleOpt t of
92 SOME t => t
93 | NONE => Error.bug "TypeOps.deTuple"
94
95val unitRef = reff unit
96
97fun deArrowOpt t =
98 case deConOpt t of
99 SOME (c, ts) => if Tycon.equals (c, Tycon.arrow)
100 then SOME (Vector.sub (ts, 0), Vector.sub (ts, 1))
101 else NONE
102 | _ => NONE
103
104fun deArrow t =
105 case deArrowOpt t of
106 SOME x => x
107 | NONE => Error.bug "TypeOps.deArrow"
108
109val deArrow =
110 Trace.trace
111 ("TypeOps.deArrow", layout, Layout.tuple2 (layout, layout))
112 deArrow
113
114end