package alba

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

Source file gamma_algo.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
open Fmlib
open Common


type name_violation =
    | Upper_for_proposition
    | Lower_for_type
    | Upper_for_object


let strings_of_violation
    : name_violation -> string * string
=
    function
    | Upper_for_proposition ->
        "Upper", "proposition"
    | Lower_for_type ->
        "Lower", "type or type constructor"
    | Upper_for_object ->
        "Upper", "object or function"




module type GAMMA =
sig
    type t

    val count: t -> int
    val is_valid_index: int -> t -> bool
    val name_of_index: int -> t -> string
    val push_local: string -> Term.typ -> t -> t
    val type_of_literal:    Value.t -> t -> Term.typ
    val type_of_variable: int -> t -> Term.typ
    val definition_term: int -> t -> Term.t option
end











module Make (Gamma: GAMMA) =
struct
    include Gamma

    module String_print = Term_printer.String_print (Gamma)
    let string_of_term (t: Term.t) (c: t): string =
        String_print.string_of_term t c
    let _ = string_of_term


    let key_split
          (t: Term.t)
          (c: t)
        : Term.t * (Term.t * Term.Application_info.t) list
      =
      let rec split t args =
        let open Term in
        match t with
        | Variable i ->
           (match definition_term i c with
            | None ->
               t, args
            | Some def ->
               split def args)

        | Lambda (_, exp, _) ->
            (
                match args with
                | [] ->
                    t, args

                | (arg, _) :: args ->
                    split Term.(apply exp arg) args
            )

        | Appl (f, arg, mode) ->
           split f ((arg, mode) :: args)

        | Typed (term, _) ->
            split term args

        | Where (_, _, exp, def) ->
            split (apply exp def) args

        | _ ->
           t, args
      in
      split t []




    let key_normal (t: Term.t) (c: t): Term.t =
        let key, args = key_split t c in
        List.fold_left
            (fun res (arg, mode) ->
              Term.Appl (res, arg, mode))
            key
            args


    let rec normalize_pi (typ: Term.typ) (c: t): Term.typ =
        let open Term in
        match key_normal typ c with
        | Pi (tp, res, info) ->
            Pi (
                tp,
                normalize_pi
                    res
                    (push_local (Pi_info.name info) tp c),
                info
            )
        | typ ->
            typ



    let rec normalize (term: Term.t) (c: t): Term.t =
        let normalize_key key c =
            let open Term in
            match key with
            | Lambda (tp, exp, info) ->
                Lambda (
                    normalize tp c,
                    normalize exp (push_local (Lambda_info.name info) tp c),
                    info
                )

            | Pi (tp, res, info) ->
                Pi (
                    normalize tp c,
                    normalize res
                        (push_local (Pi_info.name info) tp c),
                    info
                )

            | _ ->
                key
        in
        let key, args = key_split term c in
        List.fold_left
            (fun res (arg, mode) ->
                Term.Appl (res, normalize arg c, mode))
            (normalize_key key c)
            args



    let type_of_term (t: Term.t) (c: t): Term.typ =
        let rec typ t c =
            let open Term in
            match t with
            | Sort s ->
                type_of_sort s

            | Value v ->
                type_of_literal v c

            | Variable i ->
                type_of_variable i c

            | Typed (_, tp) ->
               tp

            | Appl (f, a, _) ->
               (match key_normal (typ f c) c with
                | Pi (_, rt, _) ->
                   apply rt a
                | _ ->
                   assert false (* Illegal call! Term is not welltyped. *)
               )

            | Lambda (tp, exp, info) ->
                let c_inner = push_local (Lambda_info.name info) tp c in
                let rt      = typ exp c_inner
                in
                let info =
                    if has_variable 0 rt then
                        Pi_info.typed (Lambda_info.name info)
                    else
                        Pi_info.arrow
                in
                Pi (tp, rt, info)

            | Pi (tp, rt, info) ->
               let name = Pi_info.name info in
               (match
                  typ tp c, typ rt (push_local name tp c)
                with
                | Sort s1, Sort s2 ->
                  let open Sort in
                  (match s1, s2 with
                    | Proposition, Any i ->
                      Sort (Any i)

                    | Any i, Any j ->
                      Sort (Any (max i j))

                    | _, Proposition ->
                      Sort Proposition
                  )

                | _, _ ->
                   assert false (* Illegal call: term is not welltyped! *)
               )

            | Where (name, tp, exp, def) ->
                typ (expand_where name tp exp def) c
        in
        typ t c



    let split_type
        (typ: Term.typ)
        (c: t)
        : (Term.Pi_info.t * Term.typ) list * Term.typ
    =
        let rec split args typ c =
            let open Term in
            match key_normal typ c with
            | Pi (arg, res, info) ->
                split
                    ((info, arg) :: args)
                    res
                    (push_local (Pi_info.name info) arg c)

            | typ ->
                List.rev args, typ
        in
        split [] typ c



    let split_kind
        (k: Term.typ)
        (c: t)
        : ((Term.Pi_info.t * Term.typ) list * Sort.t) option
    =
        let args, res = split_type k c in
        let open Term in
        match res with
        | Sort s ->
            Some (args, s)
        | _ ->
            None



    let sort_of_kind (k: Term.typ) (c:t): Sort.t option =
        Option.map
            snd
            (split_kind k c)


    let is_kind (k: Term.typ) (c: t): bool =
        Option.has (sort_of_kind k c)



    let check_naming_convention
        (name: string)
        (typ: Term.typ)
        (c: t)
        : (unit, name_violation) result
    =
        (* [check_naming_convention name sort]. Check, if [name] satisfies the
        naming convention for a type of sort [sort]. If yes, return [Ok ()]. If
        not return the name violation. *)
        let is_lower, is_upper =
            if String.length name > 0 then
                let c = name.[0] in
                Char.is_lower c, Char.is_upper c
            else
                false, false
        in
        match sort_of_kind typ c with
        | Some (Sort.Any _) ->
            (* Must be upper case *)
            if is_lower then
                Error  Lower_for_type
            else
                Ok ()

        | Some Sort.Proposition ->
            (* Must be lower case *)
            if is_upper then
                Error Upper_for_proposition
            else
                Ok ()
        | None ->
            (* proof or object, must be lower case *)
            if is_upper then
                Error Upper_for_object
            else
                Ok ()
end
OCaml

Innovation. Community. Security.