package alba

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ast.ml

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
open Fmlib
open Alba_core

module Located = Character_parser.Located


type range = Position.t * Position.t


module Expression = struct
    type operator = string * Operator.t

    type argument_type =
      | Normal
      | Operand


    type t =
        t0 Located.t

    and t0 =
        | Proposition
        | Any
        | Identifier of string
        | Number of string
        | Char of int
        | String of string
        | Operator of operator
        | Typed of t * t                      (* exp, type *)
        | Application of t * (t * argument_type) list
        | Function of
            formal_argument list
            * t option                        (* result type *)
            * t                               (* defining expression *)
        | Product of formal_argument list * t
        | Where of t * definition list
        | List of t list

    and formal_argument =
        string Located.t * t option

    and signature =
        formal_argument list * t option

    and named_signature =
        string Located.t * signature

    and definition =
        (string Located.t * formal_argument list * t option * t) Located.t


    type operand = operator Located.t list * t




    let to_list (e: t): t0 =
        let rec to_list e =
            match Located.value e with
            | Application (f, [(a, _) ; (b, _)]) ->
                (
                    match Located.value f with
                    | Identifier "," ->
                        a :: to_list b
                    | _ ->
                        [e]
                )

            | _ ->
                [e]
        in
        List (to_list e)







    let rec occurs (name: string Located.t) (e: t0): bool =
        let name_occurs name exp =
            occurs name (Located.value exp)
        in
        let occurs_opt name term_opt =
            match term_opt with
            | None ->
                false
            | Some term ->
                name_occurs name term
        in
        let rec occurs_in_fargs fargs opt1 opt2=
            match fargs with
            | [] ->
                occurs_opt name opt1 || occurs_opt name opt2
            | (arg_name, arg_tp) :: fargs ->
                Located.value arg_name <> Located.value name
                &&
                (occurs_opt name arg_tp || occurs_in_fargs fargs opt1 opt2)
        in
        match e with
        | Proposition | Any | Number _ | Char _ | String _ | Operator _ ->
            false

        | Identifier str ->
            str = Located.value name

        | Typed (exp, tp) ->
            name_occurs name exp || name_occurs name tp

        | Application (f, args) ->
            name_occurs name f
            ||
            List.find (fun (arg, _) -> name_occurs name arg) args
            <>
            None

        | Function (fargs, res, exp) ->
            occurs_in_fargs fargs res (Some exp)

        | Product (fargs, res) ->
            occurs_in_fargs fargs (Some res) None

        | Where (exp, defs) ->
            (
                match defs with
                | [] ->
                    name_occurs name exp

                | def :: defs ->
                    let name2, fargs, res_tp, def_exp =
                        Located.value def
                    in
                    Located.value name <> Located.value name2
                    &&
                    (
                        occurs_in_fargs fargs (Some def_exp) None
                        ||
                        occurs_opt name res_tp
                        ||
                        occurs name (Where (exp, defs))
                    )
            )

        | List lst ->
            List.find (name_occurs name) lst <> None



    let rec find_unused_local
        (exp: t)
        (defs: definition list)
        : string Located.t option
        =
        match defs with
        | [] ->
            None

        | def :: defs ->
            let name, _, _, _ = Located.value def in
            if occurs name (Where (exp, defs)) then
                find_unused_local exp defs
            else
                Some name
end (* Expression *)







