Skip to content

Commit 64e8568

Browse files
committed
improving structure of typed tree
1 parent ce31761 commit 64e8568

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

Prover/LanguageServices/Wybe/Semantics.fs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ open AST
44

55
type Expected =
66
{ expected: Type
7-
got: SemanticResult
7+
got: TypingResult
88
atChild: int }
99

10-
and SemanticResult =
10+
and TypingResult =
1111
| Typed of Type
12-
| TypedDomain of Type * domain: SemanticTree list
12+
| TypedDomain of Type * domain: TypedTree list
1313
| Expecting of Expected list
1414
| ExpectingSameType of got: Type list
1515
| NotRecognizedOperator of Op
1616
| NotFoundVar of string
1717
| Untyped
1818
| MalformedAssignment
19-
| MultipleResults of SemanticResult list
19+
| MultipleResults of TypingResult list
2020
| UndeclaredVariable of string
2121

2222
member this.Type =
@@ -25,7 +25,7 @@ and SemanticResult =
2525
| TypedDomain(r, _) -> Some r
2626
| _ -> None
2727

28-
member this.AddDomain(expr: SemanticTree) =
28+
member this.AddDomain(expr: TypedTree) =
2929
match this with
3030
| Typed t -> TypedDomain(t, [ expr ])
3131
| TypedDomain(t, xs) -> TypedDomain(t, expr :: xs)
@@ -38,7 +38,7 @@ and SemanticResult =
3838
| [] -> None
3939
| y :: ys ->
4040
ys
41-
|> List.fold (fun acc x -> ST((Typed Type.Boolean, Binary(x.Expr, Op.And, acc.Expr)), [ acc; x ])) y
41+
|> List.fold (fun acc x -> TypedTree(Typed Type.Boolean, Binary(x.Expr, Op.And, acc.Expr), [ acc; x ])) y
4242
|> Some
4343
| _ -> None
4444

@@ -47,23 +47,23 @@ and SemanticResult =
4747
| Expecting xs -> xs
4848
| _ -> []
4949

50-
and SemanticTree =
51-
| ST of value: (SemanticResult * Expr) * children: SemanticTree list
50+
and TypedTree =
51+
| TypedTree of result: TypingResult * expr: Expr * children: TypedTree list
5252

53-
member this.AddDomain(expr: SemanticTree) =
54-
let (ST((r, e), xs)) = this
55-
ST((r.AddDomain expr, e), xs)
53+
member this.AddDomain(expr: TypedTree) =
54+
let (TypedTree(r, e, xs)) = this
55+
TypedTree(r.AddDomain expr, e, xs)
5656

5757
member this.Expr =
58-
let (ST((_, expr), _)) = this
58+
let (TypedTree(_, expr, _)) = this
5959
expr
6060

6161
member this.SemanticResult =
62-
let (ST((r, _), _)) = this
62+
let (TypedTree(r, _, _)) = this
6363
r
6464

6565
member this.Children =
66-
let (ST(_, children)) = this
66+
let (TypedTree(_, _, children)) = this
6767
children
6868

6969
let rec checkChildrenFixedType
@@ -89,10 +89,10 @@ let rec checkChildrenFixedType
8989
let childrenDomains = xs |> List.choose (fun x -> x.SemanticResult.Domain)
9090

9191
match childrenDomains with
92-
| [] -> ST((Typed resultType, e), xs)
93-
| _ -> ST((TypedDomain(resultType, childrenDomains), e), xs)
92+
| [] -> TypedTree(Typed resultType, e, xs)
93+
| _ -> TypedTree(TypedDomain(resultType, childrenDomains), e, xs)
9494

95-
| rs -> ST((Expecting rs, e), xs)
95+
| rs -> TypedTree(Expecting rs, e, xs)
9696

