GithubHelp home page GithubHelp logo

Hello_world

{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE TypeOperators              #-}
{-# LANGUAGE DeriveAnyClass             #-}
{-# LANGUAGE KindSignatures             #-}
{-# LANGUAGE RecordWildCards            #-}
{-# LANGUAGE TypeApplications           #-}
{-# LANGUAGE DerivingStrategies         #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE AllowAmbiguousTypes        #-}
{-# LANGUAGE StandaloneKindSignatures   #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{-# OPTIONS_GHC -Wno-unused-imports     #-}
{-# OPTIONS_GHC -Wno-unused-top-binds   #-}

module Pokemon() where

import Data.Kind
import Data.Proxy
import GHC.TypeLits
import System.Random
import Data.Typeable
import Data.Type.Equality

type            IsPkmType :: Type -> Constraint
class Show t => IsPkmType t

data Bug      = Bug      deriving stock Show deriving anyclass IsPkmType
data Ice      = Ice      deriving stock Show deriving anyclass IsPkmType
data Dark     = Dark     deriving stock Show deriving anyclass IsPkmType
data Rock     = Rock     deriving stock Show deriving anyclass IsPkmType
data Fire     = Fire     deriving stock Show deriving anyclass IsPkmType
data Water    = Water    deriving stock Show deriving anyclass IsPkmType
data Grass    = Grass    deriving stock Show deriving anyclass IsPkmType
data Fairy    = Fairy    deriving stock Show deriving anyclass IsPkmType
data Fight    = Fight    deriving stock Show deriving anyclass IsPkmType
data Ghost    = Ghost    deriving stock Show deriving anyclass IsPkmType
data Steel    = Steel    deriving stock Show deriving anyclass IsPkmType
data Dragon   = Dragon   deriving stock Show deriving anyclass IsPkmType
data Ground   = Ground   deriving stock Show deriving anyclass IsPkmType
data Normal   = Normal   deriving stock Show deriving anyclass IsPkmType
data Poison   = Poison   deriving stock Show deriving anyclass IsPkmType
data Flying   = Flying   deriving stock Show deriving anyclass IsPkmType
data Psychic  = Psychic  deriving stock Show deriving anyclass IsPkmType
data Electric = Electric deriving stock Show deriving anyclass IsPkmType

type            IsNumOfPkmType :: Type -> Constraint
class Show n => IsNumOfPkmType n

data MonoType = forall t . IsPkmType t => JustType t
  deriving anyclass IsNumOfPkmType

instance Show MonoType where
  show (JustType t) = "\n[Mono type]: " <> show t

data DualType = forall t1 t2 . (IsPkmType t1, IsPkmType t2, (t1 == t2) ~ 'False) => BothType t1 t2
  deriving anyclass IsNumOfPkmType

instance Show DualType where
  show (BothType t1 t2) = "\n[Dual type]: " <> show t1 <> ", " <> show t2

data PkmType = forall n . IsNumOfPkmType n => Is n

instance Show PkmType where
  show (Is n) = show n

data StatType
  =  HP
  |  Atk
  |  Def
  |  SpAtk
  |  SpDef
  |  Spd
  deriving stock Show

newtype PkmStat (pkmName :: Symbol) (statType :: StatType)
      = PkmStat Int deriving newtype Show

data Pokemon (pkmName :: Symbol)
  =  Pokemon
  { pkmType  :: PkmType
  , pkmHP    :: PkmStat pkmName 'HP
  , pkmAtk   :: PkmStat pkmName 'Atk
  , pkmDef   :: PkmStat pkmName 'Def
  , pkmSpAtk :: PkmStat pkmName 'SpAtk
  , pkmSpDef :: PkmStat pkmName 'SpDef
  , pkmSpd   :: PkmStat pkmName 'Spd
  }

instance KnownSymbol pkmName => Show (Pokemon pkmName) where
  show Pokemon {..} = "[Name]: " <> symbolVal (Proxy @pkmName)
    <> show pkmType
    <> "\n[HP]:    " <> show pkmHP
    <> "\n[Atk]:   " <> show pkmAtk
    <> "\n[Def]:   " <> show pkmDef
    <> "\n[SpAtk]: " <> show pkmSpAtk
    <> "\n[SpDef]: " <> show pkmSpDef
    <> "\n[Spd]:   " <> show pkmSpd

type  BoundedStatType :: Symbol -> Constraint
class BoundedStatType pkmName where
  maxMinStat :: forall (statType :: StatType) . Typeable statType => Proxy statType
    -> (PkmStat pkmName statType, PkmStat pkmName statType)

  randomStat :: forall (statType :: StatType) . Typeable statType => IO (PkmStat pkmName statType)
  randomStat = do
    let (PkmStat minStat, PkmStat maxStat) = maxMinStat @pkmName (Proxy @statType)
    seed <- newStdGen
    let (newStat, _) = randomR (minStat, maxStat) seed
    pure (PkmStat newStat)

is :: forall (statType' :: StatType) (statType :: StatType) . 
      (Typeable statType', Typeable statType) => Proxy statType -> Bool
is = (typeRep (Proxy @statType') ==) . typeRep

instance BoundedStatType "Pikachu" where
  maxMinStat     proxy
    | is @'HP    proxy = (PkmStat 180, PkmStat 274)
    | is @'Atk   proxy = (PkmStat 103, PkmStat 229)
    | is @'Def   proxy = (PkmStat  76, PkmStat 196)
    | is @'SpAtk proxy = (PkmStat  94, PkmStat 218)
    | is @'SpDef proxy = (PkmStat  94, PkmStat 218)
    | is @'Spd   proxy = (PkmStat 166, PkmStat 306)
    | otherwise        = error "StatType not found"

wildPikachu :: IO (Pokemon "Pikachu")
wildPikachu = do
  pkmHP    <- randomStat @_ @'HP
  pkmAtk   <- randomStat @_ @'Atk
  pkmDef   <- randomStat @_ @'Def
  pkmSpAtk <- randomStat @_ @'SpAtk
  pkmSpDef <- randomStat @_ @'SpDef
  pkmSpd   <- randomStat @_ @'Spd
  pure Pokemon { pkmType  = Is (BothType Electric Psychic), .. }

main :: IO ()
main = do
  myFstPkm <- wildPikachu
  print myFstPkm  
Pensees.intimes.piano.dans.l.orchestre.a.cordes.mp4

dickydicky7's Projects

clean-code-javascript icon clean-code-javascript

:bathtub: Clean Code cho Javascript: một số lời khuyên để giữ cho code js của bạn luôn sạch sẽ thơm tho :muscle: :muscle: :muscle:

godot icon godot

Godot Engine – Multi-platform 2D and 3D game engine

it008.n12 icon it008.n12

IT008.N12 - Visual Programming Course (Lập Trình Trực Quan)

iterm2-color-schemes icon iterm2-color-schemes

Over 250 terminal color schemes/themes for iTerm/iTerm2. Includes ports to Terminal, Konsole, PuTTY, Xresources, XRDB, Remmina, Termite, XFCE, Tilda, FreeBSD VT, Terminator, Kitty, MobaXterm, LXTerminal, Microsoft's Windows Terminal, Visual Studio, Alacritty

javascript-algorithms icon javascript-algorithms

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

kitty icon kitty

😽 Soothing pastel theme for Kitty

libgdx-chess-game icon libgdx-chess-game

A LibGDX based Parallel AI Chess Game playable on many devices from Level 1 to Level 10

markdown-here icon markdown-here

Google Chrome, Firefox, and Thunderbird extension that lets you write email in Markdown and render it before sending.

matter-js icon matter-js

a 2D rigid body physics engine for the web ▲● ■

p5.js icon p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.