module Operator_expression =
struct
    open Expression

    type rest = (operator Located.t * operand) list

    let (>>=) = Result.(>>=)


    let is_left_leaning
        (op1: operator Located.t)
        (op2: operator Located.t)
        : bool
    =
        let _, op1 = Located.value op1
        and _, op2 = Located.value op2
        in
        Operator.is_left_leaning op1 op2


    let is_right_leaning
        (op1: operator Located.t)
        (op2: operator Located.t)
        : bool
    =
        let _, op1 = Located.value op1
        and _, op2 = Located.value op2
        in
        Operator.is_right_leaning op1 op2


    let apply_unary (op: operator Located.t) (e: t): t =
        let pos1 = Located.start op
        and pos2 = Located.end_ e
        in
        let inner =
            Application (
                Located.map
                    (fun (op_str, _) -> Identifier op_str)
                    op,
                [e, Operand]
            )
        in
        Located.make pos1 inner pos2


    let apply_binary (e1: t) (op: operator Located.t) (e2: t): t =
        let pos_start = Located.start e1
        and pos_end   = Located.end_ e2
        and op_str,_    = Located.value op
        in
        Located.make
            pos_start
            (
                if op_str = ":" then
                    Typed (e1, e2)
                else if op_str = "->" then
                    (* e1 -> e2 *)
                    let name = Located.map (fun _ -> "_") e1
                    in
                    match Located.value e2 with
                    | Product (formal_arguments, result_type) ->
                        Product (
                            (name, Some e1) :: formal_arguments,
                            result_type
                        )
                    | _ ->
                         Product ([name, Some e1], e2)
                else
                    Application (
                        Located.map
                            (fun (op_str,_) -> Identifier op_str)
                            op,
                        [ e1, Operand;
                          e2, Operand]
                    )
            )
            pos_end


    let split_higher (op: operator Located.t) (rest: rest): rest * rest =
        (* Split the rest in two parts. The first part contains only operators
        with higher precedence than [op]. The second part starts with an
        operator with the same precedence or lower. *)
        let precedence op =
            Operator.precedence
                (snd (Located.value op))
        in
        let prec = precedence op
        in
        List.split_at
            (fun (op2, _) ->
                precedence op2 <= prec)
            rest


    let split_right (op: operator Located.t) (rest: rest): rest * rest =
        (* Split the rest in two parts. The first part contains only operators
        which are right leaning with respect to [op] i.e. [e0 op e1 op2 e2] must
        be parsed as [e0 op (e1 op2 e2)].
        *)
        let _, op = Located.value op in
        List.split_at
            (fun (op2, _) ->
                not (
                    Operator.is_right_leaning
                        op
                        (snd (Located.value op2))
                )
            )
            rest


    let rec make
        ((unops, e0): operand)
        (rest: rest)
        : (t, range * string * string) result
    =
        match unops with
        | [] ->
            without_unary e0 rest

        | u :: unops ->
            let higher, lower_equal =
                (* All operators in [rest1] have higher precedence than [u]. *)
                split_higher u rest
            in
            make (unops, e0) higher
            >>=
            fun e ->
            without_unary
                (apply_unary u e)
                lower_equal


    and without_unary
        (e0: t)
        (rest: rest)
        : (t, range * string * string) result
    =
        match rest with
        | [] ->
            Ok e0

        | [op1, e1] ->
            make e1 []
            >>= fun e1 ->
            Ok (apply_binary e0 op1 e1)

        | (op1, e1) :: (op2, e2) :: rest ->
            if is_left_leaning op1 op2 then
                (* (e0 op1 e1) op2 e2 rest *)
                without_unary
                    e0 [op1, e1]
                >>=
                fun e ->
                without_unary
                    e
                    ((op2, e2) :: rest)
            else if is_right_leaning op1 op2 then
                (* e0 op1 (e1 op2 higher) lower_equal *)
                let higher, lower_equal =
                    split_right op1 rest
                in
                make
                    e1 ((op2, e2) :: higher)
                >>= fun e ->
                without_unary
                    e0 ((op1, ([], e)) :: lower_equal)
            else
                let op1_str, _ = Located.value op1
                and op2_str, _ = Located.value op2
                in
                Error (
                    (Located.start e0, Located.end_ (snd e2)),
                    op1_str,
                    op2_str
                )
end (* Operator_exression *)





module Source_entry =
struct
    type named_signature =
        Expression.named_signature

    type inductive =
        named_signature * named_signature array

    type t =
        | Normal of Expression.definition
        | Inductive of inductive array
end
OCaml

Innovation. Community. Security.