9797
and checkChildrenEqualType (vars: Map<string, Type>) (e: Expr, resultType: Type) (children: Expr list) =
9898
let xs = children |> List.map (extractTypeAndDomain vars)
@@ -102,12 +102,12 @@ and checkChildrenEqualType (vars: Map<string, Type>) (e: Expr, resultType: Type)
102102
let childrenDomains = xs |> List.choose (fun x -> x.SemanticResult.Domain)
103103

104104
match childrenDomains with
105-
| [] -> ST((Typed resultType, e), xs)
106-
| _ -> ST((TypedDomain(resultType, childrenDomains), e), xs)
105+
| [] -> TypedTree(Typed resultType, e, xs)
106+
| _ -> TypedTree(TypedDomain(resultType, childrenDomains), e, xs)
107107
else
108-
ST((ExpectingSameType(Set.toList types), e), xs)
108+
TypedTree(ExpectingSameType(Set.toList types), e, xs)
109109

110-
and extractTypeAndDomain (vars: Map<string, Type>) (e: Expr) : SemanticTree =
110+
and extractTypeAndDomain (vars: Map<string, Type>) (e: Expr) : TypedTree =
111111
match e with
112112
| Binary(left, op, right) ->
113113

@@ -121,9 +121,9 @@ and extractTypeAndDomain (vars: Map<string, Type>) (e: Expr) : SemanticTree =
121121

122122
let zero = Lit(Int 0)
123123
let diffZero = Binary(right, Op.Differs, zero)
124-
let typedZero = ST((Typed Type.Integer, zero), [])
124+
let typedZero = TypedTree(Typed Type.Integer, zero, [])
125125

126-
let domain = ST((Typed Type.Boolean, diffZero), [ r.Children[1]; typedZero ])
126+
let domain = TypedTree(Typed Type.Boolean, diffZero, [ r.Children[1]; typedZero ])
127127
r.AddDomain domain
128128
| Op.Equals
129129
| Op.Differs -> checkChildrenEqualType vars (e, Type.Boolean) [ left; right ]
@@ -141,19 +141,19 @@ and extractTypeAndDomain (vars: Map<string, Type>) (e: Expr) : SemanticTree =
141141
let l, r = extractTypeAndDomain vars left, extractTypeAndDomain vars right
142142

143143
match l.SemanticResult.Type, r.SemanticResult.Type with
144-
| Some t, Some(Type.Array u) when u = t -> ST((Typed(Type.Array t), e), [ l; r ])
145-
| _ -> ST((NotRecognizedOperator op, e), [ l; r ])
144+
| Some t, Some(Type.Array u) when u = t -> TypedTree(Typed(Type.Array t), e, [ l; r ])
145+
| _ -> TypedTree(NotRecognizedOperator op, e, [ l; r ])
146146
| Op.Concat ->
147147
let l, r = extractTypeAndDomain vars left, extractTypeAndDomain vars right
148148

149149
match l.SemanticResult.Type, r.SemanticResult.Type with
150-
| Some(Type.Array t), Some(Type.Array u) when u = t -> ST((Typed(Type.Array t), e), [ l; r ])
151-
| _ -> ST((NotRecognizedOperator op, e), [ l; r ])
150+
| Some(Type.Array t), Some(Type.Array u) when u = t -> TypedTree(Typed(Type.Array t), e, [ l; r ])
151+
| _ -> TypedTree(NotRecognizedOperator op, e, [ l; r ])
152152
| Op.IsPrefix
153153
| Op.IsSuffix -> checkChildrenFixedType vars (e, Type.Boolean) (Type.Array(Type.VarType "a"), [ left; right ])
154154
| _ ->
155155
let l, r = extractTypeAndDomain vars left, extractTypeAndDomain vars right
156-
ST((NotRecognizedOperator op, e), [ l; r ])
156+
TypedTree(NotRecognizedOperator op, e, [ l; r ])
157157
| Unary(op, right) ->
158158
match op with
159159
| Op.Not -> checkChildrenFixedType vars (e, Type.Boolean) (Type.Boolean, [ right ])
@@ -163,35 +163,35 @@ and extractTypeAndDomain (vars: Map<string, Type>) (e: Expr) : SemanticTree =
163163
let r = extractTypeAndDomain vars right
164164

