adam@6: (** Derived functions dealing with polymorphic variants *) adam@6: adam@6: val read : r ::: {Unit} -> t ::: Type -> folder r -> $(mapU t r) -> variant (mapU {} r) -> t adam@6: val write : r ::: {Unit} -> t ::: Type -> folder r -> $(mapU t r) -> variant (mapU {} r) -> t -> $(mapU t r) adam@6: adam@6: val search : r ::: {Unit} -> t ::: Type -> (variant (mapU {} r) -> option t) -> folder r -> option t adam@6: val find : r ::: {Unit} -> (variant (mapU {} r) -> bool) -> folder r -> option (variant (mapU {} r)) adam@6: adam@6: val test : nm :: Name -> t ::: Type -> ts ::: {Type} -> [[nm] ~ ts] => folder ([nm = t] ++ ts) adam@6: -> variant ([nm = t] ++ ts) -> option t adam@6: val testLess : nm :: Name -> t ::: Type -> ts ::: {Type} -> [[nm] ~ ts] => folder ts -> variant ([nm = t] ++ ts) -> option t adam@6: adam@21: val weaken : r1 ::: {Type} -> r2 ::: {Type} -> [r1 ~ r2] => folder r1 adam@21: -> variant r1 -> variant (r1 ++ r2) adam@21: adam@6: val testEq : K --> f :: (K -> Type) -> nm :: Name -> t ::: K -> ts ::: {K} -> r ::: {K} -> [[nm] ~ ts] => adam@6: Eq.eq r ([nm = t] ++ ts) adam@6: -> folder r adam@6: -> variant (map f r) -> option (f t) adam@6: adam@6: val eq : r ::: {Unit} -> folder r -> variant (mapU {} r) -> variant (mapU {} r) -> bool adam@6: adam@6: val makeEq : K --> f :: (K -> Type) -> nm :: Name -> t ::: K -> ts ::: {K} -> r ::: {K} -> [[nm] ~ ts] => adam@6: Eq.eq r ([nm = t] ++ ts) adam@6: -> f t adam@6: -> variant (map f r) adam@6: adam@21: val mp : r ::: {Unit} -> t ::: Type -> folder r -> (variant (mapU {} r) -> t) -> $(mapU t r) adam@21: adam@6: val fold : r ::: {Unit} -> t ::: Type -> folder r -> (variant (mapU {} r) -> t -> t) -> t -> t adam@6: adam@20: val foldR : tr ::: Type -> r ::: {Unit} -> t ::: Type -> folder r -> (variant (mapU {} r) -> tr -> t -> t) -> $(mapU tr r) -> t -> t adam@20: adam@20: val appR : m ::: (Type -> Type) -> monad m adam@20: -> tr ::: Type -> r ::: {Unit} -> folder r -> (variant (mapU {} r) -> tr -> m {}) -> $(mapU tr r) -> m {} adam@20: adam@21: val mapR : tr ::: Type -> t ::: Type -> r ::: {Unit} -> folder r -> (variant (mapU {} r) -> tr -> t) -> $(mapU tr r) -> $(mapU t r) adam@21: adam@6: val destrR : K --> f :: (K -> Type) -> fr :: (K -> Type) -> t ::: Type adam@6: -> (p :: K -> f p -> fr p -> t) adam@6: -> r ::: {K} -> folder r -> variant (map f r) -> $(map fr r) -> t adam@6: adam@6: val destr2R : K --> f1 :: (K -> Type) -> f2 :: (K -> Type) -> fr :: (K -> Type) -> t ::: Type adam@6: -> (p :: K -> f1 p -> f2 p -> fr p -> t) adam@6: -> r ::: {K} -> folder r -> variant (map f1 r) -> variant (map f2 r) -> $(map fr r) -> option t ezyang@27: ezyang@27: (* Metaprogrammed type-directed case-match. ezyang@27: ezyang@27: This uses a combination of type classes and metaprogramming to make ezyang@27: it easy to write case-matches on very large variants with many ezyang@27: similar elements. Here's how you use it: ezyang@27: ezyang@27: 1. For every type in the variant, write a local typeclass function ezyang@27: which reduces it to t, and register as such using the 'declareCase' ezyang@27: function in the module you created. ezyang@27: ezyang@27: let val empty = declareCase (fn _ (_ : int) => True) ezyang@27: ezyang@27: These functions also take an initial argument, which has ezyang@27: type [a -> variant ts]; e.g. you can use this to create ezyang@27: a new copy of the variant with different values! ezyang@27: Make sure you specify type signatures on the argument [t] ezyang@27: so that we can identify who this typeclass is for. (If you ezyang@27: use type classes to construct the return value, you may ezyang@27: also need to declare the return type explicitly.) ezyang@27: ezyang@27: 2. Do the match using 'typeCase': ezyang@27: ezyang@27: typeCase t ezyang@27: ezyang@27: If you need to override specific constructors, use this idiom: ezyang@27: ezyang@27: @typeCase t (_ ++ { ezyang@27: YourConstr = declareCase (fn _ _ => ...) ezyang@27: }) _ ezyang@27: ezyang@27: How does it work? Very simple: it uses local typeclasses + Ur/Web's ezyang@27: support for automatically instantiating records of typeclasses. ezyang@27: *) ezyang@27: ezyang@27: class type_case :: {Type} -> Type -> Type -> Type ezyang@27: val declareCase : ts ::: {Type} -> t ::: Type -> a ::: Type -> ((a -> variant ts) -> a -> t) -> type_case ts t a ezyang@27: val typeCase : ts ::: {Type} -> t ::: Type -> variant ts -> $(map (type_case ts t) ts) -> folder ts -> t