Skip to content
This repository was archived by the owner on Dec 22, 2024. It is now read-only.

Commit 5fe0b85

Browse files
implemented new type systems
1 parent ba611ef commit 5fe0b85

File tree

3 files changed

+142
-24
lines changed

3 files changed

+142
-24
lines changed

src/basic/abstractions.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
#include <list>
3+
#include <unordered_map>
4+
#include <unordered_set>
5+
6+
#include "basic/helper_functions.hpp"
7+
namespace mimium {
8+
template <template <class> class C>
9+
struct MakeRecursive {
10+
using type = C<Box<MakeRecursive>>;
11+
};
12+
13+
template <template <class...> class Category, class T, int ID = 0>
14+
struct CategoryWrapped {
15+
using type = Category<T>;
16+
type v;
17+
};
18+
19+
template <class T>
20+
using IdentCategory = T;
21+
22+
template <class T>
23+
using List = std::list<T>;
24+
25+
template <class T>
26+
using Set = std::unordered_set<T>;
27+
template <class FROM, class TO>
28+
using Map = std::unordered_map<FROM, TO>;
29+
30+
template <class R, class... ArgTypes>
31+
using Fn = std::function<R(ArgTypes...)>;
32+
33+
template <class T, template <class...> class C, template <class...> class... Cs>
34+
struct Nested {
35+
using type = C<Nested<T, Cs...>>;
36+
};
37+
} // namespace mimium

src/basic/ast_new.hpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
#pragma once
22

33
#include <list>
4+
#include "abstractions.hpp"
45
#include "type.hpp"
56

