type modifiers -> type contracts

This commit is contained in:
Umur Ozkul 2022-07-18 18:31:09 +02:00
parent c1429e0907
commit 15d63a6a96
5 changed files with 19 additions and 19 deletions

View File

@ -20,7 +20,7 @@ describe("Peggy parse type", () => {
"{(::$_typeOf_$ :f (::$_typeFunction_$ (::$_constructArray_$ (#number #number #number))))}", "{(::$_typeOf_$ :f (::$_typeFunction_$ (::$_constructArray_$ (#number #number #number))))}",
) )
}) })
describe("high priority modifier", () => { describe("high priority contract", () => {
testParse( testParse(
"answer: number<-min<-max(100)|string", "answer: number<-min<-max(100)|string",
"{(::$_typeOf_$ :answer (::$_typeOr_$ (::$_constructArray_$ ((::$_typeModifier_max_$ (::$_typeModifier_min_$ #number) 100) #string))))}", "{(::$_typeOf_$ :answer (::$_typeOr_$ (::$_constructArray_$ ((::$_typeModifier_max_$ (::$_typeModifier_min_$ #number) 100) #string))))}",
@ -30,7 +30,7 @@ describe("Peggy parse type", () => {
"{(::$_typeOf_$ :answer (::$_typeModifier_memberOf_$ #number (::$_constructArray_$ (1 3 5))))}", "{(::$_typeOf_$ :answer (::$_typeModifier_memberOf_$ #number (::$_constructArray_$ (1 3 5))))}",
) )
}) })
describe("low priority modifier", () => { describe("low priority contract", () => {
testParse( testParse(
"answer: number | string $ opaque", "answer: number | string $ opaque",
"{(::$_typeOf_$ :answer (::$_typeModifier_opaque_$ (::$_typeOr_$ (::$_constructArray_$ (#number #string)))))}", "{(::$_typeOf_$ :answer (::$_typeModifier_opaque_$ (::$_typeOr_$ (::$_constructArray_$ (#number #string)))))}",
@ -70,7 +70,7 @@ describe("Peggy parse type", () => {
"{(::$_typeOf_$ :answer (::$_typeModifier_opaque_$ (::$_typeOr_$ (::$_constructArray_$ (#number #string)))))}", "{(::$_typeOf_$ :answer (::$_typeModifier_opaque_$ (::$_typeOr_$ (::$_constructArray_$ (#number #string)))))}",
) )
}) })
describe("squiggle expressions in type modifiers", () => { describe("squiggle expressions in type contracts", () => {
testParse( testParse(
"odds1 = [1,3,5]; odds2 = [7, 9]; type odds = number<-memberOf(concat(odds1, odds2))", "odds1 = [1,3,5]; odds2 = [7, 9]; type odds = number<-memberOf(concat(odds1, odds2))",
"{:odds1 = {(::$_constructArray_$ (1 3 5))}; :odds2 = {(::$_constructArray_$ (7 9))}; (::$_typeAlias_$ #odds (::$_typeModifier_memberOf_$ #number (::concat :odds1 :odds2)))}", "{:odds1 = {(::$_constructArray_$ (1 3 5))}; :odds2 = {(::$_constructArray_$ (7 9))}; (::$_typeAlias_$ #odds (::$_typeModifier_memberOf_$ #number (::concat :odds1 :odds2)))}",

View File

@ -40,7 +40,7 @@ describe("Peggy Types to Expression", () => {
(), (),
) )
}) })
describe("high priority modifier", () => { describe("high priority contract", () => {
testToExpression( testToExpression(
"answer: number<-min(1)<-max(100)|string", "answer: number<-min(1)<-max(100)|string",
"{(:$_typeOf_$ :answer (:$_typeOr_$ (:$_constructArray_$ ((:$_typeModifier_max_$ (:$_typeModifier_min_$ #number 1) 100) #string))))}", "{(:$_typeOf_$ :answer (:$_typeOr_$ (:$_constructArray_$ ((:$_typeModifier_max_$ (:$_typeModifier_min_$ #number 1) 100) #string))))}",
@ -78,7 +78,7 @@ describe("Peggy Types to Expression", () => {
(), (),
) )
}) })
describe("low priority modifier", () => { describe("low priority contract", () => {
testToExpression( testToExpression(
"answer: number | string $ opaque", "answer: number | string $ opaque",
"{(:$_typeOf_$ :answer (:$_typeModifier_opaque_$ (:$_typeOr_$ (:$_constructArray_$ (#number #string)))))}", "{(:$_typeOf_$ :answer (:$_typeModifier_opaque_$ (:$_typeOr_$ (:$_constructArray_$ (#number #string)))))}",
@ -86,7 +86,7 @@ describe("Peggy Types to Expression", () => {
(), (),
) )
}) })
describe("squiggle expressions in type modifiers", () => { describe("squiggle expressions in type contracts", () => {
testToExpression( testToExpression(
"odds1 = [1,3,5]; odds2 = [7, 9]; type odds = number<-memberOf(concat(odds1, odds2))", "odds1 = [1,3,5]; odds2 = [7, 9]; type odds = number<-memberOf(concat(odds1, odds2))",
"{(:$_let_$ :odds1 {(:$_constructArray_$ (1 3 5))}); (:$_let_$ :odds2 {(:$_constructArray_$ (7 9))}); (:$_typeAlias_$ #odds (:$_typeModifier_memberOf_$ #number (:concat :odds1 :odds2)))}", "{(:$_let_$ :odds1 {(:$_constructArray_$ (1 3 5))}); (:$_let_$ :odds2 {(:$_constructArray_$ (7 9))}); (:$_typeAlias_$ #odds (:$_typeModifier_memberOf_$ #number (:concat :odds1 :odds2)))}",

View File

@ -41,10 +41,10 @@ let checkModifier = (
} }
let checkModifiers = ( let checkModifiers = (
modifiers: Belt.Map.String.t<InternalExpressionValue.t>, contracts: Belt.Map.String.t<InternalExpressionValue.t>,
aValue: InternalExpressionValue.t, aValue: InternalExpressionValue.t,
): bool => { ): bool => {
modifiers->Belt.Map.String.reduce(true, (acc, key, modifierArg) => contracts->Belt.Map.String.reduce(true, (acc, key, modifierArg) =>
switch acc { switch acc {
| true => checkModifier(key, modifierArg, aValue) | true => checkModifier(key, modifierArg, aValue)
| _ => acc | _ => acc

View File

@ -3,7 +3,7 @@ open InternalExpressionValue
type rec iType = type rec iType =
| ItTypeIdentifier(string) | ItTypeIdentifier(string)
| ItModifiedType({modifiedType: iType, modifiers: Belt.Map.String.t<InternalExpressionValue.t>}) | ItModifiedType({modifiedType: iType, contracts: Belt.Map.String.t<InternalExpressionValue.t>})
| ItTypeOr({typeOr: array<iType>}) | ItTypeOr({typeOr: array<iType>})
| ItTypeFunction({inputs: array<iType>, output: iType}) | ItTypeFunction({inputs: array<iType>, output: iType})
| ItTypeArray({element: iType}) | ItTypeArray({element: iType})
@ -16,8 +16,8 @@ type typeErrorValue = TypeMismatch(t, InternalExpressionValue.t)
let rec toString = (t: t): string => { let rec toString = (t: t): string => {
switch t { switch t {
| ItTypeIdentifier(s) => s | ItTypeIdentifier(s) => s
| ItModifiedType({modifiedType, modifiers}) => | ItModifiedType({modifiedType, contracts}) =>
`${toString(modifiedType)}${modifiers->Belt.Map.String.reduce("", (acc, k, v) => `${toString(modifiedType)}${contracts->Belt.Map.String.reduce("", (acc, k, v) =>
Js.String2.concatMany(acc, ["<-", k, "(", InternalExpressionValue.toString(v), ")"]) Js.String2.concatMany(acc, ["<-", k, "(", InternalExpressionValue.toString(v), ")"])
)}` )}`
| ItTypeOr({typeOr}) => `(${Js.Array2.map(typeOr, toString)->Js.Array2.joinWith(" | ")})` | ItTypeOr({typeOr}) => `(${Js.Array2.map(typeOr, toString)->Js.Array2.joinWith(" | ")})`
@ -82,7 +82,7 @@ let rec fromTypeMap = typeMap => {
default, default,
) )
let modifiers = let contracts =
typeMap->Belt.Map.String.keep((k, _v) => ["min", "max", "memberOf"]->Js.Array2.includes(k)) typeMap->Belt.Map.String.keep((k, _v) => ["min", "max", "memberOf"]->Js.Array2.includes(k))
let makeIt = switch evTypeTag { let makeIt = switch evTypeTag {
@ -96,9 +96,9 @@ let rec fromTypeMap = typeMap => {
| _ => raise(Reducer_Exception.ImpossibleException("Reducer_Type_T-evTypeTag")) | _ => raise(Reducer_Exception.ImpossibleException("Reducer_Type_T-evTypeTag"))
} }
Belt.Map.String.isEmpty(modifiers) Belt.Map.String.isEmpty(contracts)
? makeIt ? makeIt
: ItModifiedType({modifiedType: makeIt, modifiers: modifiers}) : ItModifiedType({modifiedType: makeIt, contracts: contracts})
} }
and fromIEvValue = (ievValue: InternalExpressionValue.t): iType => and fromIEvValue = (ievValue: InternalExpressionValue.t): iType =>

View File

@ -1,7 +1,7 @@
module ExpressionT = Reducer_Expression_T module ExpressionT = Reducer_Expression_T
module InternalExpressionValue = ReducerInterface_InternalExpressionValue module InternalExpressionValue = ReducerInterface_InternalExpressionValue
module T = Reducer_Type_T module T = Reducer_Type_T
module TypeModifiers = Reducer_Type_Modifiers module TypeContracts = Reducer_Type_Contracts
open InternalExpressionValue open InternalExpressionValue
let rec isITypeOf = (anIType: T.iType, aValue): result<bool, T.typeErrorValue> => { let rec isITypeOf = (anIType: T.iType, aValue): result<bool, T.typeErrorValue> => {
@ -92,11 +92,11 @@ let rec isITypeOf = (anIType: T.iType, aValue): result<bool, T.typeErrorValue> =
let caseModifiedType = ( let caseModifiedType = (
anIType: T.iType, anIType: T.iType,
modifiedType: T.iType, modifiedType: T.iType,
modifiers: Belt.Map.String.t<InternalExpressionValue.t>, contracts: Belt.Map.String.t<InternalExpressionValue.t>,
aValue: InternalExpressionValue.t, aValue: InternalExpressionValue.t,
) => { ) => {
isITypeOf(modifiedType, aValue)->Belt.Result.flatMap(_result => { isITypeOf(modifiedType, aValue)->Belt.Result.flatMap(_result => {
if TypeModifiers.checkModifiers(modifiers, aValue) { if TypeContracts.checkModifiers(contracts, aValue) {
Ok(true) Ok(true)
} else { } else {
T.TypeMismatch(anIType, aValue)->Error T.TypeMismatch(anIType, aValue)->Error
@ -106,8 +106,8 @@ let rec isITypeOf = (anIType: T.iType, aValue): result<bool, T.typeErrorValue> =
switch anIType { switch anIType {
| ItTypeIdentifier(name) => caseTypeIdentifier(name, aValue) | ItTypeIdentifier(name) => caseTypeIdentifier(name, aValue)
| ItModifiedType({modifiedType, modifiers}) => | ItModifiedType({modifiedType, contracts}) =>
caseModifiedType(anIType, modifiedType, modifiers, aValue) //{modifiedType: iType, modifiers: Belt.Map.String.t<InternalExpressionValue.t>} caseModifiedType(anIType, modifiedType, contracts, aValue) //{modifiedType: iType, contracts: Belt.Map.String.t<InternalExpressionValue.t>}
| ItTypeOr({typeOr}) => caseOr(anIType, typeOr, aValue) | ItTypeOr({typeOr}) => caseOr(anIType, typeOr, aValue)
| ItTypeFunction(_) => | ItTypeFunction(_) =>
raise( raise(