A Framework for Extended Algebraic Data Types

نویسندگان

  • Martin Sulzmann
  • Jeremy Wazny
  • Peter J. Stuckey
چکیده

Data Types have Existential Types data Stack a = forall s. Stack s -self (a->s->s) -push (s->s) -pop (s->a) -top (s->Bool) -empty push :: a -> Stack a -> Stack a push x (Stack s push’ pop top empty) = Stack (push’ x s) push’ pop top empty pop :: Stack a -> Stack a pop (Stack s push pop’ top empty) = Stack (pop’ s) push pop’ top empty A Framework for Extended Algebraic Data Types – p.6 Abstract Data Types have Existential TypesData Types have Existential Types data Stack a = forall s. Stack s -self (a->s->s) -push (s->s) -pop (s->a) -top (s->Bool) -empty top :: Stack a -> a top (Stack s push pop top’ empty) = top’ s empty :: Stack a -> Bool empty (Stack s push pop top empty’) = empty’ s -examples mkListStack :: [a] -> Stack a mkListStack xs = Stack xs (:) tail head null s1 = mkListStack [1::Int] s2 = push 2 s1 A Framework for Extended Algebraic Data Types – p.7 Type Classes with Existential Types Type classes (Wadler/Blott [WB89], Kaes): class Eq a where (==) :: a->a->Bool instance Eq a => Eq [a] where (==) (x:xs) (y:ys) = (x==y) && (xs==ys) ... Type Classes with Existential Types (Läufer [Läu96]): class Key a where getKey::a->Int data KEY = forall a. Key a => Mk a g :: KEY -> Int g (Mk x) = getKey x A Framework for Extended Algebraic Data Types – p.8 Type Classes with Existential Types Type classes with functional dependencies (Jones [Jon00]) and existential types: class StackM s a | s -> a where pushM :: a->s->s popM :: s->s topM :: s->a emptyM :: s->Bool data Stack a = forall s. StackM s a => Stack s push :: a -> Stack a -> Stack a push x (Stack s) = Stack (pushM x s) pop :: Stack a -> Stack a pop (Stack s) = Stack (popM s) A Framework for Extended Algebraic Data Types – p.9 Type Classes with Existential Types -examples instance StackM [a] a where pushM = (:) popM = tail topM = head emptyM = null mkListStack :: [a] -> Stack a mkListStack xs = Stack xs s1 = mkListStack [1::Int] s2 = push 2 s1 A Framework for Extended Algebraic Data Types – p.10 Type Classes with Existential Types class StackM s a | s -> a where pushM :: a->s->s popM :: s->s topM :: s->a emptyM :: s->Bool data Stack a = forall s. StackM s a => Stack s How to type check function push. push :: a -> Stack a -> Stack a push x (Stack s) = Stack (pushM x s) A Framework for Extended Algebraic Data Types – p.11 Type Classes with Existential Types class StackM s a | s -> a where pushM :: a->s->s popM :: s->s topM :: s->a emptyM :: s->Bool data Stack a = forall s. StackM s a => Stack s push :: a -> Stack a -> Stack a push x (Stack s) = Stack (pushM x s) A Framework for Extended Algebraic Data Types – p.11 Type Classes with Existential Types class StackM s a | s -> a where pushM :: a->s->s popM :: s->s topM :: s->a emptyM :: s->Bool data Stack a = forall s. StackM s a => Stack s push :: a -> Stack a -> Stack a push x (Stack s) = Stack (pushM x s) x: a Stack s : Stack a s : s StackM s a A Framework for Extended Algebraic Data Types – p.11 Type Classes with Existential Types class StackM s a | s -> a where pushM :: a->s->s popM :: s->s topM :: s->a emptyM :: s->Bool data Stack a = forall s. StackM s a => Stack s push :: a -> Stack a -> Stack a push x (Stack s) = Stack (pushM x s) x: a Stack s : Stack a s : s StackM s a A Framework for Extended Algebraic Data Types – p.11 Type Classes with Existential Types class StackM s a | s -> a where pushM :: a->s->s popM :: s->s topM :: s->a emptyM :: s->Bool data Stack a = forall s. StackM s a => Stack s push :: a -> Stack a -> Stack a push x (Stack s) = Stack (pushM x s) x: a Stack s : Stack a s : s StackM s a StackM s a A Framework for Extended Algebraic Data Types – p.11 Type Classes with Existential Types class StackM s a | s -> a where pushM :: a->s->s popM :: s->s topM :: s->a emptyM :: s->Bool data Stack a = forall s. StackM s a => Stack s push :: a -> Stack a -> Stack a push x (Stack s) = Stack (pushM x s) x: a Stack s : Stack a s : s StackM s a ⊃ StackM s a A Framework for Extended Algebraic Data Types – p.11 Generalized Algebraic Data Types Enforce data invariants via types: Leijen and Meijer [LM99] Okasaki [Oka99] Cheney and Hinze [CH02] Baars and Swierstra [BS02] and of course Kiselyov! “Generalized” algebraic data types: Index types (Zenger [Zen99], Xi) Guarded recursive data types (Xi, Chen and Chen [XCC03]) Generalized algebraic data types (Peyton-Jones et. al.) First-class phantom types [CH03]. A Framework for Extended Algebraic Data Types – p.12 Well-Typed Expressions Won’t Fail e1 :: Exp e1 = App (Fun (\x-> Plus Zero x)) (Succ Zero) Eval> eval e1 Succ Zero What about e2 :: Exp e2 = App Zero (Succ Zero) Eval> eval e2 Program error! Prevent construction of ill-typed expressions A Framework for Extended Algebraic Data Types – p.13 Construction of Well-Typed Terms Typing rules: (App) ⊢ e1 : a → b ⊢ e2 : a ⊢ e1 e2 : b (Abs) x : a ⊢ e : b ⊢ λx.e : a → b

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

