Skip to content

Commit 69b60a1

Browse files
authored
Merge pull request #995 from WardBrian/rewrite-typechecker
Refactor typechecker
2 parents ca8b4dd + cc4cf50 commit 69b60a1

Some content is hidden

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

47 files changed

+1848
-2455
lines changed

README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,44 @@ The entrypoint for the compiler is in `src/stanc/stanc.ml` which sequences the v
2828
### Distinct stanc Phases
2929

3030
The phases of stanc are summarized in the following information flowchart and list.
31-
31+
<!---
32+
digraph G {
33+
rankdir=TB;
34+
ranksep=.25;
35+
bgcolor=white;
36+
size=5;
37+
node [shape="box"];
38+
39+
origin[style=invis];
40+
stanc[label="stanc/stanc.ml"];
41+
lexer[label="frontend/lexer.mll"];
42+
parser[label="frontend/parser.mly"];
43+
type[label="frontend/Typechecker.ml"];
44+
lower[label="frontend/Ast_to_Mir.ml"];
45+
transform[label="*_backend/Transform_Mir.ml"];
46+
optimize[label="analysis_and_optimization/Optimize.ml"];
47+
codegen[label="*_backend/*_code_gen.ml"];
48+
output[shape="oval" label=".hpp file"]
49+
50+
51+
origin -> stanc[label=" .stan file path"];
52+
stanc -> lexer[label=" string"];
53+
lexer -> parser[label=" tokens"];
54+
parser -> type[label=" untyped AST"];
55+
type -> lower[label=" typed AST"];
56+
lower -> transform[label=" MIR"];
57+
transform -> optimize[label=" transformed MIR"];
58+
transform -> codegen[label=" "];
59+
optimize -> codegen[headlabel="optimized MIR "];
60+
codegen -> output[label=" C++ code"];
61+
62+
}
63+
--->
3264
![stanc3 information flow](docs/img/information-flow.png)
3365

3466
1. [Lex](src/frontend/lexer.mll) the Stan language into tokens.
3567
1. [Parse](src/frontend/parser.mly) Stan language into AST that represents the syntax quite closely and aides in development of pretty-printers and linters. `stanc --debug-ast` to print this out.
36-
1. Typecheck & add type information [Semantic_check.ml](src/frontend/Semantic_check.ml). `stanc --debug-decorated-ast`
68+
1. Typecheck & add type information [Typechecker.ml](src/frontend/Typechecker.ml). `stanc --debug-decorated-ast`
3769
1. [Lower](src/frontend/Ast_to_Mir.ml) into [Middle Intermediate Representation](src/middle/Mir.ml) (AST -> MIR) `stanc --debug-mir` (or `--debug-mir-pretty`)
3870
1. Analyze & optimize (MIR -> MIR)
3971
1. Backend MIR transform (MIR -> MIR) [Transform_Mir.ml](src/stan_math_backend/Transform_Mir.ml) `stanc --debug-transformed-mir`

docs/img/information-flow.png

-8.09 KB
Loading

src/common/Common.ml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
module Foldable = Foldable
33

44
(* Other signatures *)
5-
module Validation = Validation
65
module Pretty = Pretty
76

87
(* 'Two-level type' signatures and functors *)

src/common/Common.mli

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Foldable : module type of Foldable
44

55
(** Other signatures *)
66

7-
module Validation : module type of Validation
87
module Pretty : module type of Pretty
98

109
(** 'Two-level type' signatures and functors *)

src/common/Validation.ml

-167
This file was deleted.

src/common/Validation.mli

-38
This file was deleted.

src/frontend/Ast_to_Mir.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -878,5 +878,5 @@ let trans_prog filename (p : Ast.typed_program) : Program.Typed.t =
878878
; generate_quantities
879879
; transform_inits
880880
; output_vars
881-
; prog_name= normalize_prog_name !Semantic_check.model_name
881+
; prog_name= normalize_prog_name !Typechecker.model_name
882882
; prog_path= filename }

src/frontend/Deprecation_analysis.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
open Core_kernel
22
open Ast
33

4-
type t = Middle.Warnings.t
4+
type t = Warnings.t
55

66
val find_udf_log_suffix :
77
typed_statement -> (string * Middle.UnsizedType.t) option

src/frontend/Environment.ml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
open Core_kernel
2+
open Middle
3+
4+
type originblock =
5+
| MathLibrary
6+
| Functions
7+
| Data
8+
| TData
9+
| Param
10+
| TParam
11+
| Model
12+
| GQuant
13+
[@@deriving sexp]
14+
15+
type varinfo = {origin: originblock; global: bool; readonly: bool}
16+
[@@deriving sexp]
17+
18+
type info =
19+
{ type_: UnsizedType.t
20+
; kind:
21+
[ `Variable of varinfo
22+
| `UserDeclared of Location_span.t
23+
| `StanMath
24+
| `UserDefined ] }
25+
[@@deriving sexp]
26+
27+
type t = info list String.Map.t
28+
29+
let create () =
30+
let functions =
31+
Hashtbl.to_alist Stan_math_signatures.stan_math_signatures
32+
|> List.map ~f:(fun (key, values) ->
33+
( key
34+
, List.map values ~f:(fun (rt, args, mem) ->
35+
let type_ =
36+
UnsizedType.UFun
37+
(args, rt, Fun_kind.suffix_from_name key, mem)
38+
in
39+
{type_; kind= `StanMath} ) ) )
40+
|> String.Map.of_alist_exn
41+
in
42+
functions
43+
44+
let add env key type_ kind = Map.add_multi env ~key ~data:{type_; kind}
45+
let set_raw env key data = Map.set env ~key ~data
46+
let find env key = Map.find_multi env key
47+
let mem env key = Map.mem env key
48+
let iter env f = Map.iter env ~f

src/frontend/Environment.mli

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(** Type environments used during typechecking. Maps from strings to function or variable information *)
2+
3+
open Middle
4+
5+
(** Origin blocks, to keep track of where variables are declared *)
6+
type originblock =
7+
| MathLibrary
8+
| Functions
9+
| Data
10+
| TData
11+
| Param
12+
| TParam
13+
| Model
14+
| GQuant
15+
[@@deriving sexp]
16+
17+
(** Information available for each variable *)
18+
type varinfo = {origin: originblock; global: bool; readonly: bool}
19+
[@@deriving sexp]
20+
21+
type info =
22+
{ type_: UnsizedType.t
23+
; kind:
24+
[ `Variable of varinfo
25+
| `UserDeclared of Location_span.t
26+
| `StanMath
27+
| `UserDefined ] }
28+
[@@deriving sexp]
29+
30+
type t
31+
32+
val create : unit -> t
33+
(** Return a new type environment which contains the Stan math library functions
34+
*)
35+
36+
val find : t -> string -> info list
37+
38+
val add :
39+
t
40+
-> string
41+
-> Middle.UnsizedType.t
42+
-> [ `UserDeclared of Location_span.t
43+
| `StanMath
44+
| `UserDefined
45+
| `Variable of varinfo ]
46+
-> t
47+
(** Add a new item to the type environment. Does not overwrite existing, but shadows *)
48+
49+
val set_raw : t -> string -> info list -> t
50+
(** Overwrite the existing items bound to a name *)
51+
52+
val mem : t -> string -> bool
53+
val iter : t -> (info list -> unit) -> unit

0 commit comments

Comments
 (0)