165165
match r.SemanticResult.Type with
166-
| Some(Type.Array t) -> ST((Typed t, e), [ r ])
167-
| _ -> ST((NotRecognizedOperator op, e), [ r ])
166+
| Some(Type.Array t) -> TypedTree(Typed t, e, [ r ])
167+
| _ -> TypedTree(NotRecognizedOperator op, e, [ r ])
168168
| Op.Tail ->
169169
let r = extractTypeAndDomain vars right
170170

171171
match r.SemanticResult.Type with
172-
| Some(Type.Array t) -> ST((Typed(Type.Array t), e), [ r ])
173-
| _ -> ST((NotRecognizedOperator op, e), [ r ])
172+
| Some(Type.Array t) -> TypedTree(Typed(Type.Array t), e, [ r ])
173+
| _ -> TypedTree(NotRecognizedOperator op, e, [ r ])
174174
| _ ->
175175
let r = extractTypeAndDomain vars right
176-
ST((NotRecognizedOperator op, e), [ r ])
176+
TypedTree(NotRecognizedOperator op, e, [ r ])
177177
| Var name ->
178178
match Map.tryFind name vars with
179-
| Some v -> ST((Typed v, e), [])
180-
| None -> ST((NotFoundVar name, e), [])
179+
| Some v -> TypedTree(Typed v, e, [])
180+
| None -> TypedTree(NotFoundVar name, e, [])
181181
| Lit v ->
182182
match v with
183-
| Int _ -> ST((Typed Type.Integer, e), [])
184-
| Bool _ -> ST((Typed Type.Boolean, e), [])
185-
| Str _ -> ST((Typed Type.String, e), [])
183+
| Int _ -> TypedTree(Typed Type.Integer, e, [])
184+
| Bool _ -> TypedTree(Typed Type.Boolean, e, [])
185+
| Str _ -> TypedTree(Typed Type.String, e, [])
186186
| Array xs ->
187187
match xs with
188-
| [] -> ST((Untyped, e), [])
188+
| [] -> TypedTree(Untyped, e, [])
189189
| y :: ys ->
190190
let r = extractTypeAndDomain vars y
191191
let rs = ys |> List.map (extractTypeAndDomain vars)
192192

193193
match r with
194-
| ST((Typed t, _), _) ->
194+
| TypedTree(Typed t, _, _) ->
195195
// this branch reports which array elements do not have the
196196
// same type as the first element
197197
// in case the list of different types is empty, then the array
@@ -200,45 +200,45 @@ and extractTypeAndDomain (vars: Map<string, Type>) (e: Expr) : SemanticTree =
200200
rs
201201
|> List.mapi (fun i ->
202202
function
203-
| ST((Typed u, _), _) when t = u -> None
204-
| ST((u, _), _) ->
203+
| TypedTree(Typed u, _, _) when t = u -> None
204+
| TypedTree(u, _, _) ->
205205
Some
206206
{ expected = t
207207
got = u
208208
atChild = i + 1 })
209209
|> List.choose id
210210

211211
match diffElemTypes with
212-
| [] -> ST((Typed(Type.Array t), e), r :: rs)
213-
| _ -> ST((Expecting diffElemTypes, e), r :: rs)
214-
| ST((v, _), _) -> ST((v, e), r :: rs)
212+
| [] -> TypedTree(Typed(Type.Array t), e, r :: rs)
213+
| _ -> TypedTree(Expecting diffElemTypes, e, r :: rs)
214+
| TypedTree(v, _, _) -> TypedTree(v, e, r :: rs)
215215
| ArrayElem(name, index) ->
216216
match Map.tryFind name vars with
217217
| Some(Type.Array t) ->
218218
let indexResult = extractTypeAndDomain vars index
219219

