Ur型
from linear-base
線形型の文脈内で、非線形の値として扱える
何度でも評価されるかもしれないし、1度も評価されないかもしれない
LinearTypesの論文内でのUnrestricted型
定義 ref
code:hs
-- | @Ur a@ represents unrestricted values of type @a@ in a linear
-- context. The key idea is that because the contructor holds @a@ with a
-- regular arrow, a function that uses @Ur a@ linearly can use @a@
-- however it likes.
--
-- > someLinear :: Ur a %1-> (a,a)
-- > someLinear (Ur a) = (a,a)
data Ur a where
Ur :: a -> Ur a
簡単な例
Urを使わない通常の線形型
code:hs
f' :: a %1 -> (a, a)
f' a = (a, a) -- type error!
aは線形型なのにも関わらず2回使用されているのでerror
Ur型を使う
code:hs
f :: Ur a %1 -> (a, a)
f (Ur a) = (a, a)
Ur a型は線形型だが、aは何度でも使用することができる
関連
Consumable型クラス
消費
https://github.com/tweag/linear-base/blob/bbd1ac76fcf8ba4a19892e35609b0ad1936a5355/src/Data/Unrestricted/Internal/Consumable.hs
code:hs
consume :: Consumable a => a %1-> ()
Dupable型クラス
複製
https://github.com/tweag/linear-base/blob/bbd1ac76fcf8ba4a19892e35609b0ad1936a5355/src/Data/Unrestricted/Internal/Dupable.hs
code:hs
dup2 :: Dupable a => a %1-> (a,a)
Movable型クラス
移動
https://github.com/tweag/linear-base/blob/bbd1ac76fcf8ba4a19892e35609b0ad1936a5355/src/Data/Unrestricted/Internal/Movable.hs
code:hs
move :: Movable a => a %1-> Ur a