Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8be2c9e

Browse files
committed
Update: Typpe checker implementation in progress
1 parent ef2973d commit 8be2c9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+223
-287
lines changed

.ocamlformat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = 0.14.2
1+
version = 0.17.0
22
margin = 80
33
max-indent = 68
44

@@ -53,7 +53,6 @@ let-and = sparse
5353
let-binding-indent = 2
5454
let-binding-spacing = compact
5555
let-module = compact
56-
let-open = auto
5756

5857
match-indent = 0
5958
match-indent-nested = never

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
.PHONY: info init all test clean fmt
22

33
info:
4-
echo "make [init|all|test|clean|fmt]"
4+
echo "make [init|build|test|clean|fmt]"
55

66
init:
77
eval `(opam env)`
88

9-
all:
9+
build:
1010
dune build
1111

1212
test:
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

attic/test/checker/main_test.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let () =
2+
let open Alcotest in
3+
run "checker" [ Variables_test.test_cases; Type_check_test.test_cases ]
File renamed without changes.

test/parser/entity_test.ml renamed to attic/test/parser/entity_test.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ let cases =
2121
, [
2222
Entity.Sig
2323
( "::"
24-
, Type.(Forall ("a", Kind.Type, Apply (Variable "list", Variable "a")))
24+
, Type.(
25+
Forall ("a", Kind.Type, Apply (Variable "list", Variable "a")))
2526
, None
2627
, [] )
2728
] )
@@ -141,5 +142,5 @@ let test_cases =
141142
, List.map
142143
(fun (input, expected) ->
143144
test_case ("Should parse " ^ input) `Quick (fun () ->
144-
should_parse input expected))
145+
should_parse input expected ))
145146
cases )
File renamed without changes.

test/parser/kind_test.ml renamed to attic/test/parser/kind_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ let test_cases =
3030
, List.map
3131
(fun (input, expected) ->
3232
test_case ("Should parse " ^ input) `Quick (fun () ->
33-
should_parse input expected))
33+
should_parse input expected ))
3434
cases )
File renamed without changes.
File renamed without changes.

test/parser/sources_test.ml renamed to attic/test/parser/sources_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ let test_cases =
2323
let t = Sys.time () in
2424
let fx = should_parse @@ Ioutils.read_fully filename in
2525
let () = Printf.printf "Execution time: %fs\n" (Sys.time () -. t) in
26-
fx))
26+
fx ))
2727
cases )

test/parser/term_test.ml renamed to attic/test/parser/term_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ let test_cases =
5353
, List.map
5454
(fun (input, expected) ->
5555
test_case ("Should parse " ^ input) `Quick (fun () ->
56-
should_parse input expected))
56+
should_parse input expected ))
5757
cases )

test/parser/type_test.ml renamed to attic/test/parser/type_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ let test_cases =
5454
, List.map
5555
(fun (input, expected) ->
5656
test_case ("Should parse " ^ input) `Quick (fun () ->
57-
should_parse input expected))
57+
should_parse input expected ))
5858
cases )

lib/ast/ast.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Kind = Kind
2+
module Type = Type

lib/ast/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(library
22
(name lambe_ast)
33
(public_name lambe.ast)
4-
(modules_without_implementation kind type term entity))
4+
(modules_without_implementation kind type ast))

lib/ast/entity.mli

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/ast/kind.mli

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1-
type t =
2-
| Arrow of t * t
3-
| Type
1+
(*
2+
3+
κ =
4+
| ⋆
5+
| κ→κ
6+
7+
*)
8+
9+
type 'a t =
10+
| Type of 'a
11+
| Arrow of 'a t * 'a t * 'a
12+
13+
(*
14+
The polymorphic type 'a holds open informations
15+
like locations etc. depending on the context
16+
*)

lib/ast/term.mli

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/ast/type.mli

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
1-
type t =
2-
| Path of string list (* ??? *)
3-
| Variable of string
4-
| Apply of t * t
5-
| Forall of string * Kind.t * t
1+
(*
2+
3+
Type and environment definition
4+
5+
τ =
6+
| α
7+
| m
8+
| τ → τ
9+
| τ􏰀 #> τ
10+
| τ τ
11+
| τ + τ
12+
| ∀(α : κ).τ
13+
| ∃(α : κ).τ
14+
| μ(α).τ
15+
| c S
16+
| Γ
17+
18+
K = {mi:κi}I
19+
T = {mi􏰁=τi}I
20+
S = {mi:τi}I
21+
W = {Γi}I
22+
M = {mi􏰁εi}I
23+
Γ = ⟨K,T,S,W⟩
24+
25+
*)
26+
27+
type 'a set = (string * 'a) list
28+
29+
type 'a t =
30+
| Variable of string * 'a
31+
| Arrow of 'a t * 'a t * 'a
32+
| Invoke of 'a t * 'a t * 'a
33+
| Apply of 'a t * 'a t * 'a
34+
| Sum of 'a t * 'a t * 'a
35+
| Forall of string * 'a Kind.t * 'a t * 'a
36+
| Exists of string * 'a Kind.t * 'a t * 'a
37+
| Rec of string * 'a t * 'a
38+
| Const of string * (string * 'a t) list * 'a
39+
| Trait of 'a gamma * 'a
40+
41+
and 'a gamma = Gamma of 'a Kind.t set * 'a t set * 'a t set * 'a gamma list
42+
43+
(*
44+
The polymorphic type 'a holds open informations
45+
like locations etc. depending on the context
46+
*)

lib/checker/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(library
22
(name lambe_checker)
33
(public_name lambe.checker)
4-
(libraries lambe.ast))
4+
(libraries lambe.ast)
5+
(modules kind type))

