package alba

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

Source file standard_context.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
open Alba_core
open Ast


module Inductive_parser =
    Parser_lang.Make (
        struct
            type t = Source_entry.inductive array
        end)


module Expression_parser =
    Parser_lang.Make (Expression)


module Definition_parser =
    Parser_lang.Make (
        struct
            type t = Expression.definition
        end)




let add_definition (src: string) (c: Context.t): Context.t =
    let open Definition_parser in
    let p = run (global_definition ()) src in
    assert (has_ended p);
    assert (has_succeeded p);
    match result p with
    | None ->
        assert false (* Cannot happen. *)
    | Some def ->
        match Builder.add_definition def c with
        | Error _ ->
            assert false
        | Ok c ->
            c





let add_inductive (src: string) (c: Context.t): Context.t =
    let open Inductive_parser in
    let p = run (inductive_family ()) src in
    assert (has_ended p);
    assert (has_succeeded p);
    match result p with
    | None ->
        assert false (* Cannot happen. *)
    | Some def ->
        match Builder.add_inductive def c with
        | Error _ ->
            Printf.printf "Standard_context.add_inductive\n%s\n" src;
            assert false
        | Ok c ->
            c


let add_builtin
    (fun_flag: bool)
    (descr: string) (name: string) (src: string) (c: Context.t):
    Context.t
=
    let open Expression_parser in
    let p = run (expression ()) src in
    assert (has_ended p);
    assert (has_succeeded p);
    match result p with
    | None ->
        assert false (* Cannot happen. *)
    | Some exp ->
        match Build_expression.build exp c with
        | Error _ ->
            assert false
        | Ok (typ, _) ->
            if fun_flag then
                Context.add_builtin_function descr name typ c
            else
                Context.add_builtin_type descr name typ c






let add_logic (c: Context.t): Context.t =
    c
    |>
    add_definition
        "Predicate (A: Any): Any := A -> Proposition"
    |>
    add_definition
        "Relation (A: Any) (B: Any): Any := A -> B -> Proposition"
    |>
    add_definition
        "Endorelation (A: Any): Any := Relation A A"
    |>
    add_inductive
        "class (=) (A: Any) (a: A): Predicate A := identical: a = a"
    |>
    add_definition
        "(=>) (a: Proposition) (b: Proposition): Proposition :=\
        \n  a -> b"
    |>
    add_inductive
        "class false: Proposition :="
    |>
    add_definition
        "(not) (a: Proposition): Proposition := a => false"
    |>
    add_inductive
        "class true: Proposition := trueValid"
    |>
    add_inductive
        "class (and) (a: Proposition) (b: Proposition): Proposition :=\
        \n    (,): a => b => a and b"
    |>
    add_inductive
        "class (or) (a: Proposition) (b: Proposition): Proposition :=\
        \n    left:  a => a or b\
        \n    right: b => a or b"
    |>
    add_inductive
        "class exist (A: Any) (f: A -> Proposition): Proposition :=\
        \n    witness (a: A): f a => exist f"




let add_basics (c: Context.t): Context.t =
    c
    |>
    add_definition
        "identity (A: Any) (a: A): A := a"
    |>
    add_definition
        "(|>) (A: Any) (a: A) (F: A -> Any) (f: all x: F x): F a := f a"
    |>
    add_definition
        "(<|) (A: Any) (F: A -> Any) (f: all x: F x) (a: A): F a := f a"
    |>
    add_definition
        "(>>) (A: Any) (B: Any) (C: Any) (f: A -> B) (g: B -> C): A -> C :=
        \n    \\x := g (f x)"
    |>
    add_definition
        "(<<) (A: Any) (B: Any) (C: Any) (f: B -> C) (g: A -> B): A -> C :=
        \n    \\x := f (g x)"
    |>
    add_inductive
        "class Decision (a: Proposition) (b: Proposition): Any := \
        \n    left:  a -> Decision a b
        \n    right: b -> Decision a b"
    |>
    add_inductive
        "class Maybe (A: Any): Any := \
        \n    nothing: Maybe A\
        \n    just : A -> Maybe A"
    |>
    add_inductive
        "class List (A: Any): Any := \
        \n    []: List A\
        \n    (+:): A -> List A -> List A"




let add_builtins (c: Context.t): Context.t =
    c
    |>
    add_builtin false "int_type" "Int" "Any"
    |>
    add_builtin true  "int_plus" "+" "Int -> Int -> Int"
    |>
    add_builtin true  "int_minus" "-" "Int -> Int -> Int"
    |>
    add_builtin true  "int_negate" "-" "Int -> Int"
    |>
    add_builtin true "int_times" "*" "Int -> Int -> Int"
    |>
    add_builtin false "string_type" "String" "Any"
    |>
    add_builtin true  "string_concat" "+" "String -> String -> String"
    |>
    add_builtin false "character_type" "Character" "Any"





let make (): Context.t =
    Context.empty
    |>
    add_logic
    |>
    add_builtins
    |>
    add_basics



let _ = make ()
OCaml

Innovation. Community. Security.