220220
match indexResult with
221-
| ST((TypedDomain(Type.Integer, _), _), _)
222-
| ST((Typed Type.Integer, _), _) ->
221+
| TypedTree(TypedDomain(Type.Integer, _), _, _)
222+
| TypedTree(Typed Type.Integer, _, _) ->
223223
let domain = indexResult.SemanticResult.Domain |> Option.toList
224-
let r = ST((TypedDomain(t, domain), e), [ indexResult ])
224+
let r = TypedTree(TypedDomain(t, domain), e, [ indexResult ])
225225

226226
let arrayDomain =
227227
Binary(Binary(Lit(Int 0), Op.AtMost, index), Op.And, Binary(index, Op.LessThan, Unary(Op.Length, Var name)))
228228
|> extractTypeAndDomain vars
229229

230230
r.AddDomain arrayDomain
231-
| _ -> ST((Typed t, e), [ indexResult ])
231+
| _ -> TypedTree(Typed t, e, [ indexResult ])
232232
| Some t ->
233-
ST(
234-
(Expecting
233+
TypedTree(
234+
Expecting
235235
[ { expected = Type.Array(Type.VarType "a")
236236
got = Typed t
237237
atChild = 0 } ],
238-
e),
238+
e,
239239
[]
240240
)
241-
| None -> ST((Untyped, e), [])
241+
| None -> TypedTree(Untyped, e, [])
242242

243243
open Core
244244

@@ -312,8 +312,8 @@ let rec exprToTree: Expr -> SymbolTree =
312312
SymbolTree.Node(Symbol.Atom name, [ SymbolTree.Node(Symbol.Indexed, [ exprToTree index ]) ])
313313

314314

315-
let collectSemanticTreeInfo (e: SemanticTree) =
316-
let errorInfo (e: SemanticTree) =
315+
let collectSemanticTreeInfo (e: TypedTree) =
316+
let errorInfo (e: TypedTree) =
317317
match e.SemanticResult with
318318
| Expecting xs -> xs |> List.map (fun x -> $"expecting {x.expected}, got {x.got}")
319319
| ExpectingSameType got -> [ $"expecting same type, got: {got |> List.map string}" ]
@@ -323,7 +323,7 @@ let collectSemanticTreeInfo (e: SemanticTree) =
323323
| _ -> []
324324
|> List.map (fun r -> $"{exprToTree e.Expr}: {r}")
325325

326-
let rec innerInfo (e: SemanticTree) =
326+
let rec innerInfo (e: TypedTree) =
327327
let info = errorInfo e
328328
info @ (e.Children |> List.collect innerInfo)
329329

@@ -350,8 +350,8 @@ type DomainWExpr =
350350
let (DomainWExpr(domain, _)) = this
351351
domain
352352

353-
let semanticExprToWExpr (e: SemanticTree) : DomainWExpr =
354-
let rec typedToWExpr (e: SemanticTree) : WExpr =
353+
let semanticExprToWExpr (e: TypedTree) : DomainWExpr =
354+
let rec typedToWExpr (e: TypedTree) : WExpr =
355355
match e.SemanticResult.Type with
356356
| Some Type.Boolean ->
357357
match e.Expr, e.Children with
@@ -496,7 +496,7 @@ and wpStatement (s: Statement) (postcondition: Proposition) =
496496
type AstSemanticResult =
497497
| NewVars of Map<string, Type>
498498
| NewStatement of Statement
499-
| FailedSemantic of SemanticResult
499+
| FailedSemantic of TypingResult
500500

501501
let rec astStatementToSemantic (vars: Map<string, Type>) (s: AST.Statement) : AstSemanticResult =
502502
let splitResult (rs: Result<'a, 'b> list) =

0 commit comments

Comments
 (0)