{-# LANGUAGE QuasiQuotes #-} module Core.Examples where ---------------------------------------------------------------------------------- import Core.Syntax import Core.TH ---------------------------------------------------------------------------------- -- TODO: my shitty lexer isn't inserting semicolons letrecExample :: Program letrecExample = [coreProg| pair x y f = f x y fst p = p k snd p = p k1 f x y = letrec a = pair x b b = pair y a in fst (snd (snd (snd a))); main = f 3 4; |] idExample :: Program idExample = [coreProg| main = id 3; |] indExample1 = [coreProg| main = twice twice id 3; |] indExample2 = [coreProg| main = twice twice twice id 3; |] indExample3 = [coreProg| main = letrec x = 2 y = f x x in g y y; f a b = b; g a b = a; |] negExample1 = [coreProg| main = negate# (id 3); |] negExample2 = [coreProg| main = negate# 3; |] negExample3 = [coreProg| main = twice negate# 3; |] arithExample1 = [coreProg| main = (+#) 3 (negate# 2); |] arithExample2 = [coreProg| main = negate# ((+#) 2 ((*#) 5 3)); |] ifExample1 = [coreProg| main = if# True 2 3; |] ifExample2 = [coreProg| main = if# (id True) 2 3; |] facExample = [coreProg| fac n = if# ((==#) n 0) 1 ((*#) n (fac ((-#) n 1))) main = fac 3; |] corePrelude :: Module corePrelude = Module (Just ("Prelude", [])) $ Program [ ScDef "id" ["x"] (Var "x") , ScDef "k" ["x", "y"] (Var "x") , ScDef "k1" ["x", "y"] (Var "y") , ScDef "succ" ["f", "g", "x"] (Var "f" :$ Var "x" :$ (Var "g" :$ Var "x")) , ScDef "compose" ["f", "g", "x"] (Var "f" :$ (Var "g" :$ Var "x")) , ScDef "twice" ["f", "x"] (Var "f" :$ (Var "f" :$ Var "x")) , ScDef "False" [] $ Con 0 0 , ScDef "True" [] $ Con 1 0 ]