67
namespace mimium {
78

8-
template <template <class> class C>
9-
struct MakeRecursive {
10-
using asts = C<Box<MakeRecursive>>;
11-
using type = typename asts::Expr;
12-
};
13-
149
template <class EXPR>
1510
struct Ast {
1611
template <template <class...> class Category, class T, int ID = 0>
17-
struct Aggregate {
18-
Category<T> v;
19-
using type = Category<T>;
20-
};
21-
22-
template <class T>
23-
using IdentCategory = T;
12+
using Aggregate = CategoryWrapped<Category, T, ID>;
2413

2514
template <class T, int ID = 0>
2615
using Primitive = Aggregate<IdentCategory, T, ID>;
@@ -31,9 +20,6 @@ struct Ast {
3120
using StringLit = Primitive<std::string>;
3221
using Symbol = Primitive<std::string, 1>;
3322

34-
template <class T>
35-
using List = std::list<T>;
36-
3723
using TupleLit = Aggregate<List, EXPR>;
3824
using ArrayLit = Aggregate<List, EXPR, 1>;
3925
struct StructKey {
@@ -90,16 +76,16 @@ using ExprBase = std::variant<FloatLit,
9076
// the Ast struct has to declare Expr type to use in MakeRecursive Container.
9177
using Expr = ExprBase<App, Let>;
9278
};
93-
using ExprPrim = MakeRecursive<Ast>;
79+
using ExprPrim = MakeRecursive<Ast>::type;
9480

95-
ExprPrim::type e{ExprPrim::asts::FloatLit{0.0}};
96-
auto& test = std::get<ExprPrim::asts::FloatLit>(e);
81+
ExprPrim::Expr e{ExprPrim::FloatLit{0.0}};
82+
auto& test = std::get<ExprPrim::FloatLit>(e);
9783

9884
// HIGH-LEVEL AST including Syntactic Sugar
9985
template <class EXPR>
10086
struct Hast {
10187
template <template <class...> class T, class U, int ID = 0>
102-
using Aggregate = ExprPrim::asts::Aggregate<T, U, ID>;
88+
using Aggregate = ExprPrim::Aggregate<T, U, ID>;
10389

10490
struct CurryPlaceHolder {};
10591
using CurryArg = std::variant<EXPR, CurryPlaceHolder>;
@@ -131,18 +117,18 @@ struct Hast {
131117
};
132118
using Assignment = AssignmentProto<EXPR>;
133119
template <class T>
134-
using LCategory = ExprPrim::asts::LambdaCategory<T>;
120+
using LCategory = ExprPrim::LambdaCategory<T>;
135121
using DefFnRvalue = typename Aggregate<LCategory, EXPR>::type;
136122
using DefFn = AssignmentProto<DefFnRvalue>;
137123

138-
using Statements = std::variant<Assignment, DefFn, ExprPrim::asts::App>;
124+
using Statements = std::variant<Assignment, DefFn, ExprPrim::App>;
139125

140126
// using ExtFun;TODO
141127
using Block = Aggregate<std::list, Statements>;
142-
using Expr = ExprPrim::asts::ExprBase<Infix, Schedule, EnvVar, App, Block>;
128+
using Expr = ExprPrim::ExprBase<Infix, Schedule, EnvVar, App, Block>;
143129
};
144130

145-
using HExpr = MakeRecursive<Hast>;
131+
using HExpr = MakeRecursive<Hast>::type;
146132

147133
struct TypeAlias; // type name = hoge
148134

src/basic/type_new.hpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#pragma once
2+
#include "abstractions.hpp"
3+
4+
namespace mimium {
5+
6+
template <class Value>
7+
struct Type {
8+
struct Unit {};
9+
struct Bool {};
10+
struct Int {};
11+
struct Float {};
12+
struct String {};
13+
template <template <class...> class C, int ID = 0>
14+
using Aggregate = CategoryWrapped<C, Value, ID>;
15+
// T1 * T2 * T3 ...
16+
using Tuple = Aggregate<List>;
17+
// T1 | T2 | T3...
18+
using Variant = Aggregate<List, 1>;
19+
// T1 -> T2 -> T3...
20+
using Function = Aggregate<List, 2>;
21+
22+
template <class Identifier>
23+
struct RecordCategory {
24+
Identifier key;
25+
Value v;
26+
};
27+
using Record = CategoryWrapped<List, RecordCategory<std::string>>;
28+
29+
// Identified type
30+
template <class T>
31+
struct IdentifiedCategory {
32+
T id;
33+
Value v;
34+
};
35+
using Identified = typename CategoryWrapped<IdentifiedCategory, int>::type;
36+
37+
template <class... T>
38+
using TypeBase = std::variant<Unit, Bool, Int, Float, String, Tuple, Variant, Function, Record,
39+
Identified, T...>;
40+
41+
using Intermediate = typename CategoryWrapped<IdentifiedCategory, int, 1>::type;
42+
using HType = TypeBase<>;
43+
44+
using IType = TypeBase<Intermediate>;
45+
46+
struct LType {};
47+
};
48+
49+
using types = MakeRecursive<Type>::type;
50+
51+
template <class ID, class Value>
52+
struct Alias {
53+
ID name;
54+
Value v;
55+
};
56+
57+
struct TypeResolver {
58+
using HType = types::HType;
59+
60+
// TypeIdの集合と束縛のリスト
61+
template <class T>
62+
using Binding = std::pair<T, T>;
63+
using IType = types::IType;
64+
using Constraints = Set<Binding<IType>>;
65+
using map = Map<IType, HType>;
66+
};
67+
68+
// 変数と型の写像がスコープごとに
69+
template <class Env>
70+
struct Environment {
71+
template <class T, class Identifier>
72+
struct Value {
73+
Map<Identifier, T> map;
74+
std::optional<Env> e;
75+
};
76+
};
77+
template <class T>
78+
using TypeEnv = MakeRecursive<Environment>::type::Value<T, std::string>;
79+
80+
template <class AST>
81+
struct TypeInferer {
82+
// ASTを入れると中間変数を含む型環境が帰ってくる
83+
using Pass1 = TypeEnv<types::IType>(AST);
84+
//中間変数を含む型環境を受け取り、変数同士の制約を作る
85+
using Pass2 = TypeResolver::Constraints(std::result_of_t<Pass1>);
86+
//変数の制約を受け取り、中間変数を含む型集合->中間変数を取り除いた型集合 の写像をつくる
87+
using Pass3 = TypeResolver::map(std::result_of_t<Pass2>);
88+
//中間変数を含む型環境と、中間変数を含む型集合->中間変数を取り除いた型集合の写像を受け取り、
89+
//中間変数を取り除いた型環境を返す
90+
using Pass4 = TypeEnv<types::HType>(std::result_of_t<Pass1>, std::result_of_t<Pass3>);
91+
//全体としては、ASTを入れると中間変数がない型環境が帰ってくる
92+
using type = TypeEnv<types::HType>(AST);
93+
};
94+
95+
} // namespace mimium

0 commit comments

Comments
 (0)