Skip to content

Commit 7b4badd

Browse files
committed
better trait bound format
1 parent 8b02749 commit 7b4badd

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Setup .NET Core SDK
1616
uses: actions/setup-dotnet@v4
1717
with:
18-
dotnet-version: '9.x.x'
18+
dotnet-version: "9.x.x"
1919

2020
- name: Install dependencies
2121
run: dotnet restore

README.MD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Test out various compiler technology with a toy compiler for a toy language **in
2525
- HM type inference with trait and operator overloading
2626
- c style function declaration that are hoisted and cannot capture environment
2727
- internal mutable
28-
- impl block and trait, but user need to import them
28+
- impl block and trait, but impl trait has name and user need to import them like in Scala
2929
- string is UTF8 and char is UTF32
3030
3. Pratically functional
3131
- provide immutable std lib and tail call optimization
@@ -60,6 +60,7 @@ Test out various compiler technology with a toy compiler for a toy language **in
6060
- [ ] field selection
6161
- [ ] existential type
6262
- [ ] TRIE based resolution
63+
- [ ] name instance
6364
- [ ] lower
6465
- [ ] pattern match
6566
- [ ] coverage

src/Semantic/Library.fs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -279,26 +279,38 @@ type Scheme =
279279
if this.Pred.Length = 0 then
280280
res
281281
else
282-
let print { Type = ty; Trait = tr } =
283-
let bound = ty[0].Print() + ": " + tr.Name.Sym
284-
285-
if ty.Length = 1 then
286-
bound
287-
else
288-
let print (idx, ty: Type) =
289-
let ty = ty.Print()
290-
let idx = idx + 1
291-
292-
if idx >= tr.FreeVarLength then
293-
let name = tr.DepName[idx - tr.FreeVarLength].Sym
294-
$"{name} = {ty}"
295-
else
296-
ty
297-
298-
let gen = ty[1..] |> Array.indexed |> Array.map print |> String.concat ", "
299-
$"{bound}<{gen}>"
300-
301-
let pred = this.Pred |> Array.map print |> String.concat ", "
282+
let print (ty: Type, pred: Pred[]) =
283+
let head = ty.Print() + ": "
284+
285+
let print (tr: Trait) (idx: int, ty: Type) =
286+
let idx = idx + 1
287+
288+
if idx >= tr.FreeVarLength then
289+
let name = tr.DepName[idx - tr.FreeVarLength].Sym
290+
$"{name} = {ty.Print()}"
291+
else
292+
ty.Print()
293+
294+
let print (pred: Pred) =
295+
let tr = pred.Trait.Name.Sym
296+
297+
if pred.Type.Length > 1 then
298+
let gen =
299+
pred.Type[1..]
300+
|> Array.indexed
301+
|> Array.map (print pred.Trait)
302+
|> String.concat ", "
303+
304+
$"{tr}<{gen}>"
305+
else
306+
tr
307+
308+
let bound = pred |> Array.map print |> String.concat " + "
309+
310+
head + bound
311+
312+
let pred =
313+
this.Pred |> Array.groupBy _.Type[0] |> Array.map print |> String.concat ", "
302314

303315
$"{res} where {pred}"
304316

test/Semantic/Type/Trait.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn twice(c, a, b) {
186186
c.insert(a)
187187
c.insert(b)
188188
}"
189-
|> toBe (Map [| "twice", "<T0, T1, T2>|T0, T1, T2| -> () where T0: Collect<T1>, T0: Collect<T2>" |])
189+
|> toBe (Map [| "twice", "<T0, T1, T2>|T0, T1, T2| -> () where T0: Collect<T1> + Collect<T2>" |])
190190

191191
[<Fact>]
192192
let GoodCollect () =

0 commit comments

Comments
 (0)