Example #1
0
        /// <summary>
        /// "Applicative" instance for HaxlFetch.
        /// </summary>
        /// <remarks>
        /// This isn't a true applicative instance; we don't have:
        ///
        /// > (<*>) :: f (a -> b) -> f a -> f b
        ///
        /// In Haskell Haxl, the applicative instance is used to keep fetched values in scope:
        ///
        /// > (a, b) <- (,) <$> fetch1 <*> fetch2
        ///
        /// C# can't do nested lambda scoping, and uses transparent identifers instead.
        /// Because the transparent identifers aren't accessible to us, we use our own scoping system.
        ///
        /// This means our (a -> b) function is *always* (Scope -> Scope);
        /// we therefore can write our "Applicative" instance as simply a function that takes two Fetches.
        /// </remarks>
        public static Haxl Applicative(this Haxl fetch1, Haxl fetch2)
        {
            return(Haxl.FromFunc((cache, logger) =>
            {
                var result1 = fetch1.Result(cache, logger);
                var result2 = fetch2.Result(cache, logger);
                return result1.Match
                (
                    done1 => result2.Match <Result>
                    (
                        done2 => Done.New(compose(done2.AddToScope, done1.AddToScope)),
                        blocked2 => Blocked.New(blocked2.BlockedRequests, blocked2.Continue.Map(done1.AddToScope))
                    ),

                    blocked1 => result2.Match <Result>
                    (
                        done2 => Blocked.New(blocked1.BlockedRequests, blocked1.Continue.Map(done2.AddToScope)),
                        blocked2 => Blocked.New(
                            blocked1.BlockedRequests.Concat(blocked2.BlockedRequests),
                            blocked1.Continue.Applicative(blocked2.Continue)
                            )
                    )
                );
            }));
        }
Example #2
0
 public Haxl Map(Func <Scope, Scope> addResult)
 {
     return(new Haxl((cache, logger) =>
     {
         var result = Result(cache, logger);
         return result.Match <Result>(
             done => Done.New(compose(addResult, done.AddToScope)),
             blocked => Blocked.New(blocked.BlockedRequests, blocked.Continue.Map(addResult))
             );
     }));
 }
Example #3
0
        /// <summary>
        /// Folds an applicative group into a Haxl monad.
        /// </summary>
        public static Func <Scope, Haxl> ApplicativeToHaxl(ApplicativeGroup applicative, string parentBind)
        {
            var expressions = applicative.Expressions;

            if (applicative.Expressions.Count == 1)
            {
                return(StatementToHaxl(expressions.First(), parentBind));
            }
            return(scope => applicative.Expressions.Aggregate
                   (
                       Haxl.FromFunc((c, l) => Done.New(s => s)),
                       (group, be) =>
            {
                var haxl = StatementToHaxl(be, parentBind)(scope);
                return group.Applicative(haxl);
            }
                   ));
        }
Example #4
0
 /// <summary>
 /// Converts a project expression to Haxl monad.
 /// </summary>
 public static Func <Scope, Haxl> ProjectToHaxl(ProjectStatement project, string parentBind)
 {
     return(scope => Haxl.FromFunc((cache, logger) =>
     {
         var rewritten = RebindToScope.Rebind(project.Expression);
         var result = rewritten.Compile().DynamicInvoke(scope);
         return Done.New(_ =>
         {
             if (project.Expression.BindVariable == HAXL_RESULT_NAME &&
                 !scope.IsRoot &&
                 parentBind != null)
             {
                 return scope.WriteParent(parentBind, result);
             }
             return scope.Add(project.Expression.BindVariable, result);
         });
     }));
 }
Example #5
0
 /// <summary>
 /// Identity >>= (scope -> x) = x = x >>= (scope -> Identity)
 /// </summary>
 public static Haxl Identity()
 {
     return(FromFunc((cache, logger) => Done.New(s => s)));
 }
Example #6
0
 /// <summary>
 /// Applicative pure.
 /// <remarks>
 /// We need a bind variable string because our applicative instance is specialized to (Scope -> Scope) functions.
 /// </remarks>
 /// </summary>
 public static Haxl Pure(string bindTo, object value)
 {
     return(FromFunc((cache, logger) => Done.New(scope => scope.Add(bindTo, value))));
 }