refinement-type

Confusion about type refinement syntax

試著忘記壹切 提交于 2020-11-29 10:24:27
问题 On Type Level, i stumble upon the following: sealed abstract class StSource[A] { type S def init: S // create the initial state def emit(s: S): (A, S) // emit a value, and update state } object StSource { type Aux[A, S0] = StSource[A] {type S = S0} def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] = new StSource[A] { type S = S0 def init = i def emit(s: S0) = f(s) } } The line that intrigued me is type Aux[A, S0] = StSource[A] {type S = S0} In paerticular {type S = S0} in StSource[A]

Confusion about type refinement syntax

大憨熊 提交于 2020-11-29 10:24:00
问题 On Type Level, i stumble upon the following: sealed abstract class StSource[A] { type S def init: S // create the initial state def emit(s: S): (A, S) // emit a value, and update state } object StSource { type Aux[A, S0] = StSource[A] {type S = S0} def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] = new StSource[A] { type S = S0 def init = i def emit(s: S0) = f(s) } } The line that intrigued me is type Aux[A, S0] = StSource[A] {type S = S0} In paerticular {type S = S0} in StSource[A]

Confusion about type refinement syntax

本小妞迷上赌 提交于 2020-11-29 10:23:13
问题 On Type Level, i stumble upon the following: sealed abstract class StSource[A] { type S def init: S // create the initial state def emit(s: S): (A, S) // emit a value, and update state } object StSource { type Aux[A, S0] = StSource[A] {type S = S0} def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] = new StSource[A] { type S = S0 def init = i def emit(s: S0) = f(s) } } The line that intrigued me is type Aux[A, S0] = StSource[A] {type S = S0} In paerticular {type S = S0} in StSource[A]

Using a monad to implicitly check refinement type well-formedness

拜拜、爱过 提交于 2019-12-24 06:48:20
问题 While implementing a refinement type system, I need to put in checks to make sure the types are well-formed. For example, a type like Num[100,0] shouldn't happen, where Num[lb,ub] is the type of numbers larger than lb and smaller than ub . I then wrote: -- FORMATION RULES class RefTy t where tyOK :: t -> Bool instance RefTy Ty where tyOK (NumTy (n1, n2)) = n1 <= n2 tyOK (CatTy cs) = isSet cs {- data WellFormed t = Valid t | Invalid instance Monad WellFormed where (>>=) :: RefTy a =>

Idioms/Practices for Implementing Constrained Numeric Types in F#?

守給你的承諾、 提交于 2019-12-23 07:29:12
问题 Suppose one needs a numeric data type whose allowed values fall within a specified range. More concretely, suppose one wants to define an integral type whose min value is 0 and maximum value is 5000. This type of scenario arises in many situations, such as when modeling a database data type, an XSD data type and so on. What is the best way to model such a type in F#? In C#, one way to do this would be to define a struct that implemented the range checking overloaded operators, formatting and

Can I define parametric data type where parameters are not equals between in Haskell?

雨燕双飞 提交于 2019-12-10 22:59:38
问题 Problem: Let's imagine we have a Passenger with start and end points represented by: data Passenger a = Passenger { start :: a , end :: a } Question: How can I apply a class constraints to Passenger, where the start point shouldn't be equal to the end point? P.S.: I have asked a similar question in the Scala community, but I didn't receive any answer. Considering that refined library for scala is inspired by refined for Haskell, also hearing about liquid-Haskell, I wonder how can resolve it

Is it possible to enforce that a Record respects some invariants?

巧了我就是萌 提交于 2019-11-29 09:07:06
Suppose I wanted to create a Record type that represents acceptable min/max bounds: type Bounds = { Min: float; Max: float } Is there a way to enforce that Min < Max? It is easy to write a validateBounds function, I was just wondering if there was a better way to do this. Edit: I realized that for this specific example I could probably get away with exposing two properties and re-order the arguments, so let's say we were trying to do type Person = { Name: string } and Name needs to have at least one character. Here's another solution based on protection levels: module MyModule = type Bounds =

Is it possible to enforce that a Record respects some invariants?

不问归期 提交于 2019-11-28 02:27:32
问题 Suppose I wanted to create a Record type that represents acceptable min/max bounds: type Bounds = { Min: float; Max: float } Is there a way to enforce that Min < Max? It is easy to write a validateBounds function, I was just wondering if there was a better way to do this. Edit: I realized that for this specific example I could probably get away with exposing two properties and re-order the arguments, so let's say we were trying to do type Person = { Name: string } and Name needs to have at