-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved subtyping functions to new file
- Loading branch information
tmoux
committed
Jul 3, 2020
1 parent
c8dc37b
commit 7d7a2f3
Showing
4 changed files
with
101 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
module subtyping | ||
|
||
import types | ||
import error | ||
import wyvern.collections.llist | ||
type List = llist.LinkedList | ||
type Binding = types.Binding | ||
type Type = types.Type | ||
type DeclType = types.DeclType | ||
type BaseType = types.BaseType | ||
type ValType = types.ValType | ||
type TypeType = types.TypeType | ||
type DefType = types.DefType | ||
type UnitType = types.UnitType | ||
type NominalType = types.NominalType | ||
|
||
//Check if two lists of arguments line up in the right way | ||
def checkArgList(xs:List[Type],ys:List[Type],pred:Type->Type->Boolean):Boolean | ||
if (xs.size() == ys.size()) | ||
def f(xs:List[Type],ys:List[Type]):Boolean = match xs: | ||
a:llist.Cons => match ys: | ||
b:llist.Cons => | ||
(pred(a.value)(b.value)) && f(a.next,b.next) | ||
default => true | ||
f(xs,ys) | ||
else | ||
false | ||
|
||
//Check if for each y in ys there is an x in xs s.t. (pred x y) is true | ||
def checkDeclList(xs:List[DeclType],ys:List[DeclType],pred:DeclType->DeclType->Boolean):Boolean | ||
def isValid(d:DeclType):Boolean | ||
val search = llist.find[DeclType](xs, (e:DeclType) => pred(e)(d)) | ||
match search: | ||
s:option.Some => true | ||
default => false | ||
ys.foldRight[Boolean]((x:DeclType,y:Boolean) => isValid(x) && y, true) | ||
|
||
def equalDeclType(a:DeclType, b:DeclType, gamma:List[DeclType]):Boolean | ||
match a: | ||
v:ValType => match b: | ||
u:ValType => v.name.name == u.name.name && equalType(v.typ,u.typ,gamma) | ||
default => false | ||
t:TypeType => match b: | ||
u:TypeType => | ||
val tt = types.unfoldType(types.makeNominalType(t.name),gamma) | ||
val uu = types.unfoldType(types.makeNominalType(u.name),gamma) | ||
t.name.name == u.name.name && checkDeclList(tt,uu,x=>y=>equalDeclType(x,y,gamma)) | ||
default => false | ||
d:DefType => match b: | ||
u:DefType => | ||
val pred = x:Type => y:Type => equalType(x,y,gamma) | ||
d.name.name == u.name.name && checkArgList(d.args,u.args,pred) && equalType(d.retTyp,u.retTyp,gamma) | ||
default => false | ||
|
||
def equalBaseType(a:BaseType, b:BaseType):Boolean = match a: | ||
u:UnitType => match b: | ||
v:UnitType => true | ||
default => false | ||
n:NominalType => match b: | ||
v:NominalType => types.equalBinding(n.L,v.L) | ||
default => false | ||
|
||
def equalType(t1:Type, t2:Type, gamma:List[DeclType]):Boolean | ||
val pred = x:DeclType => y:DeclType => equalDeclType(x,y,gamma) | ||
equalBaseType(t1.base,t2.base) && checkDeclList(t1.refines,t2.refines,pred) | ||
|
||
type SubtypePair | ||
val S:Type | ||
val T:Type | ||
def SubtypePair(S:Type, T:Type):SubtypePair = new | ||
val S = S | ||
val T = T | ||
|
||
def isSubtypeDecl(a:DeclType, b:DeclType, gamma:List[DeclType]):Boolean = match a: | ||
v:ValType => match b: | ||
u:ValType => v.name.name == u.name.name && isSubtype(v.typ,u.typ,gamma) | ||
default => false | ||
t:TypeType => match b: | ||
u:TypeType => equalDeclType(a,b,gamma) | ||
default => false | ||
d:DefType => match b: | ||
u:DefType => | ||
val pred = x:Type => y:Type => isSubtype(x,y,gamma) | ||
d.name.name == u.name.name && checkArgList(u.args,d.args,pred) && isSubtype(d.retTyp,u.retTyp,gamma) | ||
default => false | ||
|
||
def isSubtype(t1:Type, t2:Type, gamma:List[DeclType]):Boolean | ||
if (equalType(t2,types.theUnit,gamma)) | ||
true | ||
else | ||
isSubtypeBase(t1,t2.base,gamma) && checkDeclList(t1.refines,t2.refines,x=>y=>isSubtypeDecl(x,y,gamma)) | ||
|
||
def isSubtypeBase(t1:Type, t2:BaseType, gamma:List[DeclType]):Boolean | ||
true //TODO--also need to add "subtype extends" decl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters