blob: 68d8ed9e423336f93c24c5fdb9c6640e3d6e2cb4 (
plain)
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
138
139
140
141
|
(.require
[library
[lux (.except function)
[abstract
[monad (.only do)]]
[control
["[0]" maybe]
["[0]" try]
["[0]" exception (.only exception)]]
[data
["[0]" product]
["[0]" text (.only)
["%" \\format (.only format)]]
[collection
["[0]" list (.use "[1]#[0]" monoid monad)]]]
[math
[number
["n" nat]]]
["[0]" meta (.only)
["[0]" type (.only)
["[0]" check]]]]]
["[0]" ///
["[1][0]" extension]
[//
["/" analysis (.only Analysis Operation Phase)
["[1][0]" type]
["[1][0]" inference]
["[1][0]" scope]]
[///
["[1]" phase (.use "[1]#[0]" functor)]
[reference (.only)
[variable (.only)]]]]])
(exception .public (cannot_analyse [expected Type
function Text
argument Text
body Code])
(exception.report
"Type" (%.type expected)
"Function" function
"Argument" argument
"Body" (%.code body)))
(exception .public (cannot_apply [:function: Type
functionC Code
arguments (List Code)])
(exception.report
"Function type" (%.type :function:)
"Function" (%.code functionC)
"Arguments" (|> arguments
list.enumeration
(list#each (.function (_ [idx argC])
(format (%.nat idx) " " (%.code argC))))
(text.interposed text.new_line))))
(def .public (function analyse function_name arg_name archive body)
(-> Phase Text Text Phase)
(do [! ///.monad]
[expectedT (///extension.lifted meta.expected_type)]
(loop (again [expectedT expectedT])
(/.with_exception ..cannot_analyse [expectedT function_name arg_name body]
(case expectedT
{.#Function :input: :output:}
(<| (at ! each (.function (_ [scope bodyA])
{/.#Function (list#each (|>> /.variable)
(/scope.environment scope))
bodyA}))
/scope.with
... Functions have access not only to their argument, but
... also to themselves, through a local variable.
(/scope.with_local [function_name expectedT])
(/scope.with_local [arg_name :input:])
(/type.expecting :output:)
(analyse archive body))
{.#Named name :anonymous:}
(again :anonymous:)
{.#Apply argT funT}
(case (type.applied (list argT) funT)
{.#Some value}
(again value)
{.#None}
(/.failure (exception.error ..cannot_analyse [expectedT function_name arg_name body])))
{.#UnivQ _}
(do !
[[@instance :instance:] (/type.check check.existential)]
(again (maybe.trusted (type.applied (list :instance:) expectedT))))
{.#ExQ _}
(<| /type.with_var
(.function (_ [@instance :instance:]))
(again (maybe.trusted (type.applied (list :instance:) expectedT))))
{.#Var id}
(do !
[?expectedT' (/type.check (check.peek id))]
(case ?expectedT'
{.#Some expectedT'}
(again expectedT')
... Inference
_
(<| /type.with_var
(.function (_ [@input :input:]))
/type.with_var
(.function (_ [@output :output:]))
(do !
[functionA (again {.#Function :input: :output:})])
/type.check
(do check.monad
[:output: (check.identity (list) @output)
?:input: (check.try (check.identity (list @output) @input))
? (check.linked? @input @output)
_ (<| (check.check expectedT)
(case ?:input:
{try.#Success :input:}
{.#Function :input: (if ?
:input:
:output:)}
{try.#Failure _}
(|> (if ?
:input:
:output:)
{.#Function :input:}
(/inference.quantified @input 1)
{.#UnivQ (list)})))]
(in functionA)))))
_
(/.failure "")
)))))
(def .public (apply analyse argsC+ :function: functionA archive functionC)
(-> Phase (List Code) Type Analysis Phase)
(|> (/inference.general archive analyse :function: argsC+)
(///#each (|>> product.right [functionA] /.reified))
(/.with_exception ..cannot_apply [:function: functionC argsC+])))
|