lib/checker/kind.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(*
2+
3+
------
4+
k ⊆κ ⋆
5+
6+
k1 ⊆κ k3 k2 ⊆κ k4
7+
--------------------
8+
k1 → k2 ⊆κ k3 → k4
9+
10+
*)
11+
12+
open Lambe_ast
13+
14+
module Checker = struct
15+
let rec subsume k1 k2 =
16+
let open Kind in
17+
match k1, k2 with
18+
| _, Type _ -> true
19+
| Arrow (k1, k2, _), Arrow (k3, k4, _) -> subsume k1 k3 && subsume k2 k4
20+
| _ -> false
21+
end

lib/checker/kind.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
open Lambe_ast
2+
3+
module Checker : sig
4+
val subsume : 'a Kind.t -> 'a Kind.t -> bool
5+
end

lib/checker/subtype.ml

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/checker/subtype.mli

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/checker/type.ml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
open Lambe_ast
2+
3+
(*
4+
module Gamma = struct
5+
open Type
6+
7+
let neutral = Gamma ([], [], [], [])
8+
9+
let combine (Gamma (k, t, s, w)) (Gamma (k', t', s', w')) =
10+
Gamma (k @ k', t @ t', s @ s', w @ w')
11+
12+
let k_get = function Gamma (k, _, _, _) -> k
13+
14+
let k_set k = Gamma (k, [], [], [])
15+
16+
let t_get = function Gamma (_, t, _, _) -> t
17+
18+
let t_set t = Gamma ([], t, [], [])
19+
20+
let s_get = function Gamma (_, _, s, _) -> s
21+
22+
let w_get = function Gamma (_, _, _, w) -> w
23+
end
24+
*)
25+
26+
module Substitution = struct
27+
(* Substituste v by r in t *)
28+
let rec substitute v r t =
29+
let open Type in
30+
let subst_field (n, t1) = n, substitute v r t1 in
31+
let rec subst_gamma (Gamma (kd, ty, si, wi)) =
32+
Gamma
33+
( kd
34+
, List.map subst_field ty
35+
, List.map subst_field si
36+
, List.map subst_gamma wi )
37+
in
38+
match t with
39+
| Variable (a, _) when v = a -> r
40+
| Variable _ -> t
41+
| Arrow (t1, t2, s) -> Arrow (substitute v r t1, substitute v r t2, s)
42+
| Invoke (t1, t2, s) -> Invoke (substitute v r t1, substitute v r t2, s)
43+
| Apply (t1, t2, s) -> Apply (substitute v r t1, substitute v r t2, s)
44+
| Sum (t1, t2, s) -> Sum (substitute v r t1, substitute v r t2, s)
45+
| Forall (a, _, _, _) when a = v -> t
46+
| Forall (a, k, t1, s) -> Forall (a, k, substitute v r t1, s)
47+
| Exists (a, _, _, _) when a = v -> t
48+
| Exists (a, k, t1, s) -> Exists (a, k, substitute v r t1, s)
49+
| Rec (a, _, _) when v = a -> t
50+
| Rec (a, t1, s) -> Rec (a, substitute v r t1, s)
51+
| Const (a, l1, s) -> Const (a, List.map subst_field l1, s)
52+
| Trait (gamma, s) -> Trait (subst_gamma gamma, s)
53+
end
54+
55+
module Checker = struct
56+
let rec subsume g t1 t2 =
57+
let open Type in
58+
let open Substitution in
59+
match t1, t2 with
60+
| _ when t1 = t2 -> true
61+
| Arrow (t1, t2, _), Arrow (t3, t4, _) -> subsume g t3 t1 && subsume g t2 t4
62+
| Invoke (t1, t2, _), Invoke (t3, t4, _) ->
63+
subsume g t3 t1 && subsume g t2 t4
64+
| t1, Sum (t2, t3, _) -> subsume g t1 t2 || subsume g t1 t3
65+
| Rec(a,t1,_),Rec(b,t2,s) -> subsume g t1 (substitute b (Variable (a,s)) t2) (* Fresh variable required *)
66+
| _ -> false
67+
end

lib/checker/type.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
open Lambe_ast
2+
3+
module Checker : sig
4+
val subsume : 'a Type.gamma -> 'a Type.t -> 'a Type.t -> bool
5+
end

lib/checker/type_check.ml

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/checker/type_check.mli

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/checker/variables.ml

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/checker/variables.mli

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/lambe/dune

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)