An extended complete Chebyshev system of 3 Abelian integrals related to a non-algebraic Hamiltonian system

In this paper, we study the Chebyshev property of the 3-dimentional vector space $E =langle I_0, I_1, I_2rangle$, where $I_k(h)=int_{H=h}x^ky,dx$ and $H(x,y)=frac{1}{2}y^2+frac{1}{2}(e^{-2x}+1)-e^{-x}$ is a non-algebraic Hamiltonian function. Our main result asserts that $E$ is an extended complete Chebyshev space for $hin(0,frac{1}{2})$. To this end, we use the criterion and tools developed by...

متن کامل

Algebraic Data Types and Induction in CRL

In this paper a thorough treatment of elementary data types is given in the speciication language CRL, which integrates data speciication with process speciication. Data speciication in CRL is directly based on the theory of algebraic data types, which is an equational theory, extended with a rule for constructor induction. Speciications of the following data types and structures are given: boo...

متن کامل

First-Order Logic with Dependent Types

We present DFOL, an extension of classical first-order logic with dependent types, i.e., as in Martin-Löf type theory, signatures may contain type-valued function symbols. A model theory for the logic is given that stays close to the established first-order model theory. The logic is presented as an institution, and the logical framework LF is used to define signatures, terms and formulas. We s...

متن کامل

Multiversal Polymorphic Algebraic Theories

We formalise and study the notion of polymorphic algebraic theory, as understood in the mathematical vernacular as a theory presented by equations between polymorphically-typed terms with both type and term variable binding. The prototypical example of a polymorphic algebraic theory is System F, but our framework applies more widely. The extra generality stems from a mathematical analysis that ...

متن کامل

From abstract data types to algebraic development techniques: A shift of paradigms

The concept of abstract data types and the corresponding series of ADT-workshops have been most fruitful for the development of algebraic speciication techniques within the last two decades. Since abstract data types by now are well-established in all areas of Computer Science and algebraic speciication techniques go far beyond their classical roots of universal algebra the name of the ADT-work...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2006