-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtypes.lisp
More file actions
137 lines (110 loc) · 3.69 KB
/
types.lisp
File metadata and controls
137 lines (110 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
(coalton/utils:defstdlib-package #:coalton/types
(:use
#:coalton)
(:export
#:Proxy
#:proxy-of
#:as-proxy-of
#:proxy-inner
#:TypeScheme
#:LispType
#:RuntimeRepr
#:runtime-repr
#:runtime-repr-of))
(in-package #:coalton/types)
(named-readtables:in-readtable coalton:coalton)
#+coalton-release
(cl:declaim #.coalton-impl/settings:*coalton-optimize-library*)
(coalton-toplevel
(repr :enum)
(define-type (Proxy :a)
"Proxy holds no data, but has a phantom type parameter."
Proxy)
(inline)
(declare proxy-of (:a -> Proxy :a))
(define (proxy-of _)
"Returns a Proxy containing the type of the parameter."
Proxy)
(inline)
(declare as-proxy-of (:a * (Proxy :a) -> :a))
(define (as-proxy-of x _)
"Returns the parameter, forcing the proxy to have the same type as the parameter."
x)
(inline)
(declare proxy-inner (Proxy (:a :b) -> Proxy :b))
(define (proxy-inner _)
Proxy)
(repr :native (cl:or cl:symbol cl:list))
(define-type LispType
"The runtime representation of a Coalton type as a Lisp type.")
(repr :native coalton-impl/typechecker:ty-scheme)
(define-type TypeScheme
"An opaque reflection object representing a Coalton type scheme.")
(define-class (RuntimeRepr :a)
"Types which have a runtime LispType representation.
The compiler will auto-generate instances of `RuntimeRepr` for all defined types."
(runtime-repr
"The type emitted by the Coalton compiler for the type parameter to the given Proxy."
(Proxy :a -> LispType)))
(inline)
(declare runtime-repr-of
(forall (:a)
((RuntimeRepr :a) => :a -> LispType)))
(define (runtime-repr-of _)
"Returns the runtime representation of the type of the given value."
(runtime-repr (the (Proxy :a) Proxy)))
;; Additional RuntimeRepr instances for early-defined types
(define-instance (RuntimeRepr Boolean)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:boolean)))
(define-instance (RuntimeRepr Char)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:character)))
(define-instance (RuntimeRepr Integer)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:integer)))
(define-instance (RuntimeRepr F32)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:single-float)))
(define-instance (RuntimeRepr F64)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:double-float)))
(define-instance (RuntimeRepr String)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:string)))
(define-instance (RuntimeRepr Fraction)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:rational)))
(define-instance (RuntimeRepr (:a -> :b))
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'coalton-impl/runtime/function-entry:function-entry)))
(define-instance (RuntimeRepr (List :a))
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () 'cl:list)))
(define-instance (RuntimeRepr (Optional :a))
(inline)
(define (runtime-repr _)
;; If using `cl:t` proves to be inefficient we could try to
;; improve this, perhaps using proxy-inner.
(lisp (-> LispType) () 'cl:t)))
;; The compiler will not auto-generate RuntimeRepr instances for
;; types defined in this file to avoid circular dependencies.
(define-instance (RuntimeRepr LispType)
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () '(cl:or cl:symbol cl:list))))
(define-instance (RuntimeRepr (Proxy :a))
(inline)
(define (runtime-repr _)
(lisp (-> LispType) () '(cl:member 'proxy/proxy)))))
#+sb-package-locks
(sb-ext:lock-package "COALTON/TYPES")