นี่คือตามต้อง ของฉันก่อนหน้านี้คำถาม. ฉัน recieved ดีบางอย่างคำตอบนั้นแต่เพราะฉันประยุกต์@item text character set ที่แท้จริงของผมปัญหาคือฉันคิดว่าฉันเข้าใจผิดที่ answerers และฉันหวังว่าจะแก้ไขเรื่องนั้นอยู่ที่นี่
TL;ด็อกเตอร์ ฉันมี typeclass Category
จาก constrained-categories
ซึ่ง constrains งโดเมน/codomain ของมัน Category
s ใช้ TypeFamily("constraint ครอบครัว")เรียกว่า Object
. ฉันต้องการจะทำให้เป็นอิสระ Category
แต่ฉันต้องดิ้นรนเพื่อหาลักฐานนั่น/n พอใจคน Object
constraint.
พิจารณาของฉัน Free
ข้อมูลประเภทและของมัน constrained-categories.Category
ตัวอย่าง:
data Free p a b where
Id :: Object p a => Free p a a
Comp :: Object p b => Free p b c -> Free p a b -> Free p a c
instance Category (Free p) where
type Object (Free p) a = Object p a -- passes through the constraints from instantiated `p`
id = Id
(.) = Comp
จะอธิบายเรื่องอีกหน่อยได้มัยกลับมามายังถือว่าเป็นคนที่ไม่ใช่อิสระ Category
ฉันอาจชอบ eval
ไปยัง:
newtype MyArr a b = MyArr (a -> b)
instance Category MyArr where
type Object MyArr a = () -- trivial, no constraints needed
id = MyArr P.id
MyArr g . MyArr f = MyArr (g . f)
ฉันต้องการจะเขียนการแสดงเหมือนนี้(ซึ่งมันเห็นได้ชัดว่าไกลที่ง่ายกว่า/n ฉันจะเขียนอยู่ซ้อม):
-- error: Could not deduce: Object p Int arising from a use of ‘Id’
e0 :: Free p Int Int
e0 = Id
ฉันสามารถแก้ไขเรื่องนี้ที่เห็นได้ชัดแต่มันจะกลายเป็น verbose สำหรับที่ยิ่งใหญ่แสดง. นึกถึงการเขียนจดหมายฟังก์ชันของ tuples และต้องใช้ไปในข้อบังคับให้เป็น Object p _
ตัวอย่างสำหรับทุกชั่วคราวอยู่ในขั้นตองค์ประกอบ:
e1 :: Object p Int => Free p Int Int
e1 = Id
ฉันจะเลือกที่จะ ไม่ใช่ คนแบบนามปธรรม Category p
และนั่นทำงาน:
e2 :: Free MyArr Int Int
e2 = Id
แต่ฉันอยากจะจับมัน ฉันควรจะคิดว่าการเพิ่มเป็น constraint Category p
ควรจะทำงาน,และนำไปสู่ขอบเขตของมัน constraint นั้น type Object (Free p) a = Object p a
และให้ฉันมี Object p _
ตัวอย่างผมต้องการแต่อนิจจามันไม่ทำงาน
-- error: Could not deduce: Object p Int arising from a use of ‘Id’
e3 :: Category p => Free p Int Int
e3 = Id
มันดูเหมือนว่า QuantifiedConstraints
อาจจะช่วย,eg forall a. Object (Free p) a => Object p a
แต่คุณไม่สามารถมี TypeFamily
ในหัวของ predicate.
ฉันยังถูกพิจารณาโดยใช้ type Object p :: * -> Constraint
เหมือนอยู่ใน Conal เอลเลียตเป็น concat องสมุด,แต่นั่นมันต้องการ\n ที่จะแตกต่างห้องสมุดคนละ Category
ชั้นเรียนและครั้งสุดท้ายที่ฉันเคยใช้หมวดหมู่ constrained ทางนั้นมันดูเหมืออีกหน่อยได้มัยกลับมา cumbersome และฉันไม่ค่อยแน่ใจว่า นั่น จะแก้ปัญหาของฉัน boilerplate เหมือนไม่มีปัญหาอยู่เลยสินะ เป็น ความคิดเห็น อยู่ในห้องสมุดบ่งบอกคนของ usefulness QuantifiedConstraints
แต่ฉันค่อนข้างบิดเบือนตอนนี้และไม่รู้จักซึ่งในทางที่จะ trudge ไปข้างหน้า
Runnable
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
module ConstrainedCategoryFreeArrow3 where
import Prelude hiding (id, (.))
import qualified Prelude as P
import Control.Category.Constrained
-- * A Free 'Category'
data Free p a b where
Id :: Object p a => Free p a a
Comp :: Object p b => Free p b c -> Free p a b -> Free p a c
instance Category (Free p) where
type Object (Free p) a = Object p a -- passes through the constraints from instantiated `p`
id = Id
(.) = Comp
eval :: (Category p, Object p a, Object p b) => Free p a b -> p a b
eval Id = id
eval (Comp g f) = eval g . eval f
-- * A specific (trivial) 'Category'
newtype MyArr a b = MyArr (a -> b)
instance Category MyArr where
type Object MyArr a = () -- trivial, no constraints needed
id = MyArr P.id
MyArr g . MyArr f = MyArr (g . f)
-- * A generic expression
-- -- error: Could not deduce: Object p Int arising from a use of ‘Id’
-- e0 :: Free p Int Int
-- e0 = Id
-- works, but becomes verbose. Imagine for instance building an expression with
-- functions of tuples; you would need to provide an incredible amount of
-- boilerplate constraints for each permutation of types in tuples used anywhere
-- in the body. The problem is a little worse than this once you account for
-- `Cartesian` which has a Constraint Family called `PairObjects` where you need
-- to prove that each tuple is an allowed product in the current constrained
-- category.
e1 :: Object p Int => Free p Int Int
e1 = Id
-- works, but is no longer abstract in the category `p`
e2 :: Free MyArr Int Int
e2 = Id
-- -- ideal solution, but alas
-- -- error: Could not deduce: Object p Int arising from a use of ‘Id’
-- e3 :: Category p => Free p Int Int
-- e3 = Id
IntTupleFlavor
คือสิ่งที่เป็น singleton และIntTuple
ชั้นเรียนก็คือโดยปรกติจะถูกเรียกว่า"ที่แยกกำหนด singleton"หรือSingI
ฉันคิดว่านะ แล้วแต่ละ constructor องเราก็เก็บเอาพฤติกรรมแบเป็น singleton หลักฐานของมันเป็นพารามิเตอร์เป็นประเภท. น่าสนใจมาก,ขอบคุณผมจะปล่อยให้คุณรู้ว่าพวกมันทำงาน!